23ab7b6e0fdf6625ba9b7ebc1decb2d3f85df0ae
[osm/SO.git] / rwlaunchpad / plugins / rwnsm / rift / tasklets / rwnsmtasklet / rwnsm_conman.py
1
2 #
3 # Copyright 2016 RIFT.IO Inc
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 import asyncio
19 import ncclient
20 import ncclient.asyncio_manager
21 import re
22 import time
23
24 import gi
25 gi.require_version('RwYang', '1.0')
26 gi.require_version('RwNsmYang', '1.0')
27 gi.require_version('RwDts', '1.0')
28 gi.require_version('RwTypes', '1.0')
29 gi.require_version('RwConmanYang', '1.0')
30 gi.require_version('NsrYang', '1.0')
31
32 from gi.repository import (
33 NsrYang as nsrY,
34 RwYang,
35 RwNsmYang as nsmY,
36 RwDts as rwdts,
37 RwTypes,
38 RwConmanYang as conmanY,
39 )
40
41 import rift.tasklets
42
43 class ROConfigManager(object):
44 def __init__(self, log, loop, dts, parent):
45 self._log = log
46 self._loop = loop
47 self._dts = dts
48 self.nsm = parent
49 self._log.debug("Initialized ROConfigManager")
50
51 def is_ready(self):
52 return True
53
54 @property
55 def cm_state_xpath(self):
56 return ("/rw-conman:cm-state/rw-conman:cm-nsr")
57
58 @classmethod
59 def map_config_status(cls, status):
60 cfg_map = {
61 'init': nsrY.ConfigStates.INIT,
62 'received': nsrY.ConfigStates.CONFIGURING,
63 'cfg_delay': nsrY.ConfigStates.CONFIGURING,
64 'cfg_process': nsrY.ConfigStates.CONFIGURING,
65 'cfg_process_failed': nsrY.ConfigStates.CONFIGURING,
66 'cfg_sched': nsrY.ConfigStates.CONFIGURING,
67 'connecting': nsrY.ConfigStates.CONFIGURING,
68 'failed_connection': nsrY.ConfigStates.CONFIGURING,
69 'netconf_connected': nsrY.ConfigStates.CONFIGURING,
70 'netconf_ssh_connected': nsrY.ConfigStates.CONFIGURING,
71 'restconf_connected': nsrY.ConfigStates.CONFIGURING,
72 'cfg_send': nsrY.ConfigStates.CONFIGURING,
73 'cfg_failed': nsrY.ConfigStates.FAILED,
74 'ready_no_cfg': nsrY.ConfigStates.CONFIG_NOT_NEEDED,
75 'ready': nsrY.ConfigStates.CONFIGURED,
76 }
77
78 return cfg_map[status]
79
80 @asyncio.coroutine
81 def update_ns_cfg_state(self, cm_nsr):
82 if cm_nsr is None:
83 return
84
85 try:
86 nsrid = cm_nsr['id']
87
88 # Update the VNFRs' config status
89 gen = []
90 if 'cm_vnfr' in cm_nsr:
91 gen = (vnfr for vnfr in cm_nsr['cm_vnfr']
92 if vnfr['id'] in self.nsm._vnfrs)
93
94 for vnfr in gen:
95 vnfrid = vnfr['id']
96 new_status = ROConfigManager.map_config_status(vnfr['state'])
97 self._log.debug("Updating config status of VNFR {} " \
98 "in NSR {} to {}({})".
99 format(vnfrid, nsrid, new_status,
100 vnfr['state']))
101 yield from \
102 self.nsm.vnfrs[vnfrid].set_config_status(new_status)
103
104 # Update the NSR's config status
105 new_status = ROConfigManager.map_config_status(cm_nsr['state'])
106 self._log.info("Updating config status of NSR {} to {}({})".
107 format(nsrid, new_status, cm_nsr['state']))
108
109 # If terminate nsr request comes when NS instantiation is in 'Configuring state'; self.nsm.nsrs dict
110 # is already empty when self.nsm.nsrs[nsrid].set_config_status gets executed. So adding a check here.
111 if nsrid in self.nsm.nsrs:
112 yield from self.nsm.nsrs[nsrid].set_config_status(new_status, cm_nsr.get('state_details'))
113
114 except Exception as e:
115 self._log.error("Failed to process cm-state for nsr {}: {}".
116 format(nsrid, e))
117 self._log.exception(e)
118
119 @asyncio.coroutine
120 def register(self):
121 """ Register for cm-state changes """
122
123 @asyncio.coroutine
124 def on_prepare(xact_info, query_action, ks_path, msg):
125 """ cm-state changed """
126
127 #print("###>>> cm-state change ({}), msg_dict = {}".format(query_action, msg_dict))
128 self._log.debug("Received cm-state on_prepare (%s:%s:%s)",
129 query_action,
130 ks_path,
131 msg)
132
133 if (query_action == rwdts.QueryAction.UPDATE or
134 query_action == rwdts.QueryAction.CREATE):
135 # Update Each NSR/VNFR state
136 msg_dict = msg.as_dict()
137 yield from self.update_ns_cfg_state(msg_dict)
138 elif query_action == rwdts.QueryAction.DELETE:
139 self._log.debug("DELETE action in on_prepare for cm-state, ignoring")
140 else:
141 raise NotImplementedError(
142 "%s on cm-state is not supported",
143 query_action)
144
145 xact_info.respond_xpath(rwdts.XactRspCode.ACK)
146
147 try:
148 handler = rift.tasklets.DTS.RegistrationHandler(on_prepare=on_prepare)
149 self.dts_reg_hdl = yield from self._dts.register(self.cm_state_xpath,
150 flags=rwdts.Flag.SUBSCRIBER,
151 handler=handler)
152 except Exception as e:
153 self._log.error("Failed to register for cm-state changes as %s", str(e))
154