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
, log
, loop
, cli_api
, http_api
, vnfr
, nsd
, ssh_key
=None):
167 self
._cli
_api
= cli_api
168 self
._http
_api
= http_api
170 self
._vnfd
_id
= vnfr
.vnfd
.id
174 self
._created
= False
177 self
._ssh
_key
= ssh_key
181 return rift2openmano
.RiftVNFD(self
._vnfr
.vnfd
)
188 def rift_vnfd_id(self
):
192 def openmano_vnfd_id(self
):
196 def openmano_vnfd(self
):
197 self
._log
.debug("Converting vnfd %s from rift to openmano", self
.vnfd
.id)
198 openmano_vnfd
= rift2openmano
.rift2openmano_vnfd(self
.vnfd
, self
.nsd
, self
._http
_api
)
202 def openmano_vnfd_yaml(self
):
203 return yaml
.safe_dump(self
.openmano_vnfd
, default_flow_style
=False)
208 self
._log
.debug("Created openmano vnfd")
209 # The self.openmano_vnfd_yaml internally creates the vnf if not found.
210 # Assigning the yaml to a variable so that the api is not fired unnecessarily.
211 openmano_vnfd
= self
.openmano_vnfd
212 name
= openmano_vnfd
["name"]
214 self
._vnf
_id
= openmano_vnfd
['uuid']
217 except Exception as e
:
218 self
._log
.error("Failed to create vnf on Openmano RO : %s", e
)
222 if not self
._created
:
225 self
._log
.debug("Deleting openmano vnfd")
226 if self
._vnf
_id
is None:
227 self
._log
.warning("Openmano vnf id not set. Cannot delete.")
230 self
._cli
_api
.vnf_delete(self
._vnf
_id
)
233 class OpenmanoNSRecordState(Enum
):
234 """ Network Service Record State """
235 # Make sure the values match with NetworkServiceRecordState
237 INSTANTIATION_PENDING
= 102
249 class OpenmanoNsr(object):
251 INSTANCE_TERMINATE_TIMEOUT
= 60
253 def __init__(self
, project
, dts
, log
, loop
, publisher
, cli_api
, http_api
, nsd_msg
,
254 nsr_config_msg
, key_pairs
, ssh_key
, rift_vnfd_id
=None ):
255 self
._project
= project
259 self
._publisher
= publisher
260 self
._cli
_api
= cli_api
261 self
._http
_api
= http_api
263 self
._nsd
_msg
= nsd_msg
264 self
._nsr
_config
_msg
= nsr_config_msg
268 self
._vdur
_console
_handler
= {}
269 self
._key
_pairs
= key_pairs
270 self
._ssh
_key
= ssh_key
272 self
._nsd
_uuid
= None
273 self
._nsr
_uuid
= None
274 self
._nsd
_msg
= nsd_msg
278 self
._created
= False
280 self
._monitor
_task
= None
281 self
._rift
_vnfd
_id
= rift_vnfd_id
282 self
._state
= OpenmanoNSRecordState
.INIT
285 self
._active
_nets
= 0
289 return rift2openmano
.RiftNSD(self
._nsd
_msg
)
292 def rift_vnfd_id(self
):
293 return self
._rift
_vnfd
_id
300 def nsr_config_msg(self
):
301 return self
._nsr
_config
_msg
306 return {v
.rift_vnfd_id
: v
.vnfd
for v
in self
._vnfrs
}
310 return {v
.rift_vnfd_id
: v
.openmano_vnfd_id
for v
in self
._vnfrs
}
318 return self
._key
_pairs
330 return self
._http
_api
333 def openmano_nsd_yaml(self
):
334 self
._log
.debug("Converting nsd %s from rift to openmano", self
.nsd
.id)
335 openmano_nsd
= rift2openmano
.rift2openmano_nsd(self
.nsd
, self
.vnfds
,self
.vnfr_ids
, self
.http_api
)
336 return yaml
.safe_dump(openmano_nsd
, default_flow_style
=False)
339 def openmano_scaling_yaml(self
):
340 self
._log
.debug("Creating Openmano Scaling Descriptor %s")
342 openmano_vnfd_nsd
= rift2openmano
.rift2openmano_vnfd_nsd(self
.nsd
, self
.vnfds
, self
.vnfr_ids
, self
.http_api
, self
._rift
_vnfd
_id
)
343 return yaml
.safe_dump(openmano_vnfd_nsd
, default_flow_style
=False)
344 except Exception as e
:
345 self
._log
.exception("Scaling Descriptor Exception: %s", str(e
))
347 def get_ssh_key_pairs(self
):
350 for authorized_key
in self
._nsr
_config
_msg
.ssh_authorized_key
:
351 self
._log
.debug("Key pair ref present is %s",authorized_key
.key_pair_ref
)
352 if authorized_key
.key_pair_ref
in self
._key
_pairs
:
353 key_pairs
.append(self
._key
_pairs
[authorized_key
.key_pair_ref
].key
)
355 for authorized_key
in self
._nsd
_msg
.key_pair
:
356 self
._log
.debug("Key pair NSD is %s",authorized_key
)
357 key_pairs
.append(authorized_key
.key
)
359 if self
._ssh
_key
['public_key']:
360 self
._log
.debug("Pub key NSD is %s", self
._ssh
_key
['public_key'])
361 key_pairs
.append(self
._ssh
_key
['public_key'])
364 cloud_config
["key-pairs"] = key_pairs
367 for user_entry
in self
._nsr
_config
_msg
.user
:
368 self
._log
.debug("User present is %s",user_entry
)
370 user
["name"] = user_entry
.name
371 user
["key-pairs"] = list()
372 for ssh_key
in user_entry
.ssh_authorized_key
:
373 if ssh_key
.key_pair_ref
in self
._key
_pairs
:
374 user
["key-pairs"].append(self
._key
_pairs
[ssh_key
.key_pair_ref
].key
)
377 for user_entry
in self
._nsd
_msg
.user
:
378 self
._log
.debug("User present in NSD is %s",user_entry
)
380 user
["name"] = user_entry
.name
381 user
["key-pairs"] = list()
382 for ssh_key
in user_entry
.key_pair
:
383 user
["key-pairs"].append(ssh_key
.key
)
387 cloud_config
["users"] = users
389 self
._log
.debug("Cloud config formed is %s",cloud_config
)
394 def openmano_instance_create_yaml(self
):
395 self
._log
.debug("Creating instance-scenario-create input file for nsd %s with name %s", self
.nsd
.id, self
._nsr
_config
_msg
.name
)
396 openmano_instance_create
= {}
397 openmano_instance_create
["name"] = self
._nsr
_config
_msg
.name
398 openmano_instance_create
["description"] = self
._nsr
_config
_msg
.description
399 openmano_instance_create
["scenario"] = self
._nsd
_uuid
401 cloud_config
= self
.get_ssh_key_pairs()
403 openmano_instance_create
["cloud-config"] = cloud_config
404 if self
._nsr
_config
_msg
.has_field("datacenter"):
405 openmano_instance_create
["datacenter"] = self
._nsr
_config
_msg
.datacenter
406 openmano_instance_create
["vnfs"] = {}
407 for vnfr
in self
._vnfrs
:
408 if "datacenter" in vnfr
.vnfr
.vnfr_msg
:
409 vnfr_name
= vnfr
.vnfr
.vnfd
.name
+ "." + str(vnfr
.vnfr
.vnfr_msg
.member_vnf_index_ref
)
410 openmano_instance_create
["vnfs"][vnfr_name
] = {"datacenter": vnfr
.vnfr
.vnfr_msg
.datacenter
}
411 openmano_instance_create
["networks"] = {}
412 for vld_msg
in self
._nsd
_msg
.vld
:
413 openmano_instance_create
["networks"][vld_msg
.name
] = {}
414 openmano_instance_create
["networks"][vld_msg
.name
]["sites"] = list()
415 for vlr
in self
._vlrs
:
416 if vlr
.vld_msg
.name
== vld_msg
.name
:
417 self
._log
.debug("Received VLR name %s, VLR DC: %s for VLD: %s",vlr
.vld_msg
.name
,
418 vlr
.datacenter_name
,vld_msg
.name
)
419 #network["vim-network-name"] = vld_msg.name
422 if vld_msg
.vim_network_name
:
423 network
["netmap-use"] = vld_msg
.vim_network_name
424 elif vlr
._ip
_profile
and vlr
._ip
_profile
.has_field("ip_profile_params"):
425 ip_profile_params
= vlr
._ip
_profile
.ip_profile_params
426 if ip_profile_params
.ip_version
== "ipv6":
427 ip_profile
['ip-version'] = "IPv6"
429 ip_profile
['ip-version'] = "IPv4"
430 if ip_profile_params
.has_field('subnet_address'):
431 ip_profile
['subnet-address'] = ip_profile_params
.subnet_address
432 if ip_profile_params
.has_field('gateway_address'):
433 ip_profile
['gateway-address'] = ip_profile_params
.gateway_address
434 if ip_profile_params
.has_field('dns_server') and len(ip_profile_params
.dns_server
) > 0:
435 ip_profile
['dns-address'] = ip_profile_params
.dns_server
[0].address
436 if ip_profile_params
.has_field('dhcp_params'):
437 ip_profile
['dhcp'] = {}
438 ip_profile
['dhcp']['enabled'] = ip_profile_params
.dhcp_params
.enabled
439 ip_profile
['dhcp']['start-address'] = ip_profile_params
.dhcp_params
.start_address
440 ip_profile
['dhcp']['count'] = ip_profile_params
.dhcp_params
.count
441 if ip_profile
['dhcp']['enabled'] is True and ip_profile
['dhcp']['start-address'] is None:
442 addr_pool
= list(ipaddress
.ip_network(ip_profile
['subnet-address']).hosts())
443 gateway_ip_addr
= ip_profile
.get('gateway-address', None)
444 if gateway_ip_addr
is None:
445 gateway_ip_addr
= str(next(iter(addr_pool
)))
446 ip_profile
['gateway-address'] = gateway_ip_addr
448 self
._log
.debug("Gateway Address {}".format(gateway_ip_addr
))
450 if ipaddress
.ip_address(gateway_ip_addr
) in addr_pool
:
451 addr_pool
.remove(ipaddress
.ip_address(gateway_ip_addr
))
452 if len(addr_pool
) > 0:
453 ip_profile
['dhcp']['start-address'] = str(next(iter(addr_pool
)))
454 #DHCP count more than 200 is not instantiating any instances using OPENMANO RO
455 #So restricting it to a feasible count of 100.
456 dhcp_count
= ip_profile
['dhcp']['count']
457 if dhcp_count
is None or dhcp_count
== 0 or dhcp_count
> len(addr_pool
):
458 ip_profile
['dhcp']['count'] = min(len(addr_pool
), 100)
459 self
._log
.debug("DHCP start Address {} DHCP count {}".
460 format(ip_profile
['dhcp']['start-address'], ip_profile
['dhcp']['count']))
462 network
["netmap-create"] = vlr
.name
463 if vlr
.datacenter_name
:
464 network
["datacenter"] = vlr
.datacenter_name
465 elif vld_msg
.has_field("datacenter"):
466 network
["datacenter"] = vld_msg
.datacenter
467 elif "datacenter" in openmano_instance_create
:
468 network
["datacenter"] = openmano_instance_create
["datacenter"]
470 openmano_instance_create
["networks"][vld_msg
.name
]["sites"].append(network
)
472 openmano_instance_create
["networks"][vld_msg
.name
]['ip-profile'] = ip_profile
474 return yaml
.safe_dump(openmano_instance_create
, default_flow_style
=False,width
=1000)
477 def scaling_instance_create_yaml(self
, scaleout
=False):
478 self
._log
.debug("Creating instance-scenario-create input file for nsd %s with name %s", self
.nsd
.id, self
._nsr
_config
_msg
.name
+"scal1")
479 scaling_instance_create
= {}
480 for group_list
in self
._nsd
_msg
.scaling_group_descriptor
:
481 scaling_instance_create
["name"] = self
._nsr
_config
_msg
.name
+ "__"+group_list
.name
483 scaling_instance_create
["scenario"] = self
._nsd
_uuid
+ "__" +group_list
.name
485 scaling_instance_create
["scenario"] = self
._nsd
_uuid
486 scaling_instance_create
["description"] = self
._nsr
_config
_msg
.description
489 if self
._nsr
_config
_msg
.has_field("datacenter"):
490 scaling_instance_create
["datacenter"] = self
._nsr
_config
_msg
.datacenter
491 scaling_instance_create
["vnfs"] = {}
492 for vnfr
in self
._vnfrs
:
493 if "datacenter" in vnfr
.vnfr
.vnfr_msg
:
494 vnfr_name
= vnfr
.vnfr
.vnfd
.name
+ "__" + str(vnfr
.vnfr
.vnfr_msg
.member_vnf_index_ref
)
495 scaling_instance_create
["vnfs"][vnfr_name
] = {"datacenter": vnfr
.vnfr
.vnfr_msg
.datacenter
}
496 scaling_instance_create
["networks"] = {}
497 for vld_msg
in self
._nsd
_msg
.vld
:
498 scaling_instance_create
["networks"][vld_msg
.name
] = {}
499 scaling_instance_create
["networks"][vld_msg
.name
]["sites"] = list()
500 for vlr
in self
._vlrs
:
501 if vlr
.vld_msg
.name
== vld_msg
.name
:
502 self
._log
.debug("Received VLR name %s, VLR DC: %s for VLD: %s",vlr
.vld_msg
.name
,
503 vlr
.datacenter_name
,vld_msg
.name
)
504 #network["vim-network-name"] = vld_msg.name
507 if vld_msg
.vim_network_name
:
508 network
["netmap-use"] = vld_msg
.vim_network_name
510 # network["netmap-create"] = vlr.name
511 if vlr
.datacenter_name
:
512 network
["datacenter"] = vlr
.datacenter_name
513 elif vld_msg
.has_field("datacenter"):
514 network
["datacenter"] = vld_msg
.datacenter
515 elif "datacenter" in scaling_instance_create
:
516 network
["datacenter"] = scaling_instance_create
["datacenter"]
518 scaling_instance_create
["networks"][vld_msg
.name
]["sites"].append(network
)
520 return yaml
.safe_dump(scaling_instance_create
, default_flow_style
=False, width
=1000)
523 def add_vlr(self
, vlr
):
524 self
._vlrs
.append(vlr
)
525 yield from self
._publisher
.publish_vlr(None, vlr
.vlr_msg
)
526 yield from asyncio
.sleep(1, loop
=self
._loop
)
529 def remove_vlr(self
, vlr
):
530 if vlr
in self
._vlrs
:
531 self
._vlrs
.remove(vlr
)
532 yield from self
._publisher
.unpublish_vlr(None, vlr
.vlr_msg
)
533 yield from asyncio
.sleep(1, loop
=self
._loop
)
536 def remove_vnf(self
,vnf
):
537 if vnf
in self
._vnfrs
:
538 self
._vnfrs
.remove(vnf
)
539 yield from self
._publisher
.unpublish_vnfr(
543 yield from asyncio
.sleep(1, loop
=self
._loop
)
546 def delete_vlr(self
, vlr
):
547 if vlr
in self
._vlrs
:
548 self
._vlrs
.remove(vlr
)
549 if not vlr
.vld_msg
.vim_network_name
:
550 yield from self
._loop
.run_in_executor(
552 self
._cli
_api
.ns_vim_network_delete
,
555 yield from self
._publisher
.unpublish_vlr(None, vlr
.vlr_msg
)
556 yield from asyncio
.sleep(1, loop
=self
._loop
)
559 def add_vnfr(self
, vnfr
):
560 vnfr
= OpenmanoVnfr(self
._log
, self
._loop
, self
._cli
_api
, self
.http_api
,
561 vnfr
, nsd
=self
.nsd
, ssh_key
=self
._ssh
_key
)
562 yield from vnfr
.create()
563 self
._vnfrs
.append(vnfr
)
566 def add_nsr(self
, nsr
, vnfr
):
567 self
._nsrs
[vnfr
.id] = nsr
570 if not self
._created
:
571 self
._log
.debug("NSD wasn't created. Skipping delete.")
574 self
._log
.debug("Deleting openmano nsr")
575 self
._cli
_api
.ns_delete(self
._nsd
_uuid
)
577 self
._log
.debug("Deleting openmano vnfrs")
578 deleted_vnf_id_list
= []
579 for vnfr
in self
._vnfrs
:
580 if vnfr
.vnfr
.vnfd
.id not in deleted_vnf_id_list
:
582 deleted_vnf_id_list
.append(vnfr
.vnfr
.vnfd
.id)
588 self
._log
.debug("Created openmano scenario")
589 # The self.openmano_nsd_yaml internally creates the scenario if not found.
590 # Assigning the yaml to a variable so that the api is not fired unnecessarily.
591 nsd_yaml
= self
.openmano_nsd_yaml
593 self
._nsd
_uuid
= yaml
.load(nsd_yaml
)['uuid']
594 fpath
= dump_openmano_descriptor(
595 "{}_nsd".format(self
._nsd
_msg
.name
),
599 self
._log
.debug("Dumped Openmano NS descriptor to: %s", fpath
)
602 except Exception as e
:
603 self
._log
.error("Failed to create scenario on Openmano RO : %s", e
)
607 def scaling_scenario_create(self
):
608 self
._log
.debug("Creating scaling openmano scenario")
610 # The self.openmano_nsd_yaml internally creates the scenario if not found.
611 # Assigning the yaml to a variable so that the api is not fired unnecessarily.
612 nsd_yaml
= self
.openmano_scaling_yaml
614 self
._nsd
_uuid
= yaml
.load(nsd_yaml
)['uuid']
616 fpath
= dump_openmano_descriptor(
617 "{}_sgd".format(self
._nsd
_msg
.name
),
618 self
.scaling_instance_create_yaml
,
623 def get_nsr_opdata(self
):
624 """ NSR opdata associated with this VNFR """
625 xpath
= self
._project
.add_project(
626 "D,/nsr:ns-instance-opdata/nsr:nsr" \
627 "[nsr:ns-instance-config-ref={}]". \
628 format(quoted_key(self
.nsr_config_msg
.id)))
630 results
= yield from self
._dts
.query_read(xpath
, rwdts
.XactFlag
.MERGE
)
632 for result
in results
:
633 entry
= yield from result
634 nsr_op
= entry
.result
641 def instance_monitor_task(self
):
642 self
._log
.debug("Starting Instance monitoring task")
644 start_time
= time
.time()
646 nsr
= yield from self
.get_nsr_opdata()
651 yield from asyncio
.sleep(1, loop
=self
._loop
)
654 instance_resp_json
= yield from self
._loop
.run_in_executor(
656 self
._http
_api
.get_instance
,
660 self
._log
.debug("Got instance response: %s for NSR ID %s",
664 for vnf
in instance_resp_json
['vnfs']:
665 for vm
in vnf
['vms']:
666 if vm
['status'] == 'ACTIVE':
668 for net
in instance_resp_json
['nets']:
669 if net
['status'] == 'ACTIVE':
672 nsr
.orchestration_progress
.vms
.active
= active_vms
673 nsr
.orchestration_progress
.networks
.active
= active_nets
675 # This is for accesibility of the status from nsm when the control goes back.
676 self
._active
_vms
= active_vms
677 self
._active
_nets
= active_nets
679 yield from self
._publisher
.publish_nsr_opdata(None, nsr
)
681 except openmano_client
.InstanceStatusError
as e
:
682 self
._log
.error("Could not get NS instance status: %s", str(e
))
686 def all_vms_active(vnf
):
687 for vm
in vnf
["vms"]:
688 vm_status
= vm
["status"]
690 if vm_status
!= "ACTIVE":
691 self
._log
.debug("VM is not yet active: %s (status: %s)", vm_uuid
, vm_status
)
696 def any_vm_active_nomgmtip(vnf
):
697 for vm
in vnf
["vms"]:
698 vm_status
= vm
["status"]
700 if vm_status
!= "ACTIVE":
701 self
._log
.debug("VM is not yet active: %s (status: %s)", vm_uuid
, vm_status
)
706 def any_vms_error(vnf
):
707 for vm
in vnf
["vms"]:
708 vm_status
= vm
["status"]
709 vm_vim_info
= vm
["vim_info"]
711 if vm_status
== "ERROR":
712 self
._log
.error("VM Error: %s (vim_info: %s)", vm_uuid
, vm_vim_info
)
717 def get_vnf_ip_address(vnf
):
718 if "ip_address" in vnf
:
719 return vnf
["ip_address"].strip()
722 cp_info_list
= get_ext_cp_info(vnf
)
724 for cp_name
, ip
, mac
in cp_info_list
:
725 for vld
in self
.nsd
.vlds
:
726 if not vld
.mgmt_network
:
729 for vld_cp
in vld
.vnfd_connection_point_ref
:
730 if vld_cp
.vnfd_connection_point_ref
== cp_name
:
734 def get_vnf_mac_address(vnf
):
735 if "mac_address" in vnf
:
736 return vnf
["mac_address"].strip()
739 def get_ext_cp_info(vnf
):
741 for vm
in vnf
["vms"]:
742 if "interfaces" not in vm
:
745 for intf
in vm
["interfaces"]:
746 if "external_name" not in intf
:
749 if not intf
["external_name"]:
752 ip_address
= intf
["ip_address"]
753 if ip_address
is None:
754 ip_address
= "0.0.0.0"
756 mac_address
= intf
["mac_address"]
757 if mac_address
is None:
758 mac_address
="00:00:00:00:00:00"
760 cp_info_list
.append((intf
["external_name"], ip_address
, mac_address
))
764 def get_vnf_status(vnfr
):
765 # When we create an openmano descriptor we use <name>.<idx>
766 # to come up with openmano constituent VNF name. Use this
767 # knowledge to map the vnfr back.
768 openmano_vnfr_suffix
= ".{}".format(
769 vnfr
.vnfr
.vnfr_msg
.member_vnf_index_ref
772 for vnf
in instance_resp_json
["vnfs"]:
773 if vnf
["vnf_name"].endswith(openmano_vnfr_suffix
):
776 self
._log
.warning("Could not find vnf status with name that ends with: %s",
777 openmano_vnfr_suffix
)
780 for vnfr
in self
._vnfrs
:
781 if vnfr
in active_vnfs
:
782 # Skipping, so we don't re-publish the same VNF message.
785 vnfr_msg
= vnfr
.vnfr
.vnfr_msg
.deep_copy()
786 vnfr_msg
.operational_status
= "init"
789 vnf_status
= get_vnf_status(vnfr
)
790 self
._log
.debug("Found VNF status: %s", vnf_status
)
791 if vnf_status
is None:
792 self
._log
.error("Could not find VNF status from openmano")
793 self
._state
= OpenmanoNSRecordState
.FAILED
794 vnfr_msg
.operational_status
= "failed"
795 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
798 # If there was a VNF that has a errored VM, then just fail the VNF and stop monitoring.
799 if any_vms_error(vnf_status
):
800 self
._log
.error("VM was found to be in error state. Marking as failed.")
801 self
._state
= OpenmanoNSRecordState
.FAILED
802 vnfr_msg
.operational_status
= "failed"
803 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
806 if (time
.time() - start_time
) > OpenmanoNsr
.TIMEOUT_SECS
:
807 self
._log
.error("NSR timed out before reaching running state")
808 self
._state
= OpenmanoNSRecordState
.FAILED
809 vnfr_msg
.operational_status
= "failed"
810 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
813 if all_vms_active(vnf_status
):
814 vnf_ip_address
= get_vnf_ip_address(vnf_status
)
815 vnf_mac_address
= get_vnf_mac_address(vnf_status
)
817 if vnf_ip_address
is None:
818 self
._log
.error("No IP address obtained "
819 "for VNF: {}, will retry.".format(
820 vnf_status
['vnf_name']))
823 self
._log
.debug("All VMs in VNF are active. Marking as running.")
824 vnfr_msg
.operational_status
= "running"
826 self
._log
.debug("Got VNF ip address: %s, mac-address: %s",
827 vnf_ip_address
, vnf_mac_address
)
828 vnfr_msg
.mgmt_interface
.ip_address
= vnf_ip_address
829 vnfr_msg
.mgmt_interface
.ssh_key
.public_key
= \
830 vnfr
._ssh
_key
['public_key']
831 vnfr_msg
.mgmt_interface
.ssh_key
.private_key_file
= \
832 vnfr
._ssh
_key
['private_key']
834 for vm
in vnf_status
["vms"]:
835 if vm
["uuid"] not in self
._vdur
_console
_handler
:
836 vdur_console_handler
= VnfrConsoleOperdataDtsHandler(self
._project
, self
._dts
, self
._log
, self
._loop
,
837 self
, vnfr_msg
.id,vm
["uuid"],vm
["name"])
838 yield from vdur_console_handler
.register()
839 self
._vdur
_console
_handler
[vm
["uuid"]] = vdur_console_handler
841 vdur_msg
= vnfr_msg
.vdur
.add()
842 vdur_msg
.vim_id
= vm
["vim_vm_id"]
843 vdur_msg
.id = vm
["uuid"]
845 # Add connection point information for the config manager
846 cp_info_list
= get_ext_cp_info(vnf_status
)
847 for (cp_name
, cp_ip
, cp_mac_addr
) in cp_info_list
:
848 cp
= vnfr_msg
.connection_point
.add()
850 cp
.short_name
= cp_name
851 cp
.ip_address
= cp_ip
852 cp
.mac_address
= cp_mac_addr
854 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
855 active_vnfs
.append(vnfr
)
857 except Exception as e
:
858 vnfr_msg
.operational_status
= "failed"
859 self
._state
= OpenmanoNSRecordState
.FAILED
860 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
861 self
._log
.exception("Caught exception publishing vnfr info: %s", str(e
))
864 if len(active_vnfs
) == len(self
._vnfrs
):
865 self
._state
= OpenmanoNSRecordState
.RUNNING
866 self
._log
.debug("All VNF's are active. Exiting NSR monitoring task")
870 def deploy(self
,nsr_msg
):
871 if self
._nsd
_uuid
is None:
872 raise ValueError("Cannot deploy an uncreated nsd")
874 self
._log
.debug("Deploying openmano instance scenario")
876 name_uuid_map
= yield from self
._loop
.run_in_executor(
878 self
._cli
_api
.ns_instance_list
,
881 if self
._nsr
_config
_msg
.name
in name_uuid_map
:
882 self
._log
.debug("Found existing instance with nsr name: %s", self
._nsr
_config
_msg
.name
)
883 self
._nsr
_uuid
= name_uuid_map
[self
._nsr
_config
_msg
.name
]
885 self
._nsr
_msg
= nsr_msg
886 fpath
= dump_openmano_descriptor(
887 "{}_instance_sce_create".format(self
._nsr
_config
_msg
.name
),
888 self
.openmano_instance_create_yaml
,
890 self
._log
.debug("Dumped Openmano instance Scenario Cretae to: %s", fpath
)
892 self
._nsr
_uuid
= yield from self
._loop
.run_in_executor(
894 self
._cli
_api
.ns_instance_scenario_create
,
895 self
.openmano_instance_create_yaml
)
897 self
._state
= OpenmanoNSRecordState
.INSTANTIATION_PENDING
899 self
._monitor
_task
= asyncio
.ensure_future(
900 self
.instance_monitor_task(), loop
=self
._loop
904 def deploy_scaling(self
, nsr_msg
, rift_vnfd_id
):
905 self
._log
.debug("Deploying Scaling instance scenario")
906 self
._nsr
_msg
= nsr_msg
907 self
._rift
_vnfd
_id
= rift_vnfd_id
908 fpath
= dump_openmano_descriptor(
909 "{}_scale_instance".format(self
._nsr
_config
_msg
.name
),
910 self
.scaling_instance_create_yaml
912 self
._nsr
_uuid
= yield from self
._loop
.run_in_executor(
914 self
._cli
_api
.ns_instance_scenario_create
,
915 self
.scaling_instance_create_yaml
)
917 self
._state
= OpenmanoNSRecordState
.INSTANTIATION_PENDING
919 self
._monitor
_task
= asyncio
.ensure_future(
920 self
.instance_monitor_task(), loop
=self
._loop
923 self
._state
= OpenmanoNSRecordState
.INIT
927 if self
._nsr
_uuid
is None:
928 start_time
= time
.time()
929 while ((time
.time() - start_time
) < OpenmanoNsr
.INSTANCE_TERMINATE_TIMEOUT
) and (self
._nsr
_uuid
is None):
931 self
._log
.warning("Waiting for nsr to get instatiated")
932 if self
._nsr
_uuid
is None:
933 self
._log
.warning("Cannot terminate an un-instantiated nsr")
936 if self
._monitor
_task
is not None:
937 self
._monitor
_task
.cancel()
938 self
._monitor
_task
= None
940 self
._log
.debug("Terminating openmano nsr")
941 self
._cli
_api
.ns_terminate(self
._nsr
_uuid
)
944 def create_vlr(self
,vlr
):
945 self
._log
.error("Creating openmano vim network VLR name %s, VLR DC: %s",vlr
.vld_msg
.name
,
949 net
['name'] = vlr
.name
951 net
['type'] = 'bridge'
952 self
._log
.error("Received ip profile is %s",vlr
._ip
_profile
)
953 if vlr
._ip
_profile
and vlr
._ip
_profile
.has_field("ip_profile_params"):
954 ip_profile_params
= vlr
._ip
_profile
.ip_profile_params
956 if ip_profile_params
.ip_version
== "ipv6":
957 ip_profile
['ip_version'] = "IPv6"
959 ip_profile
['ip_version'] = "IPv4"
960 if ip_profile_params
.has_field('subnet_address'):
961 ip_profile
['subnet_address'] = ip_profile_params
.subnet_address
962 if ip_profile_params
.has_field('gateway_address'):
963 ip_profile
['gateway_address'] = ip_profile_params
.gateway_address
964 if ip_profile_params
.has_field('dns_server') and len(ip_profile_params
.dns_server
) > 0:
965 ip_profile
['dns_address'] = ip_profile_params
.dns_server
[0].address
966 if ip_profile_params
.has_field('dhcp_params'):
967 ip_profile
['dhcp_enabled'] = ip_profile_params
.dhcp_params
.enabled
968 ip_profile
['dhcp_start_address'] = ip_profile_params
.dhcp_params
.start_address
969 ip_profile
['dhcp_count'] = ip_profile_params
.dhcp_params
.count
970 net
['ip_profile'] = ip_profile
971 net_create
["network"]= net
973 net_create_msg
= yaml
.safe_dump(net_create
,default_flow_style
=False)
974 fpath
= dump_openmano_descriptor(
975 "{}_vim_net_create_{}".format(self
._nsr
_config
_msg
.name
,vlr
.name
),
977 self
._log
.error("Dumped Openmano VIM Net create to: %s", fpath
)
979 vim_network_uuid
= yield from self
._loop
.run_in_executor(
981 self
._cli
_api
.ns_vim_network_create
,
984 self
._vlrs
.append(vlr
)
988 class OpenmanoNsPlugin(nsmpluginbase
.NsmPluginBase
):
990 RW Implentation of the NsmPluginBase
992 def __init__(self
, dts
, log
, loop
, publisher
, ro_account
, project
):
996 self
._publisher
= publisher
997 self
._project
= project
1000 self
._http
_api
= None
1001 self
._openmano
_nsrs
= {}
1002 self
._openmano
_nsr
_by
_vnfr
_id
= {}
1003 #self._nsr_uuid = None
1005 self
._set
_ro
_account
(ro_account
)
1007 def _set_ro_account(self
, ro_account
):
1008 self
._log
.debug("Setting openmano plugin cloud account: %s", ro_account
)
1009 self
._cli
_api
= openmano_client
.OpenmanoCliAPI(
1011 ro_account
.openmano
.host
,
1012 ro_account
.openmano
.port
,
1013 ro_account
.openmano
.tenant_id
,
1016 self
._http
_api
= openmano_client
.OpenmanoHttpAPI(
1018 ro_account
.openmano
.host
,
1019 ro_account
.openmano
.port
,
1020 ro_account
.openmano
.tenant_id
,
1023 def set_state(self
, nsr_id
, state
):
1024 # Currently we update only during terminate to
1025 # decide how to handle VL terminate
1026 if state
.value
== OpenmanoNSRecordState
.TERMINATE
.value
:
1027 self
._openmano
_nsrs
[nsr_id
]._state
= \
1028 [member
.value
for name
, member
in \
1029 OpenmanoNSRecordState
.__members
__.items() \
1030 if member
.value
== state
.value
]
1032 def create_nsr(self
, nsr_config_msg
, nsd_msg
, key_pairs
=None, ssh_key
=None):
1034 Create Network service record
1036 openmano_nsr
= OpenmanoNsr(
1049 self
.log
.debug("NSR created in openmano nsm %s", openmano_nsr
)
1050 self
._openmano
_nsrs
[nsr_config_msg
.id] = openmano_nsr
1053 def deploy(self
, nsr_msg
):
1054 self
._log
.debug("Received NSR Deploy msg : %s", nsr_msg
)
1055 openmano_nsr
= self
._openmano
_nsrs
[nsr_msg
.ns_instance_config_ref
]
1056 yield from openmano_nsr
.create()
1057 yield from openmano_nsr
.deploy(nsr_msg
)
1060 def instantiate_ns(self
, nsr
, xact
):
1062 Instantiate NSR with the passed nsr id
1064 yield from nsr
.instantiate(xact
)
1067 def instantiate_vnf(self
, nsr
, vnfr
, scaleout
=False):
1069 Instantiate NSR with the passed nsr id
1071 openmano_nsr
= self
._openmano
_nsrs
[nsr
.id]
1073 openmano_vnf_nsr
= OpenmanoNsr(
1081 openmano_nsr
.nsd_msg
,
1082 openmano_nsr
.nsr_config_msg
,
1083 openmano_nsr
.key_pairs
,
1085 rift_vnfd_id
=vnfr
.vnfd
.id,
1087 self
._openmano
_nsr
_by
_vnfr
_id
[nsr
.id] = openmano_nsr
1088 if vnfr
.id in self
._openmano
_nsr
_by
_vnfr
_id
:
1089 raise VNFExistError("VNF %s already exist", vnfr
.id)
1090 self
._openmano
_nsr
_by
_vnfr
_id
[vnfr
.id] = openmano_vnf_nsr
1091 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]))
1093 for vlr
in openmano_nsr
.vlrs
:
1094 yield from openmano_vnf_nsr
.add_vlr(vlr
)
1096 yield from openmano_nsr
.add_nsr(openmano_vnf_nsr
, vnfr
)
1097 except Exception as e
:
1098 self
.log
.exception(str(e
))
1100 yield from openmano_vnf_nsr
.add_vnfr(vnfr
)
1101 except Exception as e
:
1102 self
.log
.exception(str(e
))
1104 yield from openmano_vnf_nsr
.scaling_scenario_create()
1105 except Exception as e
:
1106 self
.log
.exception(str(e
))
1108 yield from openmano_vnf_nsr
.deploy_scaling(openmano_vnf_nsr
.nsr_msg
, vnfr
.id)
1109 except Exception as e
:
1110 self
.log
.exception(str(e
))
1112 yield from openmano_nsr
.add_vnfr(vnfr
)
1114 # Mark the VNFR as running
1115 # TODO: Create a task to monitor nsr/vnfr status
1116 vnfr_msg
= vnfr
.vnfr_msg
.deep_copy()
1117 vnfr_msg
.operational_status
= "init"
1119 self
._log
.debug("Attempting to publish openmano vnf: %s", vnfr_msg
)
1120 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
1122 def update_vnfr(self
, vnfr
):
1123 vnfr_msg
= vnfr
.vnfr_msg
.deep_copy()
1124 self
._log
.debug("Attempting to publish openmano vnf: %s", vnfr_msg
)
1125 yield from self
._publisher
.publish_vnfr(None, vnfr_msg
)
1128 def instantiate_vl(self
, nsr
, vlr
):
1130 Instantiate NSR with the passed nsr id
1132 self
._log
.debug("Received instantiate VL for NSR {}; VLR {}".format(nsr
.id,vlr
))
1133 openmano_nsr
= self
._openmano
_nsrs
[nsr
.id]
1134 if openmano_nsr
._state
== OpenmanoNSRecordState
.RUNNING
:
1135 yield from openmano_nsr
.create_vlr(vlr
)
1136 yield from self
._publisher
.publish_vlr(None, vlr
.vlr_msg
)
1138 yield from openmano_nsr
.add_vlr(vlr
)
1141 def terminate_ns(self
, nsr
):
1143 Terminate the network service
1146 openmano_nsr
= self
._openmano
_nsrs
[nsr_id
]
1148 for _
,handler
in openmano_nsr
._vdur
_console
_handler
.items():
1149 handler
._regh
.deregister()
1151 yield from self
._loop
.run_in_executor(
1157 for vnfr
in openmano_nsr
.vnfrs
:
1158 self
._log
.debug("Unpublishing VNFR: %s", vnfr
.vnfr
.vnfr_msg
)
1159 yield from self
._publisher
.unpublish_vnfr(None, vnfr
.vnfr
.vnfr_msg
)
1161 del self
._openmano
_nsrs
[nsr_id
]
1163 def terminate(self
, openmano_nsr
):
1164 openmano_nsr
.terminate()
1165 openmano_nsr
.delete()
1168 def terminate_vnf(self
, nsr
, vnfr
, scalein
=False):
1170 Terminate the network service
1173 openmano_vnf_nsr
= self
._openmano
_nsr
_by
_vnfr
_id
[vnfr
.id]
1174 openmano_vnf_nsr
.terminate()
1175 openmano_vnf_nsr
.delete()
1176 yield from openmano_vnf_nsr
.remove_vnf(vnfr
)
1179 def terminate_vl(self
, vlr
):
1181 Terminate the virtual link
1183 self
._log
.debug("Received terminate VL for VLR {}".format(vlr
))
1184 openmano_nsr
= self
._openmano
_nsrs
[vlr
._nsr
_id
]
1185 if openmano_nsr
._state
== OpenmanoNSRecordState
.RUNNING
:
1186 yield from openmano_nsr
.delete_vlr(vlr
)
1188 yield from openmano_nsr
.remove_vlr(vlr
)