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.
26 from gi
.repository
import (
28 RwConmanYang
as conmanY
,
33 import rift
.package
.script
34 import rift
.package
.store
36 from . import rwconman_conagent
as conagent
37 from . import RiftCM_rpc
38 from . import riftcm_config_plugin
41 if sys
.version_info
< (3, 4, 4):
42 asyncio
.ensure_future
= asyncio
.async
44 def get_vnf_unique_name(nsr_name
, vnfr_short_name
, member_vnf_index
):
45 return "{}.{}.{}".format(nsr_name
, vnfr_short_name
, member_vnf_index
)
47 class ConmanConfigError(Exception):
51 class InitialConfigError(ConmanConfigError
):
55 def log_this_vnf(vnf_cfg
):
57 used_item_list
= ['nsr_name', 'vnfr_name', 'member_vnf_index', 'mgmt_ip_address']
58 for item
in used_item_list
:
60 if item
== 'mgmt_ip_address':
61 log_vnf
+= "({})".format(vnf_cfg
[item
])
63 log_vnf
+= "{}/".format(vnf_cfg
[item
])
66 class PretendNsm(object):
67 def __init__(self
, dts
, log
, loop
, parent
):
73 self
._nsr
_dict
= parent
._nsr
_dict
74 self
._config
_agent
_plugins
= []
79 # Expensive, instead use get_nsr, if you know id.
81 # Update the list of nsrs (agent nsr)
82 for id, nsr_obj
in self
._nsr
_dict
.items():
83 self
._nsrs
[id] = nsr_obj
.agent_nsr
86 def get_nsr(self
, nsr_id
):
87 if nsr_id
in self
._nsr
_dict
:
88 nsr_obj
= self
._nsr
_dict
[nsr_id
]
92 def get_vnfr_msg(self
, vnfr_id
, nsr_id
=None):
93 self
._log
.debug("get_vnfr_msg(vnfr=%s, nsr=%s)",
97 if nsr_id
in self
._nsr
_dict
:
98 nsr_obj
= self
._nsr
_dict
[nsr_id
]
99 if vnfr_id
in nsr_obj
._vnfr
_dict
:
102 for nsr_obj
in self
._nsr
_dict
.values():
103 if vnfr_id
in nsr_obj
._vnfr
_dict
:
108 vnf_cfg
= nsr_obj
._vnfr
_dict
[vnfr_id
]['vnf_cfg']
109 return vnf_cfg
['agent_vnfr'].vnfr_msg
114 def get_nsd(self
, nsr_id
):
115 if nsr_id
not in self
._nsd
_msg
:
116 nsr_config
= yield from self
._parent
.cmdts_obj
.get_nsr_config(nsr_id
)
117 self
._nsd
_msg
[nsr_id
] = nsr_config
.nsd
118 return self
._nsd
_msg
[nsr_id
]
121 def config_agent_plugins(self
):
122 self
._config
_agent
_plugins
= []
123 for agent
in self
._parent
._config
_agent
_mgr
._plugin
_instances
.values():
124 self
._config
_agent
_plugins
.append(agent
)
125 return self
._config
_agent
_plugins
127 class ConfigManagerConfig(object):
128 def __init__(self
, dts
, log
, loop
, parent
):
132 self
._parent
= parent
134 self
.pending_cfg
= {}
135 self
.terminate_cfg
= {}
136 self
.pending_tasks
= [] # User for NSRid get retry
137 # (mainly excercised at restart case)
138 self
._config
_xpath
= "C,/cm-config"
139 self
._opdata
_xpath
= "D,/rw-conman:cm-state"
141 self
.cm_config
= conmanY
.SoConfig()
142 # RO specific configuration
144 for key
in self
.cm_config
.ro_endpoint
.fields
:
145 self
.ro_config
[key
] = None
147 # Initialize cm-state
149 self
.cm_state
['cm_nsr'] = []
150 self
.cm_state
['states'] = "Initialized"
152 # Initialize objects to register
153 self
.cmdts_obj
= ConfigManagerDTS(self
._log
, self
._loop
, self
, self
._dts
)
154 self
._config
_agent
_mgr
= conagent
.RiftCMConfigAgent(
162 self
._config
_agent
_mgr
,
163 RiftCM_rpc
.RiftCMRPCHandler(self
._dts
, self
._log
, self
._loop
,
165 self
._dts
, self
._log
, self
._loop
, self
)),
168 def is_nsr_valid(self
, nsr_id
):
169 if nsr_id
in self
._nsr
_dict
:
173 def add_to_pending_tasks(self
, task
):
174 if self
.pending_tasks
:
175 for p_task
in self
.pending_tasks
:
176 if p_task
['nsrid'] == task
['nsrid']:
180 self
.pending_tasks
.append(task
)
181 self
._log
.debug("add_to_pending_tasks (nsrid:%s)",
183 if len(self
.pending_tasks
) == 1:
184 self
._loop
.create_task(self
.ConfigManagerConfig_pending_loop())
185 # TBD - change to info level
186 self
._log
.debug("Started pending_loop!")
187 except Exception as e
:
188 self
._log
.error("Failed adding to pending tasks (%s)", str(e
))
190 def del_from_pending_tasks(self
, task
):
192 self
.pending_tasks
.remove(task
)
193 except Exception as e
:
194 self
._log
.error("Failed removing from pending tasks (%s)", str(e
))
197 def ConfigManagerConfig_pending_loop(self
):
200 yield from asyncio
.sleep(loop_sleep
, loop
=self
._loop
)
202 This pending task queue is ordred by events,
203 must finish previous task successfully to be able to go on to the next task
205 if self
.pending_tasks
:
206 self
._log
.debug("self.pending_tasks len=%s", len(self
.pending_tasks
))
207 task
= self
.pending_tasks
[0]
210 nsrid
= task
['nsrid']
211 self
._log
.debug("Will execute pending task for NSR id(%s)", nsrid
)
213 # Try to configure this NSR
215 done
= yield from self
.config_NSR(nsrid
)
216 self
._log
.info("self.config_NSR status=%s", done
)
218 except Exception as e
:
219 self
._log
.error("Failed(%s) configuring NSR(%s)," \
220 "retries remained:%d!",
221 str(e
), nsrid
, task
['retries'])
223 self
.pending_tasks
.remove(task
)
226 self
._log
.debug("Finished pending task NSR id(%s):", nsrid
)
228 self
._log
.error("Failed configuring NSR(%s), retries remained:%d!",
229 nsrid
, task
['retries'])
231 # Failed, re-insert (append at the end)
232 # this failed task to be retried later
233 # If any retries remained.
235 self
.pending_tasks
.append(task
)
237 self
._log
.debug("Stopped pending_loop!")
242 yield from self
.register_cm_state_opdata()
244 # Initialize all handles that needs to be registered
245 for reg
in self
.reg_handles
:
246 yield from reg
.register()
249 def register_cm_state_opdata(self
):
251 def state_to_string(state
):
253 conmanY
.RecordState
.INIT
: "init",
254 conmanY
.RecordState
.RECEIVED
: "received",
255 conmanY
.RecordState
.CFG_PROCESS
: "cfg_process",
256 conmanY
.RecordState
.CFG_PROCESS_FAILED
: "cfg_process_failed",
257 conmanY
.RecordState
.CFG_SCHED
: "cfg_sched",
258 conmanY
.RecordState
.CFG_DELAY
: "cfg_delay",
259 conmanY
.RecordState
.CONNECTING
: "connecting",
260 conmanY
.RecordState
.FAILED_CONNECTION
: "failed_connection",
261 conmanY
.RecordState
.NETCONF_CONNECTED
: "netconf_connected",
262 conmanY
.RecordState
.NETCONF_SSH_CONNECTED
: "netconf_ssh_connected",
263 conmanY
.RecordState
.RESTCONF_CONNECTED
: "restconf_connected",
264 conmanY
.RecordState
.CFG_SEND
: "cfg_send",
265 conmanY
.RecordState
.CFG_FAILED
: "cfg_failed",
266 conmanY
.RecordState
.READY_NO_CFG
: "ready_no_cfg",
267 conmanY
.RecordState
.READY
: "ready",
269 return state_dict
[state
]
272 def on_prepare(xact_info
, action
, ks_path
, msg
):
274 self
._log
.debug("Received cm-state: msg=%s, action=%s", msg
, action
)
276 if action
== rwdts
.QueryAction
.READ
:
277 show_output
= conmanY
.CmOpdata()
278 show_output
.from_dict(self
.cm_state
)
279 self
._log
.debug("Responding to SHOW cm-state: %s", self
.cm_state
)
280 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
,
281 xpath
=self
._opdata
_xpath
,
284 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
286 self
._log
.info("Registering for cm-opdata xpath: %s",
290 handler
=rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_prepare
)
291 yield from self
._dts
.register(xpath
=self
._opdata
_xpath
,
293 flags
=rwdts
.Flag
.PUBLISHER
)
294 self
._log
.info("Successfully registered for opdata(%s)", self
._opdata
_xpath
)
295 except Exception as e
:
296 self
._log
.error("Failed to register for opdata as (%s)", e
)
299 def process_nsd_vnf_configuration(self
, nsr_obj
, vnfr
):
301 def get_config_method(vnf_config
):
302 cfg_types
= ['netconf', 'juju', 'script']
303 for method
in cfg_types
:
304 if method
in vnf_config
:
308 def get_cfg_file_extension(method
, configuration_options
):
318 if method
== "netconf":
319 return ext_dict
[method
]
320 elif method
== "script":
321 return ext_dict
[method
][configuration_options
['script_type']]
322 elif method
== "juju":
323 return ext_dict
[method
]
327 # This is how the YAML file should look like,
328 # This routine will be called for each VNF, so keep appending the file.
329 # priority order is determined by the number,
330 # hence no need to generate the file in that order. A dictionary will be
331 # used that will take care of the order by number.
333 1 : <== This is priority
336 configuration_delay : 120
337 configuration_type : netconf
338 configuration_options :
346 configuration_delay : 0
347 configuration_type : netconf
348 configuration_options :
355 # Save some parameters needed as short cuts in flat structure (Also generated)
356 vnf_cfg
= vnfr
['vnf_cfg']
357 # Prepare unique name for this VNF
358 vnf_cfg
['vnf_unique_name'] = get_vnf_unique_name(
359 vnf_cfg
['nsr_name'], vnfr
['short_name'], vnfr
['member_vnf_index_ref'])
361 nsr_obj
.cfg_path_prefix
= '{}/{}_{}'.format(
362 nsr_obj
.this_nsr_dir
, vnfr
['short_name'], vnfr
['member_vnf_index_ref'])
363 nsr_vnfr
= '{}/{}_{}'.format(
364 vnf_cfg
['nsr_name'], vnfr
['short_name'], vnfr
['member_vnf_index_ref'])
366 # Get vnf_configuration from vnfr
367 vnf_config
= vnfr
['vnf_configuration']
369 self
._log
.debug("vnf_configuration = %s", vnf_config
)
371 # Create priority dictionary
372 cfg_priority_order
= 0
373 if ('config_attributes' in vnf_config
and
374 'config_priority' in vnf_config
['config_attributes']):
375 cfg_priority_order
= vnf_config
['config_attributes']['config_priority']
377 if cfg_priority_order
not in nsr_obj
.nsr_cfg_config_attributes_dict
:
378 # No VNFR with this priority yet, initialize the list
379 nsr_obj
.nsr_cfg_config_attributes_dict
[cfg_priority_order
] = []
381 method
= get_config_method(vnf_config
)
382 if method
is not None:
383 # Create all sub dictionaries first
386 'name' : vnfr
['short_name'],
387 'member_vnf_index' : vnfr
['member_vnf_index_ref'],
390 if 'config_delay' in vnf_config
['config_attributes']:
391 config_priority
['configuration_delay'] = vnf_config
['config_attributes']['config_delay']
392 vnf_cfg
['config_delay'] = config_priority
['configuration_delay']
394 configuration_options
= {}
395 self
._log
.debug("config method=%s", method
)
396 config_priority
['configuration_type'] = method
397 vnf_cfg
['config_method'] = method
399 # Set config agent based on method
400 self
._config
_agent
_mgr
.set_config_agent(
401 nsr_obj
.agent_nsr
, vnf_cfg
['agent_vnfr'], method
)
404 'port', 'target', 'script_type', 'ip_address', 'user', 'secret',
406 for cfg_opt
in cfg_opt_list
:
407 if cfg_opt
in vnf_config
[method
]:
408 configuration_options
[cfg_opt
] = vnf_config
[method
][cfg_opt
]
409 vnf_cfg
[cfg_opt
] = configuration_options
[cfg_opt
]
411 cfg_opt_list
= ['mgmt_ip_address', 'username', 'password']
412 for cfg_opt
in cfg_opt_list
:
413 if cfg_opt
in vnf_config
['config_access']:
414 configuration_options
[cfg_opt
] = vnf_config
['config_access'][cfg_opt
]
415 vnf_cfg
[cfg_opt
] = configuration_options
[cfg_opt
]
418 vnf_cp_dict
= nsr_obj
._cp
_dict
[vnfr
['member_vnf_index_ref']]
419 vnf_cp_dict
['rw_mgmt_ip'] = vnf_cfg
['mgmt_ip_address']
420 vnf_cp_dict
['rw_username'] = vnf_cfg
['username']
421 vnf_cp_dict
['rw_password'] = vnf_cfg
['password']
424 # TBD - see if we can neatly include the config in "config_attributes" file, no need though
425 #config_priority['config_template'] = vnf_config['config_template']
427 vnf_cfg
['juju_script'] = os
.path
.join(self
._parent
.cfg_dir
, 'juju_if.py')
429 if 'config_template' in vnf_config
:
430 vnf_cfg
['cfg_template'] = '{}_{}_template.cfg'.format(nsr_obj
.cfg_path_prefix
, config_priority
['configuration_type'])
431 vnf_cfg
['cfg_file'] = '{}.{}'.format(nsr_obj
.cfg_path_prefix
, get_cfg_file_extension(method
, configuration_options
))
432 vnf_cfg
['xlate_script'] = os
.path
.join(self
._parent
.cfg_dir
, 'xlate_cfg.py')
434 # Now write this template into file
435 with
open(vnf_cfg
['cfg_template'], "w") as cf
:
436 cf
.write(vnf_config
['config_template'])
437 except Exception as e
:
438 self
._log
.error("Processing NSD, failed to generate configuration template : %s (Error : %s)",
439 vnf_config
['config_template'], str(e
))
442 self
._log
.debug("VNF endpoint so far: %s", vnf_cfg
)
444 # Populate filled up dictionary
445 config_priority
['configuration_options'] = configuration_options
446 nsr_obj
.nsr_cfg_config_attributes_dict
[cfg_priority_order
].append(config_priority
)
447 nsr_obj
.num_vnfs_to_cfg
+= 1
448 nsr_obj
._vnfr
_dict
[vnf_cfg
['vnf_unique_name']] = vnfr
449 nsr_obj
._vnfr
_dict
[vnfr
['id']] = vnfr
451 self
._log
.debug("VNF:(%s) config_attributes = %s",
452 log_this_vnf(vnfr
['vnf_cfg']),
453 nsr_obj
.nsr_cfg_config_attributes_dict
)
455 self
._log
.info("VNF:(%s) is not to be configured by Configuration Manager!",
456 log_this_vnf(vnfr
['vnf_cfg']))
457 yield from nsr_obj
.update_vnf_cm_state(vnfr
, conmanY
.RecordState
.READY_NO_CFG
)
459 # Update the cm-state
460 nsr_obj
.populate_vm_state_from_vnf_cfg()
463 def config_NSR(self
, id):
465 def my_yaml_dump(config_attributes_dict
, yf
):
467 yaml_dict
= dict(sorted(config_attributes_dict
.items()))
468 yf
.write(yaml
.dump(yaml_dict
, default_flow_style
=False))
470 nsr_dict
= self
._nsr
_dict
471 self
._log
.info("Configure NSR, id = %s", id)
473 #####################TBD###########################
474 # yield from self._config_agent_mgr.invoke_config_agent_plugins('notify_create_nsr', self.id, self._nsd)
475 # yield from self._config_agent_mgr.invoke_config_agent_plugins('notify_nsr_active', self.id, self._vnfrs)
478 if id not in nsr_dict
:
479 nsr_obj
= ConfigManagerNSR(self
._log
, self
._loop
, self
, id)
480 nsr_dict
[id] = nsr_obj
482 self
._log
.info("NSR(%s) is already initialized!", id)
483 nsr_obj
= nsr_dict
[id]
484 except Exception as e
:
485 self
._log
.error("Failed creating NSR object for (%s) as (%s)", id, str(e
))
488 # Try to configure this NSR only if not already processed
489 if nsr_obj
.cm_nsr
['state'] != nsr_obj
.state_to_string(conmanY
.RecordState
.INIT
):
490 self
._log
.debug("NSR(%s) is already processed, state=%s",
491 nsr_obj
.nsr_name
, nsr_obj
.cm_nsr
['state'])
492 yield from nsr_obj
.publish_cm_state()
495 cmdts_obj
= self
.cmdts_obj
498 nsr
= yield from cmdts_obj
.get_nsr(id)
499 self
._log
.debug("Full NSR : %s", nsr
)
500 if nsr
['operational_status'] != "running":
501 self
._log
.info("NSR(%s) is not ready yet!", nsr
['nsd_name_ref'])
505 # Create Agent NSR class
506 nsr_config
= yield from cmdts_obj
.get_nsr_config(id)
507 self
._log
.debug("NSR {} config: {}".format(id, nsr_config
))
508 nsr_obj
.agent_nsr
= riftcm_config_plugin
.RiftCMnsr(nsr
, nsr_config
)
511 yield from nsr_obj
.update_ns_cm_state(conmanY
.RecordState
.RECEIVED
)
515 nsr_obj
.set_nsr_name(nsr
['name_ref'])
516 nsr_dir
= os
.path
.join(self
._parent
.cfg_dir
, nsr_obj
.nsr_name
)
517 self
._log
.info("Checking NS config directory: %s", nsr_dir
)
518 if not os
.path
.isdir(nsr_dir
):
520 # self._log.critical("NS %s is not to be configured by Service Orchestrator!", nsr_obj.nsr_name)
521 # yield from nsr_obj.update_ns_cm_state(conmanY.RecordState.READY_NO_CFG)
524 nsr_obj
.set_config_dir(self
)
526 for const_vnfr
in nsr
['constituent_vnfr_ref']:
527 self
._log
.debug("Fetching VNFR (%s)", const_vnfr
['vnfr_id'])
528 vnfr_msg
= yield from cmdts_obj
.get_vnfr(const_vnfr
['vnfr_id'])
530 vnfr
= vnfr_msg
.as_dict()
531 self
._log
.info("create VNF:{}/{}".format(nsr_obj
.nsr_name
, vnfr
['short_name']))
532 agent_vnfr
= yield from nsr_obj
.add_vnfr(vnfr
, vnfr_msg
)
534 # Preserve order, self.process_nsd_vnf_configuration()
535 # sets up the config agent based on the method
536 yield from self
.process_nsd_vnf_configuration(nsr_obj
, vnfr
)
537 yield from self
._config
_agent
_mgr
.invoke_config_agent_plugins(
538 'notify_create_vnfr',
542 #####################TBD###########################
543 # self._log.debug("VNF active. Apply initial config for vnfr {}".format(vnfr.name))
544 # yield from self._config_agent_mgr.invoke_config_agent_plugins('apply_initial_config',
546 # yield from self._config_agent_mgr.invoke_config_agent_plugins('notify_terminate_vnf', self.id, vnfr)
548 except Exception as e
:
549 self
._log
.error("Failed processing NSR (%s) as (%s)", nsr_obj
.nsr_name
, str(e
))
550 self
._log
.exception(e
)
551 yield from nsr_obj
.update_ns_cm_state(conmanY
.RecordState
.CFG_PROCESS_FAILED
)
555 # Generate config_config_attributes.yaml (For debug reference)
556 with
open(nsr_obj
.config_attributes_file
, "w") as yf
:
557 my_yaml_dump(nsr_obj
.nsr_cfg_config_attributes_dict
, yf
)
558 except Exception as e
:
559 self
._log
.error("NS:(%s) failed to write config attributes file as (%s)", nsr_obj
.nsr_name
, str(e
))
562 # Generate nsr_xlate_dict.yaml (For debug reference)
563 with
open(nsr_obj
.xlate_dict_file
, "w") as yf
:
564 yf
.write(yaml
.dump(nsr_obj
._cp
_dict
, default_flow_style
=False))
565 except Exception as e
:
566 self
._log
.error("NS:(%s) failed to write nsr xlate tags file as (%s)", nsr_obj
.nsr_name
, str(e
))
568 self
._log
.debug("Starting to configure each VNF")
570 # Check if this NS has input parametrs
571 self
._log
.info("Checking NS configuration order: %s", nsr_obj
.config_attributes_file
)
573 if os
.path
.exists(nsr_obj
.config_attributes_file
):
574 # Apply configuration is specified order
576 # Go in loop to configure by specified order
577 self
._log
.info("Using Dynamic configuration input parametrs for NS: %s", nsr_obj
.nsr_name
)
579 # cfg_delay = nsr_obj.nsr_cfg_config_attributes_dict['configuration_delay']
581 # self._log.info("Applying configuration delay for NS (%s) ; %d seconds",
582 # nsr_obj.nsr_name, cfg_delay)
583 # yield from asyncio.sleep(cfg_delay, loop=self._loop)
585 for config_attributes_dict
in nsr_obj
.nsr_cfg_config_attributes_dict
.values():
586 # Iterate through each priority level
587 for vnf_config_attributes_dict
in config_attributes_dict
:
588 # Iterate through each vnfr at this priority level
590 # Make up vnf_unique_name with vnfd name and member index
591 #vnfr_name = "{}.{}".format(nsr_obj.nsr_name, vnf_config_attributes_dict['name'])
592 vnf_unique_name
= get_vnf_unique_name(
594 vnf_config_attributes_dict
['name'],
595 str(vnf_config_attributes_dict
['member_vnf_index']),
597 self
._log
.info("NS (%s) : VNF (%s) - Processing configuration attributes",
598 nsr_obj
.nsr_name
, vnf_unique_name
)
600 # Find vnfr for this vnf_unique_name
601 if vnf_unique_name
not in nsr_obj
._vnfr
_dict
:
602 self
._log
.error("NS (%s) - Can not find VNF to be configured: %s", nsr_obj
.nsr_name
, vnf_unique_name
)
604 # Save this unique VNF's config input parameters
605 nsr_obj
.vnf_config_attributes_dict
[vnf_unique_name
] = vnf_config_attributes_dict
606 nsr_obj
.ConfigVNF(nsr_obj
._vnfr
_dict
[vnf_unique_name
])
608 # Now add the entire NS to the pending config list.
609 self
._log
.info("Scheduling NSR:{} configuration".format(nsr_obj
.nsr_name
))
610 self
._parent
.add_to_pending(nsr_obj
)
611 self
._parent
.add_nsr_obj(nsr_obj
)
613 except Exception as e
:
614 self
._log
.error("Failed processing input parameters for NS (%s) as %s", nsr_obj
.nsr_name
, str(e
))
617 self
._log
.error("No configuration input parameters for NSR (%s)", nsr_obj
.nsr_name
)
619 except Exception as e
:
620 self
._log
.error("Failed to configure NS (%s) as (%s)", nsr_obj
.nsr_name
, str(e
))
621 yield from nsr_obj
.update_ns_cm_state(conmanY
.RecordState
.CFG_PROCESS_FAILED
)
627 def terminate_NSR(self
, id):
628 nsr_dict
= self
._nsr
_dict
629 if id not in nsr_dict
:
630 self
._log
.error("NSR(%s) does not exist!", id)
633 # Remove this NSR if we have it on pending task list
634 for task
in self
.pending_tasks
:
635 if task
['nsrid'] == id:
636 self
.del_from_pending_tasks(task
)
638 # Remove this object from global list
639 nsr_obj
= nsr_dict
.pop(id, None)
641 # Remove this NS cm-state from global status list
642 self
.cm_state
['cm_nsr'].remove(nsr_obj
.cm_nsr
)
644 # Also remove any scheduled configuration event
645 for nsr_obj_p
in self
._parent
.pending_cfg
:
646 if nsr_obj_p
== nsr_obj
:
647 assert id == nsr_obj_p
._nsr
_id
648 #self._parent.pending_cfg.remove(nsr_obj_p)
649 # Mark this as being deleted so we do not try to configure it if we are in cfg_delay (will wake up and continue to process otherwise)
650 nsr_obj_p
.being_deleted
= True
651 self
._log
.info("Removed scheduled configuration for NSR(%s)", nsr_obj
.nsr_name
)
653 self
._parent
.remove_nsr_obj(id)
655 # Call Config Agent to clean up for each VNF
656 for agent_vnfr
in nsr_obj
.agent_nsr
.vnfrs
:
657 yield from self
._config
_agent
_mgr
.invoke_config_agent_plugins(
658 'notify_terminate_vnfr',
662 # publish delete cm-state (cm-nsr)
663 yield from nsr_obj
.delete_cm_nsr()
665 #####################TBD###########################
666 # yield from self._config_agent_mgr.invoke_config_agent_plugins('notify_terminate_ns', self.id)
668 self
._log
.info("NSR(%s/%s) is deleted", nsr_obj
.nsr_name
, id)
671 def process_initial_config(self
, nsr_obj
, conf
, script
, vnfr_name
=None):
672 '''Apply the initial-config-primitives specified in NSD or VNFD'''
674 def get_input_file(parameters
):
677 # Add NSR name to file
678 inp
['nsr_name'] = nsr_obj
.nsr_name
680 # Add VNFR name if available
682 inp
['vnfr_name'] = vnfr_name
684 # Add parameters for initial config
685 inp
['parameter'] = {}
686 for parameter
in parameters
:
688 inp
['parameter'][parameter
['name']] = parameter
['value']
689 except KeyError as e
:
691 self
._log
.info("VNFR {} initial config parameter {} with no value: {}".
692 format(vnfr_name
, parameter
, e
))
694 self
._log
.info("NSR {} initial config parameter {} with no value: {}".
695 format(nsr_obj
.nsr_name
, parameter
, e
))
698 # Add config agents specific to each VNFR
699 inp
['config-agent'] = {}
700 for vnfr
in nsr_obj
.agent_nsr
.vnfrs
:
701 # Get the config agent for the VNFR
702 # If vnfr name is specified, add only CA specific to that
703 if (vnfr_name
is None) or \
704 (vnfr_name
== vnfr
.name
):
705 agent
= self
._config
_agent
_mgr
.get_vnfr_config_agent(vnfr
.vnfr_msg
)
707 if agent
.agent_type
!= riftcm_config_plugin
.DEFAULT_CAP_TYPE
:
708 inp
['config-agent'][vnfr
.member_vnf_index
] = agent
.agent_data
709 inp
['config-agent'][vnfr
.member_vnf_index
] \
710 ['service-name'] = agent
.get_service_name(vnfr
.id)
712 # Add vnfrs specific data
714 for vnfr
in nsr_obj
.vnfrs
:
717 v
['name'] = vnfr
['name']
718 v
['mgmt_ip_address'] = vnfr
['vnf_cfg']['mgmt_ip_address']
719 v
['mgmt_port'] = vnfr
['vnf_cfg']['port']
721 if 'dashboard_url' in vnfr
:
722 v
['dashboard_url'] = vnfr
['dashboard_url']
724 if 'connection_point' in vnfr
:
725 v
['connection_point'] = []
726 for cp
in vnfr
['connection_point']:
727 v
['connection_point'].append(
730 'ip_address': cp
['ip_address'],
736 for vdu
in vnfr
['vdur']:
738 for k
in ['name','management_ip', 'vm_management_ip', 'id', 'vdu_id_ref']:
744 inp
['vnfr'][vnfr
['member_vnf_index_ref']] = v
747 self
._log
.debug("Input data for {}: {}".
748 format((vnfr_name
if vnfr_name
else nsr_obj
.nsr_name
),
751 # Convert to YAML string
752 yaml_string
= yaml
.dump(inp
, default_flow_style
=False)
754 # Write the inputs as yaml file
756 with tempfile
.NamedTemporaryFile(delete
=False) as tmp_file
:
757 tmp_file
.write(yaml_string
.encode("UTF-8"))
758 self
._log
.debug("Input file created for {}: {}".
759 format((vnfr_name
if vnfr_name \
760 else nsr_obj
.nsr_name
),
767 parameters
= conf
['parameter']
768 except Exception as e
:
769 self
._log
.debug("Parameter conf: {}, e: {}".
772 inp_file
= get_input_file(parameters
)
774 cmd
= "{0} {1}".format(script
, inp_file
)
775 self
._log
.debug("Running the CMD: {}".format(cmd
))
777 process
= yield from asyncio
.create_subprocess_shell(cmd
,
779 stdout
=subprocess
.PIPE
,
780 stderr
=subprocess
.PIPE
)
781 stdout
, stderr
= yield from process
.communicate()
782 rc
= yield from process
.wait()
785 msg
= "NSR/VNFR {} initial config using {} failed with {}: {}". \
786 format(vnfr_name
if vnfr_name
else nsr_obj
.nsr_name
,
789 raise InitialConfigError(msg
)
793 except Exception as e
:
794 self
._log
.debug("Error removing input file {}: {}".
797 def get_script_file(self
, script_name
, d_name
, d_id
, d_type
):
799 package_store
= rift
.package
.store
.VnfdPackageFilesystemStore(self
._log
)
800 package_store
.refresh()
801 elif d_type
== "nsd":
802 package_store
= rift
.package
.store
.NsdPackageFilesystemStore(self
._log
)
803 package_store
.refresh()
806 script_extractor
= rift
.package
.script
.PackageScriptExtractor(self
._log
)
807 script
= script_extractor
.get_extracted_script_path(d_id
, script_name
)
809 self
._log
.debug("Checking for script at %s", script
)
810 if not os
.path
.exists(script
):
811 self
._log
.warning("Did not find script %s", script
)
814 # Seen cases in jenkins, where the script execution fails
815 # with permission denied. Setting the permission on script
816 # to make sure it has execute permission
817 perm
= os
.stat(script
).st_mode
818 if not (perm
& stat
.S_IXUSR
):
819 self
._log
.warning("NSR/VNFR {} script {} " \
820 "without execute permission: {}".
821 format(d_name
, script
, perm
))
822 os
.chmod(script
, perm | stat
.S_IXUSR
)
826 def process_ns_initial_config(self
, nsr_obj
):
827 '''Apply the initial-config-primitives specified in NSD'''
829 nsr
= yield from self
.cmdts_obj
.get_nsr(nsr_obj
.nsr_id
)
830 if 'initial_config_primitive' not in nsr
:
834 nsd
= yield from self
.cmdts_obj
.get_nsd(nsr_obj
.nsr_id
)
835 for conf
in nsr
['initial_config_primitive']:
836 self
._log
.debug("NSR {} initial config: {}".
837 format(nsr_obj
.nsr_name
, conf
))
838 script
= self
.get_script_file(conf
['user_defined_script'],
843 yield from self
.process_initial_config(nsr_obj
, conf
, script
)
846 def process_vnf_initial_config(self
, nsr_obj
, vnfr
):
847 '''Apply the initial-config-primitives specified in VNFD'''
849 vnfr_name
= vnfr
.name
852 vnf_cfg
= vnfd
.vnf_configuration
854 for conf
in vnf_cfg
.initial_config_primitive
:
855 self
._log
.debug("VNFR {} initial config: {} for vnfd id {}".
856 format(vnfr_name
, conf
, vnfd
.id))
858 if not conf
.user_defined_script
:
859 self
._log
.debug("VNFR {} did not find user defined script: {}".
860 format(vnfr_name
, conf
))
863 script
= self
.get_script_file(conf
.user_defined_script
,
868 yield from self
.process_initial_config(nsr_obj
,
874 class ConfigManagerNSR(object):
875 def __init__(self
, log
, loop
, parent
, id):
882 self
._parent
= parent
883 self
._log
.info("Instantiated NSR entry for id=%s", id)
884 self
.nsr_cfg_config_attributes_dict
= {}
885 self
.vnf_config_attributes_dict
= {}
886 self
.num_vnfs_to_cfg
= 0
888 self
.vnf_cfg_list
= []
889 self
.this_nsr_dir
= None
890 self
.being_deleted
= False
891 self
.dts_obj
= self
._parent
.cmdts_obj
893 # Initialize cm-state for this NS
895 self
.cm_nsr
['cm_vnfr'] = []
896 self
.cm_nsr
['id'] = id
897 self
.cm_nsr
['state'] = self
.state_to_string(conmanY
.RecordState
.INIT
)
898 self
.cm_nsr
['state_details'] = None
900 self
.set_nsr_name('Not Set')
902 # Add this NSR cm-state object to global cm-state
903 parent
.cm_state
['cm_nsr'].append(self
.cm_nsr
)
905 # Place holders for NSR & VNFR classes
906 self
.agent_nsr
= None
909 def nsr_opdata_xpath(self
):
910 ''' Returns full xpath for this NSR cm-state opdata '''
912 "D,/rw-conman:cm-state" +
913 "/rw-conman:cm-nsr[rw-conman:id='{}']"
914 ).format(self
._nsr
_id
)
918 return self
._vnfr
_list
929 def publish_cm_state(self
):
930 ''' This function publishes cm_state for this NSR '''
932 cm_state
= conmanY
.CmOpdata()
933 cm_state_nsr
= cm_state
.cm_nsr
.add()
934 cm_state_nsr
.from_dict(self
.cm_nsr
)
935 #with self._dts.transaction() as xact:
936 yield from self
.dts_obj
.update(self
.nsr_opdata_xpath
, cm_state_nsr
)
937 self
._log
.info("Published cm-state with xpath %s and nsr %s",
938 self
.nsr_opdata_xpath
,
942 def delete_cm_nsr(self
):
943 ''' This function publishes cm_state for this NSR '''
945 yield from self
.dts_obj
.delete(self
.nsr_opdata_xpath
)
946 self
._log
.info("Deleted cm-nsr with xpath %s",
947 self
.nsr_opdata_xpath
)
949 def set_nsr_name(self
, name
):
951 self
.cm_nsr
['name'] = name
953 def set_config_dir(self
, caller
):
954 self
.this_nsr_dir
= os
.path
.join(
955 caller
._parent
.cfg_dir
, self
.nsr_name
, caller
._nsr
['name_ref'])
956 if not os
.path
.exists(self
.this_nsr_dir
):
957 os
.makedirs(self
.this_nsr_dir
)
958 self
._log
.debug("NSR:(%s), Created configuration directory(%s)",
959 caller
._nsr
['name_ref'], self
.this_nsr_dir
)
960 self
.config_attributes_file
= os
.path
.join(self
.this_nsr_dir
, "configuration_config_attributes.yml")
961 self
.xlate_dict_file
= os
.path
.join(self
.this_nsr_dir
, "nsr_xlate_dict.yml")
963 def xlate_conf(self
, vnfr
, vnf_cfg
):
965 # If configuration type is not already set, try to read from attributes
966 if vnf_cfg
['interface_type'] is None:
967 # Prepare unique name for this VNF
968 vnf_unique_name
= get_vnf_unique_name(
971 vnfr
['member_vnf_index_ref'],
974 # Find this particular (unique) VNF's config attributes
975 if (vnf_unique_name
in self
.vnf_config_attributes_dict
):
976 vnf_cfg_config_attributes_dict
= self
.vnf_config_attributes_dict
[vnf_unique_name
]
977 vnf_cfg
['interface_type'] = vnf_cfg_config_attributes_dict
['configuration_type']
978 if 'configuration_options' in vnf_cfg_config_attributes_dict
:
979 cfg_opts
= vnf_cfg_config_attributes_dict
['configuration_options']
980 for key
, value
in cfg_opts
.items():
983 cfg_path_prefix
= '{}/{}/{}_{}'.format(
984 self
._parent
._parent
.cfg_dir
,
987 vnfr
['member_vnf_index_ref'],
990 vnf_cfg
['cfg_template'] = '{}_{}_template.cfg'.format(cfg_path_prefix
, vnf_cfg
['interface_type'])
991 vnf_cfg
['cfg_file'] = '{}.cfg'.format(cfg_path_prefix
)
992 vnf_cfg
['xlate_script'] = self
._parent
._parent
.cfg_dir
+ '/xlate_cfg.py'
994 self
._log
.debug("VNF endpoint so far: %s", vnf_cfg
)
996 self
._log
.info("Checking cfg_template %s", vnf_cfg
['cfg_template'])
997 if os
.path
.exists(vnf_cfg
['cfg_template']):
1001 def ConfigVNF(self
, vnfr
):
1003 vnf_cfg
= vnfr
['vnf_cfg']
1004 vnf_cm_state
= self
.find_or_create_vnfr_cm_state(vnf_cfg
)
1006 if (vnf_cm_state
['state'] == self
.state_to_string(conmanY
.RecordState
.READY_NO_CFG
)
1008 vnf_cm_state
['state'] == self
.state_to_string(conmanY
.RecordState
.READY
)):
1009 self
._log
.warning("NS/VNF (%s/%s) is already configured! Skipped.", self
.nsr_name
, vnfr
['short_name'])
1013 vnf_cm_state
['state'] = self
.state_to_string(conmanY
.RecordState
.CFG_PROCESS
)
1015 # Now translate the configuration for iP addresses
1017 # Add cp_dict members (TAGS) for this VNF
1018 self
._cp
_dict
['rw_mgmt_ip'] = vnf_cfg
['mgmt_ip_address']
1019 self
._cp
_dict
['rw_username'] = vnf_cfg
['username']
1020 self
._cp
_dict
['rw_password'] = vnf_cfg
['password']
1021 ############################################################
1022 # TBD - Need to lookup above 3 for a given VNF, not global #
1023 # Once we do that no need to dump below file again before #
1024 # each VNF configuration translation. #
1025 # This will require all existing config templates to be #
1026 # changed for above three tags to include member index #
1027 ############################################################
1029 nsr_obj
= vnf_cfg
['nsr_obj']
1030 # Generate config_config_attributes.yaml (For debug reference)
1031 with
open(nsr_obj
.xlate_dict_file
, "w") as yf
:
1032 yf
.write(yaml
.dump(nsr_obj
._cp
_dict
, default_flow_style
=False))
1033 except Exception as e
:
1034 self
._log
.error("NS:(%s) failed to write nsr xlate tags file as (%s)", nsr_obj
.nsr_name
, str(e
))
1036 if 'cfg_template' in vnf_cfg
:
1037 script_cmd
= 'python3 {} -i {} -o {} -x "{}"'.format(vnf_cfg
['xlate_script'], vnf_cfg
['cfg_template'], vnf_cfg
['cfg_file'], self
.xlate_dict_file
)
1038 self
._log
.debug("xlate script command (%s)", script_cmd
)
1039 #xlate_msg = subprocess.check_output(script_cmd).decode('utf-8')
1040 xlate_msg
= subprocess
.check_output(script_cmd
, shell
=True).decode('utf-8')
1041 self
._log
.info("xlate script output (%s)", xlate_msg
)
1042 except Exception as e
:
1043 vnf_cm_state
['state'] = self
.state_to_string(conmanY
.RecordState
.CFG_PROCESS_FAILED
)
1044 self
._log
.error("Failed to execute translation script for VNF: %s with (%s)", log_this_vnf(vnf_cfg
), str(e
))
1047 self
._log
.info("Applying config to VNF: %s = %s!", log_this_vnf(vnf_cfg
), vnf_cfg
)
1049 #self.vnf_cfg_list.append(vnf_cfg)
1050 self
._log
.debug("Scheduled configuration!")
1051 vnf_cm_state
['state'] = self
.state_to_string(conmanY
.RecordState
.CFG_SCHED
)
1052 except Exception as e
:
1053 self
._log
.error("Failed apply_vnf_config to VNF: %s as (%s)", log_this_vnf(vnf_cfg
), str(e
))
1054 vnf_cm_state
['state'] = self
.state_to_string(conmanY
.RecordState
.CFG_PROCESS_FAILED
)
1058 self
._log
.info("Adding NS Record for id=%s", id)
1061 def sample_cm_state(self
):
1068 'cfg_location': 'location1',
1069 'cfg_type': 'script',
1070 'connection_point': [
1071 {'ip_address': '1.1.1.1', 'name': 'vnf1cp1'},
1072 {'ip_address': '1.1.1.2', 'name': 'vnf1cp2'}
1075 'mgmt_interface': {'ip_address': '7.1.1.1',
1077 'name': 'vnfrname1',
1081 'cfg_location': 'location2',
1082 'cfg_type': 'netconf',
1083 'connection_point': [{'ip_address': '2.1.1.1', 'name': 'vnf2cp1'},
1084 {'ip_address': '2.1.1.2', 'name': 'vnf2cp2'}],
1086 'mgmt_interface': {'ip_address': '7.1.1.2',
1088 'name': 'vnfrname2',
1095 'states': 'Initialized, '
1098 def populate_vm_state_from_vnf_cfg(self
):
1099 # Fill in each VNFR from this nsr object
1100 vnfr_list
= self
._vnfr
_list
1101 for vnfr
in vnfr_list
:
1102 vnf_cfg
= vnfr
['vnf_cfg']
1103 vnf_cm_state
= self
.find_vnfr_cm_state(vnfr
['id'])
1106 # Fill in VNF management interface
1107 vnf_cm_state
['mgmt_interface']['ip_address'] = vnf_cfg
['mgmt_ip_address']
1108 vnf_cm_state
['mgmt_interface']['port'] = vnf_cfg
['port']
1110 # Fill in VNF configuration details
1111 vnf_cm_state
['cfg_type'] = vnf_cfg
['config_method']
1112 vnf_cm_state
['cfg_location'] = vnf_cfg
['cfg_file']
1114 # Fill in each connection-point for this VNF
1115 if "connection_point" in vnfr
:
1116 cp_list
= vnfr
['connection_point']
1117 for cp_item_dict
in cp_list
:
1118 vnf_cm_state
['connection_point'].append(
1120 'name' : cp_item_dict
['name'],
1121 'ip_address' : cp_item_dict
['ip_address'],
1125 def state_to_string(self
, state
):
1127 conmanY
.RecordState
.INIT
: "init",
1128 conmanY
.RecordState
.RECEIVED
: "received",
1129 conmanY
.RecordState
.CFG_PROCESS
: "cfg_process",
1130 conmanY
.RecordState
.CFG_PROCESS_FAILED
: "cfg_process_failed",
1131 conmanY
.RecordState
.CFG_SCHED
: "cfg_sched",
1132 conmanY
.RecordState
.CFG_DELAY
: "cfg_delay",
1133 conmanY
.RecordState
.CONNECTING
: "connecting",
1134 conmanY
.RecordState
.FAILED_CONNECTION
: "failed_connection",
1135 conmanY
.RecordState
.NETCONF_CONNECTED
: "netconf_connected",
1136 conmanY
.RecordState
.NETCONF_SSH_CONNECTED
: "netconf_ssh_connected",
1137 conmanY
.RecordState
.RESTCONF_CONNECTED
: "restconf_connected",
1138 conmanY
.RecordState
.CFG_SEND
: "cfg_send",
1139 conmanY
.RecordState
.CFG_FAILED
: "cfg_failed",
1140 conmanY
.RecordState
.READY_NO_CFG
: "ready_no_cfg",
1141 conmanY
.RecordState
.READY
: "ready",
1143 return state_dict
[state
]
1145 def find_vnfr_cm_state(self
, id):
1146 if self
.cm_nsr
['cm_vnfr']:
1147 for vnf_cm_state
in self
.cm_nsr
['cm_vnfr']:
1148 if vnf_cm_state
['id'] == id:
1152 def find_or_create_vnfr_cm_state(self
, vnf_cfg
):
1153 vnfr
= vnf_cfg
['vnfr']
1154 vnf_cm_state
= self
.find_vnfr_cm_state(vnfr
['id'])
1156 if vnf_cm_state
is None:
1157 # Not found, Create and Initialize this VNF cm-state
1160 'name' : vnfr
['short_name'],
1161 'state' : self
.state_to_string(conmanY
.RecordState
.RECEIVED
),
1164 'ip_address' : vnf_cfg
['mgmt_ip_address'],
1165 'port' : vnf_cfg
['port'],
1167 'cfg_type' : vnf_cfg
['config_method'],
1168 'cfg_location' : vnf_cfg
['cfg_file'],
1169 'connection_point' : [],
1171 self
.cm_nsr
['cm_vnfr'].append(vnf_cm_state
)
1173 # Publish newly created cm-state
1179 def get_vnf_cm_state(self
, vnfr
):
1181 vnf_cm_state
= self
.find_vnfr_cm_state(vnfr
['id'])
1183 return vnf_cm_state
['state']
1187 def update_vnf_cm_state(self
, vnfr
, state
):
1189 vnf_cm_state
= self
.find_vnfr_cm_state(vnfr
['id'])
1190 if vnf_cm_state
is None:
1191 self
._log
.error("No opdata found for NS/VNF:%s/%s!",
1192 self
.nsr_name
, vnfr
['short_name'])
1195 if vnf_cm_state
['state'] != self
.state_to_string(state
):
1196 old_state
= vnf_cm_state
['state']
1197 vnf_cm_state
['state'] = self
.state_to_string(state
)
1199 yield from self
.publish_cm_state()
1200 self
._log
.info("VNF ({}/{}/{}) state change: {} -> {}"
1201 .format(self
.nsr_name
,
1203 vnfr
['member_vnf_index_ref'],
1205 vnf_cm_state
['state']))
1208 self
._log
.error("No VNFR supplied for state update (NS=%s)!",
1212 def get_ns_cm_state(self
):
1213 return self
.cm_nsr
['state']
1216 def update_ns_cm_state(self
, state
, state_details
=None):
1217 if self
.cm_nsr
['state'] != self
.state_to_string(state
):
1218 old_state
= self
.cm_nsr
['state']
1219 self
.cm_nsr
['state'] = self
.state_to_string(state
)
1220 self
.cm_nsr
['state_details'] = state_details
if state_details
is not None else None
1221 self
._log
.info("NS ({}) state change: {} -> {}"
1222 .format(self
.nsr_name
,
1224 self
.cm_nsr
['state']))
1226 yield from self
.publish_cm_state()
1229 def add_vnfr(self
, vnfr
, vnfr_msg
):
1232 def populate_subnets_from_vlr(id):
1234 # Populate cp_dict with VLR subnet info
1235 vlr
= yield from self
.dts_obj
.get_vlr(id)
1236 if vlr
is not None and 'assigned_subnet' in vlr
:
1237 subnet
= {vlr
.name
:vlr
.assigned_subnet
}
1238 self
._cp
_dict
[vnfr
['member_vnf_index_ref']].update(subnet
)
1239 self
._cp
_dict
.update(subnet
)
1240 self
._log
.debug("VNF:(%s) Updated assigned subnet = %s",
1241 vnfr
['short_name'], subnet
)
1242 except Exception as e
:
1243 self
._log
.error("VNF:(%s) VLR Error = %s",
1244 vnfr
['short_name'], e
)
1246 if vnfr
['id'] not in self
._vnfr
_dict
:
1247 self
._log
.info("NSR(%s) : Adding VNF Record for name=%s, id=%s", self
._nsr
_id
, vnfr
['short_name'], vnfr
['id'])
1248 # Add this vnfr to the list for show, or single traversal
1249 self
._vnfr
_list
.append(vnfr
)
1251 self
._log
.warning("NSR(%s) : VNF Record for name=%s, id=%s already exists, overwriting", self
._nsr
_id
, vnfr
['short_name'], vnfr
['id'])
1253 # Make vnfr available by id as well as by name
1254 unique_name
= get_vnf_unique_name(self
.nsr_name
, vnfr
['short_name'], vnfr
['member_vnf_index_ref'])
1255 self
._vnfr
_dict
[unique_name
] = vnfr
1256 self
._vnfr
_dict
[vnfr
['id']] = vnfr
1258 # Create vnf_cfg dictionary with default values
1262 'agent_vnfr' : self
.agent_nsr
.add_vnfr(vnfr
, vnfr_msg
),
1263 'nsr_name' : self
.nsr_name
,
1264 'nsr_id' : self
._nsr
_id
,
1265 'vnfr_name' : vnfr
['short_name'],
1266 'member_vnf_index' : vnfr
['member_vnf_index_ref'],
1268 'username' : 'admin',
1269 'password' : 'admin',
1270 'config_method' : 'None',
1271 'protocol' : 'None',
1272 'mgmt_ip_address' : '0.0.0.0',
1273 'cfg_file' : 'None',
1275 'script_type' : 'bash',
1278 # Update the mgmt ip address
1279 # In case the config method is none, this is not
1282 vnf_cfg
['mgmt_ip_address'] = vnfr_msg
.mgmt_interface
.ip_address
1283 vnf_cfg
['port'] = vnfr_msg
.mgmt_interface
.port
1284 except Exception as e
:
1286 "VNFR {}({}), unable to retrieve mgmt ip address: {}".
1287 format(vnfr
['short_name'], vnfr
['id'], e
))
1289 vnfr
['vnf_cfg'] = vnf_cfg
1290 self
.find_or_create_vnfr_cm_state(vnf_cfg
)
1293 Build the connection-points list for this VNF (self._cp_dict)
1295 # Populate global CP list self._cp_dict from VNFR
1297 if 'connection_point' in vnfr
:
1298 cp_list
= vnfr
['connection_point']
1300 self
._cp
_dict
[vnfr
['member_vnf_index_ref']] = {}
1302 for vdur
in vnfr
['vdur']:
1303 if 'internal_connection_point' in vdur
:
1304 cp_list
+= vdur
['internal_connection_point']
1306 for cp_item_dict
in cp_list
:
1307 # Populate global dictionary
1309 cp_item_dict
['name']
1310 ] = cp_item_dict
['ip_address']
1312 # Populate unique member specific dictionary
1314 vnfr
['member_vnf_index_ref']
1316 cp_item_dict
['name']
1317 ] = cp_item_dict
['ip_address']
1319 # Fill in the subnets from vlr
1320 if 'vlr_ref' in cp_item_dict
:
1321 ### HACK: Internal connection_point do not have VLR reference
1322 yield from populate_subnets_from_vlr(cp_item_dict
['vlr_ref'])
1324 if 'internal_vlr' in vnfr
:
1325 for ivlr
in vnfr
['internal_vlr']:
1326 yield from populate_subnets_from_vlr(ivlr
['vlr_ref'])
1329 vnf_cfg
['agent_vnfr']._vnfr
= vnfr
1330 return vnf_cfg
['agent_vnfr']
1333 class XPaths(object):
1335 def nsr_opdata(k
=None):
1336 return ("D,/nsr:ns-instance-opdata/nsr:nsr" +
1337 ("[nsr:ns-instance-config-ref='{}']".format(k
) if k
is not None else ""))
1340 def nsd_msg(k
=None):
1341 return ("C,/nsd:nsd-catalog/nsd:nsd" +
1342 "[nsd:id = '{}']".format(k
) if k
is not None else "")
1345 def vnfr_opdata(k
=None):
1346 return ("D,/vnfr:vnfr-catalog/vnfr:vnfr" +
1347 ("[vnfr:id='{}']".format(k
) if k
is not None else ""))
1351 return ("C,/vnfd:vnfd-catalog/vnfd:vnfd" +
1352 ("[vnfd:id='{}']".format(k
) if k
is not None else ""))
1355 def config_agent(k
=None):
1356 return ("D,/rw-config-agent:config-agent/rw-config-agent:account" +
1357 ("[rw-config-agent:name='{}']".format(k
) if k
is not None else ""))
1360 def nsr_config(k
=None):
1361 return ("C,/nsr:ns-instance-config/nsr:nsr[nsr:id='{}']".format(k
) if k
is not None else "")
1365 return ("D,/vlr:vlr-catalog/vlr:vlr[vlr:id='{}']".format(k
) if k
is not None else "")
1367 class ConfigManagerDTS(object):
1368 ''' This class either reads from DTS or publishes to DTS '''
1370 def __init__(self
, log
, loop
, parent
, dts
):
1373 self
._parent
= parent
1377 def _read_dts(self
, xpath
, do_trace
=False):
1378 self
._log
.debug("_read_dts path = %s", xpath
)
1379 flags
= rwdts
.XactFlag
.MERGE
1380 res_iter
= yield from self
._dts
.query_read(
1387 result
= yield from i
1388 if result
is not None:
1389 results
.append(result
.result
)
1397 def get_nsr(self
, id):
1398 self
._log
.debug("Attempting to get NSR: %s", id)
1399 nsrl
= yield from self
._read
_dts
(XPaths
.nsr_opdata(id), False)
1402 nsr
= nsrl
[0].as_dict()
1406 def get_nsr_config(self
, id):
1407 self
._log
.debug("Attempting to get config NSR: %s", id)
1408 nsrl
= yield from self
._read
_dts
(XPaths
.nsr_config(id), False)
1415 def get_nsd_msg(self
, id):
1416 self
._log
.debug("Attempting to get NSD: %s", id)
1417 nsdl
= yield from self
._read
_dts
(XPaths
.nsd_msg(id), False)
1424 def get_nsd(self
, nsr_id
):
1425 self
._log
.debug("Attempting to get NSD for NSR: %s", id)
1426 nsr_config
= yield from self
.get_nsr_config(nsr_id
)
1427 nsd_msg
= nsr_config
.nsd
1431 def get_vnfr(self
, id):
1432 self
._log
.debug("Attempting to get VNFR: %s", id)
1433 vnfrl
= yield from self
._read
_dts
(XPaths
.vnfr_opdata(id), do_trace
=False)
1440 def get_vnfd(self
, vnfd_id
):
1441 self
._log
.debug("Attempting to get VNFD: %s", vnfd_id
)
1442 vnfdl
= yield from self
._read
_dts
(XPaths
.vnfd(vnfd_id
), do_trace
=False)
1449 def get_vlr(self
, id):
1450 self
._log
.debug("Attempting to get VLR subnet: %s", id)
1451 vlrl
= yield from self
._read
_dts
(XPaths
.vlr(id), do_trace
=True)
1458 def get_config_agents(self
, name
):
1459 self
._log
.debug("Attempting to get config_agents: %s", name
)
1460 cfgagentl
= yield from self
._read
_dts
(XPaths
.config_agent(name
), False)
1464 def update(self
, path
, msg
, flags
=rwdts
.XactFlag
.REPLACE
):
1466 Update a cm-state (cm-nsr) record in DTS with the path and message
1468 self
._log
.debug("Updating cm-state %s:%s dts_pub_hdl = %s", path
, msg
, self
.dts_pub_hdl
)
1469 self
.dts_pub_hdl
.update_element(path
, msg
, flags
)
1470 self
._log
.debug("Updated cm-state, %s:%s", path
, msg
)
1473 def delete(self
, path
):
1475 Delete cm-nsr record in DTS with the path only
1477 self
._log
.debug("Deleting cm-nsr %s dts_pub_hdl = %s", path
, self
.dts_pub_hdl
)
1478 self
.dts_pub_hdl
.delete_element(path
)
1479 self
._log
.debug("Deleted cm-nsr, %s", path
)
1483 yield from self
.register_to_publish()
1484 yield from self
.register_for_nsr()
1487 def register_to_publish(self
):
1488 ''' Register to DTS for publishing cm-state opdata '''
1490 xpath
= "D,/rw-conman:cm-state/rw-conman:cm-nsr"
1491 self
._log
.debug("Registering to publish cm-state @ %s", xpath
)
1492 hdl
= rift
.tasklets
.DTS
.RegistrationHandler()
1493 with self
._dts
.group_create() as group
:
1494 self
.dts_pub_hdl
= group
.register(xpath
=xpath
,
1496 flags
=rwdts
.Flag
.PUBLISHER | rwdts
.Flag
.NO_PREP_READ
)
1499 def nsr_xpath(self
):
1500 return "D,/nsr:ns-instance-opdata/nsr:nsr"
1503 def register_for_nsr(self
):
1504 """ Register for NSR changes """
1507 def on_prepare(xact_info
, query_action
, ks_path
, msg
):
1508 """ This NSR is created """
1509 self
._log
.debug("Received NSR instantiate on_prepare (%s:%s:%s)",
1514 if (query_action
== rwdts
.QueryAction
.UPDATE
or
1515 query_action
== rwdts
.QueryAction
.CREATE
):
1516 msg_dict
= msg
.as_dict()
1517 # Update Each NSR/VNFR state)
1518 if ('operational_status' in msg_dict
and
1519 msg_dict
['operational_status'] == 'running'):
1520 # Add to the task list
1521 self
._parent
.add_to_pending_tasks({'nsrid' : msg_dict
['ns_instance_config_ref'], 'retries' : 5})
1522 elif query_action
== rwdts
.QueryAction
.DELETE
:
1523 nsr_id
= msg
.ns_instance_config_ref
1524 asyncio
.ensure_future(self
._parent
.terminate_NSR(nsr_id
), loop
=self
._loop
)
1526 raise NotImplementedError(
1527 "%s action on cm-state not supported",
1530 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
1533 handler
= rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_prepare
)
1534 self
.dts_reg_hdl
= yield from self
._dts
.register(self
.nsr_xpath
,
1535 flags
=rwdts
.Flag
.SUBSCRIBER | rwdts
.Flag
.DELTA_READY
,
1537 except Exception as e
:
1538 self
._log
.error("Failed to register for NSR changes as %s", str(e
))