8bec75633b6885f77b67a9ef388b447734135a58
[ta/config-manager.git] / cmactivationwork.py
1 # Copyright 2019 Nokia
2
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 from threading import Condition
15 import json
16
17 from cmframework.apis import cmerror
18
19
20 class CMActivationWork(object):
21     """
22     Serialize/Deserialize the work to/from json, the structure of the json data is the following:
23     {
24         'operation': <operation number>,
25         'csn': <csn number>,
26         'properties': {
27             '<name>': '<value>',
28             ....
29         }
30     }
31     """
32
33     OPER_NONE = 0
34     OPER_SET = 1
35     OPER_DELETE = 2
36     OPER_FULL = 3
37     OPER_NODE = 4
38
39     OPER_NAMES = ('NONE', 'SET', 'DELETE', 'FULL', 'NODE')
40
41     def __init__(self,
42                  operation=OPER_NONE,
43                  csn=0,
44                  props=None,
45                  target=None,
46                  startup_activation=False):
47         self.operation = operation
48         self.csn = csn
49         if not props:
50             self.props = {}
51         else:
52             self.props = props
53         self.target = target
54         self.condition = Condition()
55         self.condition.acquire()
56         self.result = None
57         self.uuid_value = None
58         self.startup_activation = startup_activation
59
60     def __str__(self):
61         return '(%r %d %r %r %r)' % (self._get_operation_name(),
62                                      self.csn,
63                                      self.props,
64                                      self.target,
65                                      self.startup_activation)
66
67     def _get_operation_name(self):
68         return CMActivationWork.OPER_NAMES[self.operation]
69
70     def get_operation(self):
71         return self.operation
72
73     def get_csn(self):
74         return self.csn
75
76     def get_props(self):
77         return self.props
78
79     def get_target(self):
80         return self.target
81
82     def add_result(self, result):
83         self.condition.acquire()
84         self.result = result
85         self.condition.notify()
86         self.condition.release()
87
88     def get_result(self):
89         self.condition.acquire()
90         self.condition.wait()
91         self.condition.release()
92         return self.result
93
94     def release(self):
95         self.condition.notify()
96         self.condition.release()
97
98     def is_startup_activation(self):
99         return self.startup_activation
100
101     def serialize(self):
102         try:
103             data = {}
104             data['operation'] = self.operation
105             data['csn'] = self.csn
106             data['properties'] = self.props
107             data['result'] = self.result
108             data['startup_activation'] = self.startup_activation
109             return json.dumps(data)
110         except Exception as exp:
111             raise cmerror.CMError(str(exp))
112
113     def deserialize(self, msg):
114         try:
115             data = json.loads(msg)
116             self.operation = data['operation']
117             self.csn = data['csn']
118             self.props = data['properties']
119             self.result = data['result']
120             self.startup_activation = data['startup_activation']
121         except Exception as exp:
122             raise cmerror.CMError(str(exp))
123
124
125 if __name__ == '__main__':
126     work = CMActivationWork(CMActivationWork.OPER_FULL, 10, {})
127     print 'Work is %s' % work