Put vnf_test pass if more than 80% of vnf test result are OK
[functest.git] / functest / opnfv_tests / sdn / onos / sfc / sfc_onos.py
1 import logging
2 import os
3 import re
4 import time
5 import json
6 import requests
7
8 from multiprocessing import Process
9 from multiprocessing import Queue
10 from pexpect import pxssh
11
12 from functest.utils.constants import CONST
13
14 OK = 200
15 CREATED = 201
16 ACCEPTED = 202
17 NO_CONTENT = 204
18
19
20 class SfcOnos(object):
21     """Defines all the def function of SFC."""
22
23     def __init__(self):
24         """Initialization of variables."""
25         self.logger = logging.getLogger(__name__)
26         self.osver = "v2.0"
27         self.token_id = 0
28         self.net_id = 0
29         self.image_id = 0
30         self.keystone_hostname = 'keystone_ip'
31         self.neutron_hostname = 'neutron_ip'
32         self.nova_hostname = 'nova_ip'
33         self.glance_hostname = 'glance_ip'
34         self.onos_hostname = 'onos_ip'
35         # Network variables #######
36         self.netname = "test_nw"
37         self.admin_state_up = True
38         self.tenant_id = 0
39         self.subnetId = 0
40         # #########################
41         # SubNet variables#########
42         self.ip_version = 4
43         self.cidr = "20.20.20.0/24"
44         self.subnetname = "test_nw_subnets"
45         # ###############################
46         # Port variable
47         self.port = "port"
48         self.port_num = []
49         self.vm_id = 0
50         self.port_ip = []
51         self.count = 0
52         self.i = 0
53         self.numTerms = 3
54         self.security_groups = []
55         self.port_security_enabled = False
56         # ###############################
57         # VM creation variable
58         self.container_format = "bare"
59         self.disk_format = "qcow2"
60         self.imagename = "TestSfcVm"
61         self.createImage = ("/home/root1/devstack/files/images/"
62                             "firewall_block_image.img")
63
64         self.vm_name = "vm"
65         self.imageRef = "test"
66         self.flavorRef = "1"
67         self.max_count = "1"
68         self.min_count = "1"
69         self.org_nw_port = []
70         self.image_id = 0
71         self.routername = "router1"
72         self.router_id = 0
73         # #####################################
74         # Port pair
75         self.port_pair_ingress = 0
76         self.port_pair_egress = 0
77         self.port_pair_name = "PP"
78         self.port_pair_id = []
79         # ####################################
80         # Port Group
81         self.port_group_name = "PG"
82         self.port_grp_id = []
83         # ####################################
84         # FlowClassifier
85         self.source_ip_prefix = "20.20.20.0/24"
86         self.destination_ip_prefix = "20.20.20.0/24"
87         self.logical_source_port = 0
88         self.fcname = "FC"
89         self.ethertype = "IPv4"
90         # #####################################
91         self.flow_class_if = 0
92         # #####################################
93         # Port Chain variables
94         self.pcname = 'PC'
95         self.PC_id = 0
96         # #####################################
97         # Port Chain variables
98         self.flowadd = ''
99         # #####################################
100         self.ip_pool = 0
101         self.vm_public_ip = []
102         self.vm_public_id = []
103         self.cirros_username = CONST.__getattribute__(
104             'openstack_image_username')
105         self.cirros_password = CONST.__getattribute__(
106             'openstack_image_password')
107         self.net_id1 = 0
108         self.vm = []
109         self.address = 0
110         self.value = 0
111         self.pub_net_id = 0
112
113     def getToken(self):
114         """Get the keystone token value from Openstack ."""
115         url = 'http://%s:5000/%s/tokens' % (self.keystone_hostname,
116                                             self.osver)
117         data = ('{"auth": {"tenantName": "admin", "passwordCredentials":'
118                 '{ "username": "admin", "password": "console"}}}')
119         headers = {"Accept": "application/json"}
120         response = requests.post(url, headers=headers, data=data)
121         if (response.status_code == OK):
122             json1_data = json.loads(response.content)
123             self.logger.debug(response.status_code)
124             self.logger.debug(response.content)
125             self.logger.debug(json1_data)
126             self.token_id = json1_data['access']['token']['id']
127             self.tenant_id = json1_data['access']['token']['tenant']['id']
128             return(response.status_code)
129         else:
130             return(response.status_code)
131
132     def createNetworks(self):
133         """Creation of networks."""
134         Dicdata = {}
135         if self.netname != '':
136             Dicdata['name'] = self.netname
137         if self.admin_state_up != '':
138             Dicdata['admin_state_up'] = self.admin_state_up
139         Dicdata = {'network': Dicdata}
140         data = json.dumps(Dicdata, indent=4)
141         url = 'http://%s:9696/%s/networks' % (self.neutron_hostname,
142                                               self.osver)
143         headers = {"Accept": "application/json",
144                    "X-Auth-Token": self.token_id}
145         response = requests.post(url, headers=headers, data=data)
146         if (response.status_code == CREATED):
147             self.logger.debug(response.status_code)
148             self.logger.debug(response.content)
149
150             json1_data = json.loads(response.content)
151             self.logger.debug(json1_data)
152             self.net_id = json1_data['network']['id']
153             return(response.status_code)
154         else:
155             return(response.status_code)
156
157     def createSubnets(self):
158         """Creation of SubNets."""
159         Dicdata = {}
160         if self.net_id != 0:
161             Dicdata['network_id'] = self.net_id
162         if self.ip_version != '':
163             Dicdata['ip_version'] = self.ip_version
164         if self.cidr != '':
165             Dicdata['cidr'] = self.cidr
166         if self.subnetname != '':
167             Dicdata['name'] = self.subnetname
168
169         Dicdata = {'subnet': Dicdata}
170         data = json.dumps(Dicdata, indent=4)
171         url = 'http://%s:9696/%s/subnets' % (self.neutron_hostname,
172                                              self.osver)
173         headers = {"Accept": "application/json",
174                    "X-Auth-Token": self.token_id}
175         response = requests.post(url, headers=headers, data=data)
176
177         if (response.status_code == CREATED):
178             self.logger.debug(response.status_code)
179             self.logger.debug(response.content)
180             json1_data = json.loads(response.content)
181             self.logger.debug(json1_data)
182             self.subnetId = json1_data['subnet']['id']
183             return(response.status_code)
184         else:
185             return(response.status_code)
186
187     def createPorts(self):
188         """Creation of Ports."""
189         for x in range(self.i, self.numTerms):
190             Dicdata = {}
191             if self.net_id != '':
192                 Dicdata['network_id'] = self.net_id
193             if self.port != '':
194                 Dicdata['name'] = "port" + str(x)
195             if self.admin_state_up != '':
196                 Dicdata['admin_state_up'] = self.admin_state_up
197             if self.security_groups != '':
198                 Dicdata['security_groups'] = self.security_groups
199             # if self.port_security_enabled != '':
200             #    Dicdata['port_security_enabled'] = self.port_security_enabled
201
202             Dicdata = {'port': Dicdata}
203             data = json.dumps(Dicdata, indent=4)
204             url = 'http://%s:9696/%s/ports' % (self.neutron_hostname,
205                                                self.osver)
206             headers = {"Accept": "application/json",
207                        "X-Auth-Token": self.token_id}
208             response = requests.post(url, headers=headers, data=data)
209
210             if (response.status_code == CREATED):
211                 self.logger.debug(response.status_code)
212                 self.logger.debug(response.content)
213
214                 json1_data = json.loads(response.content)
215                 self.logger.debug(json1_data)
216                 self.port_num.append(json1_data['port']['id'])
217                 self.port_ip.append(json1_data['port']['fixed_ips'][0]
218                                     ['ip_address'])
219             else:
220                 return(response.status_code)
221         return(response.status_code)
222
223     def createVm(self):
224         """Creation of Instance, using  firewall image."""
225         url = ("http://%s:9292/v2/images?"
226                "name=TestSfcVm" % (self.glance_hostname))
227         headers = {"Accept": "application/json",
228                    "Content-Type": "application/octet-stream",
229                    "X-Auth-Token": self.token_id}
230         response = requests.get(url, headers=headers)
231         if (response.status_code == OK):
232             self.logger.debug(response.status_code)
233             self.logger.debug(response.content)
234             self.logger.info("FireWall Image is available")
235             json1_data = json.loads(response.content)
236             self.logger.debug(json1_data)
237             self.image_id = json1_data['images'][0]['id']
238         else:
239             return(response.status_code)
240
241         url = ("http://%s:8774/v2.1/%s/flavors?"
242                "name=m1.tiny" % (self.nova_hostname, self.tenant_id))
243         headers = {"Accept": "application/json", "Content-Type":
244                    "application/json", "X-Auth-Token": self.token_id}
245         response = requests.get(url, headers=headers)
246
247         if (response.status_code == OK):
248             self.logger.debug(response.status_code)
249             self.logger.debug(response.content)
250             self.logger.info("Flavor is available")
251             json1_data = json.loads(response.content)
252             self.logger.debug(json1_data)
253             self.flavorRef = json1_data['flavors'][0]['id']
254         else:
255             return(response.status_code)
256
257         for y in range(0, 3):
258             Dicdata = {}
259             org_nw_port = []
260             org_nw_port.append({'port': self.port_num[y]})
261             if self.vm_name != '':
262                 Dicdata['name'] = "vm" + str(y)
263             if self.imageRef != '':
264                 Dicdata['imageRef'] = self.image_id
265             if self.flavorRef != '':
266                 Dicdata['flavorRef'] = self.flavorRef
267             if self.max_count != '':
268                 Dicdata['max_count'] = self.max_count
269             if self.min_count != '':
270                 Dicdata['min_count'] = self.min_count
271             if self.org_nw_port != '':
272                 Dicdata['networks'] = org_nw_port
273             Dicdata = {'server': Dicdata}
274             data = json.dumps(Dicdata, indent=4)
275             url = 'http://%s:8774/v2.1/%s/servers' % (self.nova_hostname,
276                                                       self.tenant_id)
277             headers = {"Accept": "application/json", "Content-Type":
278                        "application/json", "X-Auth-Token": self.token_id}
279             response = requests.post(url, headers=headers, data=data)
280             if (response.status_code == ACCEPTED):
281                 self.logger.debug(response.status_code)
282                 self.logger.debug(response.content)
283                 info = "Creation of VM" + str(y) + " is successfull"
284                 self.logger.debug(info)
285
286                 json1_data = json.loads(response.content)
287                 self.logger.debug(json1_data)
288                 self.vm_id = json1_data['server']['id']
289                 self.vm.append(json1_data['server']['id'])
290             else:
291                 return(response.status_code)
292
293         return(response.status_code)
294
295     def checkVmState(self):
296         """Checking the Status of the Instance."""
297         time.sleep(10)
298         for y in range(0, 3):
299             url = ("http://%s:8774/v2.1/servers/"
300                    "detail?name=vm" + str(y)) % (self.neutron_hostname)
301             headers = {"Accept": "application/json",
302                        "X-Auth-Token": self.token_id}
303             response = requests.get(url, headers=headers)
304             if (response.status_code == OK):
305                 self.logger.debug(response.status_code)
306                 self.logger.debug(response.content)
307                 json1_data = json.loads(response.content)
308                 self.logger.debug(json1_data)
309                 self.vm_active = json1_data['servers'][0]['status']
310                 if (self.vm_active == "ACTIVE"):
311                     info = "VM" + str(y) + " is Active : " + self.vm_active
312                 else:
313                     info = "VM" + str(y) + " is NOT Active : " + self.vm_active
314                 self.logger.debug(info)
315             else:
316                 return(response.status_code)
317         return(response.status_code)
318         time.sleep(10)
319
320     def createPortPair(self):
321         """Creation of Port Pair."""
322         for p in range(1, 2):
323             Dicdata = {}
324             if self.port_pair_ingress != '':
325                 Dicdata['ingress'] = self.port_num[p]
326             if self.port_pair_egress != '':
327                 egress = p
328                 Dicdata['egress'] = self.port_num[egress]
329             if self.port_pair_name != '':
330                 Dicdata['name'] = "PP" + str(p)
331
332             Dicdata = {'port_pair': Dicdata}
333             data = json.dumps(Dicdata, indent=4)
334             url = 'http://%s:9696/%s/sfc/port_pairs' % (self.neutron_hostname,
335                                                         self.osver)
336             headers = {"Accept": "application/json", "X-Auth-Token":
337                        self.token_id}
338             response = requests.post(url, headers=headers, data=data)
339             if (response.status_code == CREATED):
340                 info = ("Creation of Port Pair PP" + str(p) +
341                         " is successful")
342                 self.logger.debug(info)
343             else:
344                 return(response.status_code)
345
346         return(response.status_code)
347
348     def getPortPair(self):
349         """Query the Portpair id value."""
350         for p in range(0, 1):
351             url = ("http://%s:9696/%s/"
352                    "sfc/port_pairs?name=PP1" % (self.neutron_hostname,
353                                                 self.osver))
354             headers = {"Accept": "application/json",
355                        "X-Auth-Token": self.token_id}
356             response = requests.get(url, headers=headers)
357
358             if (response.status_code == OK):
359                 self.logger.debug(response.status_code)
360                 self.logger.debug(response.content)
361                 json1_data = json.loads(response.content)
362                 self.logger.debug(json1_data)
363                 self.port_pair_id.append(json1_data['port_pairs'][0]['id'])
364             else:
365                 return(response.status_code)
366         return(response.status_code)
367
368     def createPortGroup(self):
369         """Creation of PortGroup."""
370         for p in range(0, 1):
371             Dicdata = {}
372             port_pair_list = []
373             port_pair_list.append(self.port_pair_id[p])
374             if self.port_group_name != '':
375                 Dicdata['name'] = "PG" + str(p)
376             if self.port_pair_id != '':
377                 Dicdata['port_pairs'] = port_pair_list
378
379             Dicdata = {'port_pair_group': Dicdata}
380             data = json.dumps(Dicdata, indent=4)
381             url = ("http://%s:9696/%s/"
382                    "sfc/port_pair_groups" % (self.neutron_hostname,
383                                              self.osver))
384             headers = {"Accept": "application/json", "X-Auth-Token":
385                        self.token_id}
386             response = requests.post(url, headers=headers, data=data)
387             if (response.status_code == CREATED):
388                 info = ("Creation of Port Group PG" + str(p) +
389                         "is successful")
390                 self.logger.debug(info)
391             else:
392                 return(response.status_code)
393
394         return(response.status_code)
395
396     def getPortGroup(self):
397         """Query the PortGroup id."""
398         for p in range(0, 1):
399             url = ("http://%s:9696/%s/sfc/port_pair_groups"
400                    "?name=PG" + str(p)) % (self.neutron_hostname,
401                                            self.osver)
402             headers = {"Accept": "application/json", "X-Auth-Token":
403                        self.token_id}
404             response = requests.get(url, headers=headers)
405
406             if (response.status_code == OK):
407                 self.logger.debug(response.status_code)
408                 self.logger.debug(response.content)
409                 json1_data = json.loads(response.content)
410                 self.port_grp_id.append(json1_data['port_pair_groups']
411                                         [0]['id'])
412             else:
413                 return(response.status_code)
414         return(response.status_code)
415
416     def createFlowClassifier(self):
417         """Creation of Flow Classifier."""
418         Dicdata = {}
419         if self.source_ip_prefix != '':
420             Dicdata['source_ip_prefix'] = self.source_ip_prefix
421         if self.destination_ip_prefix != '':
422             Dicdata['destination_ip_prefix'] = self.destination_ip_prefix
423         if self.logical_source_port != '':
424             Dicdata['logical_source_port'] = self.port_num[0]
425         if self.fcname != '':
426             Dicdata['name'] = "FC1"
427         if self.ethertype != '':
428             Dicdata['ethertype'] = self.ethertype
429
430         Dicdata = {'flow_classifier': Dicdata}
431         data = json.dumps(Dicdata, indent=4)
432         url = ("http://%s:9696/%s/"
433                "sfc/flow_classifiers" % (self.neutron_hostname,
434                                          self.osver))
435         headers = {"Accept": "application/json",
436                    "X-Auth-Token": self.token_id}
437         response = requests.post(url, headers=headers, data=data)
438         if (response.status_code == CREATED):
439             json1_data = json.loads(response.content)
440             self.flow_class_if = json1_data['flow_classifier']['id']
441             self.logger.debug("Creation of Flow Classifier is successful")
442             return(response.status_code)
443         else:
444             return(response.status_code)
445
446     def createPortChain(self):
447         """Creation of PortChain."""
448         Dicdata = {}
449         flow_class_list = []
450         flow_class_list.append(self.flow_class_if)
451         port_pair_groups_list = []
452         port_pair_groups_list.append(self.port_grp_id[0])
453
454         if flow_class_list != '':
455             Dicdata['flow_classifiers'] = flow_class_list
456         if self.pcname != '':
457             Dicdata['name'] = "PC1"
458         if port_pair_groups_list != '':
459             Dicdata['port_pair_groups'] = port_pair_groups_list
460
461         Dicdata = {'port_chain': Dicdata}
462         data = json.dumps(Dicdata, indent=4)
463         url = 'http://%s:9696/%s/sfc/port_chains' % (self.neutron_hostname,
464                                                      self.osver)
465         headers = {"Accept": "application/json",
466                    "Content-Type": "application/json",
467                    "X-Auth-Token": self.token_id}
468         response = requests.post(url, headers=headers, data=data)
469         if (response.status_code == CREATED):
470             self.logger.debug("Creation of PORT CHAIN is successful")
471             json1_data = json.loads(response.content)
472             self.PC_id = json1_data['port_chain']['id']
473             return(response.status_code)
474         else:
475             return(response.status_code)
476
477     def checkFlowAdded(self):
478         """Check whether the Flows are downloaded successfully."""
479         time.sleep(5)
480         response = requests.get('http://' + self.onos_hostname +
481                                 ':8181/onos/v1/flows',
482                                 auth=("karaf", "karaf"))
483         if (response.status_code == OK):
484             self.logger.debug("Flow is successfully Queries")
485             json1_data = json.loads(response.content)
486             self.flowadd = json1_data['flows'][0]['state']
487
488             if (self.flowadd == "ADDED"):
489                 self.logger.info("Flow is successfully added to OVS")
490                 return(response.status_code)
491             else:
492                 return(404)
493         else:
494             return(response.status_code)
495 ####################################################################
496
497     def createRouter(self):
498         """Creation of Router."""
499         Dicdata = {}
500         if self.routername != '':
501             Dicdata['name'] = "router1"
502         if self.admin_state_up != '':
503             Dicdata['admin_state_up'] = self.admin_state_up
504
505         Dicdata = {'router': Dicdata}
506         data = json.dumps(Dicdata, indent=4)
507         url = 'http://%s:9696/%s/routers.json' % (self.neutron_hostname,
508                                                   self.osver)
509         headers = {"Accept": "application/json",
510                    "X-Auth-Token": self.token_id}
511         response = requests.post(url, headers=headers, data=data)
512         if (response.status_code == CREATED):
513             self.logger.debug(response.status_code)
514             self.logger.debug(response.content)
515             self.logger.debug("Creation of Router is successfull")
516             json1_data = json.loads(response.content)
517             self.logger.debug(json1_data)
518             self.router_id = json1_data['router']['id']
519             return(response.status_code)
520         else:
521             return(response.status_code)
522
523     def attachInterface(self):
524         """Attachment of instance ports to the Router."""
525         url = ("http://%s:9696/%s/networks"
526                "?name=admin_floating_net" % (self.neutron_hostname,
527                                              self.osver))
528         headers = {"Accept": "application/json", "X-Auth-Token": self.token_id}
529         response = requests.get(url, headers=headers)
530         if (response.status_code == OK):
531             self.logger.debug(response.status_code)
532             self.logger.debug(response.content)
533             json1_data = json.loads(response.content)
534             self.logger.debug(json1_data)
535             self.net_name = json1_data['networks'][0]['name']
536             if (self.net_name == "admin_floating_net"):
537                 self.pub_net_id = json1_data['networks'][0]['id']
538             else:
539                 return(response.status_code)
540         ############################################################
541
542         self.logger.info("Attachment of Instance interface to Router")
543         Dicdata = {}
544         if self.subnetId != '':
545             Dicdata['subnet_id'] = self.subnetId
546
547         data = json.dumps(Dicdata, indent=4)
548         url = ("http://%s:9696/%s/routers"
549                "/%s/add_router_interface" % (self.neutron_hostname,
550                                              self.osver,
551                                              self.router_id))
552         headers = {"Accept": "application/json",
553                    "X-Auth-Token": self.token_id}
554         response = requests.put(url, headers=headers, data=data)
555         if (response.status_code == OK):
556             self.logger.debug(response.status_code)
557             self.logger.debug(response.content)
558             self.logger.info("Interface attached successfull")
559         else:
560             return(response.status_code)
561         ############################################################
562         self.logger.info("Attachment of Gateway to Router")
563
564         Dicdata1 = {}
565         if self.pub_net_id != 0:
566             Dicdata1['network_id'] = self.pub_net_id
567
568         Dicdata1 = {'external_gateway_info': Dicdata1}
569         Dicdata1 = {'router': Dicdata1}
570         data = json.dumps(Dicdata1, indent=4)
571         url = 'http://%s:9696/%s/routers/%s' % (self.neutron_hostname,
572                                                 self.osver,
573                                                 self.router_id)
574         headers = {"Accept": "application/json",
575                    "X-Auth-Token": self.token_id}
576         response = requests.put(url, headers=headers, data=data)
577         if (response.status_code == OK):
578             self.logger.debug(response.status_code)
579             self.logger.debug(response.content)
580             self.logger.info("Gateway Interface attached successfull")
581             return(response.status_code)
582         else:
583             return(response.status_code)
584
585     def addFloatingIp(self):
586         """Attachment of Floating Ip to the Router."""
587         for ip_num in range(0, 2):
588             Dicdata = {}
589             Dicdata['pool'] = "admin_floating_net"
590
591             data = json.dumps(Dicdata, indent=4)
592             url = ("http://%s:8774/v2.1/"
593                    "os-floating-ips" % (self.nova_hostname))
594             headers = {"Accept": "application/json",
595                        "X-Auth-Token": self.token_id}
596             response = requests.post(url, headers=headers, data=data)
597             if (response.status_code == OK):
598                 self.logger.debug(response.status_code)
599                 self.logger.debug(response.content)
600                 self.logger.info("Floating ip created successfully")
601                 json1_data = json.loads(response.content)
602                 self.logger.debug(json1_data)
603                 self.vm_public_ip.append(json1_data['floating_ip']['ip'])
604                 self.vm_public_id.append(json1_data['floating_ip']['id'])
605             else:
606                 self.logger.error("Floating ip NOT created successfully")
607
608             Dicdata1 = {}
609             if self.address != '':
610                 Dicdata1['address'] = self.vm_public_ip[ip_num]
611
612             Dicdata1 = {'addFloatingIp': Dicdata1}
613             data = json.dumps(Dicdata1, indent=4)
614             url = ("http://%s:8774/v2.1/"
615                    "servers/%s/action" % (self.nova_hostname,
616                                           self.vm[ip_num]))
617
618             headers = {"Accept": "application/json",
619                        "X-Auth-Token": self.token_id}
620             response = requests.post(url, headers=headers, data=data)
621             if(response.status_code == ACCEPTED):
622                 self.logger.debug(response.status_code)
623                 self.logger.debug(response.content)
624                 self.logger.info("Public Ip successfully added to VM")
625             else:
626                 return(response.status_code)
627         return(response.status_code)
628
629     def loginToVM(self):
630         """Login to the VM to check NSH packets are received."""
631         queue1 = "0"
632
633         def vm0():
634
635             s = pxssh.pxssh()
636             hostname = self.vm_public_ip[0]
637             s.login(hostname, self.cirros_username, self.cirros_password)
638             s.sendline("ping -c 5 " + str(self.port_ip[2]))
639             s.prompt()             # match the prompt
640
641             ping_re = re.search("transmitted.*received", s.before).group()
642             x = re.split('\s+', ping_re)
643             if (x[1] >= "1"):
644                 self.logger.info("Ping is Successfull")
645             else:
646                 self.logger.info("Ping is NOT Successfull")
647
648         def vm1(queue1):
649             s = pxssh.pxssh()
650             hostname = self.vm_public_ip[1]
651             s.login(hostname, self.cirros_username, self.cirros_password)
652             s.sendline('sudo ./firewall')
653             s.prompt()
654             output_pack = s.before
655
656             if(output_pack.find("nshc") != -1):
657                 self.logger.info("The packet has reached VM2 Instance")
658                 queue1.put("1")
659             else:
660                 self.logger.info("Packet not received in Instance")
661                 queue1.put("0")
662
663         def ping(ip, timeout=300):
664             while True:
665                 time.sleep(1)
666                 self.logger.debug("Pinging %s. Waiting for response..." % ip)
667                 response = os.system("ping -c 1 " + ip + " >/dev/null 2>&1")
668                 if response == 0:
669                     self.logger.info("Ping " + ip + " detected!")
670                     return 0
671
672                 elif timeout == 0:
673                     self.logger.info("Ping " + ip + " timeout reached.")
674                     return 1
675                 timeout -= 1
676
677         result0 = ping(self.vm_public_ip[0])
678         result1 = ping(self.vm_public_ip[1])
679         if result0 == 0 and result1 == 0:
680             time.sleep(300)
681             queue1 = Queue()
682             p1 = Process(target=vm1, args=(queue1, ))
683             p1.start()
684             p2 = Process(target=vm0)
685             p2.start()
686             p1.join(10)
687             return (queue1.get())
688         else:
689             self.logger.error("Thread didnt run")
690
691     """##################################################################"""
692     """ ########################  Stats Functions ################# #####"""
693
694     def portChainDeviceMap(self):
695         """Check the PC Device Stats in the ONOS."""
696         response = requests.get('http://' + self.onos_hostname +
697                                 ':8181/onos/vtn/portChainDeviceMap/' +
698                                 self.PC_id, auth=("karaf", "karaf"))
699         if (response.status_code == OK):
700             self.logger.info("PortChainDeviceMap is successfully Queries")
701             return(response.status_code)
702         else:
703             return(response.status_code)
704
705     def portChainSfMap(self):
706         """Check the PC SF Map Stats in the ONOS."""
707         response = requests.get('http://' + self.onos_hostname +
708                                 ':8181/onos/vtn/portChainSfMap/' +
709                                 self.PC_id, auth=("karaf", "karaf"))
710         if (response.status_code == OK):
711             self.logger.info("portChainSfMap is successfully Queries")
712             return(response.status_code)
713         else:
714             return(response.status_code)
715
716     """###################################################################"""
717
718     def deletePortChain(self):
719         """Deletion of PortChain."""
720         url = ('http://' + self.neutron_hostname + ':9696/' +
721                self.osver + '/sfc/port_chains/' + self.PC_id)
722         headers = {"Accept": "application/json", "Content-Type":
723                    "application/json", "X-Auth-Token": self.token_id}
724         response = requests.delete(url, headers=headers)
725         if (response.status_code == OK):
726             self.logger.debug(response.status_code)
727             self.logger.debug(response.content)
728             return(response.status_code)
729         else:
730             return(response.status_code)
731
732     def deleteFlowClassifier(self):
733         """Deletion of Flow Classifier."""
734         url = ("http://%s:9696/%s/sfc/"
735                "flow_classifiers/%s" % (self.neutron_hostname,
736                                         self.osver,
737                                         self.flow_class_if))
738         headers = {"Accept": "application/json",
739                    "X-Auth-Token": self.token_id}
740         response = requests.delete(url, headers=headers)
741         if (response.status_code == OK):
742             self.logger.debug(response.status_code)
743             self.logger.debug(response.content)
744             return(response.status_code)
745         else:
746             return(response.status_code)
747
748     def deletePortGroup(self):
749         """Deletion of PortGroup."""
750         for p in range(0, 1):
751             url = ("http://%s:9696/%s/sfc/"
752                    "port_pair_groups/%s" % (self.neutron_hostname,
753                                             self.osver,
754                                             self.port_grp_id[p]))
755             headers = {"Accept": "application/json", "X-Auth-Token":
756                        self.token_id}
757             response = requests.delete(url, headers=headers)
758             if (response.status_code == NO_CONTENT):
759                 self.logger.debug(response.status_code)
760                 self.logger.debug(response.content)
761             else:
762                 return(response.status_code)
763         return(response.status_code)
764
765     def deletePortPair(self):
766         """Deletion of Portpair."""
767         for p in range(1, 2):
768             url = ("http://%s:9696/%s/sfc/"
769                    "port_pairs/%s" % (self.neutron_hostname,
770                                       self.osver,
771                                       self.port_pair_id[0]))
772             headers = {"Accept": "application/json",
773                        "X-Auth-Token": self.token_id}
774             response = requests.delete(url, headers=headers)
775             if (response.status_code == NO_CONTENT):
776                 self.logger.debug(response.status_code)
777                 self.logger.debug(response.content)
778             else:
779                 return(response.status_code)
780         return(response.status_code)
781
782     def cleanup(self):
783         """Cleanup."""
784         self.logger.info("Deleting VMs")
785         for y in range(0, 3):
786             url = ("http://%s:8774/v2.1/"
787                    "/servers/%s" % (self.nova_hostname,
788                                     self.vm[y]))
789             headers = {"Accept": "application/json",
790                        "X-Auth-Token": self.token_id}
791             response = requests.delete(url, headers=headers)
792             if (response.status_code == NO_CONTENT):
793                 self.logger.debug(response.status_code)
794                 self.logger.debug(response.content)
795                 self.logger.debug("VM" + str(y) + " is Deleted : ")
796                 time.sleep(10)
797             else:
798                 return(response.status_code)
799         self.logger.info("Deleting Ports")
800         for x in range(self.i, self.numTerms):
801             url = ('http://' + self.neutron_hostname + ':9696/' +
802                    self.osver + '/ports/' + self.port_num[x])
803             headers = {"Accept": "application/json", "X-Auth-Token":
804                        self.token_id}
805             response = requests.delete(url, headers=headers)
806
807             if (response.status_code == NO_CONTENT):
808                 self.logger.debug(response.status_code)
809                 self.logger.debug(response.content)
810                 self.logger.debug("Port" + str(x) + "  Deleted")
811             else:
812                 return(response.status_code)
813         self.logger.info("Deleting Router")
814
815         Dicdata = {}
816         Dicdata['external_gateway_info'] = {}
817         Dicdata = {'router': Dicdata}
818         data = json.dumps(Dicdata, indent=4)
819         url = ("http://%s:9696/%s/"
820                "/routers/%s" % (self.neutron_hostname,
821                                 self.osver,
822                                 self.router_id))
823         headers = {"Accept": "application/json",
824                    "X-Auth-Token": self.token_id}
825         response = requests.put(url, headers=headers, data=data)
826         if (response.status_code == OK):
827             self.logger.debug(response.status_code)
828             self.logger.debug(response.content)
829             Dicdata1 = {}
830             if self.subnetId != '':
831                 Dicdata1['subnet_id'] = self.subnetId
832             data = json.dumps(Dicdata1, indent=4)
833             url = ("http://%s:9696/%s/routers/%s"
834                    "/remove_router_interface.json" % (self.neutron_hostname,
835                                                       self.osver,
836                                                       self.router_id))
837             headers = {"Accept": "application/json",
838                        "X-Auth-Token": self.token_id}
839             response = requests.put(url, headers=headers, data=data)
840             if (response.status_code == OK):
841                 url = ("http://%s:9696/%s/"
842                        "routers/%s" % (self.neutron_hostname,
843                                        self.osver,
844                                        self.router_id))
845                 headers = {"Accept": "application/json",
846                            "X-Auth-Token": self.token_id}
847                 response = requests.delete(url, headers=headers)
848                 if (response.status_code == NO_CONTENT):
849                     self.logger.debug(response.status_code)
850                     self.logger.debug(response.content)
851                 else:
852                     return(response.status_code)
853             else:
854                 return(response.status_code)
855         else:
856             return(response.status_code)
857
858         self.logger.info("Deleting Network")
859         url = "http://%s:9696/%s/networks/%s" % (self.neutron_hostname,
860                                                  self.osver,
861                                                  self.net_id)
862
863         headers = {"Accept": "application/json",
864                    "X-Auth-Token": self.token_id}
865         response = requests.delete(url, headers=headers)
866         if (response.status_code == NO_CONTENT):
867             self.logger.debug(response.status_code)
868             self.logger.debug(response.content)
869         else:
870             return(response.status_code)
871
872         self.logger.info("Deleting Floating ip")
873         for ip_num in range(0, 2):
874             url = ("http://%s:9696/%s/floatingips"
875                    "/%s" % (self.neutron_hostname,
876                             self.osver,
877                             self.vm_public_id[ip_num]))
878
879             headers = {"Accept": "application/json", "X-Auth-Token":
880                        self.token_id}
881             response = requests.delete(url, headers=headers)
882             if (response.status_code == NO_CONTENT):
883                 self.logger.debug(response.status_code)
884                 self.logger.debug(response.content)
885             else:
886                 return(response.status_code)
887         return(response.status_code)