2 # Copyright 2016 RIFT.IO Inc
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
24 import rift
.mano
.utils
.juju_api
as juju
25 from . import riftcm_config_plugin
28 def get_vnf_unique_name(nsr_name
, vnfr_name
, member_vnf_index
):
29 """Get the unique VNF name.
30 Charm names accepts only a to z and non-consecutive - characters."""
31 name
= "{}-{}-{}".format(nsr_name
, vnfr_name
, member_vnf_index
)
39 return re
.sub('\-+', '-', new_name
.lower())
42 class JujuConfigPlugin(riftcm_config_plugin
.RiftCMConfigPluginBase
):
44 Juju implementation of the riftcm_config_plugin.RiftCMConfigPluginBase
46 def __init__(self
, dts
, log
, loop
, project
, account
):
47 riftcm_config_plugin
.RiftCMConfigPluginBase
.__init
__(self
, dts
, log
, loop
,
49 self
._name
= account
.name
51 self
._ip
_address
= account
.juju
.ip_address
52 self
._port
= account
.juju
.port
53 self
._user
= account
.juju
.user
54 self
._secret
= account
.juju
.secret
55 self
._rift
_install
_dir
= os
.environ
['RIFT_INSTALL']
56 self
._rift
_var
_root
_dir
= os
.environ
['RIFT_VAR_ROOT']
58 ############################################################
59 # This is wrongfully overloaded with 'juju' private data. #
60 # Really need to separate agent_vnfr from juju vnfr data. #
61 # Currently, this holds agent_vnfr, which has actual vnfr, #
62 # then this juju overloads actual vnfr with its own #
63 # dictionary elemetns (WRONG!!!) #
65 ############################################################
68 self
._api
= juju
.JujuApi(log
, loop
,
69 self
._ip
_address
, self
._port
,
70 self
._user
, self
._secret
)
89 host
=self
._ip
_address
,
95 def vnfr(self
, vnfr_id
):
97 vnfr
= self
._juju
_vnfs
[vnfr_id
].vnfr
99 self
._log
.error("jujuCA: Did not find VNFR %s in juju plugin", vnfr_id
)
104 def get_service_name(self
, vnfr_id
):
105 vnfr
= self
.vnfr(vnfr_id
)
106 if vnfr
and 'vnf_juju_name' in vnfr
:
107 return vnfr
['vnf_juju_name']
110 def juju_log(self
, level
, name
, log_str
, *args
):
112 g_log_str
= 'jujuCA:({}) {}'.format(name
, log_str
)
114 g_log_str
= 'jujuCA: {}'.format(log_str
)
115 getattr(self
._log
, level
)(g_log_str
, *args
)
117 # TBD: Do a better, similar to config manager
118 def xlate(self
, tag
, tags
):
123 if re
.search('<.*>', tag
):
124 self
._log
.debug("jujuCA: Xlate value %s", tag
)
126 if tag
== '<rw_mgmt_ip>':
127 val
= tags
['rw_mgmt_ip']
128 except KeyError as e
:
129 self
._log
.info("jujuCA: Did not get a value for tag %s, e=%s",
134 def notify_create_vlr(self
, agent_nsr
, agent_vnfr
, vld
, vlr
):
136 Notification of create VL record
141 def notify_create_vnfr(self
, agent_nsr
, agent_vnfr
):
143 Notification of create Network VNF record
144 Returns True if configured using config_agent
146 # Deploy the charm if specified for the vnf
147 self
._log
.debug("jujuCA: create vnfr nsr=%s vnfr=%s",
148 agent_nsr
.name
, agent_vnfr
.name
)
149 self
._log
.debug("jujuCA: Config = %s",
150 agent_vnfr
.vnf_configuration
)
152 vnf_config
= agent_vnfr
.vnfr_msg
.vnf_configuration
153 self
._log
.debug("jujuCA: vnf_configuration = %s", vnf_config
)
154 if not vnf_config
.has_field('juju'):
156 charm
= vnf_config
.juju
.charm
157 self
._log
.debug("jujuCA: charm = %s", charm
)
158 except Exception as e
:
159 self
._log
.Error("jujuCA: vnf_configuration error for vnfr {}: {}".
160 format(agent_vnfr
.name
, e
))
163 # Prepare unique name for this VNF
164 vnf_unique_name
= get_vnf_unique_name(agent_nsr
.name
,
166 agent_vnfr
.member_vnf_index
)
167 if vnf_unique_name
in self
._tasks
:
168 self
._log
.warn("jujuCA: Service %s already deployed",
171 vnfr_dict
= agent_vnfr
.vnfr
172 vnfr_dict
.update({'vnf_juju_name': vnf_unique_name
,
174 'nsr_id': agent_nsr
.id,
175 'member_vnf_index': agent_vnfr
.member_vnf_index
,
178 'config': vnf_config
,
179 'vnfr_name': agent_vnfr
.name
})
180 self
._log
.debug("jujuCA: Charm %s for vnf %s to be deployed as %s",
181 charm
, agent_vnfr
.name
, vnf_unique_name
)
183 # Find the charm directory
185 path
= os
.path
.join(self
._rift
_var
_root
_dir
,
186 'launchpad/packages/vnfd',
188 agent_vnfr
.vnfr_msg
.vnfd
.id,
191 self
._log
.debug("jujuCA: Charm dir is {}".format(path
))
192 if not os
.path
.isdir(path
):
193 msg
= "jujuCA: Did not find the charm directory at {}".format(path
)
196 # Return from here instead of forwarding the config request to juju_api
198 except Exception as e
:
199 self
.log
.exception(e
)
202 if vnf_unique_name
not in self
._tasks
:
203 self
._tasks
[vnf_unique_name
] = {}
205 self
._log
.debug("jujuCA: Deploying service %s",
207 yield from self
.api
.deploy_application(
215 def notify_instantiate_vnfr(self
, agent_nsr
, agent_vnfr
):
217 Notification of Instantiate NSR with the passed nsr id
222 def notify_instantiate_vlr(self
, agent_nsr
, agent_vnfr
, vlr
):
224 Notification of Instantiate NSR with the passed nsr id
229 def notify_terminate_nsr(self
, agent_nsr
, agent_vnfr
):
231 Notification of Terminate the network service
236 def notify_terminate_vnfr(self
, agent_nsr
, agent_vnfr
):
238 Notification of Terminate the network service
240 self
._log
.debug("jujuCA: Terminate VNFr {}, current vnfrs={}".
241 format(agent_vnfr
.name
, self
._juju
_vnfs
))
243 vnfr
= agent_vnfr
.vnfr
244 service
= vnfr
['vnf_juju_name']
246 self
._log
.debug("jujuCA: Terminating VNFr {}, {}".format(
250 yield from self
.api
.remove_application(service
)
252 del self
._juju
_vnfs
[agent_vnfr
.id]
253 self
._log
.debug("jujuCA: current vnfrs={}".
254 format(self
._juju
_vnfs
))
255 if service
in self
._tasks
:
257 for action
in self
._tasks
[service
].keys():
260 except KeyError as e
:
261 self
._log
.debug ("jujuCA: Terminating charm service for VNFr {}, e={}".
262 format(agent_vnfr
.name
, e
))
263 except Exception as e
:
264 self
._log
.error("jujuCA: Exception terminating charm service for VNFR {}: {}".
265 format(agent_vnfr
.name
, e
))
270 def notify_terminate_vlr(self
, agent_nsr
, agent_vnfr
, vlr
):
272 Notification of Terminate the virtual link
277 def _vnf_config_primitive(self
, nsr_id
, vnfr_id
, primitive
,
278 vnf_config
=None, wait
=False):
279 self
._log
.debug("jujuCA: VNF config primitive {} for nsr {}, "
281 format(primitive
, nsr_id
, vnfr_id
))
283 if vnf_config
is None:
284 vnfr_msg
= yield from self
.get_vnfr(vnfr_id
)
286 msg
= "Unable to get VNFR {} through DTS".format(vnfr_id
)
290 vnf_config
= vnfr_msg
.vnf_configuration
291 self
._log
.debug("VNF config= %s", vnf_config
.as_dict())
294 vnfr
= self
._juju
_vnfs
[vnfr_id
].vnfr
295 service
= vnfr
['vnf_juju_name']
296 self
._log
.debug("VNF config %s", vnf_config
)
297 configs
= vnf_config
.config_primitive
298 for config
in configs
:
299 if config
.name
== primitive
.name
:
300 self
._log
.debug("jujuCA: Found the config primitive %s",
303 for parameter
in config
.parameter
:
305 for p
in primitive
.parameter
:
306 if p
.name
== parameter
.name
:
308 val
= self
.xlate(p
.value
, vnfr
['tags'])
312 val
= parameter
.default_value
315 # Check if mandatory parameter
316 if parameter
.mandatory
:
317 msg
= "VNFR {}: Primitive {} called " \
318 "without mandatory parameter {}". \
319 format(vnfr_msg
.name
, config
.name
,
322 return 'failed', '', msg
325 val
= self
.convert_value(val
, parameter
.data_type
)
326 params
.update({parameter
.name
: val
})
331 if config
.name
== 'config':
334 self
._log
.debug("jujuCA: applying config with "
335 "params {} for service {}".
336 format(params
, service
))
338 rc
= yield from self
.api
.apply_config(params
, application
=service
)
342 self
._log
.debug("jujuCA: applied config {} "
343 "on {}".format(params
, service
))
347 'Failed to apply config: {}'.format(params
)
348 self
._log
.error("jujuCA: Error applying "
349 "config {} on service {}".
350 format(params
, service
))
352 self
._log
.warn("jujuCA: Did not find valid "
353 "parameters for config : {}".
354 format(primitive
.parameter
))
357 self
._log
.debug("jujuCA: Execute action {} on "
358 "service {} with params {}".
359 format(config
.name
, service
, params
))
361 resp
= yield from self
.api
.execute_action(
369 details
= resp
['error']['message']
371 exec_id
= resp
['action']['tag']
374 details
= resp
['message']
376 self
._log
.debug("jujuCA: execute action {} on "
377 "service {} returned {}".
378 format(config
.name
, service
, rc
))
380 self
._log
.error("jujuCA: error executing action "
382 format(config
.name
, service
,
386 details
= "Failed to queue the action"
389 except KeyError as e
:
390 msg
= "VNF %s does not have config primitives, e=%s", \
392 self
._log
.exception(msg
)
393 raise ValueError(msg
)
395 while wait
and (rc
in ['pending', 'running']):
396 self
._log
.debug("JujuCA: action {}, rc {}".
398 yield from asyncio
.sleep(0.2, loop
=self
._loop
)
399 status
= yield from self
.api
.get_action_status(exec_id
)
400 rc
= status
['status']
402 return rc
, exec_id
, details
405 def vnf_config_primitive(self
, nsr_id
, vnfr_id
, primitive
, output
):
407 vnfr
= self
._juju
_vnfs
[vnfr_id
].vnfr
409 msg
= "Did not find VNFR {} in Juju plugin".format(vnfr_id
)
413 output
.execution_status
= "failed"
414 output
.execution_id
= ''
415 output
.execution_error_details
= ''
417 rc
, exec_id
, err
= yield from self
._vnf
_config
_primitive
(
422 self
._log
.debug("VNFR {} primitive {} exec status: {}".
423 format(vnfr_id
, primitive
.name
, rc
))
424 output
.execution_status
= rc
425 output
.execution_id
= exec_id
426 output
.execution_error_details
= err
429 def apply_config(self
, agent_nsr
, agent_vnfr
, config
, rpc_ip
):
430 """ Notification on configuration of an NSR """
434 def apply_ns_config(self
, agent_nsr
, agent_vnfrs
, rpc_ip
):
437 ###### TBD - This really does not belong here. Looks more like NS level script ####
438 ###### apply_config should be called for a particular VNF only here ###############
440 Hook: Runs the user defined script. Feeds all the necessary data
441 for the script thro' yaml file.
444 rpc_ip (YangInput_Nsr_ExecNsConfigPrimitive): The input data.
445 nsr (NetworkServiceRecord): Description
446 vnfrs (dict): VNFR ID => VirtualNetworkFunctionRecord
449 def get_meta(agent_nsr
):
450 unit_names
, initial_params
, vnfr_index_map
= {}, {}, {}
452 for vnfr_id
in agent_nsr
.vnfr_ids
:
453 juju_vnf
= self
._juju
_vnfs
[vnfr_id
].vnfr
456 vnfr_index_map
[vnfr_id
] = juju_vnf
['member_vnf_index']
459 unit_names
[vnfr_id
] = juju_vnf
['vnf_juju_name']
461 # Flatten the data for simplicity
463 self
._log
.debug("Juju Config:%s", juju_vnf
['config'])
464 for primitive
in juju_vnf
['config'].initial_config_primitive
:
465 for parameter
in primitive
.parameter
:
466 value
= self
.xlate(parameter
.value
, juju_vnf
['tags'])
467 param_data
[parameter
.name
] = value
469 initial_params
[vnfr_id
] = param_data
472 return unit_names
, initial_params
, vnfr_index_map
474 unit_names
, init_data
, vnfr_index_map
= get_meta(agent_nsr
)
476 # The data consists of 4 sections
478 # 2. The input passed.
479 # 3. Juju unit names (keyed by vnfr ID).
480 # 4. Initial config data (keyed by vnfr ID).
482 data
['config_agent'] = dict(
484 host
=self
._ip
_address
,
489 data
["rpc_ip"] = rpc_ip
.as_dict()
490 data
["unit_names"] = unit_names
491 data
["init_config"] = init_data
492 data
["vnfr_index_map"] = vnfr_index_map
495 with tempfile
.NamedTemporaryFile(delete
=False) as tmp_file
:
496 tmp_file
.write(yaml
.dump(data
, default_flow_style
=True)
499 self
._log
.debug("jujuCA: Creating a temp file: {} with input data".format(
502 # Get the full path to the script
504 if rpc_ip
.user_defined_script
[0] == '/':
505 # The script has full path, use as is
506 script
= rpc_ip
.user_defined_script
508 script
= os
.path
.join(self
._rift
_var
_root
_dir
, 'launchpad/nsd',
510 agent_nsr
.id, 'scripts',
511 rpc_ip
.user_defined_script
)
512 self
.log
.debug("jujuCA: Checking for script in %s", script
)
513 if not os
.path
.exists(script
):
514 script
= os
.path
.join(self
._rift
_install
_dir
, 'usr/bin', rpc_ip
.user_defined_script
)
516 cmd
= "{} {}".format(rpc_ip
.user_defined_script
, tmp_file
.name
)
517 self
._log
.debug("jujuCA: Running the CMD: {}".format(cmd
))
519 coro
= asyncio
.create_subprocess_shell(cmd
, loop
=self
._loop
,
520 stderr
=asyncio
.subprocess
.PIPE
)
521 process
= yield from coro
522 err
= yield from process
.stderr
.read()
523 task
= self
._loop
.create_task(process
.wait())
528 def apply_initial_config(self
, agent_nsr
, agent_vnfr
):
530 Apply the initial configuration
531 Expect config directives mostly, not actions
532 Actions in initial config may not work based on charm design
536 vnfr
= self
._juju
_vnfs
[agent_vnfr
.id].vnfr
537 service
= vnfr
['vnf_juju_name']
539 self
._log
.debug("Did not find VNFR %s in Juju plugin",
543 vnfr_msg
= yield from self
.get_vnfr(agent_vnfr
.id)
545 msg
= "Unable to get VNFR {} ({}) through DTS". \
546 format(agent_vnfr
.id, agent_vnfr
.name
)
548 raise RuntimeError(msg
)
550 vnf_config
= vnfr_msg
.vnf_configuration
551 self
._log
.debug("VNFR %s config: %s", vnfr_msg
.name
,
552 vnf_config
.as_dict())
554 # Sort the primitive based on the sequence number
555 primitives
= sorted(vnf_config
.initial_config_primitive
,
558 self
._log
.debug("VNFR {}: No initial-config-primitive specified".
559 format(vnfr_msg
.name
))
562 rc
= yield from self
.api
.is_application_up(application
=service
)
567 if vnfr_msg
.mgmt_interface
.ip_address
:
568 vnfr
['tags'].update({'rw_mgmt_ip': vnfr_msg
.mgmt_interface
.ip_address
})
569 self
._log
.debug("jujuCA:(%s) tags: %s", vnfr
['vnf_juju_name'], vnfr
['tags'])
571 for primitive
in primitives
:
572 self
._log
.debug("(%s) Initial config primitive %s",
573 vnfr
['vnf_juju_name'], primitive
.as_dict())
574 if primitive
.config_primitive_ref
:
575 # Reference to a primitive in config primitive
577 def __init__(self
, name
):
582 prim
= Primitive(primitive
.config_primitive_ref
)
583 rc
, eid
, err
= yield from self
._vnf
_config
_primitive
(
591 msg
= "Error executing initial config primitive" \
592 " {} in VNFR {}: rc={}, stderr={}". \
593 format(prim
.name
, vnfr_msg
.name
, rc
, err
)
599 if primitive
.name
== 'config':
600 for param
in primitive
.parameter
:
602 val
= self
.xlate(param
.value
,
604 config
.update({param
.name
: val
})
607 self
.juju_log('info', vnfr
['vnf_juju_name'],
608 "Applying Initial config:%s",
611 rc
= yield from self
.api
.apply_config(
616 self
.log
.error("Service {} is in error state".format(service
))
619 # Apply any actions specified as part of initial config
620 for primitive
in vnfr
['config'].initial_config_primitive
:
621 if primitive
.name
!= 'config':
622 self
._log
.debug("jujuCA:(%s) Initial config action primitive %s",
623 vnfr
['vnf_juju_name'], primitive
)
624 action
= primitive
.name
626 for param
in primitive
.parameter
:
627 val
= self
.xlate(param
.value
, vnfr
['tags'])
628 params
.update({param
.name
: val
})
630 self
._log
.info("jujuCA:(%s) Action %s with params %s",
631 vnfr
['vnf_juju_name'], action
, params
)
632 self
._log
.debug("executing action")
633 resp
= yield from self
.api
.execute_action(
638 self
._log
.debug("executed action")
640 self
._log
.error("Applying initial config on {} failed for {} with {}: {}".
641 format(vnfr
['vnf_juju_name'], action
, params
, resp
))
643 except KeyError as e
:
644 self
._log
.info("Juju config agent(%s): VNFR %s not managed by Juju",
645 vnfr
['vnf_juju_name'], agent_vnfr
.id)
647 except Exception as e
:
648 self
._log
.exception("jujuCA:(%s) Exception juju "
649 "apply_initial_config for VNFR {}: {}".
650 format(vnfr
['vnf_juju_name'],
656 def add_vnfr_managed(self
, agent_vnfr
):
657 if agent_vnfr
.id not in self
._juju
_vnfs
.keys():
658 self
._log
.info("juju config agent: add vnfr={}/{}".
659 format(agent_vnfr
.name
, agent_vnfr
.id))
660 self
._juju
_vnfs
[agent_vnfr
.id] = agent_vnfr
662 def is_vnfr_managed(self
, vnfr_id
):
664 if vnfr_id
in self
._juju
_vnfs
:
666 except Exception as e
:
667 self
._log
.debug("jujuCA: Is VNFR {} managed: {}".
672 def is_configured(self
, vnfr_id
):
674 agent_vnfr
= self
._juju
_vnfs
[vnfr_id
]
675 vnfr
= agent_vnfr
.vnfr
679 vnfr
= self
._juju
_vnfs
[vnfr_id
].vnfr
680 service
= vnfr
['vnf_juju_name']
681 resp
= self
.api
.is_application_active(application
=service
)
682 self
._juju
_vnfs
[vnfr_id
]['active'] = resp
683 self
._log
.debug("jujuCA: Service state for {} is {}".
684 format(service
, resp
))
688 self
._log
.debug("jujuCA: VNFR id {} not found in config agent".
691 except Exception as e
:
692 self
._log
.error("jujuCA: VNFR id {} is_configured: {}".
697 def get_config_status(self
, agent_nsr
, agent_vnfr
):
698 """Get the configuration status for the VNF"""
702 vnfr
= agent_vnfr
.vnfr
703 service
= vnfr
['vnf_juju_name']
705 # This VNF is not managed by Juju
711 # Get the status of the application
712 resp
= yield from self
.api
.get_application_status(service
)
714 # No status means the application is still pending deployment
723 self
._log
.error("jujuCA: Check unknown service %s status", service
)
724 except Exception as e
:
725 self
._log
.error("jujuCA: Caught exception when checking for service is active: %s", e
)
726 self
._log
.exception(e
)
730 def get_action_status(self
, execution_id
):
731 ''' Get the action status for an execution ID
732 *** Make sure this is NOT a asyncio coroutine function ***
736 self
._log
.debug("jujuCA: Get action status for {}".format(execution_id
))
737 resp
= self
.api
._get
_action
_status
(execution_id
)
738 self
._log
.debug("jujuCA: Action status: {}".format(resp
))
740 except Exception as e
:
741 self
._log
.error("jujuCA: Error fetching execution status for %s",
743 self
._log
.exception(e
)
746 def get_service_status(self
, vnfr_id
):
747 '''Get the service status, used by job status handle
748 Make sure this is NOT a coroutine
750 service
= self
.get_service_name(vnfr_id
)
752 self
._log
.error("jujuCA: VNFR {} not managed by this Juju agent".
756 # Delay for 3 seconds before checking as config apply takes a
757 # few seconds to transfer to the service
759 return self
.api
._get
_service
_status
(service
=service
)