Removal of deprecated SNAPS-OO classes.
[functest.git] / functest / opnfv_tests / vnf / ims / orchestra_openims.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016 Orange and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """Orchestra OpenIMS testcase implementation."""
11
12 import json
13 import logging
14 import os
15 import socket
16 import time
17 import pkg_resources
18 import yaml
19
20 from snaps.config.flavor import FlavorConfig
21 from snaps.config.image import ImageConfig
22 from snaps.config.network import NetworkConfig, PortConfig, SubnetConfig
23 from snaps.config.router import RouterConfig
24 from snaps.config.security_group import (
25     Direction, Protocol, SecurityGroupConfig, SecurityGroupRuleConfig)
26 from snaps.config.vm_inst import VmInstanceConfig
27
28 from snaps.openstack.create_image import OpenStackImage
29 from snaps.openstack.create_flavor import OpenStackFlavor
30 from snaps.openstack.create_security_group import OpenStackSecurityGroup
31 from snaps.openstack.create_network import OpenStackNetwork
32 from snaps.openstack.create_router import OpenStackRouter
33 from snaps.openstack.create_instance import OpenStackVmInstance
34
35 from functest.opnfv_tests.openstack.snaps import snaps_utils
36
37 import functest.core.vnf as vnf
38 import functest.utils.openstack_utils as os_utils
39 from functest.utils.constants import CONST
40
41 from org.openbaton.cli.errors.errors import NfvoException
42 from org.openbaton.cli.agents.agents import MainAgent
43
44
45 __author__ = "Pauls, Michael <michael.pauls@fokus.fraunhofer.de>"
46 # ----------------------------------------------------------
47 #
48 #               UTILS
49 #
50 # -----------------------------------------------------------
51
52
53 def get_config(parameter, file_path):
54     """
55     Get config parameter.
56
57     Returns the value of a given parameter in file.yaml
58     parameter must be given in string format with dots
59     Example: general.openstack.image_name
60     """
61     with open(file_path) as config_file:
62         file_yaml = yaml.safe_load(config_file)
63     config_file.close()
64     value = file_yaml
65     for element in parameter.split("."):
66         value = value.get(element)
67         if value is None:
68             raise ValueError("The parameter %s is not defined in"
69                              " reporting.yaml", parameter)
70     return value
71
72
73 def servertest(host, port):
74     """Method to test that a server is reachable at IP:port"""
75     args = socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM)
76     for family, socktype, proto, canonname, sockaddr in args:
77         sock = socket.socket(family, socktype, proto)
78         try:
79             sock.connect(sockaddr)
80         except socket.error:
81             return False
82         else:
83             sock.close()
84             return True
85
86
87 def get_userdata(orchestrator=dict):
88     """Build userdata for Open Baton machine"""
89     userdata = "#!/bin/bash\n"
90     userdata += "echo \"Executing userdata...\"\n"
91     userdata += "set -x\n"
92     userdata += "set -e\n"
93     userdata += "echo \"Set nameserver to '8.8.8.8'...\"\n"
94     userdata += "echo \"nameserver   8.8.8.8\" >> /etc/resolv.conf\n"
95     userdata += "echo \"Install curl...\"\n"
96     userdata += "apt-get install curl\n"
97     userdata += "echo \"Inject public key...\"\n"
98     userdata += ("echo \"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuPXrV3"
99                  "geeHc6QUdyUr/1Z+yQiqLcOskiEGBiXr4z76MK4abiFmDZ18OMQlc"
100                  "fl0p3kS0WynVgyaOHwZkgy/DIoIplONVr2CKBKHtPK+Qcme2PVnCtv"
101                  "EqItl/FcD+1h5XSQGoa+A1TSGgCod/DPo+pes0piLVXP8Ph6QS1k7S"
102                  "ic7JDeRQ4oT1bXYpJ2eWBDMfxIWKZqcZRiGPgMIbJ1iEkxbpeaAd9O"
103                  "4MiM9nGCPESmed+p54uYFjwEDlAJZShcAZziiZYAvMZhvAhe6USljc"
104                  "7YAdalAnyD/jwCHuwIrUw/lxo7UdNCmaUxeobEYyyFA1YVXzpNFZya"
105                  "XPGAAYIJwEq/ openbaton@opnfv\" >> /home/ubuntu/.ssh/aut"
106                  "horized_keys\n")
107     userdata += "echo \"Download bootstrap...\"\n"
108     userdata += ("curl -s %s "
109                  "> ./bootstrap\n" % orchestrator['bootstrap']['url'])
110     userdata += ("curl -s %s" "> ./config_file\n" %
111                  orchestrator['bootstrap']['config']['url'])
112     userdata += ("echo \"Disable usage of mysql...\"\n")
113     userdata += "sed -i s/mysql=.*/mysql=no/g /config_file\n"
114     userdata += ("echo \"Setting 'rabbitmq_broker_ip' to '%s'\"\n"
115                  % orchestrator['details']['fip'].ip)
116     userdata += ("sed -i s/rabbitmq_broker_ip=localhost/rabbitmq_broker_ip"
117                  "=%s/g /config_file\n" % orchestrator['details']['fip'].ip)
118     userdata += "echo \"Set autostart of components to 'false'\"\n"
119     userdata += "export OPENBATON_COMPONENT_AUTOSTART=false\n"
120     userdata += "echo \"Execute bootstrap...\"\n"
121     bootstrap = "sh ./bootstrap release -configFile=./config_file"
122     userdata += bootstrap + "\n"
123     userdata += "echo \"Setting 'nfvo.plugin.timeout' to '300000'\"\n"
124     userdata += ("echo \"nfvo.plugin.timeout=600000\" >> "
125                  "/etc/openbaton/openbaton-nfvo.properties\n")
126     userdata += (
127         "wget %s -O /etc/openbaton/openbaton-vnfm-generic-user-data.sh\n" %
128         orchestrator['gvnfm']['userdata']['url'])
129     userdata += "sed -i '113i"'\ \ \ \ '"sleep 60' " \
130                 "/etc/openbaton/openbaton-vnfm-generic-user-data.sh\n"
131     userdata += ("sed -i s/nfvo.marketplace.port=8082/nfvo.marketplace."
132                  "port=8080/g /etc/openbaton/openbaton-nfvo.properties\n")
133     userdata += "echo \"Starting NFVO\"\n"
134     userdata += "service openbaton-nfvo restart\n"
135     userdata += "echo \"Starting Generic VNFM\"\n"
136     userdata += "service openbaton-vnfm-generic restart\n"
137     userdata += "echo \"...end of userdata...\"\n"
138     return userdata
139
140
141 class OpenImsVnf(vnf.VnfOnBoarding):
142     """OpenIMS VNF deployed with openBaton orchestrator"""
143
144     # logger = logging.getLogger(__name__)
145
146     def __init__(self, **kwargs):
147         if "case_name" not in kwargs:
148             kwargs["case_name"] = "orchestra_openims"
149         super(OpenImsVnf, self).__init__(**kwargs)
150         self.logger = logging.getLogger("functest.ci.run_tests.orchestra")
151         self.logger.info("kwargs %s", (kwargs))
152
153         self.case_dir = pkg_resources.resource_filename(
154             'functest', 'opnfv_tests/vnf/ims/')
155         self.data_dir = CONST.__getattribute__('dir_ims_data')
156         self.test_dir = CONST.__getattribute__('dir_repo_vims_test')
157         self.created_resources = []
158         self.logger.info("%s VNF onboarding test starting", self.case_name)
159
160         try:
161             self.config = CONST.__getattribute__(
162                 'vnf_{}_config'.format(self.case_name))
163         except BaseException:
164             raise Exception("Orchestra VNF config file not found")
165         config_file = self.case_dir + self.config
166
167         self.mano = dict(
168             get_config("mano", config_file),
169             details={}
170         )
171         self.logger.debug("Orchestrator configuration %s", self.mano)
172
173         self.details['orchestrator'] = dict(
174             name=self.mano['name'],
175             version=self.mano['version'],
176             status='ERROR',
177             result=''
178         )
179
180         self.vnf = dict(
181             get_config(self.case_name, config_file),
182         )
183         self.logger.debug("VNF configuration: %s", self.vnf)
184
185         self.details['vnf'] = dict(
186             name=self.vnf['name'],
187         )
188
189         self.details['test_vnf'] = dict(
190             name=self.case_name,
191         )
192
193         # Orchestra base Data directory creation
194         if not os.path.exists(self.data_dir):
195             os.makedirs(self.data_dir)
196
197         self.images = get_config("tenant_images.orchestrator", config_file)
198         self.images.update(get_config("tenant_images.%s" %
199                                       self.case_name, config_file))
200
201     def prepare(self):
202         """Prepare testscase (Additional pre-configuration steps)."""
203         super(OpenImsVnf, self).prepare()
204
205         self.logger.info("Additional pre-configuration steps")
206         self.creds = {
207                 "tenant": self.tenant_name,
208                 "username": self.tenant_name,
209                 "password": self.tenant_name,
210                 "auth_url": os_utils.get_credentials()['auth_url']
211                 }
212         self.prepare_images()
213         self.prepare_flavor()
214         self.prepare_security_groups()
215         self.prepare_network()
216         self.prepare_floating_ip()
217
218     def prepare_images(self):
219         """Upload images if they doen't exist yet"""
220         self.logger.info("Upload images if they doen't exist yet")
221         for image_name, image_file in self.images.iteritems():
222             self.logger.info("image: %s, file: %s", image_name, image_file)
223             if image_file and image_name:
224                 image = OpenStackImage(
225                     self.snaps_creds,
226                     ImageConfig(name=image_name,
227                                 image_user='cloud',
228                                 img_format='qcow2',
229                                 image_file=image_file,
230                                 public=True))
231                 image.create()
232                 # self.created_resources.append(image);
233
234     def prepare_security_groups(self):
235         """Create Open Baton security group if it doesn't exist yet"""
236         self.logger.info(
237             "Creating security group for Open Baton if not yet existing...")
238         sg_rules = list()
239         sg_rules.append(
240             SecurityGroupRuleConfig(
241                 sec_grp_name="orchestra-sec-group-allowall",
242                 direction=Direction.ingress,
243                 protocol=Protocol.tcp,
244                 port_range_min=1,
245                 port_range_max=65535))
246         sg_rules.append(
247             SecurityGroupRuleConfig(
248                 sec_grp_name="orchestra-sec-group-allowall",
249                 direction=Direction.egress,
250                 protocol=Protocol.tcp,
251                 port_range_min=1,
252                 port_range_max=65535))
253         sg_rules.append(
254             SecurityGroupRuleConfig(
255                 sec_grp_name="orchestra-sec-group-allowall",
256                 direction=Direction.ingress,
257                 protocol=Protocol.udp,
258                 port_range_min=1,
259                 port_range_max=65535))
260         sg_rules.append(
261             SecurityGroupRuleConfig(
262                 sec_grp_name="orchestra-sec-group-allowall",
263                 direction=Direction.egress,
264                 protocol=Protocol.udp,
265                 port_range_min=1,
266                 port_range_max=65535))
267         security_group = OpenStackSecurityGroup(
268             self.snaps_creds,
269             SecurityGroupConfig(
270                 name="orchestra-sec-group-allowall",
271                 rule_settings=sg_rules))
272
273         security_group_info = security_group.create()
274         self.created_resources.append(security_group)
275         self.mano['details']['sec_group'] = security_group_info.name
276         self.logger.info(
277             "Security group orchestra-sec-group-allowall prepared")
278
279     def prepare_flavor(self):
280         """Create Open Baton flavor if it doesn't exist yet"""
281         self.logger.info(
282             "Create Flavor for Open Baton NFVO if not yet existing")
283
284         flavor_settings = FlavorConfig(
285             name=self.mano['requirements']['flavor']['name'],
286             ram=self.mano['requirements']['flavor']['ram_min'],
287             disk=self.mano['requirements']['flavor']['disk'],
288             vcpus=self.mano['requirements']['flavor']['vcpus'])
289         flavor = OpenStackFlavor(self.snaps_creds, flavor_settings)
290         flavor_info = flavor.create()
291         self.created_resources.append(flavor)
292         self.mano['details']['flavor'] = {}
293         self.mano['details']['flavor']['name'] = flavor_settings.name
294         self.mano['details']['flavor']['id'] = flavor_info.id
295
296     def prepare_network(self):
297         """Create network/subnet/router if they doen't exist yet"""
298         self.logger.info(
299             "Creating network/subnet/router if they doen't exist yet...")
300         subnet_settings = SubnetConfig(
301             name='%s_subnet' %
302             self.case_name,
303             cidr="192.168.100.0/24")
304         network_settings = NetworkConfig(
305             name='%s_net' %
306             self.case_name,
307             subnet_settings=[subnet_settings])
308         orchestra_network = OpenStackNetwork(
309             self.snaps_creds, network_settings)
310         orchestra_network_info = orchestra_network.create()
311         self.mano['details']['network'] = {}
312         self.mano['details']['network']['id'] = orchestra_network_info.id
313         self.mano['details']['network']['name'] = orchestra_network_info.name
314         self.mano['details']['external_net_name'] = \
315             snaps_utils.get_ext_net_name(self.snaps_creds)
316         self.created_resources.append(orchestra_network)
317         orchestra_router = OpenStackRouter(
318             self.snaps_creds,
319             RouterConfig(
320                 name='%s_router' %
321                 self.case_name,
322                 external_gateway=self.mano['details']['external_net_name'],
323                 internal_subnets=[
324                     subnet_settings.name]))
325         orchestra_router.create()
326         self.created_resources.append(orchestra_router)
327         self.logger.info("Created network and router for Open Baton NFVO...")
328
329     def prepare_floating_ip(self):
330         """Select/Create Floating IP if it doesn't exist yet"""
331         self.logger.info("Retrieving floating IP for Open Baton NFVO")
332         neutron_client = snaps_utils.neutron_utils.neutron_client(
333             self.snaps_creds)
334         # Finding Tenant ID to check to which tenant the Floating IP belongs
335         tenant_id = os_utils.get_tenant_id(
336             os_utils.get_keystone_client(self.creds),
337             self.tenant_name)
338         # Use os_utils to retrieve complete information of Floating IPs
339         floating_ips = os_utils.get_floating_ips(neutron_client)
340         my_floating_ips = []
341         # Filter Floating IPs with tenant id
342         for floating_ip in floating_ips:
343             # self.logger.info("Floating IP: %s", floating_ip)
344             if floating_ip.get('tenant_id') == tenant_id:
345                 my_floating_ips.append(floating_ip.get('floating_ip_address'))
346         # Select if Floating IP exist else create new one
347         if len(my_floating_ips) >= 1:
348             # Get Floating IP object from snaps for clean up
349             snaps_floating_ips = snaps_utils.neutron_utils.get_floating_ips(
350                 neutron_client)
351             for my_floating_ip in my_floating_ips:
352                 for snaps_floating_ip in snaps_floating_ips:
353                     if snaps_floating_ip.ip == my_floating_ip:
354                         self.mano['details']['fip'] = snaps_floating_ip
355                         self.logger.info(
356                             "Selected floating IP for Open Baton NFVO %s",
357                             (self.mano['details']['fip'].ip))
358                         break
359                 if self.mano['details']['fip'] is not None:
360                     break
361         else:
362             self.logger.info("Creating floating IP for Open Baton NFVO")
363             self.mano['details']['fip'] = (
364                 snaps_utils.neutron_utils. create_floating_ip(
365                     neutron_client, self.mano['details']['external_net_name']))
366             self.logger.info(
367                 "Created floating IP for Open Baton NFVO %s",
368                 (self.mano['details']['fip'].ip))
369
370     def get_vim_descriptor(self):
371         """"Create VIM descriptor to be used for onboarding"""
372         self.logger.info(
373             "Building VIM descriptor with PoP creds: %s",
374             self.creds)
375         # Depending on API version either tenant ID or project name must be
376         # used
377         if os_utils.is_keystone_v3():
378             self.logger.info(
379                 "Using v3 API of OpenStack... -> Using OS_PROJECT_ID")
380             project_id = os_utils.get_tenant_id(
381                 os_utils.get_keystone_client(),
382                 self.creds.get("project_name"))
383         else:
384             self.logger.info(
385                 "Using v2 API of OpenStack... -> Using OS_TENANT_NAME")
386             project_id = self.creds.get("tenant_name")
387         self.logger.debug("VIM project/tenant id: %s", project_id)
388         vim_json = {
389             "name": "vim-instance",
390             "authUrl": self.creds.get("auth_url"),
391             "tenant": project_id,
392             "username": self.creds.get("username"),
393             "password": self.creds.get("password"),
394             "securityGroups": [
395                 self.mano['details']['sec_group']
396             ],
397             "type": "openstack",
398             "location": {
399                 "name": "opnfv",
400                 "latitude": "52.525876",
401                 "longitude": "13.314400"
402             }
403         }
404         self.logger.info("Built VIM descriptor: %s", vim_json)
405         return vim_json
406
407     def deploy_orchestrator(self):
408         self.logger.info("Deploying Open Baton...")
409         self.logger.info("Details: %s", self.mano['details'])
410         start_time = time.time()
411
412         self.logger.info("Creating orchestra instance...")
413         userdata = get_userdata(self.mano)
414         self.logger.info("flavor: %s\n"
415                          "image: %s\n"
416                          "network_id: %s\n",
417                          self.mano['details']['flavor']['name'],
418                          self.mano['requirements']['image'],
419                          self.mano['details']['network']['id'])
420         self.logger.debug("userdata: %s\n", userdata)
421         # setting up image
422         image_settings = ImageConfig(
423             name=self.mano['requirements']['image'],
424             image_user='ubuntu',
425             exists=True)
426         # setting up port
427         port_settings = PortConfig(
428             name='%s_port' % self.case_name,
429             network_name=self.mano['details']['network']['name'])
430         # build configuration of vm
431         orchestra_settings = VmInstanceConfig(
432             name=self.case_name,
433             flavor=self.mano['details']['flavor']['name'],
434             port_settings=[port_settings],
435             security_group_names=[self.mano['details']['sec_group']],
436             userdata=str(userdata))
437         orchestra_vm = OpenStackVmInstance(self.snaps_creds,
438                                            orchestra_settings,
439                                            image_settings)
440
441         orchestra_vm.create()
442         self.created_resources.append(orchestra_vm)
443         self.mano['details']['id'] = orchestra_vm.get_vm_info()['id']
444         self.logger.info(
445             "Created orchestra instance: %s",
446             self.mano['details']['id'])
447
448         self.logger.info("Associating floating ip: '%s' to VM '%s' ",
449                          self.mano['details']['fip'].ip,
450                          self.case_name)
451         nova_client = os_utils.get_nova_client()
452         if not os_utils.add_floating_ip(
453                 nova_client,
454                 self.mano['details']['id'],
455                 self.mano['details']['fip'].ip):
456             duration = time.time() - start_time
457             self.details["orchestrator"].update(
458                 status='FAIL', duration=duration)
459             self.logger.error("Cannot associate floating IP to VM.")
460             return False
461
462         self.logger.info("Waiting for Open Baton NFVO to be up and running...")
463         timeout = 0
464         while timeout < 45:
465             if servertest(
466                     self.mano['details']['fip'].ip,
467                     "8080"):
468                 break
469             else:
470                 self.logger.info("Open Baton NFVO is not started yet (%ss)",
471                                  (timeout * 60))
472                 time.sleep(60)
473                 timeout += 1
474
475         if timeout >= 45:
476             duration = time.time() - start_time
477             self.details["orchestrator"].update(
478                 status='FAIL', duration=duration)
479             self.logger.error("Open Baton is not started correctly")
480             return False
481
482         self.logger.info("Waiting for all components to be up and running...")
483         time.sleep(60)
484         duration = time.time() - start_time
485         self.details["orchestrator"].update(status='PASS', duration=duration)
486         self.logger.info("Deploy Open Baton NFVO: OK")
487         return True
488
489     def deploy_vnf(self):
490         start_time = time.time()
491         self.logger.info("Deploying %s...", self.vnf['name'])
492
493         main_agent = MainAgent(
494             nfvo_ip=self.mano['details']['fip'].ip,
495             nfvo_port=8080,
496             https=False,
497             version=1,
498             username=self.mano['credentials']['username'],
499             password=self.mano['credentials']['password'])
500
501         self.logger.info(
502             "Create %s Flavor if not existing", self.vnf['name'])
503         flavor_settings = FlavorConfig(
504             name=self.vnf['requirements']['flavor']['name'],
505             ram=self.vnf['requirements']['flavor']['ram_min'],
506             disk=self.vnf['requirements']['flavor']['disk'],
507             vcpus=self.vnf['requirements']['flavor']['vcpus'])
508         flavor = OpenStackFlavor(self.snaps_creds, flavor_settings)
509         flavor_info = flavor.create()
510         self.logger.debug("Flavor id: %s", flavor_info.id)
511
512         self.logger.info("Getting project 'default'...")
513         project_agent = main_agent.get_agent("project", "")
514         for project in json.loads(project_agent.find()):
515             if project.get("name") == "default":
516                 self.mano['details']['project_id'] = project.get("id")
517                 self.logger.info("Found project 'default': %s", project)
518                 break
519
520         vim_json = self.get_vim_descriptor()
521         self.logger.info("Registering VIM: %s", vim_json)
522
523         main_agent.get_agent(
524             "vim", project_id=self.mano['details']['project_id']).create(
525                 entity=json.dumps(vim_json))
526
527         market_agent = main_agent.get_agent(
528             "market", project_id=self.mano['details']['project_id'])
529
530         try:
531             self.logger.info("sending: %s", self.vnf['descriptor']['url'])
532             nsd = market_agent.create(entity=self.vnf['descriptor']['url'])
533             if nsd.get('id') is None:
534                 self.logger.error("NSD not onboarded correctly")
535                 duration = time.time() - start_time
536                 self.details["vnf"].update(status='FAIL', duration=duration)
537                 return False
538             self.mano['details']['nsd_id'] = nsd.get('id')
539             self.logger.info("Onboarded NSD: " + nsd.get("name"))
540
541             nsr_agent = main_agent.get_agent(
542                 "nsr", project_id=self.mano['details']['project_id'])
543
544             self.mano['details']['nsr'] = nsr_agent.create(
545                 self.mano['details']['nsd_id'])
546         except NfvoException as exc:
547             self.logger.error(exc.message)
548             duration = time.time() - start_time
549             self.details["vnf"].update(status='FAIL', duration=duration)
550             return False
551
552         if self.mano['details']['nsr'].get('code') is not None:
553             self.logger.error(
554                 "%s cannot be deployed: %s -> %s",
555                 self.vnf['name'],
556                 self.mano['details']['nsr'].get('code'),
557                 self.mano['details']['nsr'].get('message'))
558             self.logger.error("%s cannot be deployed", self.vnf['name'])
559             duration = time.time() - start_time
560             self.details["vnf"].update(status='FAIL', duration=duration)
561             return False
562
563         timeout = 0
564         self.logger.info("Waiting for NSR to go to ACTIVE...")
565         while self.mano['details']['nsr'].get("status") != 'ACTIVE' \
566                 and self.mano['details']['nsr'].get("status") != 'ERROR':
567             timeout += 1
568             self.logger.info("NSR is not yet ACTIVE... (%ss)", 60 * timeout)
569             if timeout == 30:
570                 self.logger.error("INACTIVE NSR after %s sec..", 60 * timeout)
571                 duration = time.time() - start_time
572                 self.details["vnf"].update(status='FAIL', duration=duration)
573                 return False
574             time.sleep(60)
575             self.mano['details']['nsr'] = json.loads(
576                 nsr_agent.find(self.mano['details']['nsr'].get('id')))
577
578         duration = time.time() - start_time
579         if self.mano['details']['nsr'].get("status") == 'ACTIVE':
580             self.details["vnf"].update(status='PASS', duration=duration)
581             self.logger.info("Sleep for 60s to ensure that all "
582                              "services are up and running...")
583             time.sleep(60)
584             result = True
585         else:
586             self.details["vnf"].update(status='FAIL', duration=duration)
587             self.logger.error("NSR: %s", self.mano['details'].get('nsr'))
588             result = False
589         return result
590
591     def test_vnf(self):
592         self.logger.info("Testing VNF OpenIMS...")
593         start_time = time.time()
594         self.logger.info(
595             "Testing if %s works properly...",
596             self.mano['details']['nsr'].get('name'))
597         for vnfr in self.mano['details']['nsr'].get('vnfr'):
598             self.logger.info(
599                 "Checking ports %s of VNF %s",
600                 self.vnf['test'][vnfr.get('name')]['ports'],
601                 vnfr.get('name'))
602             for vdu in vnfr.get('vdu'):
603                 for vnfci in vdu.get('vnfc_instance'):
604                     self.logger.debug(
605                         "Checking ports of VNFC instance %s",
606                         vnfci.get('hostname'))
607                     for floating_ip in vnfci.get('floatingIps'):
608                         self.logger.debug(
609                             "Testing %s:%s",
610                             vnfci.get('hostname'),
611                             floating_ip.get('ip'))
612                         for port in self.vnf['test'][vnfr.get(
613                                 'name')]['ports']:
614                             if servertest(floating_ip.get('ip'), port):
615                                 self.logger.info(
616                                     "VNFC instance %s is reachable at %s:%s",
617                                     vnfci.get('hostname'),
618                                     floating_ip.get('ip'),
619                                     port)
620                             else:
621                                 self.logger.error(
622                                     "VNFC instance %s is not reachable "
623                                     "at %s:%s",
624                                     vnfci.get('hostname'),
625                                     floating_ip.get('ip'),
626                                     port)
627                                 duration = time.time() - start_time
628                                 self.details["test_vnf"].update(
629                                     status='FAIL', duration=duration, esult=(
630                                         "Port %s of server %s -> %s is "
631                                         "not reachable",
632                                         port,
633                                         vnfci.get('hostname'),
634                                         floating_ip.get('ip')))
635                                 self.logger.error("Test VNF: ERROR")
636                                 return False
637         duration = time.time() - start_time
638         self.details["test_vnf"].update(status='PASS', duration=duration)
639         self.logger.info("Test VNF: OK")
640         return True
641
642     def clean(self):
643         self.logger.info("Cleaning %s...", self.case_name)
644         try:
645             main_agent = MainAgent(
646                 nfvo_ip=self.mano['details']['fip'].ip,
647                 nfvo_port=8080,
648                 https=False,
649                 version=1,
650                 username=self.mano['credentials']['username'],
651                 password=self.mano['credentials']['password'])
652             self.logger.info("Terminating %s...", self.vnf['name'])
653             if (self.mano['details'].get('nsr')):
654                 main_agent.get_agent(
655                     "nsr",
656                     project_id=self.mano['details']['project_id']).\
657                         delete(self.mano['details']['nsr'].get('id'))
658                 self.logger.info("Sleeping 60 seconds...")
659                 time.sleep(60)
660             else:
661                 self.logger.info("No need to terminate the VNF...")
662             # os_utils.delete_instance(nova_client=os_utils.get_nova_client(),
663             #                          instance_id=self.mano_instance_id)
664         except (NfvoException, KeyError) as exc:
665             self.logger.error('Unexpected error cleaning - %s', exc)
666
667         try:
668             neutron_client = os_utils.get_neutron_client(self.creds)
669             self.logger.info("Deleting Open Baton Port...")
670             port = snaps_utils.neutron_utils.get_port(
671                 neutron_client,
672                 port_name='%s_port' % self.case_name)
673             snaps_utils.neutron_utils.delete_port(neutron_client, port)
674             time.sleep(10)
675         except Exception as exc:
676             self.logger.error('Unexpected error cleaning - %s', exc)
677         try:
678             self.logger.info("Deleting Open Baton Floating IP...")
679             snaps_utils.neutron_utils.delete_floating_ip(
680                 neutron_client, self.mano['details']['fip'])
681         except Exception as exc:
682             self.logger.error('Unexpected error cleaning - %s', exc)
683
684         for resource in reversed(self.created_resources):
685             try:
686                 self.logger.info("Cleaning %s", str(resource))
687                 resource.clean()
688             except Exception as exc:
689                 self.logger.error('Unexpected error cleaning - %s', exc)
690         super(OpenImsVnf, self).clean()