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_short_name
, member_vnf_index
):
30 name
= "{}-{}-{}".format(nsr_name
, vnfr_short_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
, account
):
46 riftcm_config_plugin
.RiftCMConfigPluginBase
.__init
__(self
, dts
, log
, loop
, account
)
47 self
._name
= account
.name
49 self
._ip
_address
= account
.juju
.ip_address
50 self
._port
= account
.juju
.port
51 self
._user
= account
.juju
.user
52 self
._secret
= account
.juju
.secret
53 self
._rift
_install
_dir
= os
.environ
['RIFT_INSTALL']
54 self
._rift
_artif
_dir
= os
.environ
['RIFT_ARTIFACTS']
56 ############################################################
57 # This is wrongfully overloaded with 'juju' private data. #
58 # Really need to separate agent_vnfr from juju vnfr data. #
59 # Currently, this holds agent_vnfr, which has actual vnfr, #
60 # then this juju overloads actual vnfr with its own #
61 # dictionary elemetns (WRONG!!!) #
63 ############################################################
66 self
._api
= juju
.JujuApi(log
, loop
,
67 self
._ip
_address
, self
._port
,
68 self
._user
, self
._secret
)
87 host
=self
._ip
_address
,
93 def vnfr(self
, vnfr_id
):
95 vnfr
= self
._juju
_vnfs
[vnfr_id
].vnfr
97 self
._log
.error("jujuCA: Did not find VNFR %s in juju plugin", vnfr_id
)
102 def get_service_name(self
, vnfr_id
):
103 vnfr
= self
.vnfr(vnfr_id
)
104 if vnfr
and 'vnf_juju_name' in vnfr
:
105 return vnfr
['vnf_juju_name']
108 def juju_log(self
, level
, name
, log_str
, *args
):
110 g_log_str
= 'jujuCA:({}) {}'.format(name
, log_str
)
112 g_log_str
= 'jujuCA: {}'.format(log_str
)
113 getattr(self
._log
, level
)(g_log_str
, *args
)
115 # TBD: Do a better, similar to config manager
116 def xlate(self
, tag
, tags
):
121 if re
.search('<.*>', tag
):
122 self
._log
.debug("jujuCA: Xlate value %s", tag
)
124 if tag
== '<rw_mgmt_ip>':
125 val
= tags
['rw_mgmt_ip']
126 except KeyError as e
:
127 self
._log
.info("jujuCA: Did not get a value for tag %s, e=%s",
132 def notify_create_vlr(self
, agent_nsr
, agent_vnfr
, vld
, vlr
):
134 Notification of create VL record
139 def notify_create_vnfr(self
, agent_nsr
, agent_vnfr
):
141 Notification of create Network VNF record
142 Returns True if configured using config_agent
144 # Deploy the charm if specified for the vnf
145 self
._log
.debug("jujuCA: create vnfr nsr=%s vnfr=%s",
146 agent_nsr
.name
, agent_vnfr
.name
)
147 self
._log
.debug("jujuCA: Config = %s",
148 agent_vnfr
.vnf_configuration
)
150 vnf_config
= agent_vnfr
.vnfr_msg
.vnf_configuration
151 self
._log
.debug("jujuCA: vnf_configuration = %s", vnf_config
)
152 if not vnf_config
.has_field('juju'):
154 charm
= vnf_config
.juju
.charm
155 self
._log
.debug("jujuCA: charm = %s", charm
)
156 except Exception as e
:
157 self
._log
.Error("jujuCA: vnf_configuration error for vnfr {}: {}".
158 format(agent_vnfr
.name
, e
))
161 # Prepare unique name for this VNF
162 vnf_unique_name
= get_vnf_unique_name(agent_nsr
.name
,
164 agent_vnfr
.member_vnf_index
)
165 if vnf_unique_name
in self
._tasks
:
166 self
._log
.warn("jujuCA: Service %s already deployed",
169 vnfr_dict
= agent_vnfr
.vnfr
170 vnfr_dict
.update({'vnf_juju_name': vnf_unique_name
,
172 'nsr_id': agent_nsr
.id,
173 'member_vnf_index': agent_vnfr
.member_vnf_index
,
176 'config': vnf_config
,
177 'vnfr_name' : agent_vnfr
.name
})
178 self
._log
.debug("jujuCA: Charm %s for vnf %s to be deployed as %s",
179 charm
, agent_vnfr
.name
, vnf_unique_name
)
181 # Find the charm directory
183 path
= os
.path
.join(self
._rift
_artif
_dir
,
185 agent_vnfr
.vnfr_msg
.vnfd
.id,
188 self
._log
.debug("jujuCA: Charm dir is {}".format(path
))
189 if not os
.path
.isdir(path
):
190 self
._log
.error("jujuCA: Did not find the charm directory at {}".
193 except Exception as e
:
194 self
.log
.exception(e
)
197 if vnf_unique_name
not in self
._tasks
:
198 self
._tasks
[vnf_unique_name
] = {}
200 self
._tasks
[vnf_unique_name
]['deploy'] = self
.loop
.create_task(
201 self
.api
.deploy_service(charm
, vnf_unique_name
, path
=path
))
203 self
._log
.debug("jujuCA: Deploying service %s",
209 def notify_instantiate_vnfr(self
, agent_nsr
, agent_vnfr
):
211 Notification of Instantiate NSR with the passed nsr id
216 def notify_instantiate_vlr(self
, agent_nsr
, agent_vnfr
, vlr
):
218 Notification of Instantiate NSR with the passed nsr id
223 def notify_terminate_nsr(self
, agent_nsr
, agent_vnfr
):
225 Notification of Terminate the network service
230 def notify_terminate_vnfr(self
, agent_nsr
, agent_vnfr
):
232 Notification of Terminate the network service
234 self
._log
.debug("jujuCA: Terminate VNFr {}, current vnfrs={}".
235 format(agent_vnfr
.name
, self
._juju
_vnfs
))
237 vnfr
= agent_vnfr
.vnfr
238 service
= vnfr
['vnf_juju_name']
240 self
._log
.debug ("jujuCA: Terminating VNFr %s, %s",
241 agent_vnfr
.name
, service
)
242 self
._tasks
[service
]['destroy'] = self
.loop
.create_task(
243 self
.api
.destroy_service(service
)
246 del self
._juju
_vnfs
[agent_vnfr
.id]
247 self
._log
.debug ("jujuCA: current vnfrs={}".
248 format(self
._juju
_vnfs
))
249 if service
in self
._tasks
:
251 for action
in self
._tasks
[service
].keys():
252 #if self.check_task_status(service, action):
255 except KeyError as e
:
256 self
._log
.debug ("jujuCA: Termiating charm service for VNFr {}, e={}".
257 format(agent_vnfr
.name
, e
))
258 except Exception as e
:
259 self
._log
.error("jujuCA: Exception terminating charm service for VNFR {}: {}".
260 format(agent_vnfr
.name
, e
))
265 def notify_terminate_vlr(self
, agent_nsr
, agent_vnfr
, vlr
):
267 Notification of Terminate the virtual link
271 def check_task_status(self
, service
, action
):
272 #self.log.debug("jujuCA: check task status for %s, %s" % (service, action))
274 task
= self
._tasks
[service
][action
]
276 self
.log
.debug("jujuCA: Task for %s, %s done" % (service
, action
))
279 self
.log
.error("jujuCA: Error in task for {} and {} : {}".
280 format(service
, action
, e
))
284 self
.log
.debug("jujuCA: Task for {} and {}, returned {}".
285 format(service
, action
,r
))
288 self
.log
.debug("jujuCA: task {}, {} not done".
289 format(service
, action
))
291 except KeyError as e
:
292 self
.log
.error("jujuCA: KeyError for task for {} and {}: {}".
293 format(service
, action
, e
))
294 except Exception as e
:
295 self
.log
.error("jujuCA: Error for task for {} and {}: {}".
296 format(service
, action
, e
))
301 def vnf_config_primitive(self
, nsr_id
, vnfr_id
, primitive
, output
):
302 self
._log
.debug("jujuCA: VNF config primititve {} for nsr {}, vnfr_id {}".
303 format(primitive
, nsr_id
, vnfr_id
))
305 vnfr
= self
._juju
_vnfs
[vnfr_id
].vnfr
307 self
._log
.error("jujuCA: Did not find VNFR %s in juju plugin",
311 output
.execution_status
= "failed"
312 output
.execution_id
= ''
313 output
.execution_error_details
= ''
316 service
= vnfr
['vnf_juju_name']
317 vnf_config
= vnfr
['config']
318 self
._log
.debug("VNF config %s", vnf_config
)
319 configs
= vnf_config
.service_primitive
320 for config
in configs
:
321 if config
.name
== primitive
.name
:
322 self
._log
.debug("jujuCA: Found the config primitive %s",
325 for parameter
in primitive
.parameter
:
327 val
= self
.xlate(parameter
.value
, vnfr
['tags'])
328 # TBD do validation of the parameters
331 for ca_param
in config
.parameter
:
332 if ca_param
.name
== parameter
.name
:
333 data_type
= ca_param
.data_type
337 if data_type
== 'INTEGER':
340 except Exception as e
:
344 self
._log
.warn("jujuCA: Did not find parameter {} for {}".
345 format(parameter
, config
.name
))
346 params
.update({parameter
.name
: val
})
348 if config
.name
== 'config':
349 output
.execution_id
= 'config'
351 self
._log
.debug("jujuCA: applying config with params {} for service {}".
352 format(params
, service
))
354 rc
= yield from self
.api
.apply_config(params
, service
=service
, wait
=False)
357 # Mark as pending and check later for the status
358 output
.execution_status
= "pending"
359 self
._log
.debug("jujuCA: applied config {} on {}".
360 format(params
, service
))
362 output
.execution_status
= 'failed'
363 output
.execution_error_details
= \
364 'Failed to apply config: {}'.format(params
)
365 self
._log
.error("jujuCA: Error applying config {} on service {}".
366 format(params
, service
))
368 self
._log
.warn("jujuCA: Did not find valid parameters for config : {}".
369 format(primitive
.parameter
))
370 output
.execution_status
= "completed"
372 self
._log
.debug("jujuCA: Execute action {} on service {} with params {}".
373 format(config
.name
, service
, params
))
375 resp
= yield from self
.api
.execute_action(config
.name
,
381 output
.execution_error_details
= resp
['error']['Message']
383 output
.execution_id
= resp
['action']['tag']
384 output
.execution_status
= resp
['status']
385 if output
.execution_status
== 'failed':
386 output
.execution_error_details
= resp
['message']
387 self
._log
.debug("jujuCA: execute action {} on service {} returned {}".
388 format(config
.name
, service
, output
.execution_status
))
390 self
._log
.error("jujuCA: error executing action {} for {} with {}".
391 format(config
.name
, service
, params
))
392 output
.execution_id
= ''
393 output
.execution_status
= 'failed'
394 output
.execution_error_details
= "Failed to queue the action"
397 except KeyError as e
:
398 self
._log
.info("VNF %s does not have config primititves, e=%s", vnfr_id
, e
)
401 def apply_config(self
, agent_nsr
, agent_vnfr
, config
, rpc_ip
):
402 """ Notification on configuration of an NSR """
406 def apply_ns_config(self
, agent_nsr
, agent_vnfrs
, rpc_ip
):
409 ###### TBD - This really does not belong here. Looks more like NS level script ####
410 ###### apply_config should be called for a particular VNF only here ###############
412 Hook: Runs the user defined script. Feeds all the necessary data
413 for the script thro' yaml file.
416 rpc_ip (YangInput_Nsr_ExecNsConfigPrimitive): The input data.
417 nsr (NetworkServiceRecord): Description
418 vnfrs (dict): VNFR ID => VirtualNetworkFunctionRecord
421 def get_meta(agent_nsr
):
422 unit_names
, initial_params
, vnfr_index_map
= {}, {}, {}
424 for vnfr_id
in agent_nsr
.vnfr_ids
:
425 juju_vnf
= self
._juju
_vnfs
[vnfr_id
].vnfr
428 vnfr_index_map
[vnfr_id
] = juju_vnf
['member_vnf_index']
431 unit_names
[vnfr_id
] = juju_vnf
['vnf_juju_name']
433 # Flatten the data for simplicity
435 self
._log
.debug("Juju Config:%s", juju_vnf
['config'])
436 for primitive
in juju_vnf
['config'].initial_config_primitive
:
437 for parameter
in primitive
.parameter
:
438 value
= self
.xlate(parameter
.value
, juju_vnf
['tags'])
439 param_data
[parameter
.name
] = value
441 initial_params
[vnfr_id
] = param_data
444 return unit_names
, initial_params
, vnfr_index_map
446 unit_names
, init_data
, vnfr_index_map
= get_meta(agent_nsr
)
448 # The data consists of 4 sections
450 # 2. The input passed.
451 # 3. Juju unit names (keyed by vnfr ID).
452 # 4. Initial config data (keyed by vnfr ID).
454 data
['config_agent'] = dict(
456 host
=self
._ip
_address
,
461 data
["rpc_ip"] = rpc_ip
.as_dict()
462 data
["unit_names"] = unit_names
463 data
["init_config"] = init_data
464 data
["vnfr_index_map"] = vnfr_index_map
467 with tempfile
.NamedTemporaryFile(delete
=False) as tmp_file
:
468 tmp_file
.write(yaml
.dump(data
, default_flow_style
=True)
471 self
._log
.debug("jujuCA: Creating a temp file: {} with input data".format(
474 # Get the full path to the script
476 if rpc_ip
.user_defined_script
[0] == '/':
477 # The script has full path, use as is
478 script
= rpc_ip
.user_defined_script
480 script
= os
.path
.join(self
._rift
_artif
_dir
, 'launchpad/libs', agent_nsr
.id, 'scripts',
481 rpc_ip
.user_defined_script
)
482 self
.log
.debug("jujuCA: Checking for script in %s", script
)
483 if not os
.path
.exists(script
):
484 script
= os
.path
.join(self
._rift
_install
_dir
, 'usr/bin', rpc_ip
.user_defined_script
)
486 cmd
= "{} {}".format(rpc_ip
.user_defined_script
, tmp_file
.name
)
487 self
._log
.debug("jujuCA: Running the CMD: {}".format(cmd
))
489 coro
= asyncio
.create_subprocess_shell(cmd
, loop
=self
._loop
,
490 stderr
=asyncio
.subprocess
.PIPE
)
491 process
= yield from coro
492 err
= yield from process
.stderr
.read()
493 task
= self
._loop
.create_task(process
.wait())
498 def apply_initial_config(self
, agent_nsr
, agent_vnfr
):
500 Apply the initial configuration
501 Expect config directives mostly, not actions
502 Actions in initial config may not work based on charm design
505 vnfr
= agent_vnfr
.vnfr
506 service
= vnfr
['vnf_juju_name']
508 rc
= yield from self
.api
.is_service_up(service
=service
)
514 vnf_cat
= agent_vnfr
.vnfr_msg
515 if vnf_cat
and vnf_cat
.mgmt_interface
.ip_address
:
516 vnfr
['tags'].update({'rw_mgmt_ip': vnf_cat
.mgmt_interface
.ip_address
})
517 self
._log
.debug("jujuCA:(%s) tags: %s", vnfr
['vnf_juju_name'], vnfr
['tags'])
521 for primitive
in vnfr
['config'].initial_config_primitive
:
522 self
._log
.debug("jujuCA:(%s) Initial config primitive %s", vnfr
['vnf_juju_name'], primitive
)
523 if primitive
.name
== 'config':
524 for param
in primitive
.parameter
:
526 val
= self
.xlate(param
.value
, vnfr
['tags'])
527 config
.update({param
.name
: val
})
528 except KeyError as e
:
529 self
._log
.exception("jujuCA:(%s) Initial config error(%s): config=%s",
530 vnfr
['vnf_juju_name'], str(e
), config
)
535 self
.juju_log('info', vnfr
['vnf_juju_name'],
536 "Applying Initial config:%s",
539 rc
= yield from self
.api
.apply_config(config
, service
=service
)
541 self
.log
.error("Service {} is in error state".format(service
))
545 # Apply any actions specified as part of initial config
546 for primitive
in vnfr
['config'].initial_config_primitive
:
547 if primitive
.name
!= 'config':
548 self
._log
.debug("jujuCA:(%s) Initial config action primitive %s",
549 vnfr
['vnf_juju_name'], primitive
)
550 action
= primitive
.name
552 for param
in primitive
.parameter
:
553 val
= self
.xlate(param
.value
, vnfr
['tags'])
554 params
.update({param
.name
: val
})
556 self
._log
.info("jujuCA:(%s) Action %s with params %s",
557 vnfr
['vnf_juju_name'], action
, params
)
559 resp
= yield from self
.api
.execute_action(action
, params
,
562 self
._log
.error("Applying initial config on {} failed for {} with {}: {}".
563 format(vnfr
['vnf_juju_name'], action
, params
, resp
))
566 action_ids
.append(resp
['action']['tag'])
568 except KeyError as e
:
569 self
._log
.info("Juju config agent(%s): VNFR %s not managed by Juju",
570 vnfr
['vnf_juju_name'], agent_vnfr
.id)
572 except Exception as e
:
573 self
._log
.exception("jujuCA:(%s) Exception juju apply_initial_config for VNFR {}: {}".
574 format(vnfr
['vnf_juju_name'], agent_vnfr
.id, e
))
577 # Check if all actions completed
581 for act
in action_ids
:
582 resp
= yield from self
.api
.get_action_status(act
)
584 self
._log
.error("Initial config failed: {}".format(resp
))
587 if resp
['status'] == 'failed':
588 self
._log
.error("Initial config action failed: {}".format(resp
))
591 if resp
['status'] == 'pending':
596 def add_vnfr_managed(self
, agent_vnfr
):
597 if agent_vnfr
.id not in self
._juju
_vnfs
.keys():
598 self
._log
.info("juju config agent: add vnfr={}/{}".
599 format(agent_vnfr
.name
, agent_vnfr
.id))
600 self
._juju
_vnfs
[agent_vnfr
.id] = agent_vnfr
602 def is_vnfr_managed(self
, vnfr_id
):
604 if vnfr_id
in self
._juju
_vnfs
:
606 except Exception as e
:
607 self
._log
.debug("jujuCA: Is VNFR {} managed: {}".
612 def is_configured(self
, vnfr_id
):
614 agent_vnfr
= self
._juju
_vnfs
[vnfr_id
]
615 vnfr
= agent_vnfr
.vnfr
619 vnfr
= self
._juju
_vnfs
[vnfr_id
].vnfr
620 service
= vnfr
['vnf_juju_name']
621 resp
= self
.api
.is_service_active(service
=service
)
622 self
._juju
_vnfs
[vnfr_id
]['active'] = resp
623 self
._log
.debug("jujuCA: Service state for {} is {}".
624 format(service
, resp
))
628 self
._log
.debug("jujuCA: VNFR id {} not found in config agent".
631 except Exception as e
:
632 self
._log
.error("jujuCA: VNFR id {} is_configured: {}".
637 def get_config_status(self
, agent_nsr
, agent_vnfr
):
638 """Get the configuration status for the VNF"""
642 vnfr
= agent_vnfr
.vnfr
643 service
= vnfr
['vnf_juju_name']
645 # This VNF is not managed by Juju
650 if not self
.check_task_status(service
, 'deploy'):
654 resp
= yield from self
.api
.get_service_status(service
=service
)
655 self
._log
.debug("jujuCA: Get service %s status? %s", service
, resp
)
662 self
._log
.error("jujuCA: Check unknown service %s status", service
)
663 except Exception as e
:
664 self
._log
.error("jujuCA: Caught exception when checking for service is active: %s", e
)
665 self
._log
.exception(e
)
669 def get_action_status(self
, execution_id
):
670 ''' Get the action status for an execution ID
671 *** Make sure this is NOT a asyncio coroutine function ***
675 self
._log
.debug("jujuCA: Get action status for {}".format(execution_id
))
676 resp
= self
.api
._get
_action
_status
(execution_id
)
677 self
._log
.debug("jujuCA: Action status: {}".format(resp
))
679 except Exception as e
:
680 self
._log
.error("jujuCA: Error fetching execution status for %s",
682 self
._log
.exception(e
)
685 def get_service_status(self
, vnfr_id
):
686 '''Get the service status, used by job status handle
687 Make sure this is NOT a coroutine
689 service
= self
.get_service_name(vnfr_id
)
691 self
._log
.error("jujuCA: VNFR {} not managed by this Juju agent".
695 # Delay for 3 seconds before checking as config apply takes a
696 # few seconds to transfer to the service
698 return self
.api
._get
_service
_status
(service
=service
)