blob: 5e6dcdfef0ab13fa9e4cd1a81aee5ea40202e8cf [file] [log] [blame]
A.R Karthick95d044e2016-06-10 18:44:36 -07001#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002# Copyright 2016-present Ciena Corporation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
A.R Karthick95d044e2016-06-10 18:44:36 -07007#
Chetan Gaonkercfcce782016-05-10 10:10:42 -07008# http://www.apache.org/licenses/LICENSE-2.0
A.R Karthick95d044e2016-06-10 18:44:36 -07009#
Chetan Gaonkercfcce782016-05-10 10:10:42 -070010# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15#
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080016import json
17import requests
18import os,sys,time
A R Karthickb03cecd2016-07-27 10:27:55 -070019from OltConfig import OltConfig
20import fcntl, socket, struct
21
A R Karthick078e63a2016-07-28 13:59:31 -070022def get_mac(iface = None, pad = 4):
23 if iface is None:
24 iface = os.getenv('TEST_SWITCH', 'ovsbr0')
A R Karthickb03cecd2016-07-27 10:27:55 -070025 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
26 try:
27 info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(iface[:15])))
28 except:
29 info = ['0'] * 24
30 s.close()
31 sep = ''
32 if pad == 0:
33 sep = ':'
34 return '0'*pad + sep.join(['%02x' %ord(char) for char in info[18:24]])
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080035
A R Karthick2b93d6a2016-09-06 15:19:09 -070036def get_controller():
37 controller = os.getenv('ONOS_CONTROLLER_IP') or 'localhost'
38 controller = controller.split(',')[0]
39 return controller
40
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080041class OnosCtrl:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070042
43 auth = ('karaf', 'karaf')
A R Karthick2b93d6a2016-09-06 15:19:09 -070044 controller = get_controller()
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070045 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
A.R Karthick95d044e2016-06-10 18:44:36 -070046 maven_repo = 'http://central.maven.org/maven2/org/onosproject'
Chetan Gaonker93e302d2016-04-05 10:51:07 -070047 applications_url = 'http://%s:8181/onos/v1/applications' %(controller)
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -070048 host_cfg_url = 'http://%s:8181/onos/v1/hosts/' %(controller)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070049
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080050 def __init__(self, app, controller = None):
51 self.app = app
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070052 if controller is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080053 self.controller = controller
54 self.app_url = 'http://%s:8181/onos/v1/applications/%s' %(self.controller, self.app)
55 self.cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(self.controller)
56 self.auth = ('karaf', 'karaf')
57
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070058 @classmethod
ChetanGaonker689b3862016-10-17 16:25:01 -070059 def config(cls, config,controller=None):
A R Karthick4e0c0912016-08-17 16:57:42 -070060 if config is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080061 json_data = json.dumps(config)
ChetanGaonker689b3862016-10-17 16:25:01 -070062 if controller is None:
63 print('default Onos config url is %s'%cls.cfg_url)
64 resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data)
65 else:
66 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
67 print('non-default Onos config url is %s'%cfg_url)
68 resp = requests.post(cfg_url, auth = cls.auth, data = json_data)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080069 return resp.ok, resp.status_code
70 return False, 400
71
72 def activate(self):
73 resp = requests.post(self.app_url + '/active', auth = self.auth)
74 return resp.ok, resp.status_code
75
76 def deactivate(self):
77 resp = requests.delete(self.app_url + '/active', auth = self.auth)
78 return resp.ok, resp.status_code
79
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070080 @classmethod
81 def get_devices(cls):
82 url = 'http://%s:8181/onos/v1/devices' %(cls.controller)
83 result = requests.get(url, auth = cls.auth)
84 if result.ok:
85 devices = result.json()['devices']
86 return filter(lambda d: d['available'], devices)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080087
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070088 return None
89
90 @classmethod
A R Karthickb03cecd2016-07-27 10:27:55 -070091 def get_device_id(cls):
92 '''If running under olt, we get the first switch connected to onos'''
93 olt = OltConfig()
A R Karthick078e63a2016-07-28 13:59:31 -070094 did = 'of:' + get_mac()
A R Karthickb03cecd2016-07-27 10:27:55 -070095 if olt.on_olt():
96 devices = cls.get_devices()
97 if devices:
98 dids = map(lambda d: d['id'], devices)
99 if len(dids) == 1:
100 did = dids[0]
101 else:
102 ###If we have more than 1, then check for env before using first one
103 did = os.getenv('OLT_DEVICE_ID', dids[0])
A R Karthickb03cecd2016-07-27 10:27:55 -0700104
105 return did
106
107 @classmethod
ChetanGaonker689b3862016-10-17 16:25:01 -0700108 def get_flows(cls, device_id,controller=None):
109 if controller is None:
110 url = 'http://%s:8181/onos/v1/flows/' %(cls.controller) + device_id
111 else:
112 url = 'http://%s:8181/onos/v1/flows/' %(controller) + device_id
113 print('get flows url is %s'%url)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700114 result = requests.get(url, auth = cls.auth)
115 if result.ok:
116 return result.json()['flows']
117 return None
118
119 @classmethod
ChetanGaonker689b3862016-10-17 16:25:01 -0700120 def cord_olt_config(cls, olt_device_data = None,controller=None):
Chetan Gaonkera2b87df2016-03-31 15:41:31 -0700121 '''Configures OLT data for existing devices/switches'''
122 if olt_device_data is None:
123 return
124 did_dict = {}
125 config = { 'devices' : did_dict }
126 devices = cls.get_devices()
127 if not devices:
128 return
129 device_ids = map(lambda d: d['id'], devices)
130 for did in device_ids:
131 access_device_dict = {}
132 access_device_dict['accessDevice'] = olt_device_data
133 did_dict[did] = access_device_dict
134
135 ##configure the device list with access information
ChetanGaonker689b3862016-10-17 16:25:01 -0700136 return cls.config(config,controller=controller)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700137
138 @classmethod
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700139 def install_app(cls, app_file, onos_ip = None):
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700140 params = {'activate':'true'}
141 headers = {'content-type':'application/octet-stream'}
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700142 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700143 with open(app_file, 'rb') as payload:
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700144 result = requests.post(url, auth = cls.auth,
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700145 params = params, headers = headers,
146 data = payload)
ChetanGaonker689b3862016-10-17 16:25:01 -0700147 print('result.ok, result.status_code are %s and %s'%(result.ok, result.status_code))
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700148 return result.ok, result.status_code
A R Karthickb7e80902016-05-17 09:38:31 -0700149
150 @classmethod
A.R Karthick95d044e2016-06-10 18:44:36 -0700151 def install_app_from_url(cls, app_name, app_version, app_url = None, onos_ip = None):
152 params = {'activate':'true'}
153 headers = {'content-type':'application/json'}
154 if app_url is None:
155 app_oar_file = '{}-{}.oar'.format(app_name, app_version)
156 app_url = '{0}/{1}/{2}/{3}'.format(cls.maven_repo, app_name, app_version, app_oar_file)
157 params['url'] = app_url
158 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
159 result = requests.post(url, auth = cls.auth,
160 json = params, headers = headers)
161 return result.ok, result.status_code
162
163 @classmethod
A R Karthickb7e80902016-05-17 09:38:31 -0700164 def uninstall_app(cls, app_name, onos_ip = None):
165 params = {'activate':'true'}
166 headers = {'content-type':'application/octet-stream'}
167 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
168 app_url = '{}/{}'.format(url, app_name)
169 resp = requests.delete(app_url, auth = cls.auth)
170 return resp.ok, resp.status_code
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700171
172 @classmethod
ChetanGaonker689b3862016-10-17 16:25:01 -0700173 def host_config(cls, config,onos_ip=None):
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700174 if config:
175 json_data = json.dumps(config)
ChetanGaonker689b3862016-10-17 16:25:01 -0700176 url = cls.host_cfg_url if onos_ip is None else 'http://%s:8181/onos/v1/hosts/'.format(onos_ip)
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700177 resp = requests.post(cls.host_cfg_url, auth = cls.auth, data = json_data)
178 return resp.ok, resp.status_code
179 return False, 400