1988cc4a483182c2adec97b87a04c251b22cdd07
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
.project
= parent
._project
50 self
._log
.debug("Initialized ROConfigManager")
56 def cm_state_xpath(self
):
57 return self
.project
.add_project("/rw-conman:cm-state/rw-conman:cm-nsr")
60 def map_config_status(cls
, status
):
62 'init': nsrY
.ConfigStates
.INIT
,
63 'received': nsrY
.ConfigStates
.CONFIGURING
,
64 'cfg_delay': nsrY
.ConfigStates
.CONFIGURING
,
65 'cfg_process': nsrY
.ConfigStates
.CONFIGURING
,
66 'cfg_process_failed': nsrY
.ConfigStates
.CONFIGURING
,
67 'cfg_sched': nsrY
.ConfigStates
.CONFIGURING
,
68 'connecting': nsrY
.ConfigStates
.CONFIGURING
,
69 'failed_connection': nsrY
.ConfigStates
.CONFIGURING
,
70 'netconf_connected': nsrY
.ConfigStates
.CONFIGURING
,
71 'netconf_ssh_connected': nsrY
.ConfigStates
.CONFIGURING
,
72 'restconf_connected': nsrY
.ConfigStates
.CONFIGURING
,
73 'cfg_send': nsrY
.ConfigStates
.CONFIGURING
,
74 'cfg_failed': nsrY
.ConfigStates
.FAILED
,
75 'ready_no_cfg': nsrY
.ConfigStates
.CONFIG_NOT_NEEDED
,
76 'ready': nsrY
.ConfigStates
.CONFIGURED
,
79 return cfg_map
[status
]
82 def update_ns_cfg_state(self
, cm_nsr
):
89 # Update the VNFRs' config status
91 if 'cm_vnfr' in cm_nsr
:
92 gen
= (vnfr
for vnfr
in cm_nsr
['cm_vnfr']
93 if vnfr
['id'] in self
.nsm
._vnfrs
)
97 new_status
= ROConfigManager
.map_config_status(vnfr
['state'])
98 self
._log
.debug("Updating config status of VNFR {} " \
99 "in NSR {} to {}({})".
100 format(vnfrid
, nsrid
, new_status
,
103 self
.nsm
.vnfrs
[vnfrid
].set_config_status(new_status
)
105 # Update the NSR's config status
106 new_status
= ROConfigManager
.map_config_status(cm_nsr
['state'])
107 self
._log
.info("Updating config status of NSR {} to {}({})".
108 format(nsrid
, new_status
, cm_nsr
['state']))
110 # If terminate nsr request comes when NS instantiation is in 'Configuring state'; self.nsm.nsrs dict
111 # is already empty when self.nsm.nsrs[nsrid].set_config_status gets executed. So adding a check here.
112 if nsrid
in self
.nsm
.nsrs
:
113 yield from self
.nsm
.nsrs
[nsrid
].set_config_status(new_status
, cm_nsr
.get('state_details'))
115 except Exception as e
:
116 self
._log
.error("Failed to process cm-state for nsr {}: {}".
118 self
._log
.exception(e
)
122 """ Register for cm-state changes """
125 def on_prepare(xact_info
, query_action
, ks_path
, msg
):
126 """ cm-state changed """
128 #print("###>>> cm-state change ({}), msg_dict = {}".format(query_action, msg_dict))
129 self
._log
.debug("Received cm-state on_prepare (%s:%s:%s)",
134 if (query_action
== rwdts
.QueryAction
.UPDATE
or
135 query_action
== rwdts
.QueryAction
.CREATE
):
136 # Update Each NSR/VNFR state
137 msg_dict
= msg
.as_dict()
138 yield from self
.update_ns_cfg_state(msg_dict
)
139 elif query_action
== rwdts
.QueryAction
.DELETE
:
140 self
._log
.debug("DELETE action in on_prepare for cm-state, ignoring")
142 raise NotImplementedError(
143 "%s on cm-state is not supported",
146 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
149 handler
= rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_prepare
)
150 self
.dts_reg_hdl
= yield from self
._dts
.register(self
.cm_state_xpath
,
151 flags
=rwdts
.Flag
.SUBSCRIBER
,
153 except Exception as e
:
154 self
._log
.error("Failed to register for cm-state changes as %s", str(e
))