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.
18 from cmframework.apis import cmerror
22 CONFIG_NAME = 'cloud.cmframework'
24 def __init__(self, backend_handler):
25 self.backend_handler = backend_handler
26 self.config = {'csn': {'global': 0, 'nodes': {}}}
28 logging.info('Current csn is %d', self.get())
30 def _update_csn(self, node_name=None):
31 new_config = copy.deepcopy(self.config)
34 new_config['csn']['global'] += 1
35 logging.info('Updating csn to %d', new_config['csn']['global'])
37 new_config['csn']['nodes'][node_name] = new_config['csn']['global']
38 logging.info('Updating csn for node %s to %s', node_name, new_config['csn']['global'])
40 self.backend_handler.set_property(CMCSN.CONFIG_NAME, json.dumps(new_config))
41 self.config = new_config
43 def _get_config(self):
45 self.config = json.loads(self.backend_handler.get_property(CMCSN.CONFIG_NAME))
46 except cmerror.CMError as exp:
47 logging.info('Context id not defined')
48 except Exception as exp: # pylint: disable=broad-except
49 logging.warning('Got error: %s', exp)
55 return self.config['csn']['global']
57 def get_node_csn(self, node_name):
58 return self.config['csn']['nodes'].get(node_name, 1)
60 def sync_node_csn(self, node_name):
61 self._update_csn(node_name)