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
,
34 from . import rwconman_conagent
as conagent
35 from . import RiftCM_rpc
36 from . import riftcm_config_plugin
38 if sys
.version_info
< (3, 4, 4):
39 asyncio
.ensure_future
= asyncio
.async
41 def get_vnf_unique_name(nsr_name
, vnfr_short_name
, member_vnf_index
):
42 return "{}.{}.{}".format(nsr_name
, vnfr_short_name
, member_vnf_index
)
44 class ConmanConfigError(Exception):
48 class InitialConfigError(ConmanConfigError
):
52 def log_this_vnf(vnf_cfg
):
54 used_item_list
= ['nsr_name', 'vnfr_name', 'member_vnf_index', 'mgmt_ip_address']
55 for item
in used_item_list
:
57 if item
== 'mgmt_ip_address':
58 log_vnf
+= "({})".format(vnf_cfg
[item
])
60 log_vnf
+= "{}/".format(vnf_cfg
[item
])
63 class PretendNsm(object):
64 def __init__(self
, dts
, log
, loop
, parent
):
70 self
._nsr
_dict
= parent
._nsr
_dict
71 self
._config
_agent
_plugins
= []
76 # Expensive, instead use get_nsr, if you know id.
78 # Update the list of nsrs (agent nsr)
79 for id, nsr_obj
in self
._nsr
_dict
.items():
80 self
._nsrs
[id] = nsr_obj
.agent_nsr
83 def get_nsr(self
, nsr_id
):
84 if nsr_id
in self
._nsr
_dict
:
85 nsr_obj
= self
._nsr
_dict
[nsr_id
]
89 def get_vnfr_msg(self
, vnfr_id
, nsr_id
=None):
90 self
._log
.debug("get_vnfr_msg(vnfr=%s, nsr=%s)",
94 if nsr_id
in self
._nsr
_dict
:
95 nsr_obj
= self
._nsr
_dict
[nsr_id
]
96 if vnfr_id
in nsr_obj
._vnfr
_dict
:
99 for nsr_obj
in self
._nsr
_dict
.values():
100 if vnfr_id
in nsr_obj
._vnfr
_dict
:
105 vnf_cfg
= nsr_obj
._vnfr
_dict
[vnfr_id
]['vnf_cfg']
106 return vnf_cfg
['agent_vnfr'].vnfr_msg
111 def get_nsd(self
, nsr_id
):
112 if nsr_id
not in self
._nsd
_msg
:
113 nsr_config
= yield from self
._parent
.cmdts_obj
.get_nsr_config(nsr_id
)
114 self
._nsd
_msg
[nsr_id
] = nsr_config
.nsd
115 return self
._nsd
_msg
[nsr_id
]
118 def config_agent_plugins(self
):
119 self
._config
_agent
_plugins
= []
120 for agent
in self
._parent
._config
_agent
_mgr
._plugin
_instances
.values():
121 self
._config
_agent
_plugins
.append(agent
)
122 return self
._config
_agent
_plugins
124 class ConfigManagerConfig(object):
125 def __init__(self
, dts
, log
, loop
, parent
):
129 self
._parent
= parent
131 self
.pending_cfg
= {}
132 self
.terminate_cfg
= {}
133 self
.pending_tasks
= [] # User for NSRid get retry
134 # (mainly excercised at restart case)
135 self
._config
_xpath
= "C,/cm-config"
136 self
._opdata
_xpath
= "D,/rw-conman:cm-state"
138 self
.cm_config
= conmanY
.SoConfig()
139 # RO specific configuration
141 for key
in self
.cm_config
.ro_endpoint
.fields
:
142 self
.ro_config
[key
] = None
144 # Initialize cm-state
146 self
.cm_state
['cm_nsr'] = []
147 self
.cm_state
['states'] = "Initialized"
149 # Initialize objects to register
150 self
.cmdts_obj
= ConfigManagerDTS(self
._log
, self
._loop
, self
, self
._dts
)
151 self
._config
_agent
_mgr
= conagent
.RiftCMConfigAgent(
159 self
._config
_agent
_mgr
,
160 RiftCM_rpc
.RiftCMRPCHandler(self
._dts
, self
._log
, self
._loop
,
162 self
._dts
, self
._log
, self
._loop
, self
)),
165 def is_nsr_valid(self
, nsr_id
):
166 if nsr_id
in self
._nsr
_dict
:
170 def add_to_pending_tasks(self
, task
):
171 if self
.pending_tasks
:
172 for p_task
in self
.pending_tasks
:
173 if p_task
['nsrid'] == task
['nsrid']:
177 self
.pending_tasks
.append(task
)
178 self
._log
.debug("add_to_pending_tasks (nsrid:%s)",
180 if len(self
.pending_tasks
) == 1:
181 self
._loop
.create_task(self
.ConfigManagerConfig_pending_loop())
182 # TBD - change to info level
183 self
._log
.debug("Started pending_loop!")
184 except Exception as e
:
185 self
._log
.error("Failed adding to pending tasks (%s)", str(e
))
187 def del_from_pending_tasks(self
, task
):
189 self
.pending_tasks
.remove(task
)
190 except Exception as e
:
191 self
._log
.error("Failed removing from pending tasks (%s)", str(e
))
194 def ConfigManagerConfig_pending_loop(self
):
197 yield from asyncio
.sleep(loop_sleep
, loop
=self
._loop
)
199 This pending task queue is ordred by events,
200 must finish previous task successfully to be able to go on to the next task
202 if self
.pending_tasks
:
203 self
._log
.debug("self.pending_tasks len=%s", len(self
.pending_tasks
))
204 task
= self
.pending_tasks
[0]
207 nsrid
= task
['nsrid']
208 self
._log
.debug("Will execute pending task for NSR id(%s)", nsrid
)
210 # Try to configure this NSR
212 done
= yield from self
.config_NSR(nsrid
)
213 self
._log
.info("self.config_NSR status=%s", done
)
215 except Exception as e
:
216 self
._log
.error("Failed(%s) configuring NSR(%s)," \
217 "retries remained:%d!",
218 str(e
), nsrid
, task
['retries'])
220 self
.pending_tasks
.remove(task
)
223 self
._log
.debug("Finished pending task NSR id(%s):", nsrid
)
225 self
._log
.error("Failed configuring NSR(%s), retries remained:%d!",
226 nsrid
, task
['retries'])
228 # Failed, re-insert (append at the end)
229 # this failed task to be retried later
230 # If any retries remained.
232 self
.pending_tasks
.append(task
)
234 self
._log
.debug("Stopped pending_loop!")
239 yield from self
.register_cm_state_opdata()
241 # Initialize all handles that needs to be registered
242 for reg
in self
.reg_handles
:
243 yield from reg
.register()
246 def register_cm_state_opdata(self
):
248 def state_to_string(state
):
250 conmanY
.RecordState
.INIT
: "init",
251 conmanY
.RecordState
.RECEIVED
: "received",
252 conmanY
.RecordState
.CFG_PROCESS
: "cfg_process",
253 conmanY
.RecordState
.CFG_PROCESS_FAILED
: "cfg_process_failed",
254 conmanY
.RecordState
.CFG_SCHED
: "cfg_sched",
255 conmanY
.RecordState
.CFG_DELAY
: "cfg_delay",
256 conmanY
.RecordState
.CONNECTING
: "connecting",
257 conmanY
.RecordState
.FAILED_CONNECTION
: "failed_connection",
258 conmanY
.RecordState
.NETCONF_CONNECTED
: "netconf_connected",
259 conmanY
.RecordState
.NETCONF_SSH_CONNECTED
: "netconf_ssh_connected",
260 conmanY
.RecordState
.RESTCONF_CONNECTED
: "restconf_connected",
261 conmanY
.RecordState
.CFG_SEND
: "cfg_send",
262 conmanY
.RecordState
.CFG_FAILED
: "cfg_failed",
263 conmanY
.RecordState
.READY_NO_CFG
: "ready_no_cfg",
264 conmanY
.RecordState
.READY
: "ready",
266 return state_dict
[state
]
269 def on_prepare(xact_info
, action
, ks_path
, msg
):
271 self
._log
.debug("Received cm-state: msg=%s, action=%s", msg
, action
)
273 if action
== rwdts
.QueryAction
.READ
:
274 show_output
= conmanY
.CmOpdata()
275 show_output
.from_dict(self
.cm_state
)
276 self
._log
.debug("Responding to SHOW cm-state: %s", self
.cm_state
)
277 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
,
278 xpath
=self
._opdata
_xpath
,
281 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
283 self
._log
.info("Registering for cm-opdata xpath: %s",
287 handler
=rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_prepare
)
288 yield from self
._dts
.register(xpath
=self
._opdata
_xpath
,
290 flags
=rwdts
.Flag
.PUBLISHER
)
291 self
._log
.info("Successfully registered for opdata(%s)", self
._opdata
_xpath
)
292 except Exception as e
:
293 self
._log
.error("Failed to register for opdata as (%s)", e
)
296 def process_nsd_vnf_configuration(self
, nsr_obj
, vnfr
):
298 def get_config_method(vnf_config
):
299 cfg_types
= ['netconf', 'juju', 'script']
300 for method
in cfg_types
:
301 if method
in vnf_config
:
305 def get_cfg_file_extension(method
, configuration_options
):
315 if method
== "netconf":
316 return ext_dict
[method
]
317 elif method
== "script":
318 return ext_dict
[method
][configuration_options
['script_type']]
319 elif method
== "juju":
320 return ext_dict
[method
]
324 # This is how the YAML file should look like,
325 # This routine will be called for each VNF, so keep appending the file.
326 # priority order is determined by the number,
327 # hence no need to generate the file in that order. A dictionary will be
328 # used that will take care of the order by number.
330 1 : <== This is priority
333 configuration_delay : 120
334 configuration_type : netconf
335 configuration_options :
343 configuration_delay : 0
344 configuration_type : netconf
345 configuration_options :
352 # Save some parameters needed as short cuts in flat structure (Also generated)
353 vnf_cfg
= vnfr
['vnf_cfg']
354 # Prepare unique name for this VNF
355 vnf_cfg
['vnf_unique_name'] = get_vnf_unique_name(
356 vnf_cfg
['nsr_name'], vnfr
['short_name'], vnfr
['member_vnf_index_ref'])
358 nsr_obj
.cfg_path_prefix
= '{}/{}_{}'.format(
359 nsr_obj
.this_nsr_dir
, vnfr
['short_name'], vnfr
['member_vnf_index_ref'])
360 nsr_vnfr
= '{}/{}_{}'.format(
361 vnf_cfg
['nsr_name'], vnfr
['short_name'], vnfr
['member_vnf_index_ref'])
363 # Get vnf_configuration from vnfr
364 vnf_config
= vnfr
['vnf_configuration']
366 self
._log
.debug("vnf_configuration = %s", vnf_config
)
368 # Create priority dictionary
369 cfg_priority_order
= 0
370 if ('config_attributes' in vnf_config
and
371 'config_priority' in vnf_config
['config_attributes']):
372 cfg_priority_order
= vnf_config
['config_attributes']['config_priority']
374 if cfg_priority_order
not in nsr_obj
.nsr_cfg_config_attributes_dict
:
375 # No VNFR with this priority yet, initialize the list
376 nsr_obj
.nsr_cfg_config_attributes_dict
[cfg_priority_order
] = []
378 method
= get_config_method(vnf_config
)
379 if method
is not None:
380 # Create all sub dictionaries first
383 'name' : vnfr
['short_name'],
384 'member_vnf_index' : vnfr
['member_vnf_index_ref'],
387 if 'config_delay' in vnf_config
['config_attributes']:
388 config_priority
['configuration_delay'] = vnf_config
['config_attributes']['config_delay']
389 vnf_cfg
['config_delay'] = config_priority
['configuration_delay']
391 configuration_options
= {}
392 self
._log
.debug("config method=%s", method
)
393 config_priority
['configuration_type'] = method
394 vnf_cfg
['config_method'] = method
396 # Set config agent based on method
397 self
._config
_agent
_mgr
.set_config_agent(
398 nsr_obj
.agent_nsr
, vnf_cfg
['agent_vnfr'], method
)
401 'port', 'target', 'script_type', 'ip_address', 'user', 'secret',
403 for cfg_opt
in cfg_opt_list
:
404 if cfg_opt
in vnf_config
[method
]:
405 configuration_options
[cfg_opt
] = vnf_config
[method
][cfg_opt
]
406 vnf_cfg
[cfg_opt
] = configuration_options
[cfg_opt
]
408 cfg_opt_list
= ['mgmt_ip_address', 'username', 'password']
409 for cfg_opt
in cfg_opt_list
:
410 if cfg_opt
in vnf_config
['config_access']:
411 configuration_options
[cfg_opt
] = vnf_config
['config_access'][cfg_opt
]
412 vnf_cfg
[cfg_opt
] = configuration_options
[cfg_opt
]
415 vnf_cp_dict
= nsr_obj
._cp
_dict
[vnfr
['member_vnf_index_ref']]
416 vnf_cp_dict
['rw_mgmt_ip'] = vnf_cfg
['mgmt_ip_address']
417 vnf_cp_dict
['rw_username'] = vnf_cfg
['username']
418 vnf_cp_dict
['rw_password'] = vnf_cfg
['password']
421 # TBD - see if we can neatly include the config in "config_attributes" file, no need though
422 #config_priority['config_template'] = vnf_config['config_template']
424 vnf_cfg
['juju_script'] = os
.path
.join(self
._parent
.cfg_dir
, 'juju_if.py')
426 if 'config_template' in vnf_config
:
427 vnf_cfg
['cfg_template'] = '{}_{}_template.cfg'.format(nsr_obj
.cfg_path_prefix
, config_priority
['configuration_type'])
428 vnf_cfg
['cfg_file'] = '{}.{}'.format(nsr_obj
.cfg_path_prefix
, get_cfg_file_extension(method
, configuration_options
))
429 vnf_cfg
['xlate_script'] = os
.path
.join(self
._parent
.cfg_dir
, 'xlate_cfg.py')
431 # Now write this template into file
432 with
open(vnf_cfg
['cfg_template'], "w") as cf
:
433 cf
.write(vnf_config
['config_template'])
434 except Exception as e
:
435 self
._log
.error("Processing NSD, failed to generate configuration template : %s (Error : %s)",
436 vnf_config
['config_template'], str(e
))
439 self
._log
.debug("VNF endpoint so far: %s", vnf_cfg
)
441 # Populate filled up dictionary
442 config_priority
['configuration_options'] = configuration_options
443 nsr_obj
.nsr_cfg_config_attributes_dict
[cfg_priority_order
].append(config_priority
)
444 nsr_obj
.num_vnfs_to_cfg
+= 1
445 nsr_obj
._vnfr
_dict
[vnf_cfg
['vnf_unique_name']] = vnfr
446 nsr_obj
._vnfr
_dict
[vnfr
['id']] = vnfr
448 self
._log
.debug("VNF:(%s) config_attributes = %s",
449 log_this_vnf(vnfr
['vnf_cfg']),
450 nsr_obj
.nsr_cfg_config_attributes_dict
)
452 self
._log
.info("VNF:(%s) is not to be configured by Configuration Manager!",
453 log_this_vnf(vnfr
['vnf_cfg']))
454 yield from nsr_obj
.update_vnf_cm_state(vnfr
, conmanY
.RecordState
.READY_NO_CFG
)
456 # Update the cm-state
457 nsr_obj
.populate_vm_state_from_vnf_cfg()
460 def config_NSR(self
, id):
462 def my_yaml_dump(config_attributes_dict
, yf
):
464 yaml_dict
= dict(sorted(config_attributes_dict
.items()))
465 yf
.write(yaml
.dump(yaml_dict
, default_flow_style
=False))
467 nsr_dict
= self
._nsr
_dict
468 self
._log
.info("Configure NSR, id = %s", id)
470 #####################TBD###########################
471 # yield from self._config_agent_mgr.invoke_config_agent_plugins('notify_create_nsr', self.id, self._nsd)
472 # yield from self._config_agent_mgr.invoke_config_agent_plugins('notify_nsr_active', self.id, self._vnfrs)
475 if id not in nsr_dict
:
476 nsr_obj
= ConfigManagerNSR(self
._log
, self
._loop
, self
, id)
477 nsr_dict
[id] = nsr_obj
479 self
._log
.info("NSR(%s) is already initialized!", id)
480 nsr_obj
= nsr_dict
[id]
481 except Exception as e
:
482 self
._log
.error("Failed creating NSR object for (%s) as (%s)", id, str(e
))
485 # Try to configure this NSR only if not already processed
486 if nsr_obj
.cm_nsr
['state'] != nsr_obj
.state_to_string(conmanY
.RecordState
.INIT
):
487 self
._log
.debug("NSR(%s) is already processed, state=%s",
488 nsr_obj
.nsr_name
, nsr_obj
.cm_nsr
['state'])
489 yield from nsr_obj
.publish_cm_state()
492 cmdts_obj
= self
.cmdts_obj
495 nsr
= yield from cmdts_obj
.get_nsr(id)
496 self
._log
.debug("Full NSR : %s", nsr
)
497 if nsr
['operational_status'] != "running":
498 self
._log
.info("NSR(%s) is not ready yet!", nsr
['nsd_name_ref'])
502 # Create Agent NSR class
503 nsr_config
= yield from cmdts_obj
.get_nsr_config(id)
504 self
._log
.debug("NSR {} config: {}".format(id, nsr_config
))
505 nsr_obj
.agent_nsr
= riftcm_config_plugin
.RiftCMnsr(nsr
, nsr_config
)
508 yield from nsr_obj
.update_ns_cm_state(conmanY
.RecordState
.RECEIVED
)
512 nsr_obj
.set_nsr_name(nsr
['name_ref'])
513 nsr_dir
= os
.path
.join(self
._parent
.cfg_dir
, nsr_obj
.nsr_name
)
514 self
._log
.info("Checking NS config directory: %s", nsr_dir
)
515 if not os
.path
.isdir(nsr_dir
):
517 # self._log.critical("NS %s is not to be configured by Service Orchestrator!", nsr_obj.nsr_name)
518 # yield from nsr_obj.update_ns_cm_state(conmanY.RecordState.READY_NO_CFG)
521 nsr_obj
.set_config_dir(self
)
523 for const_vnfr
in nsr
['constituent_vnfr_ref']:
524 self
._log
.debug("Fetching VNFR (%s)", const_vnfr
['vnfr_id'])
525 vnfr_msg
= yield from cmdts_obj
.get_vnfr(const_vnfr
['vnfr_id'])
527 vnfr
= vnfr_msg
.as_dict()
528 self
._log
.info("create VNF:{}/{}".format(nsr_obj
.nsr_name
, vnfr
['short_name']))
529 agent_vnfr
= yield from nsr_obj
.add_vnfr(vnfr
, vnfr_msg
)
531 # Preserve order, self.process_nsd_vnf_configuration()
532 # sets up the config agent based on the method
533 yield from self
.process_nsd_vnf_configuration(nsr_obj
, vnfr
)
534 yield from self
._config
_agent
_mgr
.invoke_config_agent_plugins(
535 'notify_create_vnfr',
539 #####################TBD###########################
540 # self._log.debug("VNF active. Apply initial config for vnfr {}".format(vnfr.name))
541 # yield from self._config_agent_mgr.invoke_config_agent_plugins('apply_initial_config',
543 # yield from self._config_agent_mgr.invoke_config_agent_plugins('notify_terminate_vnf', self.id, vnfr)
545 except Exception as e
:
546 self
._log
.error("Failed processing NSR (%s) as (%s)", nsr_obj
.nsr_name
, str(e
))
547 self
._log
.exception(e
)
548 yield from nsr_obj
.update_ns_cm_state(conmanY
.RecordState
.CFG_PROCESS_FAILED
)
552 # Generate config_config_attributes.yaml (For debug reference)
553 with
open(nsr_obj
.config_attributes_file
, "w") as yf
:
554 my_yaml_dump(nsr_obj
.nsr_cfg_config_attributes_dict
, yf
)
555 except Exception as e
:
556 self
._log
.error("NS:(%s) failed to write config attributes file as (%s)", nsr_obj
.nsr_name
, str(e
))
559 # Generate nsr_xlate_dict.yaml (For debug reference)
560 with
open(nsr_obj
.xlate_dict_file
, "w") as yf
:
561 yf
.write(yaml
.dump(nsr_obj
._cp
_dict
, default_flow_style
=False))
562 except Exception as e
:
563 self
._log
.error("NS:(%s) failed to write nsr xlate tags file as (%s)", nsr_obj
.nsr_name
, str(e
))
565 self
._log
.debug("Starting to configure each VNF")
567 # Check if this NS has input parametrs
568 self
._log
.info("Checking NS configuration order: %s", nsr_obj
.config_attributes_file
)
570 if os
.path
.exists(nsr_obj
.config_attributes_file
):
571 # Apply configuration is specified order
573 # Go in loop to configure by specified order
574 self
._log
.info("Using Dynamic configuration input parametrs for NS: %s", nsr_obj
.nsr_name
)
576 # cfg_delay = nsr_obj.nsr_cfg_config_attributes_dict['configuration_delay']
578 # self._log.info("Applying configuration delay for NS (%s) ; %d seconds",
579 # nsr_obj.nsr_name, cfg_delay)
580 # yield from asyncio.sleep(cfg_delay, loop=self._loop)
582 for config_attributes_dict
in nsr_obj
.nsr_cfg_config_attributes_dict
.values():
583 # Iterate through each priority level
584 for vnf_config_attributes_dict
in config_attributes_dict
:
585 # Iterate through each vnfr at this priority level
587 # Make up vnf_unique_name with vnfd name and member index
588 #vnfr_name = "{}.{}".format(nsr_obj.nsr_name, vnf_config_attributes_dict['name'])
589 vnf_unique_name
= get_vnf_unique_name(
591 vnf_config_attributes_dict
['name'],
592 str(vnf_config_attributes_dict
['member_vnf_index']),
594 self
._log
.info("NS (%s) : VNF (%s) - Processing configuration attributes",
595 nsr_obj
.nsr_name
, vnf_unique_name
)
597 # Find vnfr for this vnf_unique_name
598 if vnf_unique_name
not in nsr_obj
._vnfr
_dict
:
599 self
._log
.error("NS (%s) - Can not find VNF to be configured: %s", nsr_obj
.nsr_name
, vnf_unique_name
)
601 # Save this unique VNF's config input parameters
602 nsr_obj
.vnf_config_attributes_dict
[vnf_unique_name
] = vnf_config_attributes_dict
603 nsr_obj
.ConfigVNF(nsr_obj
._vnfr
_dict
[vnf_unique_name
])
605 # Now add the entire NS to the pending config list.
606 self
._log
.info("Scheduling NSR:{} configuration".format(nsr_obj
.nsr_name
))
607 self
._parent
.add_to_pending(nsr_obj
)
608 self
._parent
.add_nsr_obj(nsr_obj
)
610 except Exception as e
:
611 self
._log
.error("Failed processing input parameters for NS (%s) as %s", nsr_obj
.nsr_name
, str(e
))
614 self
._log
.error("No configuration input parameters for NSR (%s)", nsr_obj
.nsr_name
)
616 except Exception as e
:
617 self
._log
.error("Failed to configure NS (%s) as (%s)", nsr_obj
.nsr_name
, str(e
))
618 yield from nsr_obj
.update_ns_cm_state(conmanY
.RecordState
.CFG_PROCESS_FAILED
)
624 def terminate_NSR(self
, id):
625 nsr_dict
= self
._nsr
_dict
626 if id not in nsr_dict
:
627 self
._log
.error("NSR(%s) does not exist!", id)
630 # Remove this NSR if we have it on pending task list
631 for task
in self
.pending_tasks
:
632 if task
['nsrid'] == id:
633 self
.del_from_pending_tasks(task
)
635 # Remove this object from global list
636 nsr_obj
= nsr_dict
.pop(id, None)
638 # Remove this NS cm-state from global status list
639 self
.cm_state
['cm_nsr'].remove(nsr_obj
.cm_nsr
)
641 # Also remove any scheduled configuration event
642 for nsr_obj_p
in self
._parent
.pending_cfg
:
643 if nsr_obj_p
== nsr_obj
:
644 assert id == nsr_obj_p
._nsr
_id
645 #self._parent.pending_cfg.remove(nsr_obj_p)
646 # 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)
647 nsr_obj_p
.being_deleted
= True
648 self
._log
.info("Removed scheduled configuration for NSR(%s)", nsr_obj
.nsr_name
)
650 self
._parent
.remove_nsr_obj(id)
652 # Call Config Agent to clean up for each VNF
653 for agent_vnfr
in nsr_obj
.agent_nsr
.vnfrs
:
654 yield from self
._config
_agent
_mgr
.invoke_config_agent_plugins(
655 'notify_terminate_vnfr',
659 # publish delete cm-state (cm-nsr)
660 yield from nsr_obj
.delete_cm_nsr()
662 #####################TBD###########################
663 # yield from self._config_agent_mgr.invoke_config_agent_plugins('notify_terminate_ns', self.id)
665 self
._log
.info("NSR(%s/%s) is deleted", nsr_obj
.nsr_name
, id)
668 def process_initial_config(self
, nsr_obj
, conf
, script
, vnfr_name
=None):
669 '''Apply the initial-config-primitives specified in NSD or VNFD'''
671 def get_input_file(parameters
):
674 # Add NSR name to file
675 inp
['nsr_name'] = nsr_obj
.nsr_name
677 # Add VNFR name if available
679 inp
['vnfr_name'] = vnfr_name
681 # TODO (pjoseph): Add config agents, we need to identify which all
682 # config agents are required from this NS and provide only those
683 inp
['config-agent'] = {}
685 # Add parameters for initial config
686 inp
['parameter'] = {}
687 for parameter
in parameters
:
689 inp
['parameter'][parameter
['name']] = parameter
['value']
690 except KeyError as e
:
692 self
._log
.info("VNFR {} initial config parameter {} with no value: {}".
693 format(vnfr_name
, parameter
, e
))
695 self
._log
.info("NSR {} initial config parameter {} with no value: {}".
696 format(nsr_obj
.nsr_name
, parameter
, e
))
699 # Add vnfrs specific data
701 for vnfr
in nsr_obj
.vnfrs
:
704 v
['name'] = vnfr
['name']
705 v
['mgmt_ip_address'] = vnfr
['vnf_cfg']['mgmt_ip_address']
706 v
['mgmt_port'] = vnfr
['vnf_cfg']['port']
708 if 'dashboard_url' in vnfr
:
709 v
['dashboard_url'] = vnfr
['dashboard_url']
711 if 'connection_point' in vnfr
:
712 v
['connection_point'] = []
713 for cp
in vnfr
['connection_point']:
714 v
['connection_point'].append(
717 'ip_address': cp
['ip_address'],
723 for vdu
in vnfr
['vdur']:
725 for k
in ['name','management_ip', 'vm_management_ip', 'id']:
729 v
['vdur'].append(vdu_data
)
731 inp
['vnfr'][vnfr
['member_vnf_index_ref']] = v
733 self
._log
.debug("Input data for {}: {}".
734 format((vnfr_name
if vnfr_name
else nsr_obj
.nsr_name
),
737 # Convert to YAML string
738 yaml_string
= yaml
.dump(inp
, default_flow_style
=False)
740 # Write the inputs as yaml file
742 with tempfile
.NamedTemporaryFile(delete
=False) as tmp_file
:
743 tmp_file
.write(yaml_string
.encode("UTF-8"))
744 self
._log
.debug("Input file created for {}: {}".
745 format((vnfr_name
if vnfr_name \
746 else nsr_obj
.nsr_name
),
753 parameters
= conf
['parameter']
754 except Exception as e
:
755 self
._log
.debug("Parameter conf: {}, e: {}".
758 inp_file
= get_input_file(parameters
)
760 cmd
= "{0} {1}".format(script
, inp_file
)
761 self
._log
.debug("Running the CMD: {}".format(cmd
))
763 process
= yield from asyncio
.create_subprocess_shell(cmd
,
765 stdout
=subprocess
.PIPE
,
766 stderr
=subprocess
.PIPE
)
767 stdout
, stderr
= yield from process
.communicate()
768 rc
= yield from process
.wait()
771 msg
= "NSR/VNFR {} initial config using {} failed with {}: {}". \
772 format(vnfr_name
if vnfr_name
else nsr_obj
.nsr_name
,
775 raise InitialConfigError(msg
)
779 except Exception as e
:
780 self
._log
.debug("Error removing input file {}: {}".
783 def get_script_file(self
, script_name
, d_name
, d_id
, d_type
):
784 # Get the full path to the script
786 # If script name starts with /, assume it is full path
787 if script_name
[0] == '/':
788 # The script has full path, use as is
791 script
= os
.path
.join(os
.environ
['RIFT_ARTIFACTS'],
792 'launchpad/packages',
798 self
._log
.debug("Checking for script at %s", script
)
799 if not os
.path
.exists(script
):
800 self
._log
.warning("Did not find script %s", script
)
801 script
= os
.path
.join(os
.environ
['RIFT_INSTALL'],
805 # Seen cases in jenkins, where the script execution fails
806 # with permission denied. Setting the permission on script
807 # to make sure it has execute permission
808 perm
= os
.stat(script
).st_mode
809 if not (perm
& stat
.S_IXUSR
):
810 self
._log
.warning("NSR/VNFR {} initial config script {} " \
811 "without execute permission: {}".
812 format(d_name
, script
, perm
))
813 os
.chmod(script
, perm | stat
.S_IXUSR
)
817 def process_ns_initial_config(self
, nsr_obj
):
818 '''Apply the initial-config-primitives specified in NSD'''
820 nsr
= yield from self
.cmdts_obj
.get_nsr(nsr_obj
.nsr_id
)
821 if 'initial_config_primitive' not in nsr
:
825 nsd
= yield from self
.cmdts_obj
.get_nsd(nsr_obj
.nsr_id
)
826 for conf
in nsr
['initial_config_primitive']:
827 self
._log
.debug("NSR {} initial config: {}".
828 format(nsr_obj
.nsr_name
, conf
))
829 script
= self
.get_script_file(conf
['user_defined_script'],
834 yield from self
.process_initial_config(nsr_obj
, conf
, script
)
837 def process_vnf_initial_config(self
, nsr_obj
, vnfr
):
838 '''Apply the initial-config-primitives specified in VNFD'''
840 vnfr_name
= vnfr
.name
843 vnf_cfg
= vnfd
.vnf_configuration
845 for conf
in vnf_cfg
.initial_config_primitive
:
846 self
._log
.debug("VNFR {} initial config: {}".
847 format(vnfr_name
, conf
))
849 if not conf
.user_defined_script
:
850 self
._log
.debug("VNFR {} did not fine user defined script: {}".
851 format(vnfr_name
, conf
))
854 script
= self
.get_script_file(conf
.user_defined_script
,
859 yield from self
.process_initial_config(nsr_obj
,
865 class ConfigManagerNSR(object):
866 def __init__(self
, log
, loop
, parent
, id):
873 self
._parent
= parent
874 self
._log
.info("Instantiated NSR entry for id=%s", id)
875 self
.nsr_cfg_config_attributes_dict
= {}
876 self
.vnf_config_attributes_dict
= {}
877 self
.num_vnfs_to_cfg
= 0
879 self
.vnf_cfg_list
= []
880 self
.this_nsr_dir
= None
881 self
.being_deleted
= False
882 self
.dts_obj
= self
._parent
.cmdts_obj
884 # Initialize cm-state for this NS
886 self
.cm_nsr
['cm_vnfr'] = []
887 self
.cm_nsr
['id'] = id
888 self
.cm_nsr
['state'] = self
.state_to_string(conmanY
.RecordState
.INIT
)
889 self
.cm_nsr
['state_details'] = None
891 self
.set_nsr_name('Not Set')
893 # Add this NSR cm-state object to global cm-state
894 parent
.cm_state
['cm_nsr'].append(self
.cm_nsr
)
896 # Place holders for NSR & VNFR classes
897 self
.agent_nsr
= None
900 def nsr_opdata_xpath(self
):
901 ''' Returns full xpath for this NSR cm-state opdata '''
903 "D,/rw-conman:cm-state" +
904 "/rw-conman:cm-nsr[rw-conman:id='{}']"
905 ).format(self
._nsr
_id
)
909 return self
._vnfr
_list
920 def publish_cm_state(self
):
921 ''' This function publishes cm_state for this NSR '''
923 cm_state
= conmanY
.CmOpdata()
924 cm_state_nsr
= cm_state
.cm_nsr
.add()
925 cm_state_nsr
.from_dict(self
.cm_nsr
)
926 #with self._dts.transaction() as xact:
927 yield from self
.dts_obj
.update(self
.nsr_opdata_xpath
, cm_state_nsr
)
928 self
._log
.info("Published cm-state with xpath %s and nsr %s",
929 self
.nsr_opdata_xpath
,
933 def delete_cm_nsr(self
):
934 ''' This function publishes cm_state for this NSR '''
936 yield from self
.dts_obj
.delete(self
.nsr_opdata_xpath
)
937 self
._log
.info("Deleted cm-nsr with xpath %s",
938 self
.nsr_opdata_xpath
)
940 def set_nsr_name(self
, name
):
942 self
.cm_nsr
['name'] = name
944 def set_config_dir(self
, caller
):
945 self
.this_nsr_dir
= os
.path
.join(
946 caller
._parent
.cfg_dir
, self
.nsr_name
, caller
._nsr
['name_ref'])
947 if not os
.path
.exists(self
.this_nsr_dir
):
948 os
.makedirs(self
.this_nsr_dir
)
949 self
._log
.debug("NSR:(%s), Created configuration directory(%s)",
950 caller
._nsr
['name_ref'], self
.this_nsr_dir
)
951 self
.config_attributes_file
= os
.path
.join(self
.this_nsr_dir
, "configuration_config_attributes.yml")
952 self
.xlate_dict_file
= os
.path
.join(self
.this_nsr_dir
, "nsr_xlate_dict.yml")
954 def xlate_conf(self
, vnfr
, vnf_cfg
):
956 # If configuration type is not already set, try to read from attributes
957 if vnf_cfg
['interface_type'] is None:
958 # Prepare unique name for this VNF
959 vnf_unique_name
= get_vnf_unique_name(
962 vnfr
['member_vnf_index_ref'],
965 # Find this particular (unique) VNF's config attributes
966 if (vnf_unique_name
in self
.vnf_config_attributes_dict
):
967 vnf_cfg_config_attributes_dict
= self
.vnf_config_attributes_dict
[vnf_unique_name
]
968 vnf_cfg
['interface_type'] = vnf_cfg_config_attributes_dict
['configuration_type']
969 if 'configuration_options' in vnf_cfg_config_attributes_dict
:
970 cfg_opts
= vnf_cfg_config_attributes_dict
['configuration_options']
971 for key
, value
in cfg_opts
.items():
974 cfg_path_prefix
= '{}/{}/{}_{}'.format(
975 self
._parent
._parent
.cfg_dir
,
978 vnfr
['member_vnf_index_ref'],
981 vnf_cfg
['cfg_template'] = '{}_{}_template.cfg'.format(cfg_path_prefix
, vnf_cfg
['interface_type'])
982 vnf_cfg
['cfg_file'] = '{}.cfg'.format(cfg_path_prefix
)
983 vnf_cfg
['xlate_script'] = self
._parent
._parent
.cfg_dir
+ '/xlate_cfg.py'
985 self
._log
.debug("VNF endpoint so far: %s", vnf_cfg
)
987 self
._log
.info("Checking cfg_template %s", vnf_cfg
['cfg_template'])
988 if os
.path
.exists(vnf_cfg
['cfg_template']):
992 def ConfigVNF(self
, vnfr
):
994 vnf_cfg
= vnfr
['vnf_cfg']
995 vnf_cm_state
= self
.find_or_create_vnfr_cm_state(vnf_cfg
)
997 if (vnf_cm_state
['state'] == self
.state_to_string(conmanY
.RecordState
.READY_NO_CFG
)
999 vnf_cm_state
['state'] == self
.state_to_string(conmanY
.RecordState
.READY
)):
1000 self
._log
.warning("NS/VNF (%s/%s) is already configured! Skipped.", self
.nsr_name
, vnfr
['short_name'])
1004 vnf_cm_state
['state'] = self
.state_to_string(conmanY
.RecordState
.CFG_PROCESS
)
1006 # Now translate the configuration for iP addresses
1008 # Add cp_dict members (TAGS) for this VNF
1009 self
._cp
_dict
['rw_mgmt_ip'] = vnf_cfg
['mgmt_ip_address']
1010 self
._cp
_dict
['rw_username'] = vnf_cfg
['username']
1011 self
._cp
_dict
['rw_password'] = vnf_cfg
['password']
1012 ############################################################
1013 # TBD - Need to lookup above 3 for a given VNF, not global #
1014 # Once we do that no need to dump below file again before #
1015 # each VNF configuration translation. #
1016 # This will require all existing config templates to be #
1017 # changed for above three tags to include member index #
1018 ############################################################
1020 nsr_obj
= vnf_cfg
['nsr_obj']
1021 # Generate config_config_attributes.yaml (For debug reference)
1022 with
open(nsr_obj
.xlate_dict_file
, "w") as yf
:
1023 yf
.write(yaml
.dump(nsr_obj
._cp
_dict
, default_flow_style
=False))
1024 except Exception as e
:
1025 self
._log
.error("NS:(%s) failed to write nsr xlate tags file as (%s)", nsr_obj
.nsr_name
, str(e
))
1027 if 'cfg_template' in vnf_cfg
:
1028 script_cmd
= 'python3 {} -i {} -o {} -x "{}"'.format(vnf_cfg
['xlate_script'], vnf_cfg
['cfg_template'], vnf_cfg
['cfg_file'], self
.xlate_dict_file
)
1029 self
._log
.debug("xlate script command (%s)", script_cmd
)
1030 #xlate_msg = subprocess.check_output(script_cmd).decode('utf-8')
1031 xlate_msg
= subprocess
.check_output(script_cmd
, shell
=True).decode('utf-8')
1032 self
._log
.info("xlate script output (%s)", xlate_msg
)
1033 except Exception as e
:
1034 vnf_cm_state
['state'] = self
.state_to_string(conmanY
.RecordState
.CFG_PROCESS_FAILED
)
1035 self
._log
.error("Failed to execute translation script for VNF: %s with (%s)", log_this_vnf(vnf_cfg
), str(e
))
1038 self
._log
.info("Applying config to VNF: %s = %s!", log_this_vnf(vnf_cfg
), vnf_cfg
)
1040 #self.vnf_cfg_list.append(vnf_cfg)
1041 self
._log
.debug("Scheduled configuration!")
1042 vnf_cm_state
['state'] = self
.state_to_string(conmanY
.RecordState
.CFG_SCHED
)
1043 except Exception as e
:
1044 self
._log
.error("Failed apply_vnf_config to VNF: %s as (%s)", log_this_vnf(vnf_cfg
), str(e
))
1045 vnf_cm_state
['state'] = self
.state_to_string(conmanY
.RecordState
.CFG_PROCESS_FAILED
)
1049 self
._log
.info("Adding NS Record for id=%s", id)
1052 def sample_cm_state(self
):
1059 'cfg_location': 'location1',
1060 'cfg_type': 'script',
1061 'connection_point': [
1062 {'ip_address': '1.1.1.1', 'name': 'vnf1cp1'},
1063 {'ip_address': '1.1.1.2', 'name': 'vnf1cp2'}
1066 'mgmt_interface': {'ip_address': '7.1.1.1',
1068 'name': 'vnfrname1',
1072 'cfg_location': 'location2',
1073 'cfg_type': 'netconf',
1074 'connection_point': [{'ip_address': '2.1.1.1', 'name': 'vnf2cp1'},
1075 {'ip_address': '2.1.1.2', 'name': 'vnf2cp2'}],
1077 'mgmt_interface': {'ip_address': '7.1.1.2',
1079 'name': 'vnfrname2',
1086 'states': 'Initialized, '
1089 def populate_vm_state_from_vnf_cfg(self
):
1090 # Fill in each VNFR from this nsr object
1091 vnfr_list
= self
._vnfr
_list
1092 for vnfr
in vnfr_list
:
1093 vnf_cfg
= vnfr
['vnf_cfg']
1094 vnf_cm_state
= self
.find_vnfr_cm_state(vnfr
['id'])
1097 # Fill in VNF management interface
1098 vnf_cm_state
['mgmt_interface']['ip_address'] = vnf_cfg
['mgmt_ip_address']
1099 vnf_cm_state
['mgmt_interface']['port'] = vnf_cfg
['port']
1101 # Fill in VNF configuration details
1102 vnf_cm_state
['cfg_type'] = vnf_cfg
['config_method']
1103 vnf_cm_state
['cfg_location'] = vnf_cfg
['cfg_file']
1105 # Fill in each connection-point for this VNF
1106 if "connection_point" in vnfr
:
1107 cp_list
= vnfr
['connection_point']
1108 for cp_item_dict
in cp_list
:
1109 vnf_cm_state
['connection_point'].append(
1111 'name' : cp_item_dict
['name'],
1112 'ip_address' : cp_item_dict
['ip_address'],
1116 def state_to_string(self
, state
):
1118 conmanY
.RecordState
.INIT
: "init",
1119 conmanY
.RecordState
.RECEIVED
: "received",
1120 conmanY
.RecordState
.CFG_PROCESS
: "cfg_process",
1121 conmanY
.RecordState
.CFG_PROCESS_FAILED
: "cfg_process_failed",
1122 conmanY
.RecordState
.CFG_SCHED
: "cfg_sched",
1123 conmanY
.RecordState
.CFG_DELAY
: "cfg_delay",
1124 conmanY
.RecordState
.CONNECTING
: "connecting",
1125 conmanY
.RecordState
.FAILED_CONNECTION
: "failed_connection",
1126 conmanY
.RecordState
.NETCONF_CONNECTED
: "netconf_connected",
1127 conmanY
.RecordState
.NETCONF_SSH_CONNECTED
: "netconf_ssh_connected",
1128 conmanY
.RecordState
.RESTCONF_CONNECTED
: "restconf_connected",
1129 conmanY
.RecordState
.CFG_SEND
: "cfg_send",
1130 conmanY
.RecordState
.CFG_FAILED
: "cfg_failed",
1131 conmanY
.RecordState
.READY_NO_CFG
: "ready_no_cfg",
1132 conmanY
.RecordState
.READY
: "ready",
1134 return state_dict
[state
]
1136 def find_vnfr_cm_state(self
, id):
1137 if self
.cm_nsr
['cm_vnfr']:
1138 for vnf_cm_state
in self
.cm_nsr
['cm_vnfr']:
1139 if vnf_cm_state
['id'] == id:
1143 def find_or_create_vnfr_cm_state(self
, vnf_cfg
):
1144 vnfr
= vnf_cfg
['vnfr']
1145 vnf_cm_state
= self
.find_vnfr_cm_state(vnfr
['id'])
1147 if vnf_cm_state
is None:
1148 # Not found, Create and Initialize this VNF cm-state
1151 'name' : vnfr
['short_name'],
1152 'state' : self
.state_to_string(conmanY
.RecordState
.RECEIVED
),
1155 'ip_address' : vnf_cfg
['mgmt_ip_address'],
1156 'port' : vnf_cfg
['port'],
1158 'cfg_type' : vnf_cfg
['config_method'],
1159 'cfg_location' : vnf_cfg
['cfg_file'],
1160 'connection_point' : [],
1162 self
.cm_nsr
['cm_vnfr'].append(vnf_cm_state
)
1164 # Publish newly created cm-state
1170 def get_vnf_cm_state(self
, vnfr
):
1172 vnf_cm_state
= self
.find_vnfr_cm_state(vnfr
['id'])
1174 return vnf_cm_state
['state']
1178 def update_vnf_cm_state(self
, vnfr
, state
):
1180 vnf_cm_state
= self
.find_vnfr_cm_state(vnfr
['id'])
1181 if vnf_cm_state
is None:
1182 self
._log
.error("No opdata found for NS/VNF:%s/%s!",
1183 self
.nsr_name
, vnfr
['short_name'])
1186 if vnf_cm_state
['state'] != self
.state_to_string(state
):
1187 old_state
= vnf_cm_state
['state']
1188 vnf_cm_state
['state'] = self
.state_to_string(state
)
1190 yield from self
.publish_cm_state()
1191 self
._log
.info("VNF ({}/{}/{}) state change: {} -> {}"
1192 .format(self
.nsr_name
,
1194 vnfr
['member_vnf_index_ref'],
1196 vnf_cm_state
['state']))
1199 self
._log
.error("No VNFR supplied for state update (NS=%s)!",
1203 def get_ns_cm_state(self
):
1204 return self
.cm_nsr
['state']
1207 def update_ns_cm_state(self
, state
, state_details
=None):
1208 if self
.cm_nsr
['state'] != self
.state_to_string(state
):
1209 old_state
= self
.cm_nsr
['state']
1210 self
.cm_nsr
['state'] = self
.state_to_string(state
)
1211 self
.cm_nsr
['state_details'] = state_details
if state_details
is not None else None
1212 self
._log
.info("NS ({}) state change: {} -> {}"
1213 .format(self
.nsr_name
,
1215 self
.cm_nsr
['state']))
1217 yield from self
.publish_cm_state()
1220 def add_vnfr(self
, vnfr
, vnfr_msg
):
1223 def populate_subnets_from_vlr(id):
1225 # Populate cp_dict with VLR subnet info
1226 vlr
= yield from self
.dts_obj
.get_vlr(id)
1227 if vlr
is not None and 'assigned_subnet' in vlr
:
1228 subnet
= {vlr
.name
:vlr
.assigned_subnet
}
1229 self
._cp
_dict
[vnfr
['member_vnf_index_ref']].update(subnet
)
1230 self
._cp
_dict
.update(subnet
)
1231 self
._log
.debug("VNF:(%s) Updated assigned subnet = %s",
1232 vnfr
['short_name'], subnet
)
1233 except Exception as e
:
1234 self
._log
.error("VNF:(%s) VLR Error = %s",
1235 vnfr
['short_name'], e
)
1237 if vnfr
['id'] not in self
._vnfr
_dict
:
1238 self
._log
.info("NSR(%s) : Adding VNF Record for name=%s, id=%s", self
._nsr
_id
, vnfr
['short_name'], vnfr
['id'])
1239 # Add this vnfr to the list for show, or single traversal
1240 self
._vnfr
_list
.append(vnfr
)
1242 self
._log
.warning("NSR(%s) : VNF Record for name=%s, id=%s already exists, overwriting", self
._nsr
_id
, vnfr
['short_name'], vnfr
['id'])
1244 # Make vnfr available by id as well as by name
1245 unique_name
= get_vnf_unique_name(self
.nsr_name
, vnfr
['short_name'], vnfr
['member_vnf_index_ref'])
1246 self
._vnfr
_dict
[unique_name
] = vnfr
1247 self
._vnfr
_dict
[vnfr
['id']] = vnfr
1249 # Create vnf_cfg dictionary with default values
1253 'agent_vnfr' : self
.agent_nsr
.add_vnfr(vnfr
, vnfr_msg
),
1254 'nsr_name' : self
.nsr_name
,
1255 'nsr_id' : self
._nsr
_id
,
1256 'vnfr_name' : vnfr
['short_name'],
1257 'member_vnf_index' : vnfr
['member_vnf_index_ref'],
1259 'username' : 'admin',
1260 'password' : 'admin',
1261 'config_method' : 'None',
1262 'protocol' : 'None',
1263 'mgmt_ip_address' : '0.0.0.0',
1264 'cfg_file' : 'None',
1266 'script_type' : 'bash',
1269 # Update the mgmt ip address
1270 # In case the config method is none, this is not
1273 vnf_cfg
['mgmt_ip_address'] = vnfr_msg
.mgmt_interface
.ip_address
1274 vnf_cfg
['port'] = vnfr_msg
.mgmt_interface
.port
1275 except Exception as e
:
1277 "VNFR {}({}), unable to retrieve mgmt ip address: {}".
1278 format(vnfr
['short_name'], vnfr
['id'], e
))
1280 vnfr
['vnf_cfg'] = vnf_cfg
1281 self
.find_or_create_vnfr_cm_state(vnf_cfg
)
1284 Build the connection-points list for this VNF (self._cp_dict)
1286 # Populate global CP list self._cp_dict from VNFR
1288 if 'connection_point' in vnfr
:
1289 cp_list
= vnfr
['connection_point']
1291 self
._cp
_dict
[vnfr
['member_vnf_index_ref']] = {}
1293 for vdur
in vnfr
['vdur']:
1294 if 'internal_connection_point' in vdur
:
1295 cp_list
+= vdur
['internal_connection_point']
1297 for cp_item_dict
in cp_list
:
1298 # Populate global dictionary
1300 cp_item_dict
['name']
1301 ] = cp_item_dict
['ip_address']
1303 # Populate unique member specific dictionary
1305 vnfr
['member_vnf_index_ref']
1307 cp_item_dict
['name']
1308 ] = cp_item_dict
['ip_address']
1310 # Fill in the subnets from vlr
1311 if 'vlr_ref' in cp_item_dict
:
1312 ### HACK: Internal connection_point do not have VLR reference
1313 yield from populate_subnets_from_vlr(cp_item_dict
['vlr_ref'])
1315 if 'internal_vlr' in vnfr
:
1316 for ivlr
in vnfr
['internal_vlr']:
1317 yield from populate_subnets_from_vlr(ivlr
['vlr_ref'])
1320 vnf_cfg
['agent_vnfr']._vnfr
= vnfr
1321 return vnf_cfg
['agent_vnfr']
1324 class XPaths(object):
1326 def nsr_opdata(k
=None):
1327 return ("D,/nsr:ns-instance-opdata/nsr:nsr" +
1328 ("[nsr:ns-instance-config-ref='{}']".format(k
) if k
is not None else ""))
1331 def nsd_msg(k
=None):
1332 return ("C,/nsd:nsd-catalog/nsd:nsd" +
1333 "[nsd:id = '{}']".format(k
) if k
is not None else "")
1336 def vnfr_opdata(k
=None):
1337 return ("D,/vnfr:vnfr-catalog/vnfr:vnfr" +
1338 ("[vnfr:id='{}']".format(k
) if k
is not None else ""))
1342 return ("C,/vnfd:vnfd-catalog/vnfd:vnfd" +
1343 ("[vnfd:id='{}']".format(k
) if k
is not None else ""))
1346 def config_agent(k
=None):
1347 return ("D,/rw-config-agent:config-agent/rw-config-agent:account" +
1348 ("[rw-config-agent:name='{}']".format(k
) if k
is not None else ""))
1351 def nsr_config(k
=None):
1352 return ("C,/nsr:ns-instance-config/nsr:nsr[nsr:id='{}']".format(k
) if k
is not None else "")
1356 return ("D,/vlr:vlr-catalog/vlr:vlr[vlr:id='{}']".format(k
) if k
is not None else "")
1358 class ConfigManagerDTS(object):
1359 ''' This class either reads from DTS or publishes to DTS '''
1361 def __init__(self
, log
, loop
, parent
, dts
):
1364 self
._parent
= parent
1368 def _read_dts(self
, xpath
, do_trace
=False):
1369 self
._log
.debug("_read_dts path = %s", xpath
)
1370 flags
= rwdts
.XactFlag
.MERGE
1371 res_iter
= yield from self
._dts
.query_read(
1378 result
= yield from i
1379 if result
is not None:
1380 results
.append(result
.result
)
1388 def get_nsr(self
, id):
1389 self
._log
.debug("Attempting to get NSR: %s", id)
1390 nsrl
= yield from self
._read
_dts
(XPaths
.nsr_opdata(id), False)
1393 nsr
= nsrl
[0].as_dict()
1397 def get_nsr_config(self
, id):
1398 self
._log
.debug("Attempting to get config NSR: %s", id)
1399 nsrl
= yield from self
._read
_dts
(XPaths
.nsr_config(id), False)
1406 def get_nsd_msg(self
, id):
1407 self
._log
.debug("Attempting to get NSD: %s", id)
1408 nsdl
= yield from self
._read
_dts
(XPaths
.nsd_msg(id), False)
1415 def get_nsd(self
, nsr_id
):
1416 self
._log
.debug("Attempting to get NSD for NSR: %s", id)
1417 nsr_config
= yield from self
.get_nsr_config(nsr_id
)
1418 nsd_msg
= nsr_config
.nsd
1422 def get_vnfr(self
, id):
1423 self
._log
.debug("Attempting to get VNFR: %s", id)
1424 vnfrl
= yield from self
._read
_dts
(XPaths
.vnfr_opdata(id), do_trace
=False)
1431 def get_vnfd(self
, vnfd_id
):
1432 self
._log
.debug("Attempting to get VNFD: %s", vnfd_id
)
1433 vnfdl
= yield from self
._read
_dts
(XPaths
.vnfd(vnfd_id
), do_trace
=False)
1440 def get_vlr(self
, id):
1441 self
._log
.debug("Attempting to get VLR subnet: %s", id)
1442 vlrl
= yield from self
._read
_dts
(XPaths
.vlr(id), do_trace
=True)
1449 def get_config_agents(self
, name
):
1450 self
._log
.debug("Attempting to get config_agents: %s", name
)
1451 cfgagentl
= yield from self
._read
_dts
(XPaths
.config_agent(name
), False)
1455 def update(self
, path
, msg
, flags
=rwdts
.XactFlag
.REPLACE
):
1457 Update a cm-state (cm-nsr) record in DTS with the path and message
1459 self
._log
.debug("Updating cm-state %s:%s dts_pub_hdl = %s", path
, msg
, self
.dts_pub_hdl
)
1460 self
.dts_pub_hdl
.update_element(path
, msg
, flags
)
1461 self
._log
.debug("Updated cm-state, %s:%s", path
, msg
)
1464 def delete(self
, path
):
1466 Delete cm-nsr record in DTS with the path only
1468 self
._log
.debug("Deleting cm-nsr %s dts_pub_hdl = %s", path
, self
.dts_pub_hdl
)
1469 self
.dts_pub_hdl
.delete_element(path
)
1470 self
._log
.debug("Deleted cm-nsr, %s", path
)
1474 yield from self
.register_to_publish()
1475 yield from self
.register_for_nsr()
1478 def register_to_publish(self
):
1479 ''' Register to DTS for publishing cm-state opdata '''
1481 xpath
= "D,/rw-conman:cm-state/rw-conman:cm-nsr"
1482 self
._log
.debug("Registering to publish cm-state @ %s", xpath
)
1483 hdl
= rift
.tasklets
.DTS
.RegistrationHandler()
1484 with self
._dts
.group_create() as group
:
1485 self
.dts_pub_hdl
= group
.register(xpath
=xpath
,
1487 flags
=rwdts
.Flag
.PUBLISHER | rwdts
.Flag
.NO_PREP_READ
)
1490 def nsr_xpath(self
):
1491 return "D,/nsr:ns-instance-opdata/nsr:nsr"
1494 def register_for_nsr(self
):
1495 """ Register for NSR changes """
1498 def on_prepare(xact_info
, query_action
, ks_path
, msg
):
1499 """ This NSR is created """
1500 self
._log
.debug("Received NSR instantiate on_prepare (%s:%s:%s)",
1505 if (query_action
== rwdts
.QueryAction
.UPDATE
or
1506 query_action
== rwdts
.QueryAction
.CREATE
):
1507 msg_dict
= msg
.as_dict()
1508 # Update Each NSR/VNFR state)
1509 if ('operational_status' in msg_dict
and
1510 msg_dict
['operational_status'] == 'running'):
1511 # Add to the task list
1512 self
._parent
.add_to_pending_tasks({'nsrid' : msg_dict
['ns_instance_config_ref'], 'retries' : 5})
1513 elif query_action
== rwdts
.QueryAction
.DELETE
:
1514 nsr_id
= msg
.ns_instance_config_ref
1515 asyncio
.ensure_future(self
._parent
.terminate_NSR(nsr_id
), loop
=self
._loop
)
1517 raise NotImplementedError(
1518 "%s action on cm-state not supported",
1521 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
1524 handler
= rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_prepare
)
1525 self
.dts_reg_hdl
= yield from self
._dts
.register(self
.nsr_xpath
,
1526 flags
=rwdts
.Flag
.SUBSCRIBER | rwdts
.Flag
.DELTA_READY
,
1528 except Exception as e
:
1529 self
._log
.error("Failed to register for NSR changes as %s", str(e
))