blob: fd00bb40cf9355ef19ce882706cb9f6d5137182e [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
admin530b4c92013-08-14 16:54:35 -070096 def find_host(self,RestIP,RestPort,RestAPI,hostMAC):
97 retcode = 0
98 url ="http://%s:%s%s" %(RestIP,RestPort,RestAPI)
99
100 try:
101 command = "curl -s %s" % (url)
102 result = os.popen(command).read()
103 parsedResult = json.loads(result)
104 print parsedResult
105 except:
106 print "REST IF %s has issue" % command
107 parsedResult = ""
108 if type(parsedResult) == 'dict' and parsedResult.has_key('code'):
109 print "REST %s returned code %s" % (command, parsedResult['code'])
110 parsedResult = ""
111
112 if parsedResult == "":
113 return (retcode, "Rest API has an error")
114 else:
115 found = [item for item in parsedResult if item['mac'] == [str(hostMAC)]]
116 retcode = 1
117 return (retcode, found)
118