Initial commit
[ta/config-manager.git] / cmframework / src / cmframework / server / cmchangemonitor.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
15 import logging
16 import uuid
17 import copy
18 from cmframework.apis import cmchangestate
19 from cmframework.server import cmeventletrwlock
20
21
22 class CMChangeMonitorState(object):
23     def __init__(self):
24         self.state = cmchangestate.CM_CHANGE_STATE_ONGOING
25         self.failed_plugins = {}
26
27
28 class CMChangeMonitor(object):
29     def __init__(self):
30         self.changes = {}
31         self.lock = cmeventletrwlock.CMEventletRWLock()
32
33     def start_change(self):
34         with self.lock.writer():
35             changestate = CMChangeMonitorState()
36             uuid_value = str(uuid.uuid4())
37             self.changes[uuid_value] = changestate
38             return uuid_value
39
40     def change_nok(self, uuid_value, failed_plugins):
41         with self.lock.writer():
42             if uuid_value in self.changes:
43                 self.changes[uuid_value].state = cmchangestate.CM_CHANGE_STATE_NOK
44                 self.changes[uuid_value].failed_plugins = failed_plugins
45             else:
46                 logging.warning('Invalid change uuid %s', uuid_value)
47
48     def change_ok(self, uuid_value):
49         with self.lock.writer():
50             if uuid_value in self.changes:
51                 self.changes[uuid_value].state = cmchangestate.CM_CHANGE_STATE_OK
52             else:
53                 logging.warning('Invalid change uuid %s', uuid_value)
54
55     def get_change_state(self, uuid_value):
56         with self.lock.reader():
57             return self.changes[uuid_value]
58
59     def get_all_changes_states(self):
60         with self.lock.reader():
61             return copy.deepcopy(self.changes)