3 # Copyright 2016 RIFT.IO Inc
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
20 import ncclient
.asyncio_manager
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')
32 from gi
.repository
import (
38 RwConmanYang
as conmanY
,
43 class ROConfigManager(object):
44 def __init__(self
, log
, loop
, dts
, parent
):
49 self
._log
.debug("Initialized ROConfigManager")
55 def cm_state_xpath(self
):
56 return ("/rw-conman:cm-state/rw-conman:cm-nsr")
59 def map_config_status(cls
, status
):
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
,
78 return cfg_map
[status
]
81 def update_ns_cfg_state(self
, cm_nsr
):
88 # Update the VNFRs' config status
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
)
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
,
102 self
.nsm
.vnfrs
[vnfrid
].set_config_status(new_status
)
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']))
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'))
114 except Exception as e
:
115 self
._log
.error("Failed to process cm-state for nsr {}: {}".
117 self
._log
.exception(e
)
121 """ Register for cm-state changes """
124 def on_prepare(xact_info
, query_action
, ks_path
, msg
):
125 """ cm-state changed """
127 #print("###>>> cm-state change ({}), msg_dict = {}".format(query_action, msg_dict))
128 self
._log
.debug("Received cm-state on_prepare (%s:%s:%s)",
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")
141 raise NotImplementedError(
142 "%s on cm-state is not supported",
145 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
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
,
152 except Exception as e
:
153 self
._log
.error("Failed to register for cm-state changes as %s", str(e
))