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 . import riftcm_config_plugin
27 import rift
.mano
.config_agent
30 gi
.require_version('RwDts', '1.0')
31 gi
.require_version('RwNsrYang', '1.0')
32 from gi
.repository
import (
37 class RiftCMRPCHandler(object):
38 """ The Network service Monitor DTS handler """
39 EXEC_NS_CONF_XPATH
= "I,/nsr:exec-ns-service-primitive"
40 EXEC_NS_CONF_O_XPATH
= "O,/nsr:exec-ns-service-primitive"
42 GET_NS_CONF_XPATH
= "I,/nsr:get-ns-service-primitive-values"
43 GET_NS_CONF_O_XPATH
= "O,/nsr:get-ns-service-primitive-values"
45 def __init__(self
, dts
, log
, loop
, nsm
):
53 self
._get
_ns
_conf
_regh
= None
55 self
.job_manager
= rift
.mano
.config_agent
.ConfigAgentJobManager(dts
, log
, loop
, nsm
)
57 self
._rift
_install
_dir
= os
.environ
['RIFT_INSTALL']
58 self
._rift
_artif
_dir
= os
.environ
['RIFT_ARTIFACTS']
62 """ Return registration handles """
63 return (self
._ns
_regh
, self
._vnf
_regh
, self
._get
_ns
_conf
_regh
)
67 """ Return the NS manager instance """
70 def prepare_meta(self
, rpc_ip
):
73 nsr_id
= rpc_ip
.nsr_id_ref
74 nsr
= self
._nsm
.nsrs
[nsr_id
]
76 for vnfr
in nsr
.vnfrs
:
78 # vnfr is a dict containing all attributes
83 raise ValueError("Record not found", str(e
))
86 def _get_ns_cfg_primitive(self
, nsr_id
, ns_cfg_name
):
87 nsd_msg
= yield from self
._nsm
.get_nsd(nsr_id
)
89 def get_nsd_cfg_prim(name
):
90 for ns_cfg_prim
in nsd_msg
.service_primitive
:
91 if ns_cfg_prim
.name
== name
:
95 ns_cfg_prim_msg
= get_nsd_cfg_prim(ns_cfg_name
)
96 if ns_cfg_prim_msg
is not None:
97 ret_cfg_prim_msg
= ns_cfg_prim_msg
.deep_copy()
98 return ret_cfg_prim_msg
102 def _get_vnf_primitive(self
, vnfr_id
, nsr_id
, primitive_name
):
103 vnf
= self
._nsm
.get_vnfr_msg(vnfr_id
, nsr_id
)
104 self
._log
.debug("vnfr_msg:%s", vnf
)
106 self
._log
.debug("nsr/vnf {}/{}, vnf_configuration: %s",
107 vnf
.vnf_configuration
)
108 for primitive
in vnf
.vnf_configuration
.service_primitive
:
109 if primitive
.name
== primitive_name
:
112 raise ValueError("Could not find nsr/vnf {}/{} primitive {}"
113 .format(nsr_id
, vnfr_id
, primitive_name
))
116 def _apply_ns_config(self
, agent_nsr
, agent_vnfrs
, rpc_ip
):
118 Hook: Runs the user defined script. Feeds all the necessary data
119 for the script thro' yaml file.
121 TBD: Add support to pass multiple CA accounts if configures
122 Remove apply_ns_config from the Config Agent Plugins
125 rpc_ip (YangInput_Nsr_ExecNsConfigPrimitive): The input data.
126 nsr (NetworkServiceRecord): Description
127 vnfrs (dict): VNFR ID => VirtualNetworkFunctionRecord
130 def xlate(tag
, tags
):
132 if tag
is None or tags
is None:
135 if re
.search('<.*>', tag
):
137 if tag
== '<rw_mgmt_ip>':
138 val
= tags
['rw_mgmt_ip']
139 except KeyError as e
:
140 self
._log
.info("RiftCA: Did not get a value for tag %s, e=%s",
144 def get_meta(agent_nsr
, agent_vnfrs
):
145 unit_names
, initial_params
, vnfr_index_map
, vnfr_data_map
= {}, {}, {}, {}
147 for vnfr_id
in agent_nsr
.vnfr_ids
:
148 vnfr
= agent_vnfrs
[vnfr_id
]
149 self
._log
.debug("CA-RPC: VNFR metadata: {}".format(vnfr
))
152 vnfr_index_map
[vnfr
.member_vnf_index
] = vnfr_id
153 vnfr_data_dict
= dict()
154 if 'mgmt_interface' in vnfr
.vnfr
:
155 vnfr_data_dict
['mgmt_interface'] = vnfr
.vnfr
['mgmt_interface']
157 vnfr_data_dict
['connection_point'] = []
158 if 'connection_point' in vnfr
.vnfr
:
159 for cp
in vnfr
.vnfr
['connection_point']:
161 cp_dict
['name'] = cp
['name']
162 cp_dict
['ip_address'] = cp
['ip_address']
163 vnfr_data_dict
['connection_point'].append(cp_dict
)
166 vnfr_data_dict
['vdur'] = []
167 vdu_data
= [(vdu
['name'], vdu
['management_ip'], vdu
['vm_management_ip'], vdu
['id'])
168 for vdu
in vnfr
.vnfr
['vdur']]
170 for data
in vdu_data
:
171 data
= dict(zip(['name', 'management_ip', 'vm_management_ip', 'id'] , data
))
172 vnfr_data_dict
['vdur'].append(data
)
174 vnfr_data_map
[vnfr
.member_vnf_index
] = vnfr_data_dict
175 except KeyError as e
:
176 self
._log
.warn("Error getting VDU data for VNFR {}".format(vnfr
))
179 unit_names
[vnfr_id
] = None
180 for config_plugin
in self
.nsm
.config_agent_plugins
:
181 name
= config_plugin
.get_service_name(vnfr_id
)
183 unit_names
[vnfr_id
] = name
186 # Flatten the data for simplicity
188 if 'initial_config_primitive' in vnfr
.vnf_configuration
:
189 for primitive
in vnfr
.vnf_configuration
['initial_config_primitive']:
190 if 'parameter' in primitive
:
191 for parameter
in primitive
['parameter']:
193 value
= xlate(parameter
['value'], vnfr
.tags
)
194 param_data
[parameter
['name']] = value
195 except KeyError as e
:
196 self
._log
.warn("Unable to parse the parameter{}: {}".
199 initial_params
[vnfr_id
] = param_data
202 return unit_names
, initial_params
, vnfr_index_map
, vnfr_data_map
204 def get_config_agent():
206 for config_plugin
in self
.nsm
.config_agent_plugins
:
207 if config_plugin
.agent_type
in [riftcm_config_plugin
.DEFAULT_CAP_TYPE
]:
208 ret
= config_plugin
.agent_data
210 # Currently the first non default plugin is returned
211 return config_plugin
.agent_data
214 unit_names
, init_data
, vnfr_index_map
, vnfr_data_map
= get_meta(agent_nsr
, agent_vnfrs
)
216 # The data consists of 4 sections
218 # 2. The input passed.
219 # 3. Juju unit names (keyed by vnfr ID).
220 # 4. Initial config data (keyed by vnfr ID).
222 data
['config_agent'] = get_config_agent()
223 data
["rpc_ip"] = rpc_ip
.as_dict()
224 data
["unit_names"] = unit_names
225 data
["init_config"] = init_data
226 data
["vnfr_index_map"] = vnfr_index_map
227 data
["vnfr_data_map"] = vnfr_data_map
230 with tempfile
.NamedTemporaryFile(delete
=False) as tmp_file
:
231 tmp_file
.write(yaml
.dump(data
, default_flow_style
=True)
234 self
._log
.debug("CA-RPC: Creating a temp file {} with input data: {}".
235 format(tmp_file
.name
, data
))
237 # Get the full path to the script
239 if rpc_ip
.user_defined_script
[0] == '/':
240 # The script has full path, use as is
241 script
= rpc_ip
.user_defined_script
243 script
= os
.path
.join(self
._rift
_artif
_dir
, 'launchpad/libs', agent_nsr
.id, 'scripts',
244 rpc_ip
.user_defined_script
)
245 self
._log
.debug("CA-RPC: Checking for script in %s", script
)
246 if not os
.path
.exists(script
):
247 script
= os
.path
.join(self
._rift
_install
_dir
, 'usr/bin', rpc_ip
.user_defined_script
)
249 cmd
= "{} {}".format(rpc_ip
.user_defined_script
, tmp_file
.name
)
250 self
._log
.debug("CA-RPC: Running the CMD: {}".format(cmd
))
252 coro
= asyncio
.create_subprocess_shell(cmd
, loop
=self
._loop
,
253 stderr
=asyncio
.subprocess
.PIPE
)
254 process
= yield from coro
255 err
= yield from process
.stderr
.read()
256 task
= self
._loop
.create_task(process
.wait())
262 """ Register for NS monitoring read from dts """
263 yield from self
.job_manager
.register()
266 def on_ns_config_prepare(xact_info
, action
, ks_path
, msg
):
267 """ prepare callback from dts exec-ns-service-primitive"""
268 assert action
== rwdts
.QueryAction
.RPC
270 rpc_op
= NsrYang
.YangOutput_Nsr_ExecNsServicePrimitive
.from_dict({
271 "triggered_by": rpc_ip
.triggered_by
,
272 "create_time": int(time
.time()),
273 "parameter": [param
.as_dict() for param
in rpc_ip
.parameter
],
274 "parameter_group": [pg
.as_dict() for pg
in rpc_ip
.parameter_group
]
278 ns_cfg_prim_name
= rpc_ip
.name
279 nsr_id
= rpc_ip
.nsr_id_ref
280 nsr
= self
._nsm
.nsrs
[nsr_id
]
282 nsd_cfg_prim_msg
= yield from self
._get
_ns
_cfg
_primitive
(nsr_id
, ns_cfg_prim_name
)
284 def find_nsd_vnf_prim_param_pool(vnf_index
, vnf_prim_name
, param_name
):
285 for vnf_prim_group
in nsd_cfg_prim_msg
.vnf_primitive_group
:
286 if vnf_prim_group
.member_vnf_index_ref
!= vnf_index
:
289 for vnf_prim
in vnf_prim_group
.primitive
:
290 if vnf_prim
.name
!= vnf_prim_name
:
294 nsr_param_pool
= nsr
.param_pools
[pool_param
.parameter_pool
]
296 raise ValueError("Parameter pool %s does not exist in nsr" % vnf_prim
.parameter_pool
)
298 self
._log
.debug("Found parameter pool %s for vnf index(%s), vnf_prim_name(%s), param_name(%s)",
299 nsr_param_pool
, vnf_index
, vnf_prim_name
, param_name
)
300 return nsr_param_pool
302 self
._log
.debug("Could not find parameter pool for vnf index(%s), vnf_prim_name(%s), param_name(%s)",
303 vnf_index
, vnf_prim_name
, param_name
)
306 rpc_op
.nsr_id_ref
= nsr_id
307 rpc_op
.name
= ns_cfg_prim_name
309 nsr
, vnfrs
= self
.prepare_meta(rpc_ip
)
310 rpc_op
.job_id
= nsr
.job_id
312 # Copy over the NS level Parameters
314 # Give preference to user defined script.
315 if nsd_cfg_prim_msg
and nsd_cfg_prim_msg
.has_field("user_defined_script"):
316 rpc_ip
.user_defined_script
= nsd_cfg_prim_msg
.user_defined_script
319 task
, err
= yield from self
._apply
_ns
_config
(
325 rpc_op
.job_status_details
= err
.decode()
327 self
.job_manager
.add_job(rpc_op
, tasks
)
329 # Otherwise create VNF primitives.
330 for vnf
in rpc_ip
.vnf_list
:
331 vnf_op
= rpc_op
.vnf_out_list
.add()
332 vnf_member_idx
= vnf
.member_vnf_index_ref
333 vnfr_id
= vnf
.vnfr_id_ref
334 vnf_op
.vnfr_id_ref
= vnfr_id
335 vnf_op
.member_vnf_index_ref
= vnf_member_idx
338 for primitive
in vnf
.vnf_primitive
:
339 op_primitive
= vnf_op
.vnf_out_primitive
.add()
340 op_primitive
.index
= idx
342 op_primitive
.name
= primitive
.name
343 op_primitive
.execution_id
= ''
344 op_primitive
.execution_status
= 'completed'
345 op_primitive
.execution_error_details
= ''
347 # Copy over the VNF pimitive's input parameters
348 for param
in primitive
.parameter
:
349 output_param
= op_primitive
.parameter
.add()
350 output_param
.name
= param
.name
351 output_param
.value
= param
.value
353 self
._log
.debug("%s:%s Got primitive %s:%s",
354 nsr_id
, vnf
.member_vnf_index_ref
, primitive
.name
, primitive
.parameter
)
356 nsd_vnf_primitive
= yield from self
._get
_vnf
_primitive
(
361 for param
in nsd_vnf_primitive
.parameter
:
362 if not param
.has_field("parameter_pool"):
366 nsr_param_pool
= nsr
.param_pools
[param
.parameter_pool
]
368 raise ValueError("Parameter pool %s does not exist in nsr" % param
.parameter_pool
)
369 nsr_param_pool
.add_used_value(param
.value
)
371 for config_plugin
in self
.nsm
.config_agent_plugins
:
372 yield from config_plugin
.vnf_config_primitive(nsr_id
,
377 self
.job_manager
.add_job(rpc_op
)
380 # Find Config Primitive
381 # For each vnf-primitive with parameter pool
382 # Find parameter pool
383 # Add used value to the pool
384 self
._log
.debug("RPC output: {}".format(rpc_op
))
385 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
,
386 RiftCMRPCHandler
.EXEC_NS_CONF_O_XPATH
,
388 except Exception as e
:
389 self
._log
.error("Exception processing the "
390 "exec-ns-service-primitive: {}".format(e
))
391 self
._log
.exception(e
)
392 xact_info
.respond_xpath(rwdts
.XactRspCode
.NACK
,
393 RiftCMRPCHandler
.EXEC_NS_CONF_O_XPATH
)
396 def on_get_ns_config_values_prepare(xact_info
, action
, ks_path
, msg
):
397 assert action
== rwdts
.QueryAction
.RPC
398 nsr_id
= msg
.nsr_id_ref
399 cfg_prim_name
= msg
.name
401 nsr
= self
._nsm
.nsrs
[nsr_id
]
403 rpc_op
= NsrYang
.YangOutput_Nsr_GetNsServicePrimitiveValues()
405 ns_cfg_prim_msg
= yield from self
._get
_ns
_cfg
_primitive
(nsr_id
, cfg_prim_name
)
407 # Get pool values for NS-level parameters
408 for ns_param
in ns_cfg_prim_msg
.parameter
:
409 if not ns_param
.has_field("parameter_pool"):
413 nsr_param_pool
= nsr
.param_pools
[ns_param
.parameter_pool
]
415 raise ValueError("Parameter pool %s does not exist in nsr" % ns_param
.parameter_pool
)
417 new_ns_param
= rpc_op
.ns_parameter
.add()
418 new_ns_param
.name
= ns_param
.name
419 new_ns_param
.value
= str(nsr_param_pool
.get_next_unused_value())
421 # Get pool values for NS-level parameters
422 for vnf_prim_group
in ns_cfg_prim_msg
.vnf_primitive_group
:
423 rsp_prim_group
= rpc_op
.vnf_primitive_group
.add()
424 rsp_prim_group
.member_vnf_index_ref
= vnf_prim_group
.member_vnf_index_ref
425 if vnf_prim_group
.has_field("vnfd_id_ref"):
426 rsp_prim_group
.vnfd_id_ref
= vnf_prim_group
.vnfd_id_ref
428 for index
, vnf_prim
in enumerate(vnf_prim_group
.primitive
):
429 rsp_prim
= rsp_prim_group
.primitive
.add()
430 rsp_prim
.name
= vnf_prim
.name
431 rsp_prim
.index
= index
432 vnf_primitive
= yield from self
._get
_vnf
_primitive
(
433 vnf_prim_group
.vnfd_id_ref
,
437 for param
in vnf_primitive
.parameter
:
438 if not param
.has_field("parameter_pool"):
441 # Get pool values for NS-level parameters
442 for ns_param
in ns_cfg_prim_msg
.parameter
:
443 if not ns_param
.has_field("parameter_pool"):
447 nsr_param_pool
= nsr
.param_pools
[ns_param
.parameter_pool
]
449 raise ValueError("Parameter pool %s does not exist in nsr" % ns_param
.parameter_pool
)
451 new_ns_param
= rpc_op
.ns_parameter
.add()
452 new_ns_param
.name
= ns_param
.name
453 new_ns_param
.value
= str(nsr_param_pool
.get_next_unused_value())
455 # Get pool values for NS-level parameters
456 for vnf_prim_group
in ns_cfg_prim_msg
.vnf_primitive_group
:
457 rsp_prim_group
= rpc_op
.vnf_primitive_group
.add()
458 rsp_prim_group
.member_vnf_index_ref
= vnf_prim_group
.member_vnf_index_ref
459 if vnf_prim_group
.has_field("vnfd_id_ref"):
460 rsp_prim_group
.vnfd_id_ref
= vnf_prim_group
.vnfd_id_ref
462 for index
, vnf_prim
in enumerate(vnf_prim_group
.primitive
):
463 rsp_prim
= rsp_prim_group
.primitive
.add()
464 rsp_prim
.name
= vnf_prim
.name
465 rsp_prim
.index
= index
466 vnf_primitive
= yield from self
._get
_vnf
_primitive
(
468 vnf_prim_group
.member_vnf_index_ref
,
471 for param
in vnf_primitive
.parameter
:
472 if not param
.has_field("parameter_pool"):
476 nsr_param_pool
= nsr
.param_pools
[param
.parameter_pool
]
478 raise ValueError("Parameter pool %s does not exist in nsr" % vnf_prim
.parameter_pool
)
480 vnf_param
= rsp_prim
.parameter
.add()
481 vnf_param
.name
= param
.name
482 vnf_param
.value
= str(nsr_param_pool
.get_next_unused_value())
484 self
._log
.debug("RPC output: {}".format(rpc_op
))
485 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
,
486 RiftCMRPCHandler
.GET_NS_CONF_O_XPATH
, rpc_op
)
487 except Exception as e
:
488 self
._log
.error("Exception processing the "
489 "get-ns-service-primitive-values: {}".format(e
))
490 self
._log
.exception(e
)
491 xact_info
.respond_xpath(rwdts
.XactRspCode
.NACK
,
492 RiftCMRPCHandler
.GET_NS_CONF_O_XPATH
)
494 hdl_ns
= rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_ns_config_prepare
,)
495 hdl_ns_get
= rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_get_ns_config_values_prepare
,)
497 with self
._dts
.group_create() as group
:
498 self
._ns
_regh
= group
.register(xpath
=RiftCMRPCHandler
.EXEC_NS_CONF_XPATH
,
500 flags
=rwdts
.Flag
.PUBLISHER
,
502 self
._get
_ns
_conf
_regh
= group
.register(xpath
=RiftCMRPCHandler
.GET_NS_CONF_XPATH
,
504 flags
=rwdts
.Flag
.PUBLISHER
,