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 # Charm service name accepts only a to z and -.
29 def get_vnf_unique_name(nsr_name
, vnfr_name
, member_vnf_index
):
30 name
= "{}-{}-{}".format(nsr_name
, vnfr_name
, member_vnf_index
)
38 return new_name
.lower()
41 class JujuConfigPlugin(riftcm_config_plugin
.RiftCMConfigPluginBase
):
43 Juju implementation of the riftcm_config_plugin.RiftCMConfigPluginBase
45 def __init__(self
, dts
, log
, loop
, project
, account
):
46 riftcm_config_plugin
.RiftCMConfigPluginBase
.__init
__(self
, dts
, log
, loop
,
48 self
._name
= account
.name
50 self
._ip
_address
= account
.juju
.ip_address
51 self
._port
= account
.juju
.port
52 self
._user
= account
.juju
.user
53 self
._secret
= account
.juju
.secret
54 self
._rift
_install
_dir
= os
.environ
['RIFT_INSTALL']
55 self
._rift
_var
_root
_dir
= os
.environ
['RIFT_VAR_ROOT']
57 ############################################################
58 # This is wrongfully overloaded with 'juju' private data. #
59 # Really need to separate agent_vnfr from juju vnfr data. #
60 # Currently, this holds agent_vnfr, which has actual vnfr, #
61 # then this juju overloads actual vnfr with its own #
62 # dictionary elemetns (WRONG!!!) #
64 ############################################################
67 self
._api
= juju
.JujuApi(log
, loop
,
68 self
._ip
_address
, self
._port
,
69 self
._user
, self
._secret
)
88 host
=self
._ip
_address
,
94 def vnfr(self
, vnfr_id
):
96 vnfr
= self
._juju
_vnfs
[vnfr_id
].vnfr
98 self
._log
.error("jujuCA: Did not find VNFR %s in juju plugin", vnfr_id
)
103 def get_service_name(self
, vnfr_id
):
104 vnfr
= self
.vnfr(vnfr_id
)
105 if vnfr
and 'vnf_juju_name' in vnfr
:
106 return vnfr
['vnf_juju_name']
109 def juju_log(self
, level
, name
, log_str
, *args
):
111 g_log_str
= 'jujuCA:({}) {}'.format(name
, log_str
)
113 g_log_str
= 'jujuCA: {}'.format(log_str
)
114 getattr(self
._log
, level
)(g_log_str
, *args
)
116 # TBD: Do a better, similar to config manager
117 def xlate(self
, tag
, tags
):
122 if re
.search('<.*>', tag
):
123 self
._log
.debug("jujuCA: Xlate value %s", tag
)
125 if tag
== '<rw_mgmt_ip>':
126 val
= tags
['rw_mgmt_ip']
127 except KeyError as e
:
128 self
._log
.info("jujuCA: Did not get a value for tag %s, e=%s",
133 def notify_create_vlr(self
, agent_nsr
, agent_vnfr
, vld
, vlr
):
135 Notification of create VL record
140 def notify_create_vnfr(self
, agent_nsr
, agent_vnfr
):
142 Notification of create Network VNF record
143 Returns True if configured using config_agent
145 # Deploy the charm if specified for the vnf
146 self
._log
.debug("jujuCA: create vnfr nsr=%s vnfr=%s",
147 agent_nsr
.name
, agent_vnfr
.name
)
148 self
._log
.debug("jujuCA: Config = %s",
149 agent_vnfr
.vnf_configuration
)
151 vnf_config
= agent_vnfr
.vnfr_msg
.vnf_configuration
152 self
._log
.debug("jujuCA: vnf_configuration = %s", vnf_config
)
153 if not vnf_config
.has_field('juju'):
155 charm
= vnf_config
.juju
.charm
156 self
._log
.debug("jujuCA: charm = %s", charm
)
157 except Exception as e
:
158 self
._log
.Error("jujuCA: vnf_configuration error for vnfr {}: {}".
159 format(agent_vnfr
.name
, e
))
162 # Prepare unique name for this VNF
163 vnf_unique_name
= get_vnf_unique_name(agent_nsr
.name
,
165 agent_vnfr
.member_vnf_index
)
166 if vnf_unique_name
in self
._tasks
:
167 self
._log
.warn("jujuCA: Service %s already deployed",
170 vnfr_dict
= agent_vnfr
.vnfr
171 vnfr_dict
.update({'vnf_juju_name': vnf_unique_name
,
173 'nsr_id': agent_nsr
.id,
174 'member_vnf_index': agent_vnfr
.member_vnf_index
,
177 'config': vnf_config
,
178 'vnfr_name' : agent_vnfr
.name
})
179 self
._log
.debug("jujuCA: Charm %s for vnf %s to be deployed as %s",
180 charm
, agent_vnfr
.name
, vnf_unique_name
)
182 # Find the charm directory
184 path
= os
.path
.join(self
._rift
_var
_root
_dir
,
185 'launchpad/packages/vnfd',
187 agent_vnfr
.vnfr_msg
.vnfd
.id,
190 self
._log
.debug("jujuCA: Charm dir is {}".format(path
))
191 if not os
.path
.isdir(path
):
192 self
._log
.error("jujuCA: Did not find the charm directory at {}".
195 except Exception as e
:
196 self
.log
.exception(e
)
199 if vnf_unique_name
not in self
._tasks
:
200 self
._tasks
[vnf_unique_name
] = {}
202 self
._tasks
[vnf_unique_name
]['deploy'] = self
.loop
.create_task(
203 self
.api
.deploy_service(charm
, vnf_unique_name
, path
=path
))
205 self
._log
.debug("jujuCA: Deploying service %s",
211 def notify_instantiate_vnfr(self
, agent_nsr
, agent_vnfr
):
213 Notification of Instantiate NSR with the passed nsr id
218 def notify_instantiate_vlr(self
, agent_nsr
, agent_vnfr
, vlr
):
220 Notification of Instantiate NSR with the passed nsr id
225 def notify_terminate_nsr(self
, agent_nsr
, agent_vnfr
):
227 Notification of Terminate the network service
232 def notify_terminate_vnfr(self
, agent_nsr
, agent_vnfr
):
234 Notification of Terminate the network service
236 self
._log
.debug("jujuCA: Terminate VNFr {}, current vnfrs={}".
237 format(agent_vnfr
.name
, self
._juju
_vnfs
))
239 vnfr
= agent_vnfr
.vnfr
240 service
= vnfr
['vnf_juju_name']
242 self
._log
.debug ("jujuCA: Terminating VNFr %s, %s",
243 agent_vnfr
.name
, service
)
244 self
._tasks
[service
]['destroy'] = self
.loop
.create_task(
245 self
.api
.destroy_service(service
)
248 del self
._juju
_vnfs
[agent_vnfr
.id]
249 self
._log
.debug ("jujuCA: current vnfrs={}".
250 format(self
._juju
_vnfs
))
251 if service
in self
._tasks
:
253 for action
in self
._tasks
[service
].keys():
254 #if self.check_task_status(service, action):
257 except KeyError as e
:
258 self
._log
.debug ("jujuCA: Termiating charm service for VNFr {}, e={}".
259 format(agent_vnfr
.name
, e
))
260 except Exception as e
:
261 self
._log
.error("jujuCA: Exception terminating charm service for VNFR {}: {}".
262 format(agent_vnfr
.name
, e
))
267 def notify_terminate_vlr(self
, agent_nsr
, agent_vnfr
, vlr
):
269 Notification of Terminate the virtual link
273 def check_task_status(self
, service
, action
):
274 #self.log.debug("jujuCA: check task status for %s, %s" % (service, action))
276 task
= self
._tasks
[service
][action
]
278 self
.log
.debug("jujuCA: Task for %s, %s done" % (service
, action
))
281 self
.log
.error("jujuCA: Error in task for {} and {} : {}".
282 format(service
, action
, e
))
286 self
.log
.debug("jujuCA: Task for {} and {}, returned {}".
287 format(service
, action
,r
))
290 self
.log
.debug("jujuCA: task {}, {} not done".
291 format(service
, action
))
293 except KeyError as e
:
294 self
.log
.error("jujuCA: KeyError for task for {} and {}: {}".
295 format(service
, action
, e
))
296 except Exception as e
:
297 self
.log
.error("jujuCA: Error for task for {} and {}: {}".
298 format(service
, action
, e
))
303 def _vnf_config_primitive(self
, nsr_id
, vnfr_id
, primitive
,
304 vnf_config
=None, wait
=False):
305 self
._log
.debug("jujuCA: VNF config primitive {} for nsr {}, "
307 format(primitive
, nsr_id
, vnfr_id
))
309 if vnf_config
is None:
310 vnfr_msg
= yield from self
.get_vnfr(vnfr_id
)
312 msg
= "Unable to get VNFR {} through DTS".format(vnfr_id
)
316 vnf_config
= vnfr_msg
.vnf_configuration
317 self
._log
.debug("VNF config= %s", vnf_config
.as_dict())
320 service
= vnfr
['vnf_juju_name']
321 self
._log
.debug("VNF config %s", vnf_config
)
322 configs
= vnf_config
.config_primitive
323 for config
in configs
:
324 if config
.name
== primitive
.name
:
325 self
._log
.debug("jujuCA: Found the config primitive %s",
328 for parameter
in config
.parameter
:
330 for p
in primitive
.parameter
:
331 if p
.name
== parameter
.name
:
333 val
= self
.xlate(p
.value
, vnfr
['tags'])
337 val
= parameter
.default_value
340 # Check if mandatory parameter
341 if parameter
.mandatory
:
342 msg
= "VNFR {}: Primitive {} called " \
343 "without mandatory parameter {}". \
344 format(vnfr_msg
.name
, config
.name
,
347 return 'failed', '', msg
350 val
= self
.convert_value(val
, parameter
.data_type
)
351 params
.update({parameter
.name
: val
})
356 if config
.name
== 'config':
359 self
._log
.debug("jujuCA: applying config with "
360 "params {} for service {}".
361 format(params
, service
))
363 rc
= yield from self
.api
.apply_config(
370 self
._log
.debug("jujuCA: applied config {} "
371 "on {}".format(params
, service
))
375 'Failed to apply config: {}'.format(params
)
376 self
._log
.error("jujuCA: Error applying "
377 "config {} on service {}".
378 format(params
, service
))
380 self
._log
.warn("jujuCA: Did not find valid "
381 "parameters for config : {}".
382 format(primitive
.parameter
))
385 self
._log
.debug("jujuCA: Execute action {} on "
386 "service {} with params {}".
387 format(config
.name
, service
, params
))
389 resp
= yield from self
.api
.execute_action(
396 details
= resp
['error']['message']
398 exec_id
= resp
['action']['tag']
401 details
= resp
['message']
403 self
._log
.debug("jujuCA: execute action {} on "
404 "service {} returned {}".
405 format(config
.name
, service
, rc
))
407 self
._log
.error("jujuCA: error executing action "
409 format(config
.name
, service
,
413 details
= "Failed to queue the action"
416 except KeyError as e
:
417 msg
= "VNF %s does not have config primitives, e=%s", \
419 self
._log
.exception(msg
)
420 raise ValueError(msg
)
422 while wait
and (rc
in ['pending', 'running']):
423 self
._log
.debug("JujuCA: action {}, rc {}".
425 yield from asyncio
.sleep(0.2, loop
=self
._loop
)
426 status
= yield from self
.api
.get_action_status(exec_id
)
427 rc
= status
['status']
429 return rc
, exec_id
, details
432 def vnf_config_primitive(self
, nsr_id
, vnfr_id
, primitive
, output
):
434 vnfr
= self
._juju
_vnfs
[vnfr_id
].vnfr
436 msg
= "Did not find VNFR {} in Juju plugin".format(vnfr_id
)
440 output
.execution_status
= "failed"
441 output
.execution_id
= ''
442 output
.execution_error_details
= ''
444 rc
, exec_id
, err
= yield from self
._vnf
_config
_primitive
(
449 self
._log
.debug("VNFR {} primitive {} exec status: {}".
450 format(vnfr
.name
, primitive
.name
, rc
))
451 output
.execution_status
= rc
452 output
.execution_id
= exec_id
453 output
.execution_error_details
= err
456 def apply_config(self
, agent_nsr
, agent_vnfr
, config
, rpc_ip
):
457 """ Notification on configuration of an NSR """
461 def apply_ns_config(self
, agent_nsr
, agent_vnfrs
, rpc_ip
):
464 ###### TBD - This really does not belong here. Looks more like NS level script ####
465 ###### apply_config should be called for a particular VNF only here ###############
467 Hook: Runs the user defined script. Feeds all the necessary data
468 for the script thro' yaml file.
471 rpc_ip (YangInput_Nsr_ExecNsConfigPrimitive): The input data.
472 nsr (NetworkServiceRecord): Description
473 vnfrs (dict): VNFR ID => VirtualNetworkFunctionRecord
476 def get_meta(agent_nsr
):
477 unit_names
, initial_params
, vnfr_index_map
= {}, {}, {}
479 for vnfr_id
in agent_nsr
.vnfr_ids
:
480 juju_vnf
= self
._juju
_vnfs
[vnfr_id
].vnfr
483 vnfr_index_map
[vnfr_id
] = juju_vnf
['member_vnf_index']
486 unit_names
[vnfr_id
] = juju_vnf
['vnf_juju_name']
488 # Flatten the data for simplicity
490 self
._log
.debug("Juju Config:%s", juju_vnf
['config'])
491 for primitive
in juju_vnf
['config'].initial_config_primitive
:
492 for parameter
in primitive
.parameter
:
493 value
= self
.xlate(parameter
.value
, juju_vnf
['tags'])
494 param_data
[parameter
.name
] = value
496 initial_params
[vnfr_id
] = param_data
499 return unit_names
, initial_params
, vnfr_index_map
501 unit_names
, init_data
, vnfr_index_map
= get_meta(agent_nsr
)
503 # The data consists of 4 sections
505 # 2. The input passed.
506 # 3. Juju unit names (keyed by vnfr ID).
507 # 4. Initial config data (keyed by vnfr ID).
509 data
['config_agent'] = dict(
511 host
=self
._ip
_address
,
516 data
["rpc_ip"] = rpc_ip
.as_dict()
517 data
["unit_names"] = unit_names
518 data
["init_config"] = init_data
519 data
["vnfr_index_map"] = vnfr_index_map
522 with tempfile
.NamedTemporaryFile(delete
=False) as tmp_file
:
523 tmp_file
.write(yaml
.dump(data
, default_flow_style
=True)
526 self
._log
.debug("jujuCA: Creating a temp file: {} with input data".format(
529 # Get the full path to the script
531 if rpc_ip
.user_defined_script
[0] == '/':
532 # The script has full path, use as is
533 script
= rpc_ip
.user_defined_script
535 script
= os
.path
.join(self
._rift
_var
_root
_dir
, 'launchpad/nsd',
537 agent_nsr
.id, 'scripts',
538 rpc_ip
.user_defined_script
)
539 self
.log
.debug("jujuCA: Checking for script in %s", script
)
540 if not os
.path
.exists(script
):
541 script
= os
.path
.join(self
._rift
_install
_dir
, 'usr/bin', rpc_ip
.user_defined_script
)
543 cmd
= "{} {}".format(rpc_ip
.user_defined_script
, tmp_file
.name
)
544 self
._log
.debug("jujuCA: Running the CMD: {}".format(cmd
))
546 coro
= asyncio
.create_subprocess_shell(cmd
, loop
=self
._loop
,
547 stderr
=asyncio
.subprocess
.PIPE
)
548 process
= yield from coro
549 err
= yield from process
.stderr
.read()
550 task
= self
._loop
.create_task(process
.wait())
555 def apply_initial_config(self
, agent_nsr
, agent_vnfr
):
557 Apply the initial configuration
558 Expect config directives mostly, not actions
559 Actions in initial config may not work based on charm design
563 vnfr
= self
._juju
_vnfs
[agent_vnfr
.id].vnfr
565 self
._log
.debug("Did not find VNFR %s in Juju plugin",
569 vnfr_msg
= yield from self
.get_vnfr(agent_vnfr
.id)
571 msg
= "Unable to get VNFR {} ({}) through DTS". \
572 format(agent_vnfr
.id, agent_vnfr
.name
)
574 raise RuntimeError(msg
)
576 vnf_config
= vnfr_msg
.vnf_configuration
577 self
._log
.debug("VNFR %s config: %s", vnfr_msg
.name
,
578 vnf_config
.as_dict())
580 # Sort the primitive based on the sequence number
581 primitives
= sorted(vnf_config
.initial_config_primitive
,
584 self
._log
.debug("VNFR {}: No initial-config-primitive specified".
585 format(vnfr_msg
.name
))
588 service
= vnfr
['vnf_juju_name']
589 rc
= yield from self
.api
.is_service_up(service
=service
)
595 if vnfr_msg
.mgmt_interface
.ip_address
:
596 vnfr
['tags'].update({'rw_mgmt_ip': vnfr_msg
.mgmt_interface
.ip_address
})
597 self
._log
.debug("jujuCA:(%s) tags: %s", vnfr
['vnf_juju_name'], vnfr
['tags'])
599 for primitive
in primitives
:
600 self
._log
.debug("(%s) Initial config primitive %s",
601 vnfr
['vnf_juju_name'], primitive
.as_dict())
602 if primitive
.config_primitive_ref
:
603 # Reference to a primitive in config primitive
605 def __init__(self
, name
):
610 prim
= Primitive(primitive
.config_primitive_ref
)
611 rc
, eid
, err
= yield from self
._vnf
_config
_primitive
(
619 msg
= "Error executing initial config primitive" \
620 " {} in VNFR {}: rc={}, stderr={}". \
621 format(prim
.name
, vnfr_msg
.name
, rc
, err
)
625 elif rc
== "pending":
626 action_ids
.append(eid
)
630 if primitive
.name
== 'config':
631 for param
in primitive
.parameter
:
633 val
= self
.xlate(param
.value
,
635 config
.update({param
.name
: val
})
638 self
.juju_log('info', vnfr
['vnf_juju_name'],
639 "Applying Initial config:%s",
642 rc
= yield from self
.api
.apply_config(
646 self
.log
.error("Service {} is in error state".
650 # Apply any actions specified as part of initial config
652 self
._log
.debug("(%s) Initial config action "
654 vnfr
['vnf_juju_name'], primitive
)
655 action
= primitive
.name
657 for param
in primitive
.parameter
:
658 val
= self
.xlate(param
.value
, vnfr
['tags'])
659 params
.update({param
.name
: val
})
661 self
._log
.info("(%s) Action %s with params %s",
662 vnfr
['vnf_juju_name'], action
,
665 resp
= yield from self
.api
.execute_action(
670 self
._log
.error("Applying initial config on {}"
671 " failed for {} with {}: {}".
672 format(vnfr
['vnf_juju_name'],
673 action
, params
, resp
))
676 action_ids
.append(resp
['action']['tag'])
678 except Exception as e
:
679 self
._log
.exception("jujuCA:(%s) Exception juju "
680 "apply_initial_config for VNFR {}: {}".
681 format(vnfr
['vnf_juju_name'],
685 # Check if all actions completed
689 for act
in action_ids
:
690 resp
= yield from self
.api
.get_action_status(act
)
692 self
._log
.error("Initial config failed for action {}: {}".
696 if resp
['status'] == 'failed':
697 self
._log
.error("Initial config action failed for "
698 "action {}: {}".format(act
, resp
))
701 if resp
['status'] == 'pending':
706 def add_vnfr_managed(self
, agent_vnfr
):
707 if agent_vnfr
.id not in self
._juju
_vnfs
.keys():
708 self
._log
.info("juju config agent: add vnfr={}/{}".
709 format(agent_vnfr
.name
, agent_vnfr
.id))
710 self
._juju
_vnfs
[agent_vnfr
.id] = agent_vnfr
712 def is_vnfr_managed(self
, vnfr_id
):
714 if vnfr_id
in self
._juju
_vnfs
:
716 except Exception as e
:
717 self
._log
.debug("jujuCA: Is VNFR {} managed: {}".
722 def is_configured(self
, vnfr_id
):
724 agent_vnfr
= self
._juju
_vnfs
[vnfr_id
]
725 vnfr
= agent_vnfr
.vnfr
729 vnfr
= self
._juju
_vnfs
[vnfr_id
].vnfr
730 service
= vnfr
['vnf_juju_name']
731 resp
= self
.api
.is_service_active(service
=service
)
732 self
._juju
_vnfs
[vnfr_id
]['active'] = resp
733 self
._log
.debug("jujuCA: Service state for {} is {}".
734 format(service
, resp
))
738 self
._log
.debug("jujuCA: VNFR id {} not found in config agent".
741 except Exception as e
:
742 self
._log
.error("jujuCA: VNFR id {} is_configured: {}".
747 def get_config_status(self
, agent_nsr
, agent_vnfr
):
748 """Get the configuration status for the VNF"""
752 vnfr
= agent_vnfr
.vnfr
753 service
= vnfr
['vnf_juju_name']
755 # This VNF is not managed by Juju
760 if not self
.check_task_status(service
, 'deploy'):
764 resp
= yield from self
.api
.get_service_status(service
=service
)
765 self
._log
.debug("jujuCA: Get service %s status? %s", service
, resp
)
772 self
._log
.error("jujuCA: Check unknown service %s status", service
)
773 except Exception as e
:
774 self
._log
.error("jujuCA: Caught exception when checking for service is active: %s", e
)
775 self
._log
.exception(e
)
779 def get_action_status(self
, execution_id
):
780 ''' Get the action status for an execution ID
781 *** Make sure this is NOT a asyncio coroutine function ***
785 self
._log
.debug("jujuCA: Get action status for {}".format(execution_id
))
786 resp
= self
.api
._get
_action
_status
(execution_id
)
787 self
._log
.debug("jujuCA: Action status: {}".format(resp
))
789 except Exception as e
:
790 self
._log
.error("jujuCA: Error fetching execution status for %s",
792 self
._log
.exception(e
)
795 def get_service_status(self
, vnfr_id
):
796 '''Get the service status, used by job status handle
797 Make sure this is NOT a coroutine
799 service
= self
.get_service_name(vnfr_id
)
801 self
._log
.error("jujuCA: VNFR {} not managed by this Juju agent".
805 # Delay for 3 seconds before checking as config apply takes a
806 # few seconds to transfer to the service
808 return self
.api
._get
_service
_status
(service
=service
)