admin | f73f051 | 2013-12-17 16:33:37 -0800 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | ''' |
| 3 | Created 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 | |
| 22 | onosrestapidriver is the basic driver which will handle the onorestapi functions |
| 23 | ''' |
| 24 | |
| 25 | import struct |
| 26 | import fcntl |
| 27 | import os |
| 28 | import signal |
| 29 | import re |
| 30 | import sys |
| 31 | import time |
| 32 | import json |
| 33 | |
| 34 | sys.path.append("../") |
| 35 | from drivers.common.apidriver import API |
| 36 | import urllib |
| 37 | import __builtin__ |
| 38 | |
| 39 | |
| 40 | class 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 | |
| 96 | def find_host(self,RestIP,RestPort,RestAPI,hostMAC): |
| 97 | retcode = 0 |
| 98 | ##### device rest API is: 'host:8080/wm/core/topology/switches/all/json' ### |
| 99 | url ="http://%s:%s%s" %(RestIP,RestPort,RestAPI) |
| 100 | |
| 101 | try: |
| 102 | command = "curl -s %s" % (url) |
| 103 | result = os.popen(command).read() |
| 104 | parsedResult = json.loads(result) |
| 105 | print parsedResult |
| 106 | except: |
| 107 | print "REST IF %s has issue" % command |
| 108 | parsedResult = "" |
| 109 | if type(parsedResult) == 'dict' and parsedResult.has_key('code'): |
| 110 | print "REST %s returned code %s" % (command, parsedResult['code']) |
| 111 | parsedResult = "" |
| 112 | |
| 113 | if parsedResult == "": |
| 114 | return (retcode, "Rest API has an error") |
| 115 | else: |
| 116 | found = [item for item in parsedResult if item['mac'] == [str(hostMAC)]] |
| 117 | retcode = 1 |
| 118 | return (retcode, found) |
| 119 | |