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.
25 gi
.require_version('RwDts', '1.0')
26 gi
.require_version('RwVnfrYang', '1.0')
27 from gi
.repository
import (
31 gi
.require_version('RwKeyspec', '1.0')
32 from gi
.repository
.RwKeyspec
import quoted_key
34 import rift
.openmano
.rift2openmano
as rift2openmano
35 import rift
.openmano
.openmano_client
as openmano_client
36 from . import nsmpluginbase
42 if sys
.version_info
< (3, 4, 4):
43 asyncio
.ensure_future
= asyncio
.async
46 DUMP_OPENMANO_DIR
= os
.path
.join(
47 os
.environ
["RIFT_VAR_ROOT"],
48 "openmano_descriptors"
52 def dump_openmano_descriptor(name
, descriptor_str
):
53 filename
= "{}_{}.yaml".format(
54 time
.strftime("%Y%m%d-%H%M%S"),
58 filepath
= os
.path
.join(
64 if not os
.path
.exists(DUMP_OPENMANO_DIR
):
65 os
.makedirs(DUMP_OPENMANO_DIR
)
67 with
open(filepath
, 'w') as hdl
:
68 hdl
.write(descriptor_str
)
71 print("Failed to dump openmano descriptor: %s" % str(e
))
75 class VNFExistError(Exception):
78 class VnfrConsoleOperdataDtsHandler(object):
79 """ registers 'D,/vnfr:vnfr-console/vnfr:vnfr[id]/vdur[id]' and handles CRUD from DTS"""
81 def vnfr_vdu_console_xpath(self
):
82 """ path for resource-mgr"""
83 return self
._project
.add_project(
84 "D,/rw-vnfr:vnfr-console/rw-vnfr:vnfr[rw-vnfr:id={}]/rw-vnfr:vdur[vnfr:id={}]".format(
85 quoted_key(self
._vnfr
_id
), quoted_key(self
._vdur
_id
)))
87 def __init__(self
, project
, dts
, log
, loop
, nsr
, vnfr_id
, vdur_id
, vdu_id
):
88 self
._project
= project
95 self
._vnfr
_id
= vnfr_id
96 self
._vdur
_id
= vdur_id
101 """ Register for VNFR VDU Operational Data read from dts """
104 def on_prepare(xact_info
, action
, ks_path
, msg
):
105 """ prepare callback from dts """
106 xpath
= ks_path
.to_xpath(RwVnfrYang
.get_schema())
108 "Got VNFR VDU Opdata xact_info: %s, action: %s): %s:%s",
109 xact_info
, action
, xpath
, msg
112 if action
== rwdts
.QueryAction
.READ
:
113 schema
= RwVnfrYang
.YangData_RwProject_Project_VnfrConsole_Vnfr_Vdur
.schema()
114 path_entry
= schema
.keyspec_to_entry(ks_path
)
117 console_url
= yield from self
._loop
.run_in_executor(
119 self
._nsr
._http
_api
.get_instance_vm_console_url
,
124 self
._log
.debug("Got console response: %s for NSR ID %s vdur ID %s",
129 vdur_console
= RwVnfrYang
.YangData_RwProject_Project_VnfrConsole_Vnfr_Vdur()
130 vdur_console
.id = self
._vdur
_id
132 vdur_console
.console_url
= console_url
134 vdur_console
.console_url
= 'none'
135 self
._log
.debug("Recevied console URL for vdu {} is {}".format(self
._vdu
_id
,vdur_console
))
136 except openmano_client
.InstanceStatusError
as e
:
137 self
._log
.error("Could not get NS instance console URL: %s",
139 vdur_console
= RwVnfrYang
.YangData_RwProject_Project_VnfrConsole_Vnfr_Vdur()
140 vdur_console
.id = self
._vdur
_id
141 vdur_console
.console_url
= 'none'
143 xact_info
.respond_xpath(rsp_code
=rwdts
.XactRspCode
.ACK
,
144 xpath
=self
.vnfr_vdu_console_xpath
,
147 #raise VnfRecordError("Not supported operation %s" % action)
148 self
._log
.error("Not supported operation %s" % action
)
149 xact_info
.respond_xpath(rsp_code
=rwdts
.XactRspCode
.ACK
)
152 self
._log
.debug("Registering for VNFR VDU using xpath: %s",
153 self
.vnfr_vdu_console_xpath
)
154 hdl
= rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_prepare
,)
155 with self
._dts
.group_create() as group
:
156 self
._regh
= group
.register(xpath
=self
.vnfr_vdu_console_xpath
,
158 flags
=rwdts
.Flag
.PUBLISHER
,
163 class OpenmanoVnfr(object):
164 def __init__(self
, project
, log
, loop
, cli_api
, http_api
, vnfr
, nsd
, ssh_key
=None):
165 self
._project
= project
168 self
._cli
_api
= cli_api
169 self
._http
_api
= http_api
171 self
._vnfd
_id
= vnfr
.vnfd
.id
175 self
._created
= False
178 self
._ssh
_key
= ssh_key
182 return rift2openmano
.RiftVNFD(self
._vnfr
.vnfd
)
189 def rift_vnfd_id(self
):
193 def openmano_vnfd_id(self
):
197 def openmano_vnfd(self
):
198 self
._log
.debug("Converting vnfd %s from rift to openmano", self
.vnfd
.id)
199 openmano_vnfd
= rift2openmano
.rift2openmano_vnfd(self
.vnfd
, self
.nsd
, self
._http
_api
, self
._project
)
203 def openmano_vnfd_yaml(self
):
204 return yaml
.safe_dump(self
.openmano_vnfd
, default_flow_style
=False)
209 self
._log
.debug("Created openmano vnfd")
210 # The self.openmano_vnfd_yaml internally creates the vnf if not found.
211 # Assigning the yaml to a variable so that the api is not fired unnecessarily.
212 openmano_vnfd
= self
.openmano_vnfd
213 name
= openmano_vnfd
["name"]
215 self
._vnf
_id
= openmano_vnfd
['uuid']
218 except Exception as e
:
219 self
._log
.error("Failed to create vnf on Openmano RO : %s", e
)
223 if not self
._created
:
226 self
._log
.debug("Deleting openmano vnfd")
227 if self
._vnf
_id
is None:
228 self
._log
.warning("Openmano vnf id not set. Cannot delete.")
231 self
._cli
_api
.vnf_delete(self
._vnf
_id
)
234 class OpenmanoNSRecordState(Enum
):
235 """ Network Service Record State """
236 # Make sure the values match with NetworkServiceRecordState
238 INSTANTIATION_PENDING
= 102
250 class OpenmanoNsr(object):
252 INSTANCE_TERMINATE_TIMEOUT
= 60
254 def __init__(self
, project
, dts
, log
, loop
, publisher
, cli_api
, http_api
, nsd_msg
,
255 nsr_config_msg
, key_pairs
, ssh_key
, rift_vnfd_id
=None ):
256 self
._project
= project
260 self
._publisher
= publisher
261 self
._cli
_api
= cli_api
262 self
._http
_api
= http_api
264 self
._nsd
_msg
= nsd_msg
265 self
._nsr
_config
_msg
= nsr_config_msg
269 self
._vdur
_console
_handler
= {}
270 self
._key
_pairs
= key_pairs
271 self
._ssh
_key
= ssh_key
273 self
._nsd
_uuid
= None
274 self
._nsr
_uuid
= None
275 self
._nsd
_msg
= nsd_msg
279 self
._created
= False
281 self
._monitor
_task
= None
282 self
._rift
_vnfd
_id
= rift_vnfd_id
283 self
._state
= OpenmanoNSRecordState
.INIT
286 self
._active
_nets
= 0
290 return rift2openmano
.RiftNSD(self
._nsd
_msg
)
293 def rift_vnfd_id(self
):
294 return self
._rift
_vnfd
_id
301 def nsr_config_msg(self
):
302 return self
._nsr
_config
_msg
307 return {v
.rift_vnfd_id
: v
.vnfd
for v
in self
._vnfrs
}
311 return {v
.rift_vnfd_id
: v
.openmano_vnfd_id
for v
in self
._vnfrs
}
319 return self
._key
_pairs
331 return self
._http
_api
334 def openmano_nsd_yaml(self
):
335 self
._log
.debug("Converting nsd %s from rift to openmano", self
.nsd
.id)
336 openmano_nsd
= rift2openmano
.rift2openmano_nsd(self
.nsd
, self
.vnfds
,self
.vnfr_ids
, self
.http_api
)
337 return yaml
.safe_dump(openmano_nsd
, default_flow_style
=False)
340 def openmano_scaling_yaml(self
):
341 self
._log
.debug("Creating Openmano Scaling Descriptor %s")
343 openmano_vnfd_nsd
= rift2openmano
.rift2openmano_vnfd_nsd(self
.nsd
, self
.vnfds
, self
.vnfr_ids
, self
.http_api
, self
._rift
_vnfd
_id
)
344 return yaml
.safe_dump(openmano_vnfd_nsd
, default_flow_style
=False)
345 except Exception as e
:
346 self
._log
.exception("Scaling Descriptor Exception: %s", str(e
))
348 def get_ssh_key_pairs(self
):
351 for authorized_key
in self
._nsr
_config
_msg
.ssh_authorized_key
:
352 self
._log
.debug("Key pair ref present is %s",authorized_key
.key_pair_ref
)
353 if authorized_key
.key_pair_ref
in self
._key
_pairs
:
354 key_pairs
.append(self
._key
_pairs
[authorized_key
.key_pair_ref
].key
)
356 for authorized_key
in self
._nsd
_msg
.key_pair
:
357 self
._log
.debug("Key pair NSD is %s",authorized_key
)
358 key_pairs
.append(authorized_key
.key
)
360 if self
._ssh
_key
['public_key']:
361 self
._log
.debug("Pub key NSD is %s", self
._ssh
_key
['public_key'])
362 key_pairs
.append(self
._ssh
_key
['public_key'])
365 cloud_config
["key-pairs"] = key_pairs
368 for user_entry
in self
._nsr
_config
_msg
.user
:
369 self
._log
.debug("User present is %s",user_entry
)
371 user
["name"] = user_entry
.name
372 user
["key-pairs"] = list()
373 for ssh_key
in user_entry
.ssh_authorized_key
:
374 if ssh_key
.key_pair_ref
in self
._key
_pairs
:
375 user
["key-pairs"].append(self
._key
_pairs
[ssh_key
.key_pair_ref
].key
)
378 for user_entry
in self
._nsd
_msg
.user
:
379 self
._log
.debug("User present in NSD is %s",user_entry
)
381 user
["name"] = user_entry
.name
382 user
["key-pairs"] = list()
383 for ssh_key
in user_entry
.key_pair
:
384 user
["key-pairs"].append(ssh_key
.key
)
388 cloud_config
["users"] = users
390 self
._log
.debug("Cloud config formed is %s",cloud_config
)
395 def openmano_instance_create_yaml(self
):
397 self
._log
.debug("Creating instance-scenario-create input file for nsd %s with name %s", self
.nsd
.id, self
._nsr
_config
_msg
.name
)
398 openmano_instance_create
= {}
399 openmano_instance_create
["name"] = self
._nsr
_config
_msg
.name
400 openmano_instance_create
["description"] = self
._nsr
_config
_msg
.description
401 openmano_instance_create
["scenario"] = self
._nsd
_uuid
403 cloud_config
= self
.get_ssh_key_pairs()
405 openmano_instance_create
["cloud-config"] = cloud_config
406 if self
._nsr
_config
_msg
.has_field("datacenter"):
407 openmano_instance_create
["datacenter"] = self
._nsr
_config
_msg
.datacenter
408 openmano_instance_create
["vnfs"] = {}
409 for vnfr
in self
._vnfrs
:
410 if "datacenter" in vnfr
.vnfr
.vnfr_msg
:
411 vnfr_name
= vnfr
.vnfr
.vnfd
.name
+ "." + str(vnfr
.vnfr
.vnfr_msg
.member_vnf_index_ref
)
412 openmano_instance_create
["vnfs"][vnfr_name
] = {"datacenter": vnfr
.vnfr
.vnfr_msg
.datacenter
}
413 openmano_instance_create
["networks"] = {}
414 for vld_msg
in self
._nsd
_msg
.vld
:
415 openmano_instance_create
["networks"][vld_msg
.name
] = {}
416 openmano_instance_create
["networks"][vld_msg
.name
]["sites"] = list()
417 for vlr
in self
._vlrs
:
418 if vlr
.vld_msg
.name
== vld_msg
.name
:
419 self
._log
.debug("Received VLR name %s, VLR DC: %s for VLD: %s",vlr
.vld_msg
.name
,
420 vlr
.datacenter_name
,vld_msg
.name
)
421 #network["vim-network-name"] = vld_msg.name
424 if vld_msg
.vim_network_name
:
425 network
["netmap-use"] = vld_msg
.vim_network_name
426 elif vlr
._ip
_profile
and vlr
._ip
_profile
.has_field("ip_profile_params"):
427 ip_profile_params
= vlr
._ip
_profile
.ip_profile_params
428 if ip_profile_params
.ip_version
== "ipv6":
429 ip_profile
['ip-version'] = "IPv6"
431 ip_profile
['ip-version'] = "IPv4"
432 if ip_profile_params
.has_field('subnet_address'):
433 ip_profile
['subnet-address'] = ip_profile_params
.subnet_address
434 if ip_profile_params
.has_field('gateway_address'):
435 ip_profile
['gateway-address'] = ip_profile_params
.gateway_address
436 if ip_profile_params
.has_field('dns_server') and len(ip_profile_params
.dns_server
) > 0:
437 ip_profile
['dns-address'] = ip_profile_params
.dns_server
[0].address
438 if ip_profile_params
.has_field('dhcp_params'):
439 ip_profile
['dhcp'] = {}
440 ip_profile
['dhcp']['enabled'] = ip_profile_params
.dhcp_params
.enabled
441 ip_profile
['dhcp']['start-address'] = ip_profile_params
.dhcp_params
.start_address
442 ip_profile
['dhcp']['count'] = ip_profile_params
.dhcp_params
.count
443 if ip_profile
['dhcp']['enabled'] is True and ip_profile
['dhcp']['start-address'] is None:
444 addr_pool
= list(ipaddress
.ip_network(ip_profile
['subnet-address']).hosts())
445 gateway_ip_addr
= ip_profile
.get('gateway-address', None)
446 if gateway_ip_addr
is None:
447 gateway_ip_addr
= str(next(iter(addr_pool
)))
448 ip_profile
['gateway-address'] = gateway_ip_addr
450 self
._log
.debug("Gateway Address {}".format(gateway_ip_addr
))
452 if ipaddress
.ip_address(gateway_ip_addr
) in addr_pool
:
453 addr_pool
.remove(ipaddress
.ip_address(gateway_ip_addr
))
454 if len(addr_pool
) > 0:
455 ip_profile
['dhcp']['start-address'] = str(next(iter(addr_pool
)))
456 #DHCP count more than 200 is not instantiating any instances using OPENMANO RO
457 #So restricting it to a feasible count of 100.
458 dhcp_count
= ip_profile
['dhcp']['count']
459 if dhcp_count
is None or dhcp_count
== 0 or dhcp_count
> len(addr_pool
):
460 ip_profile
['dhcp']['count'] = min(len(addr_pool
), 100)
461 self
._log
.debug("DHCP start Address {} DHCP count {}".
462 format(ip_profile
['dhcp']['start-address'], ip_profile
['dhcp']['count']))
464 network
["netmap-create"] = vlr
.name
465 if vlr
.datacenter_name
:
466 network
["datacenter"] = vlr
.datacenter_name
467 elif vld_msg
.has_field("datacenter"):
468 network
["datacenter"] = vld_msg
.datacenter
469 elif "datacenter" in openmano_instance_create
:
470 network
["datacenter"] = openmano_instance_create
["datacenter"]
472 openmano_instance_create
["networks"][vld_msg
.name
]["sites"].append(network
)
474 openmano_instance_create
["networks"][vld_msg
.name
]['ip-profile'] = ip_profile
475 except Exception as e
:
476 self
._log
.error("Error while creating openmano_instance_yaml : {}". format(str(e
)))
478 return yaml
.safe_dump(openmano_instance_create
, default_flow_style
=False,width
=1000)
481 def scaling_instance_create_yaml(self
, scaleout
=False):
483 self
._log
.debug("Creating instance-scenario-create input file for nsd %s with name %s", self
.nsd
.id, self
._nsr
_config
_msg
.name
+"scal1")
484 scaling_instance_create
= {}
485 for group_list
in self
._nsd
_msg
.scaling_group_descriptor
:
486 scaling_instance_create
["name"] = self
._nsr
_config
_msg
.name
+ "__"+group_list
.name
488 scaling_instance_create
["scenario"] = self
._nsd
_uuid
+ "__" +group_list
.name
490 scaling_instance_create
["scenario"] = self
._nsd
_uuid
491 scaling_instance_create
["description"] = self
._nsr
_config
_msg
.description
493 if self
._nsr
_config
_msg
.has_field("datacenter"):
494 scaling_instance_create
["datacenter"] = self
._nsr
_config
_msg
.datacenter
495 scaling_instance_create
["vnfs"] = {}
496 for vnfr
in self
._vnfrs
:
497 if "datacenter" in vnfr
.vnfr
.vnfr_msg
:
498 vnfr_name
= vnfr
.vnfr
.vnfd
.name
+ "." + str(vnfr
.vnfr
.vnfr_msg
.member_vnf_index_ref
)
499 scaling_instance_create
["vnfs"][vnfr_name
] = {"datacenter": self
._nsr
_config
_msg
.datacenter
}
500 scaling_instance_create
["networks"] = {}
501 for vld_msg
in self
._nsd
_msg
.vld
:
502 scaling_instance_create
["networks"][vld_msg
.name
] = {}
503 scaling_instance_create
["networks"][vld_msg
.name
]["sites"] = list()
504 for vlr
in self
._vlrs
:
505 if vlr
.vld_msg
.name
== vld_msg
.name
:
506 self
._log
.debug("Received VLR name %s, VLR DC: %s for VLD: %s",vlr
.vld_msg
.name
,
507 vlr
.datacenter_name
,vld_msg
.name
)
508 #network["vim-network-name"] = vld_msg.name
511 if vld_msg
.vim_network_name
:
512 network
["netmap-use"] = vld_msg
.vim_network_name
514 # network["netmap-create"] = vlr.name
515 if vlr
.datacenter_name
:
516 network
["datacenter"] = vlr
.datacenter_name
517 elif vld_msg
.has_field("datacenter"):
518 network
["datacenter"] = vld_msg
.datacenter
519 elif "datacenter" in scaling_instance_create
:
520 network
["datacenter"] = scaling_instance_create
["datacenter"]
522 scaling_instance_create
["networks"][vld_msg
.name
]["sites"].append(network
)
523 except Exception as e
:
524 self
._log
.error("Error while creating scaling_instance_yaml : {}". format(str(e
)))
526 return yaml
.safe_dump(scaling_instance_create
, default_flow_style
=False, width
=1000)
529 def add_vlr(self
, vlr
):
530 self
._vlrs
.append(vlr
)
531 yield from self
._publisher
.publish_vlr(None, vlr
.vlr_msg
)
532 yield from asyncio
.sleep(1, loop
=self
._loop
)
535 def remove_vlr(self
, vlr
):
536 if vlr
in self
._vlrs
:
537 self
._vlrs
.remove(vlr
)
538 yield from self
._publisher
.unpublish_vlr(None, vlr
.vlr_msg
)
539 yield from asyncio
.sleep(1, loop
=self
._loop
)
542 def remove_vnf(self
,vnf
):
543 self
._log
.debug("Unpublishing VNFR - {}".format(vnf
))
546 for vnfr
in self
._vnfrs
:
547 # Find the vnfr by id
548 if vnfr
.vnfr
.id == vnf
.id:
549 self
._log
.debug("Found vnfr for delete !")
554 self
._log
.debug("Removing VNFR : {}".format(delete_vnfr
.vnfr
.id))
555 self
._vnfrs
.remove(vnfr
)
556 yield from self
._publisher
.unpublish_vnfr(None, delete_vnfr
.vnfr
.vnfr_msg
)
557 self
._log
.debug("Removed VNFR : {}".format(delete_vnfr
.vnfr
.id))
559 yield from asyncio
.sleep(1, loop
=self
._loop
)
562 def delete_vlr(self
, vlr
):
563 if vlr
in self
._vlrs
:
564 self
._vlrs
.remove(vlr
)
565 if not vlr
.vld_msg
.vim_network_name
:
566 yield from self
._loop
.run_in_executor(
568 self
._cli
_api
.ns_vim_network_delete
,
571 yield from self
._publisher
.unpublish_vlr(None, vlr
.vlr_msg
)
572 yield from asyncio
.sleep(1, loop
=self
._loop
)
575 def add_vnfr(self
, vnfr
):
576 vnfr
= OpenmanoVnfr(self
._project
, self
._log
, self
._loop
, self
._cli
_api
, self
.http_api
,
577 vnfr
, nsd
=self
.nsd
, ssh_key
=self
._ssh
_key
)
578 yield from vnfr
.create()
579 self
._vnfrs
.append(vnfr
)
582 def add_nsr(self
, nsr
, vnfr
):
583 self
._nsrs
[vnfr
.id] = nsr
586 if not self
._created
:
587 self
._log
.debug("NSD wasn't created. Skipping delete.")
590 self
._log
.debug("Deleting openmano nsr")
591 # Here we need to check for existing instances using this scenario.
592 # This would exist when we use Scaling Descriptors.
593 # Deleting a scenario before deleting instances results in a orphaned state
594 # TODO: The RO should implement the check done here.
596 self
._log
.debug("Fetching Instance Scenario List before Deleting Scenario")
598 instances
= self
.http_api
.instances()
600 scenarios_instances
= False
602 self
._log
.debug("Fetched Instances List. Checking if scenario is used")
603 for instance
in instances
:
604 if instance
["scenario_id"] == self
._nsd
_uuid
:
605 scenarios_instances
= True
608 self
._log
.debug("Scenario Instances Dependency Exists : %s", scenarios_instances
)
610 if not scenarios_instances
:
612 self
._cli
_api
.ns_delete(self
._nsd
_uuid
)
613 except Exception as e
:
616 self
._log
.debug("Deleting openmano vnfrs(non scaling vnfs)")
617 deleted_vnf_id_list
= []
618 for vnfr
in self
._vnfrs
:
619 if vnfr
.vnfr
.vnfd
.id not in deleted_vnf_id_list
:
621 deleted_vnf_id_list
.append(vnfr
.vnfr
.vnfd
.id)
626 self
._log
.debug("Created openmano scenario")
627 # The self.openmano_nsd_yaml internally creates the scenario if not found.
628 # Assigning the yaml to a variable so that the api is not fired unnecessarily.
629 nsd_yaml
= self
.openmano_nsd_yaml
631 self
._nsd
_uuid
= yaml
.load(nsd_yaml
)['uuid']
632 fpath
= dump_openmano_descriptor(
633 "{}_nsd".format(self
._nsd
_msg
.name
),
637 self
._log
.debug("Dumped Openmano NS descriptor to: %s", fpath
)
640 except Exception as e
:
641 self
._log
.error("Failed to create scenario on Openmano RO : %s", e
)
645 def scaling_scenario_create(self
):
646 self
._log
.debug("Creating scaling openmano scenario")
648 # The self.openmano_nsd_yaml internally creates the scenario if not found.
649 # Assigning the yaml to a variable so that the api is not fired unnecessarily.
650 nsd_yaml
= self
.openmano_scaling_yaml
652 self
._nsd
_uuid
= yaml
.load(nsd_yaml
)['uuid']
654 fpath
= dump_openmano_descriptor(
655 "{}_sgd".format(self
._nsd
_msg
.name
),
656 self
.scaling_instance_create_yaml
,
662 def get_nsr_opdata(self
):
663 """ NSR opdata associated with this VNFR """
664 xpath
= self
._project
.add_project(
665 "D,/nsr:ns-instance-opdata/nsr:nsr" \
666 "[nsr:ns-instance-config-ref={}]". \
667 format(quoted_key(self
.nsr_config_msg
.id)))
669 results
= yield from self
._dts
.query_read(xpath
, rwdts
.XactFlag
.MERGE
)
671 for result
in results
:
672 entry
= yield from result
673 nsr_op
= entry
.result
680 def instance_monitor_task(self
):
681 self
._log
.debug("Starting Instance monitoring task")
683 start_time
= time
.time()
685 nsr
= yield from self
.get_nsr_opdata()
690 yield from asyncio
.sleep(1, loop
=self
._loop
)
693 instance_resp_json
= yield from self
._loop
.run_in_executor(
695 self
._http
_api
.get_instance
,
699 self
._log
.debug("Got instance response: %s for NSR ID %s",
703 for vnf
in instance_resp_json
['vnfs']:
704 for vm
in vnf
['vms']:
705 if vm
['status'] == 'ACTIVE':
707 for net
in instance_resp_json
['nets']:
708 if net
['status'] == 'ACTIVE':
711 nsr
.orchestration_progress
.vms
.active
= active_vms
712 nsr
.orchestration_progress
.networks
.active
= active_nets
714 # This is for accesibility of the status from nsm when the control goes back.
715 self
._active
_vms
= active_vms
716 self
._active
_nets
= active_nets
718 yield from self
._publisher
.publish_nsr_opdata(None, nsr
)
720 except openmano_client
.InstanceStatusError
as e
:
721 self
._log
.error("Could not get NS instance status: %s", str(e
))
725 def all_vms_active(vnf
):
726 for vm
in vnf
["vms"]:
727 vm_status
= vm
["status"]
729 if vm_status
!= "ACTIVE":
730 self
._log
.debug("VM is not yet active: %s (status: %s)", vm_uuid
, vm_status
)
735 def any_vm_active_nomgmtip(vnf
):
736 for vm
in vnf
["vms"]:
737 vm_status
= vm
["status"]
739 if vm_status
!= "ACTIVE":
740 self
._log
.debug("VM is not yet active: %s (status: %s)", vm_uuid
, vm_status
)
745 def any_vms_error(vnf
):
746 for vm
in vnf
["vms"]:
747 vm_status
= vm
["status"]
748 vm_vim_info
= vm
["vim_info"]
750 if "ERROR" in vm_status
:
751 self
._log
.error("VM Error: %s (vim_info: %s)", vm_uuid
, vm_vim_info
)
752 return True, vm
['error_msg']
756 def get_vnf_ip_address(vnf
):
757 if "ip_address" in vnf
:
758 return vnf
["ip_address"].strip()
761 cp_info_list
= get_ext_cp_info(vnf
)
763 for cp_name
, ip
, mac
in cp_info_list
:
764 for vld
in self
.nsd
.vlds
:
765 if not vld
.mgmt_network
:
768 for vld_cp
in vld
.vnfd_connection_point_ref
:
769 if vld_cp
.vnfd_connection_point_ref
== cp_name
:
773 def get_vnf_mac_address(vnf
):
774 if "mac_address" in vnf
:
775 return vnf
["mac_address"].strip()
778 def get_ext_cp_info(vnf
):
780 for vm
in vnf
["vms"]:
781 if "interfaces" not in vm
:
784 for intf
in vm
["interfaces"]:
785 if "external_name" not in intf
:
788 if not intf
["external_name"]:
791 ip_address
= intf
["ip_address"]
792 if ip_address
is None:
793 ip_address
= "0.0.0.0"
795 mac_address
= intf
["mac_address"]
796 if mac_address
is None:
797 mac_address
="00:00:00:00:00:00"
799 cp_info_list
.append((intf
["external_name"], ip_address
, mac_address
))
803 def get_vnf_status(vnfr
):
804 # When we create an openmano descriptor we use <name>.<idx>
805 # to come up with openmano constituent VNF name. Use this
806 # knowledge to map the vnfr back.
807 openmano_vnfr_suffix
= ".{}".format(
808 vnfr
.vnfr
.vnfr_msg
.member_vnf_index_ref
811 for vnf
in instance_resp_json
["vnfs"]:
812 if vnf
["vnf_name"].endswith(openmano_vnfr_suffix
):
815 self
._log
.warning("Could not find vnf status with name that ends with: %s",
816 openmano_vnfr_suffix
)
819 for vnfr
in self
._vnfrs
:
820 if vnfr
in active_vnfs
:
821 # Skipping, so we don't re-publish the same VNF message.
824 vnfr_msg
= vnfr
.vnfr
.vnfr_msg
.deep_copy()
825 vnfr_msg
.operational_status
= "init"
828 vnf_status
= get_vnf_status(vnfr
)
829 self
._log
.debug("Found VNF status: %s", vnf_status
)
830 if vnf_status
is None:
831 self
._log
.error("Could not find VNF status from openmano")
832 self
._state
= OpenmanoNSRecordState
.FAILED
833 vnfr_msg
.operational_status
= "failed"
834 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
837 # If there was a VNF that has a errored VM, then just fail the VNF and stop monitoring.
838 vm_error
, vm_err_msg
= any_vms_error(vnf_status
)
840 self
._log
.error("VM was found to be in error state. Marking as failed.")
841 self
._state
= OpenmanoNSRecordState
.FAILED
842 vnfr_msg
.operational_status
= "failed"
843 vnfr_msg
.operational_status_details
= vm_err_msg
844 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
847 if (time
.time() - start_time
) > OpenmanoNsr
.TIMEOUT_SECS
:
848 self
._log
.error("NSR timed out before reaching running state")
849 self
._state
= OpenmanoNSRecordState
.FAILED
850 vnfr_msg
.operational_status
= "failed"
851 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
854 if all_vms_active(vnf_status
):
855 vnf_ip_address
= get_vnf_ip_address(vnf_status
)
856 vnf_mac_address
= get_vnf_mac_address(vnf_status
)
858 if vnf_ip_address
is None:
859 self
._log
.error("No IP address obtained "
860 "for VNF: {}, will retry.".format(
861 vnf_status
['vnf_name']))
864 self
._log
.debug("All VMs in VNF are active. Marking as running.")
865 vnfr_msg
.operational_status
= "running"
867 self
._log
.debug("Got VNF ip address: %s, mac-address: %s",
868 vnf_ip_address
, vnf_mac_address
)
869 vnfr_msg
.mgmt_interface
.ip_address
= vnf_ip_address
872 vnfr_msg
.mgmt_interface
.ssh_key
.public_key
= \
873 vnfr
._ssh
_key
['public_key']
874 vnfr_msg
.mgmt_interface
.ssh_key
.private_key_file
= \
875 vnfr
._ssh
_key
['private_key']
877 for vm
in vnf_status
["vms"]:
878 if vm
["uuid"] not in self
._vdur
_console
_handler
:
879 vdur_console_handler
= VnfrConsoleOperdataDtsHandler(self
._project
, self
._dts
, self
._log
, self
._loop
,
880 self
, vnfr_msg
.id,vm
["uuid"],vm
["name"])
881 yield from vdur_console_handler
.register()
882 self
._vdur
_console
_handler
[vm
["uuid"]] = vdur_console_handler
884 vdur_msg
= vnfr_msg
.vdur
.add()
885 vdur_msg
.vim_id
= vm
["vim_vm_id"]
886 vdur_msg
.id = vm
["uuid"]
888 # Add connection point information for the config manager
889 cp_info_list
= get_ext_cp_info(vnf_status
)
890 for (cp_name
, cp_ip
, cp_mac_addr
) in cp_info_list
:
891 cp
= vnfr_msg
.connection_point
.add()
893 cp
.short_name
= cp_name
894 cp
.ip_address
= cp_ip
895 cp
.mac_address
= cp_mac_addr
897 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
898 active_vnfs
.append(vnfr
)
900 except Exception as e
:
901 vnfr_msg
.operational_status
= "failed"
902 self
._state
= OpenmanoNSRecordState
.FAILED
903 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
904 self
._log
.exception("Caught exception publishing vnfr info: %s", str(e
))
907 if len(active_vnfs
) == len(self
._vnfrs
):
908 self
._state
= OpenmanoNSRecordState
.RUNNING
909 self
._log
.debug("All VNF's are active. Exiting NSR monitoring task")
913 def deploy(self
,nsr_msg
):
914 if self
._nsd
_uuid
is None:
915 raise ValueError("Cannot deploy an uncreated nsd")
917 self
._log
.debug("Deploying openmano instance scenario")
919 name_uuid_map
= yield from self
._loop
.run_in_executor(
921 self
._cli
_api
.ns_instance_list
,
924 if self
._nsr
_config
_msg
.name
in name_uuid_map
:
925 self
._log
.debug("Found existing instance with nsr name: %s", self
._nsr
_config
_msg
.name
)
926 self
._nsr
_uuid
= name_uuid_map
[self
._nsr
_config
_msg
.name
]
928 self
._nsr
_msg
= nsr_msg
929 fpath
= dump_openmano_descriptor(
930 "{}_instance_sce_create".format(self
._nsr
_config
_msg
.name
),
931 self
.openmano_instance_create_yaml
,
933 self
._log
.debug("Dumped Openmano instance Scenario Cretae to: %s", fpath
)
935 self
._nsr
_uuid
= yield from self
._loop
.run_in_executor(
937 self
._cli
_api
.ns_instance_scenario_create
,
938 self
.openmano_instance_create_yaml
)
940 self
._state
= OpenmanoNSRecordState
.INSTANTIATION_PENDING
942 self
._monitor
_task
= asyncio
.ensure_future(
943 self
.instance_monitor_task(), loop
=self
._loop
947 def deploy_scaling(self
, nsr_msg
, rift_vnfd_id
):
948 self
._log
.debug("Deploying Scaling instance scenario")
949 self
._nsr
_msg
= nsr_msg
950 self
._rift
_vnfd
_id
= rift_vnfd_id
951 fpath
= dump_openmano_descriptor(
952 "{}_scale_instance".format(self
._nsr
_config
_msg
.name
),
953 self
.scaling_instance_create_yaml
955 self
._nsr
_uuid
= yield from self
._loop
.run_in_executor(
957 self
._cli
_api
.ns_instance_scenario_create
,
958 self
.scaling_instance_create_yaml
)
960 self
._state
= OpenmanoNSRecordState
.INSTANTIATION_PENDING
962 self
._monitor
_task
= asyncio
.ensure_future(
963 self
.instance_monitor_task(), loop
=self
._loop
966 self
._state
= OpenmanoNSRecordState
.INIT
970 if self
._nsr
_uuid
is None:
971 start_time
= time
.time()
972 while ((time
.time() - start_time
) < OpenmanoNsr
.INSTANCE_TERMINATE_TIMEOUT
) and (self
._nsr
_uuid
is None):
974 self
._log
.warning("Waiting for nsr to get instatiated")
975 if self
._nsr
_uuid
is None:
976 self
._log
.warning("Cannot terminate an un-instantiated nsr")
979 if self
._monitor
_task
is not None:
980 self
._monitor
_task
.cancel()
981 self
._monitor
_task
= None
983 self
._log
.debug("Terminating openmano nsr")
984 self
._cli
_api
.ns_terminate(self
._nsr
_uuid
)
987 def create_vlr(self
,vlr
):
988 self
._log
.error("Creating openmano vim network VLR name %s, VLR DC: %s",vlr
.vld_msg
.name
,
992 net
['name'] = vlr
.name
994 net
['type'] = 'bridge'
995 self
._log
.error("Received ip profile is %s",vlr
._ip
_profile
)
996 if vlr
._ip
_profile
and vlr
._ip
_profile
.has_field("ip_profile_params"):
997 ip_profile_params
= vlr
._ip
_profile
.ip_profile_params
999 if ip_profile_params
.ip_version
== "ipv6":
1000 ip_profile
['ip_version'] = "IPv6"
1002 ip_profile
['ip_version'] = "IPv4"
1003 if ip_profile_params
.has_field('subnet_address'):
1004 ip_profile
['subnet_address'] = ip_profile_params
.subnet_address
1005 if ip_profile_params
.has_field('gateway_address'):
1006 ip_profile
['gateway_address'] = ip_profile_params
.gateway_address
1007 if ip_profile_params
.has_field('dns_server') and len(ip_profile_params
.dns_server
) > 0:
1008 ip_profile
['dns_address'] = ip_profile_params
.dns_server
[0].address
1009 if ip_profile_params
.has_field('dhcp_params'):
1010 ip_profile
['dhcp_enabled'] = ip_profile_params
.dhcp_params
.enabled
1011 ip_profile
['dhcp_start_address'] = ip_profile_params
.dhcp_params
.start_address
1012 ip_profile
['dhcp_count'] = ip_profile_params
.dhcp_params
.count
1013 net
['ip_profile'] = ip_profile
1014 net_create
["network"]= net
1016 net_create_msg
= yaml
.safe_dump(net_create
,default_flow_style
=False)
1017 fpath
= dump_openmano_descriptor(
1018 "{}_vim_net_create_{}".format(self
._nsr
_config
_msg
.name
,vlr
.name
),
1020 self
._log
.error("Dumped Openmano VIM Net create to: %s", fpath
)
1022 vim_network_uuid
= yield from self
._loop
.run_in_executor(
1024 self
._cli
_api
.ns_vim_network_create
,
1026 vlr
.datacenter_name
)
1027 self
._vlrs
.append(vlr
)
1031 class OpenmanoNsPlugin(nsmpluginbase
.NsmPluginBase
):
1033 RW Implentation of the NsmPluginBase
1035 def __init__(self
, dts
, log
, loop
, publisher
, ro_account
, project
):
1039 self
._publisher
= publisher
1040 self
._project
= project
1042 self
._cli
_api
= None
1043 self
._http
_api
= None
1044 self
._openmano
_nsrs
= {}
1045 self
._openmano
_nsr
_by
_vnfr
_id
= {}
1046 #self._nsr_uuid = None
1048 self
._set
_ro
_account
(ro_account
)
1050 def _set_ro_account(self
, ro_account
):
1051 self
._log
.debug("Setting openmano plugin cloud account: %s", ro_account
)
1052 self
._cli
_api
= openmano_client
.OpenmanoCliAPI(
1054 ro_account
.openmano
.host
,
1055 ro_account
.openmano
.port
,
1056 ro_account
.openmano
.tenant_id
,
1059 self
._http
_api
= openmano_client
.OpenmanoHttpAPI(
1061 ro_account
.openmano
.host
,
1062 ro_account
.openmano
.port
,
1063 ro_account
.openmano
.tenant_id
,
1066 def set_state(self
, nsr_id
, state
):
1067 # Currently we update only during terminate to
1068 # decide how to handle VL terminate
1069 if state
.value
== OpenmanoNSRecordState
.TERMINATE
.value
:
1070 self
._openmano
_nsrs
[nsr_id
]._state
= \
1071 [member
.value
for name
, member
in \
1072 OpenmanoNSRecordState
.__members
__.items() \
1073 if member
.value
== state
.value
]
1075 def create_nsr(self
, nsr_config_msg
, nsd_msg
, key_pairs
=None, ssh_key
=None):
1077 Create Network service record
1079 openmano_nsr
= OpenmanoNsr(
1092 self
.log
.debug("NSR created in openmano nsm %s", openmano_nsr
)
1093 self
._openmano
_nsrs
[nsr_config_msg
.id] = openmano_nsr
1096 def deploy(self
, nsr_msg
):
1097 self
._log
.debug("Received NSR Deploy msg : %s", nsr_msg
)
1098 openmano_nsr
= self
._openmano
_nsrs
[nsr_msg
.ns_instance_config_ref
]
1099 yield from openmano_nsr
.create()
1100 yield from openmano_nsr
.deploy(nsr_msg
)
1103 def instantiate_ns(self
, nsr
, xact
):
1105 Instantiate NSR with the passed nsr id
1107 yield from nsr
.instantiate(xact
)
1110 def instantiate_vnf(self
, nsr
, vnfr
, scaleout
=False):
1112 Instantiate NSR with the passed nsr id
1114 openmano_nsr
= self
._openmano
_nsrs
[nsr
.id]
1116 self
._log
.debug("Scaleout set as True. Creating Scaling VNFR : {}".format(vnfr
))
1117 openmano_vnf_nsr
= OpenmanoNsr(
1125 openmano_nsr
.nsd_msg
,
1126 openmano_nsr
.nsr_config_msg
,
1127 openmano_nsr
.key_pairs
,
1129 rift_vnfd_id
=vnfr
.vnfd
.id,
1131 self
._openmano
_nsr
_by
_vnfr
_id
[nsr
.id] = openmano_nsr
1132 if vnfr
.id in self
._openmano
_nsr
_by
_vnfr
_id
:
1133 raise VNFExistError("VNF %s already exist", vnfr
.id)
1134 self
._openmano
_nsr
_by
_vnfr
_id
[vnfr
.id] = openmano_vnf_nsr
1135 self
._log
.debug("VNFRID %s %s %s", type(self
._openmano
_nsr
_by
_vnfr
_id
), type(openmano_vnf_nsr
), type(self
._openmano
_nsr
_by
_vnfr
_id
[vnfr
.id]))
1136 self
._log
.debug("Inserting VNFR - {}, in NSR - {}".format(vnfr
.id, self
._openmano
_nsr
_by
_vnfr
_id
))
1137 for vlr
in openmano_nsr
.vlrs
:
1138 yield from openmano_vnf_nsr
.add_vlr(vlr
)
1140 yield from openmano_nsr
.add_nsr(openmano_vnf_nsr
, vnfr
)
1141 except Exception as e
:
1142 self
.log
.exception(str(e
))
1144 yield from openmano_vnf_nsr
.add_vnfr(vnfr
)
1145 except Exception as e
:
1146 self
.log
.exception(str(e
))
1148 yield from openmano_vnf_nsr
.scaling_scenario_create()
1149 except Exception as e
:
1150 self
.log
.exception(str(e
))
1152 yield from openmano_vnf_nsr
.deploy_scaling(openmano_vnf_nsr
.nsr_msg
, vnfr
.id)
1153 except Exception as e
:
1154 self
.log
.exception(str(e
))
1156 self
._log
.debug("Creating constituent VNFR - {}; for NSR - {}".format(vnfr
, nsr
))
1157 yield from openmano_nsr
.add_vnfr(vnfr
)
1159 # Mark the VNFR as running
1160 # TODO: Create a task to monitor nsr/vnfr status
1161 vnfr_msg
= vnfr
.vnfr_msg
.deep_copy()
1162 vnfr_msg
.operational_status
= "init"
1164 self
._log
.debug("Attempting to publish openmano vnf: %s", vnfr_msg
)
1165 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
1167 def update_vnfr(self
, vnfr
):
1168 vnfr_msg
= vnfr
.vnfr_msg
.deep_copy()
1169 self
._log
.debug("Attempting to publish openmano vnf: %s", vnfr_msg
)
1170 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
1173 def instantiate_vl(self
, nsr
, vlr
):
1175 Instantiate NSR with the passed nsr id
1177 self
._log
.debug("Received instantiate VL for NSR {}; VLR {}".format(nsr
.id,vlr
))
1178 openmano_nsr
= self
._openmano
_nsrs
[nsr
.id]
1179 if openmano_nsr
._state
== OpenmanoNSRecordState
.RUNNING
:
1180 yield from openmano_nsr
.create_vlr(vlr
)
1181 yield from self
._publisher
.publish_vlr(None, vlr
.vlr_msg
)
1183 yield from openmano_nsr
.add_vlr(vlr
)
1186 def terminate_ns(self
, nsr
):
1188 Terminate the network service
1190 self
._log
.debug("Terminate Received for Openamno NSR - {}".format(nsr
))
1192 openmano_nsr
= self
._openmano
_nsrs
[nsr_id
]
1194 for _
,handler
in openmano_nsr
._vdur
_console
_handler
.items():
1195 handler
._regh
.deregister()
1197 yield from self
._loop
.run_in_executor(
1203 for vnfr
in openmano_nsr
.vnfrs
:
1204 self
._log
.debug("Unpublishing Constituent VNFR: %s", vnfr
.vnfr
.vnfr_msg
)
1205 yield from self
._publisher
.unpublish_vnfr(None, vnfr
.vnfr
.vnfr_msg
)
1207 del self
._openmano
_nsrs
[nsr_id
]
1209 def terminate(self
, openmano_nsr
):
1210 openmano_nsr
.terminate()
1211 openmano_nsr
.delete()
1214 def terminate_vnf(self
, nsr
, vnfr
, scalein
=False):
1216 Terminate the network service
1219 self
._log
.debug("Terminating Scaling VNFR - {}".format(vnfr
))
1220 openmano_vnf_nsr
= self
._openmano
_nsr
_by
_vnfr
_id
[vnfr
.id]
1221 openmano_vnf_nsr
.terminate()
1222 openmano_vnf_nsr
.delete()
1223 yield from openmano_vnf_nsr
.remove_vnf(vnfr
)
1226 def terminate_vl(self
, vlr
):
1228 Terminate the virtual link
1230 self
._log
.debug("Received terminate VL for VLR {}".format(vlr
))
1231 openmano_nsr
= self
._openmano
_nsrs
[vlr
._nsr
_id
]
1232 if openmano_nsr
._state
== OpenmanoNSRecordState
.RUNNING
:
1233 yield from openmano_nsr
.delete_vlr(vlr
)
1235 yield from openmano_nsr
.remove_vlr(vlr
)