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 | |
admin | beea003 | 2014-01-23 14:54:13 -0800 | [diff] [blame^] | 96 | def find_host(self,RestIP,RestPort,RestAPI,hostIP): |
admin | f73f051 | 2013-12-17 16:33:37 -0800 | [diff] [blame] | 97 | retcode = 0 |
admin | beea003 | 2014-01-23 14:54:13 -0800 | [diff] [blame^] | 98 | retswitch = '' |
| 99 | retport = '' |
| 100 | retmac = '' |
| 101 | foundIP = '' |
admin | f73f051 | 2013-12-17 16:33:37 -0800 | [diff] [blame] | 102 | ##### 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) |
admin | beea003 | 2014-01-23 14:54:13 -0800 | [diff] [blame^] | 109 | # print parsedResult |
admin | f73f051 | 2013-12-17 16:33:37 -0800 | [diff] [blame] | 110 | except: |
| 111 | print "REST IF %s has issue" % command |
| 112 | parsedResult = "" |
admin | f73f051 | 2013-12-17 16:33:37 -0800 | [diff] [blame] | 113 | |
| 114 | if parsedResult == "": |
| 115 | return (retcode, "Rest API has an error") |
| 116 | else: |
admin | beea003 | 2014-01-23 14:54:13 -0800 | [diff] [blame^] | 117 | 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) |
admin | f73f051 | 2013-12-17 16:33:37 -0800 | [diff] [blame] | 131 | |