blob: 4af260310c309f9d21aef8d01cd593694e0358cc [file] [log] [blame]
adminf73f0512013-12-17 16:33:37 -08001#!/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
32import json
33
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
adminbeea0032014-01-23 14:54:13 -080096 def find_host(self,RestIP,RestPort,RestAPI,hostIP):
adminf73f0512013-12-17 16:33:37 -080097 retcode = 0
adminbeea0032014-01-23 14:54:13 -080098 retswitch = ''
99 retport = ''
100 retmac = ''
101 foundIP = ''
adminf73f0512013-12-17 16:33:37 -0800102 ##### device rest API is: 'host:8080/wm/core/topology/switches/all/json' ###
103 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)
adminbeea0032014-01-23 14:54:13 -0800109 # print parsedResult
adminf73f0512013-12-17 16:33:37 -0800110 except:
111 print "REST IF %s has issue" % command
112 parsedResult = ""
adminf73f0512013-12-17 16:33:37 -0800113
114 if parsedResult == "":
115 return (retcode, "Rest API has an error")
116 else:
adminbeea0032014-01-23 14:54:13 -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:
125 retcode = retcode +1
126 retswitch = switch[1]['dpid']
127 retport = port[1]['desc']
128 retmac = port[1]['devices'][0]['mac']
129 foundIP =''
130 return(retcode, retswitch, retport, retmac)
adminf73f0512013-12-17 16:33:37 -0800131