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