Merge "Update keys of details dic for refstack_defcore"
[functest.git] / functest / opnfv_tests / vnf / ims / opera_ims.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 import json
11 import socket
12 import sys
13 import time
14 import yaml
15
16 import functest.core.vnf_base as vnf_base
17 import functest.utils.functest_logger as ft_logger
18 import functest.utils.functest_utils as ft_utils
19 import functest.utils.openstack_utils as os_utils
20 import os
21 from functest.utils.constants import CONST
22
23 from org.openbaton.cli.agents.agents import MainAgent
24 from org.openbaton.cli.errors.errors import NfvoException
25
26
27 def servertest(host, port):
28     args = socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM)
29     for family, socktype, proto, canonname, sockaddr in args:
30         s = socket.socket(family, socktype, proto)
31         try:
32             s.connect(sockaddr)
33         except socket.error:
34             return False
35         else:
36             s.close()
37             return True
38
39 # ----------------------------------------------------------
40 #
41 #               UTILS
42 #
43 # -----------------------------------------------------------
44
45
46 def get_config(parameter, file):
47     """
48     Returns the value of a given parameter in file.yaml
49     parameter must be given in string format with dots
50     Example: general.openstack.image_name
51     """
52     with open(file) as f:
53         file_yaml = yaml.safe_load(f)
54     f.close()
55     value = file_yaml
56     for element in parameter.split("."):
57         value = value.get(element)
58         if value is None:
59             raise ValueError("The parameter %s is not defined in"
60                              " reporting.yaml" % parameter)
61     return value
62
63
64 def download_and_add_image_on_glance(glance, image_name,
65                                      image_url, data_dir):
66     dest_path = data_dir
67     if not os.path.exists(dest_path):
68         os.makedirs(dest_path)
69     file_name = image_url.rsplit('/')[-1]
70     if not ft_utils.download_url(image_url, dest_path):
71         return False
72     image = os_utils.create_glance_image(
73         glance, image_name, dest_path + file_name)
74     if not image:
75         return False
76     return image
77
78
79 class ImsVnf(vnf_base.VnfOnBoardingBase):
80
81     def __init__(self, project='functest', case='orchestra_ims',
82                  repo='', cmd=''):
83         super(ImsVnf, self).__init__(project, case, repo, cmd)
84         self.ob_password = "openbaton"
85         self.ob_username = "admin"
86         self.ob_https = False
87         self.ob_port = "8080"
88         self.ob_ip = "localhost"
89         self.ob_instance_id = ""
90         self.logger = ft_logger.Logger("orchestra_ims").getLogger()
91         self.case_dir = os.path.join(CONST.dir_functest_test, 'vnf/ims/')
92         self.data_dir = CONST.dir_ims_data
93         self.test_dir = CONST.dir_repo_vims_test
94         self.ob_projectid = ""
95         self.keystone_client = os_utils.get_keystone_client()
96         self.ob_nsr_id = ""
97         self.main_agent = None
98         # vIMS Data directory creation
99         if not os.path.exists(self.data_dir):
100             os.makedirs(self.data_dir)
101         # Retrieve the configuration
102         try:
103             self.config = CONST.__getattribute__(
104                 'vnf_{}_config'.format(self.case_name))
105         except:
106             raise Exception("Orchestra VNF config file not found")
107         config_file = self.case_dir + self.config
108         self.imagename = get_config("openbaton.imagename", config_file)
109         self.market_link = get_config("openbaton.marketplace_link",
110                                       config_file)
111         self.images = get_config("tenant_images", config_file)
112
113     def deploy_orchestrator(self, **kwargs):
114         self.logger.info("Additional pre-configuration steps")
115         nova_client = os_utils.get_nova_client()
116         neutron_client = os_utils.get_neutron_client()
117         glance_client = os_utils.get_glance_client()
118
119         # needs some images
120         self.logger.info("Upload some OS images if it doesn't exist")
121         temp_dir = os.path.join(self.data_dir, "tmp/")
122         for image_name, image_url in self.images.iteritems():
123             self.logger.info("image: %s, url: %s" % (image_name, image_url))
124             try:
125                 image_id = os_utils.get_image_id(glance_client,
126                                                  image_name)
127                 self.logger.info("image_id: %s" % image_id)
128             except:
129                 self.logger.error("Unexpected error: %s" % sys.exc_info()[0])
130
131             if image_id == '':
132                 self.logger.info("""%s image doesn't exist on glance repository. Try
133                 downloading this image and upload on glance !""" % image_name)
134                 image_id = download_and_add_image_on_glance(glance_client,
135                                                             image_name,
136                                                             image_url,
137                                                             temp_dir)
138             if image_id == '':
139                 self.step_failure(
140                     "Failed to find or upload required OS "
141                     "image for this deployment")
142         network_dic = os_utils.create_network_full(neutron_client,
143                                                    "openbaton_mgmt",
144                                                    "openbaton_mgmt_subnet",
145                                                    "openbaton_router",
146                                                    "192.168.100.0/24")
147
148         # orchestrator VM flavor
149         self.logger.info("Check medium Flavor is available, if not create one")
150         flavor_exist, flavor_id = os_utils.get_or_create_flavor(
151             "m1.medium",
152             "4096",
153             '1',
154             '2',
155             public=True)
156         self.logger.debug("Flavor id: %s" % flavor_id)
157
158         if not network_dic:
159             self.logger.error("There has been a problem when creating the "
160                               "neutron network")
161
162         network_id = network_dic["net_id"]
163
164         self.logger.info("Creating floating IP for VM in advance...")
165         floatip_dic = os_utils.create_floating_ip(neutron_client)
166         floatip = floatip_dic['fip_addr']
167
168         if floatip is None:
169             self.logger.error("Cannot create floating IP.")
170
171         userdata = "#!/bin/bash\n"
172         userdata += "set -x\n"
173         userdata += "set -e\n"
174         userdata += "echo \"nameserver   8.8.8.8\" >> /etc/resolv.conf\n"
175         userdata += "apt-get install curl\n"
176         userdata += ("echo \"rabbitmq_broker_ip=%s\" > ./config_file\n"
177                      % floatip)
178         userdata += "echo \"mysql=no\" >> ./config_file\n"
179         userdata += ("echo \"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCuPXrV3"
180                      "geeHc6QUdyUr/1Z+yQiqLcOskiEGBiXr4z76MK4abiFmDZ18OMQlc"
181                      "fl0p3kS0WynVgyaOHwZkgy/DIoIplONVr2CKBKHtPK+Qcme2PVnCtv"
182                      "EqItl/FcD+1h5XSQGoa+A1TSGgCod/DPo+pes0piLVXP8Ph6QS1k7S"
183                      "ic7JDeRQ4oT1bXYpJ2eWBDMfxIWKZqcZRiGPgMIbJ1iEkxbpeaAd9O"
184                      "4MiM9nGCPESmed+p54uYFjwEDlAJZShcAZziiZYAvMZhvAhe6USljc"
185                      "7YAdalAnyD/jwCHuwIrUw/lxo7UdNCmaUxeobEYyyFA1YVXzpNFZya"
186                      "XPGAAYIJwEq/ openbaton@opnfv\" >> /home/ubuntu/.ssh/aut"
187                      "horized_keys\n")
188         userdata += "cat ./config_file\n"
189         userdata += ("curl -s http://get.openbaton.org/bootstrap "
190                      "> ./bootstrap\n")
191         userdata += "export OPENBATON_COMPONENT_AUTOSTART=false\n"
192         bootstrap = "sh ./bootstrap release -configFile=./config_file"
193         userdata += bootstrap + "\n"
194
195         userdata += ("echo \"nfvo.plugin.timeout=300000\" >> "
196                      "/etc/openbaton/openbaton-nfvo.properties\n")
197         userdata += "service openbaton-nfvo restart\n"
198         userdata += "service openbaton-vnfm-generic restart\n"
199
200         sg_id = os_utils.create_security_group_full(neutron_client,
201                                                     "orchestra-sec-group",
202                                                     "allowall")
203
204         os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
205                                       "icmp", 0, 255)
206         os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
207                                       "icmp", 0, 255)
208         os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
209                                       "tcp", 1, 65535)
210         os_utils.create_secgroup_rule(neutron_client, sg_id, "ingress",
211                                       "udp", 1, 65535)
212         os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
213                                       "tcp", 1, 65535)
214         os_utils.create_secgroup_rule(neutron_client, sg_id, "egress",
215                                       "udp", 1, 65535)
216
217         self.logger.info("Security group set")
218
219         self.logger.info("Create instance....")
220         self.logger.info("flavor: m1.medium\n"
221                          "image: %s\n"
222                          "network_id: %s\n"
223                          "userdata: %s\n"
224                          % (self.imagename, network_id, userdata))
225
226         instance = os_utils.create_instance_and_wait_for_active(
227             "m1.medium",
228             os_utils.get_image_id(glance_client, self.imagename),
229             network_id,
230             "orchestra-openbaton",
231             config_drive=False,
232             userdata=userdata)
233
234         self.ob_instance_id = instance.id
235
236         self.logger.info("Adding sec group to orchestra instance")
237         os_utils.add_secgroup_to_instance(nova_client,
238                                           self.ob_instance_id, sg_id)
239
240         self.logger.info("Associating floating ip: '%s' to VM '%s' "
241                          % (floatip, "orchestra-openbaton"))
242         if not os_utils.add_floating_ip(nova_client, instance.id, floatip):
243             self.logger.error("Cannot associate floating IP to VM.")
244             self.step_failure("Cannot associate floating IP to VM.")
245
246         self.logger.info("Waiting for nfvo to be up and running...")
247         x = 0
248         while x < 100:
249             if servertest(floatip, "8080"):
250                 break
251             else:
252                 self.logger.debug("openbaton is not started yet")
253                 time.sleep(5)
254                 x += 1
255
256         if x == 100:
257             self.logger.error("Openbaton is not started correctly")
258             self.step_failure("Openbaton is not started correctly")
259
260         self.ob_ip = floatip
261         self.ob_password = "openbaton"
262         self.ob_username = "admin"
263         self.ob_https = False
264         self.ob_port = "8080"
265
266         self.logger.info("Deploy orchestrator: OK")
267
268     def deploy_vnf(self):
269         self.logger.info("vIMS Deployment")
270
271         self.main_agent = MainAgent(nfvo_ip=self.ob_ip,
272                                     nfvo_port=self.ob_port,
273                                     https=self.ob_https,
274                                     version=1,
275                                     username=self.ob_username,
276                                     password=self.ob_password)
277
278         project_agent = self.main_agent.get_agent("project", self.ob_projectid)
279         for p in json.loads(project_agent.find()):
280             if p.get("name") == "default":
281                 self.ob_projectid = p.get("id")
282                 break
283
284         self.logger.debug("project id: %s" % self.ob_projectid)
285         if self.ob_projectid == "":
286             self.logger.error("Default project id was not found!")
287             self.step_failure("Default project id was not found!")
288
289         vim_json = {
290             "name": "vim-instance",
291             "authUrl": os_utils.get_credentials().get("auth_url"),
292             "tenant": os_utils.get_credentials().get("tenant_name"),
293             "username": os_utils.get_credentials().get("username"),
294             "password": os_utils.get_credentials().get("password"),
295             # "keyPair": "opnfv",
296             # TODO change the keypair to correct value
297             # or upload a correct one or remove it
298             "securityGroups": [
299                 "default",
300                 "orchestra-sec-group"
301             ],
302             "type": "openstack",
303             "location": {
304                 "name": "opnfv",
305                 "latitude": "52.525876",
306                 "longitude": "13.314400"
307             }
308         }
309
310         self.logger.debug("vim: %s" % vim_json)
311
312         self.main_agent.get_agent(
313             "vim",
314             project_id=self.ob_projectid).create(entity=json.dumps(vim_json))
315
316         market_agent = self.main_agent.get_agent("market",
317                                                  project_id=self.ob_projectid)
318
319         nsd = {}
320         try:
321             self.logger.info("sending: %s" % self.market_link)
322             nsd = market_agent.create(entity=self.market_link)
323             self.logger.info("Onboarded nsd: " + nsd.get("name"))
324         except NfvoException as e:
325             self.step_failure(e.message)
326
327         nsr_agent = self.main_agent.get_agent("nsr",
328                                               project_id=self.ob_projectid)
329         nsd_id = nsd.get('id')
330         if nsd_id is None:
331             self.step_failure("NSD not onboarded correctly")
332
333         nsr = None
334         try:
335             nsr = nsr_agent.create(nsd_id)
336         except NfvoException as e:
337             self.step_failure(e.message)
338
339         if nsr.get('code') is not None:
340             self.logger.error(
341                 "vIMS cannot be deployed: %s -> %s" %
342                 (nsr.get('code'), nsr.get('message')))
343             self.step_failure("vIMS cannot be deployed")
344
345         i = 0
346         self.logger.info("waiting NSR to go to active...")
347         while nsr.get("status") != 'ACTIVE' and nsr.get("status") != 'ERROR':
348             i += 1
349             if i == 100:
350                 self.step_failure("After %s sec the nsr did not go to active.."
351                                   % 5 * 100)
352             time.sleep(5)
353             nsr = json.loads(nsr_agent.find(nsr.get('id')))
354
355         if nsr.get("status") == 'ACTIVE':
356             deploy_vnf = {'status': "PASS", 'result': nsr}
357             self.logger.info("Deploy VNF: OK")
358         else:
359             deploy_vnf = {'status': "FAIL", 'result': nsr}
360             self.logger.error("Deploy VNF: ERROR")
361             self.step_failure("Deploy vIMS failed")
362         self.ob_nsr_id = nsr.get("id")
363         return deploy_vnf
364
365     def test_vnf(self):
366         # Adaptations probably needed
367         # code used for cloudify_ims
368         # ruby client on jumphost calling the vIMS on the SUT
369         return
370
371     def clean(self):
372         self.main_agent.get_agent(
373             "nsr",
374             project_id=self.ob_projectid).delete(self.ob_nsr_id)
375         time.sleep(5)
376         os_utils.delete_instance(nova_client=os_utils.get_nova_client(),
377                                  instance_id=self.ob_instance_id)
378         # TODO question is the clean removing also the VM?
379         # I think so since is goinf to remove the tenant...
380         super(ImsVnf, self).clean()
381
382     def main(self, **kwargs):
383         self.logger.info("Orchestra IMS VNF onboarding test starting")
384         self.execute()
385         self.logger.info("Orchestra IMS VNF onboarding test executed")
386         if self.criteria is "PASS":
387             return self.EX_OK
388         else:
389             return self.EX_RUN_ERROR
390
391     def run(self):
392         kwargs = {}
393         return self.main(**kwargs)
394
395
396 if __name__ == '__main__':
397     test = ImsVnf()
398     test.deploy_orchestrator()
399     test.deploy_vnf()
400     test.clean()