blob: 39c649cd15d65c3427e6ae9b1d65be693cfe4a05 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
2'''
3Created on 4-Jun-2013
4
5@author: Anil Kumar (anilkumar.s@paxterrasolutions.com)
6
7
8 TestON is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 2 of the License, or
11 (at your option) any later version.
12
13 TestON is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with TestON. If not, see <http://www.gnu.org/licenses/>.
20
21
22onosrestapidriver is the basic driver which will handle the onorestapi functions
23'''
24
25import struct
26import fcntl
27import os
28import signal
29import re
30import sys
31import time
admin530b4c92013-08-14 16:54:35 -070032import json
adminbae64d82013-08-01 10:50:15 -070033
34sys.path.append("../")
35from drivers.common.apidriver import API
36import urllib
37import __builtin__
38
39
40class OnosRestApiDriver(API):
41
42 def __init__(self):
43 super(API, self).__init__()
44
45
46 def connect(self,**connectargs):
47 for key in connectargs:
48 vars(self)[key] = connectargs[key]
49
50 self.name = self.options['name']
51 self.handle = super(OnosRestApiDriver,self).connect()
52 main.log.info(self.options['topology_url'])
53 try :
54 self.handle = urllib.urlopen(self.options['topology_url'])
55 except Exception,e:
56 main.log.error(e)
57
58 self.logFileName = main.logdir+"/"+self.name+".session"
59
60 if self.handle:
61 return self.handle
62 else :
63 return main.FALSE
64
65 def execute(self):
66 main.log.info(self.options['topology_url'])
67 response = main.FALSE
68 for i in [1,2] :
69 time.sleep(2)
70 response = self.http_request()
71 return response
72
73 def http_request(self):
74 try :
75 self.handle = urllib.urlopen(self.options['topology_url'])
76
77 resonse_lines = self.handle.readlines()
78 print resonse_lines
79 return resonse_lines
80 except Exception,e:
81 main.log.error(e)
82 return "url error"
83
84 def disconnect(self,handle):
85 response = ''
86 '''
87 if self.handle:
88 self.handle = handle
89 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
90 else :
91 main.log.error("Connection failed to the host")
92 response = main.FALSE
93 '''
94 return response
95
admin68453302013-12-16 15:40:04 -080096 def find_host(self,RestIP,RestPort,RestAPI,hostIP):
admin530b4c92013-08-14 16:54:35 -070097 retcode = 0
adminbeea0032014-01-23 14:54:13 -080098 retswitch = []
99 retport = []
100 retmac = []
101 foundIP = []
admin68453302013-12-16 15:40:04 -0800102 ##### device rest API is: 'host:8080/wm/core/topology/switches/all/json' ###
admin530b4c92013-08-14 16:54:35 -0700103 url ="http://%s:%s%s" %(RestIP,RestPort,RestAPI)
104
105 try:
106 command = "curl -s %s" % (url)
107 result = os.popen(command).read()
108 parsedResult = json.loads(result)
admin68453302013-12-16 15:40:04 -0800109 # print parsedResult
admin530b4c92013-08-14 16:54:35 -0700110 except:
111 print "REST IF %s has issue" % command
112 parsedResult = ""
admin530b4c92013-08-14 16:54:35 -0700113
114 if parsedResult == "":
115 return (retcode, "Rest API has an error")
116 else:
admin68453302013-12-16 15:40:04 -0800117 for switch in enumerate(parsedResult):
118 for port in enumerate(switch[1]['ports']):
119 if ( port[1]['devices'] != [] ):
120 try:
121 foundIP = port[1]['devices'][0]['ipv4addresses'][0]['ipv4']
122 except:
123 print "Error in detecting IP address."
124 if foundIP == hostIP:
adminbeea0032014-01-23 14:54:13 -0800125 retswitch.append(switch[1]['dpid'])
126 retport.append(port[1]['desc'])
127 retmac.append(port[1]['devices'][0]['mac'])
admin68453302013-12-16 15:40:04 -0800128 retcode = retcode +1
admin68453302013-12-16 15:40:04 -0800129 foundIP =''
130 return(retcode, retswitch, retport, retmac)
admin530b4c92013-08-14 16:54:35 -0700131