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
7 # http://www.apache.org/licenses/LICENSE-2.0
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
17 from cmframework.apis import cmerror
20 class CMActivationWork(object):
22 Serialize/Deserialize the work to/from json, the structure of the json data is the following:
24 'operation': <operation number>,
39 OPER_NAMES = ('NONE', 'SET', 'DELETE', 'FULL', 'NODE')
46 startup_activation=False):
47 self.operation = operation
54 self.condition = Condition()
55 self.condition.acquire()
57 self.uuid_value = None
58 self.startup_activation = startup_activation
61 return '(%r %d %r %r %r)' % (self._get_operation_name(),
65 self.startup_activation)
67 def _get_operation_name(self):
68 return CMActivationWork.OPER_NAMES[self.operation]
70 def get_operation(self):
82 def add_result(self, result):
83 self.condition.acquire()
85 self.condition.notify()
86 self.condition.release()
89 self.condition.acquire()
91 self.condition.release()
95 self.condition.notify()
96 self.condition.release()
98 def is_startup_activation(self):
99 return self.startup_activation
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))
113 def deserialize(self, msg):
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))
125 if __name__ == '__main__':
126 work = CMActivationWork(CMActivationWork.OPER_FULL, 10, {})
127 print 'Work is %s' % work