blob: 22afa5608ded7fca5ed62fcd9cd6100d0d71d4e2 [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
36class OnosCtrl:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070037
38 auth = ('karaf', 'karaf')
39 controller = os.getenv('ONOS_CONTROLLER_IP') or 'localhost'
40 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
A.R Karthick95d044e2016-06-10 18:44:36 -070041 maven_repo = 'http://central.maven.org/maven2/org/onosproject'
Chetan Gaonker93e302d2016-04-05 10:51:07 -070042 applications_url = 'http://%s:8181/onos/v1/applications' %(controller)
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -070043 host_cfg_url = 'http://%s:8181/onos/v1/hosts/' %(controller)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070044
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080045 def __init__(self, app, controller = None):
46 self.app = app
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070047 if controller is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080048 self.controller = controller
49 self.app_url = 'http://%s:8181/onos/v1/applications/%s' %(self.controller, self.app)
50 self.cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(self.controller)
51 self.auth = ('karaf', 'karaf')
52
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070053 @classmethod
54 def config(cls, config):
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080055 if config:
56 json_data = json.dumps(config)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070057 resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080058 return resp.ok, resp.status_code
59 return False, 400
60
61 def activate(self):
62 resp = requests.post(self.app_url + '/active', auth = self.auth)
63 return resp.ok, resp.status_code
64
65 def deactivate(self):
66 resp = requests.delete(self.app_url + '/active', auth = self.auth)
67 return resp.ok, resp.status_code
68
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070069 @classmethod
70 def get_devices(cls):
71 url = 'http://%s:8181/onos/v1/devices' %(cls.controller)
72 result = requests.get(url, auth = cls.auth)
73 if result.ok:
74 devices = result.json()['devices']
75 return filter(lambda d: d['available'], devices)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080076
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070077 return None
78
79 @classmethod
A R Karthickb03cecd2016-07-27 10:27:55 -070080 def get_device_id(cls):
81 '''If running under olt, we get the first switch connected to onos'''
82 olt = OltConfig()
A R Karthick078e63a2016-07-28 13:59:31 -070083 did = 'of:' + get_mac()
A R Karthickb03cecd2016-07-27 10:27:55 -070084 if olt.on_olt():
85 devices = cls.get_devices()
86 if devices:
87 dids = map(lambda d: d['id'], devices)
88 if len(dids) == 1:
89 did = dids[0]
90 else:
91 ###If we have more than 1, then check for env before using first one
92 did = os.getenv('OLT_DEVICE_ID', dids[0])
A R Karthickb03cecd2016-07-27 10:27:55 -070093
94 return did
95
96 @classmethod
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070097 def get_flows(cls, device_id):
98 url = 'http://%s:8181/onos/v1/flows/' %(cls.controller) + device_id
99 result = requests.get(url, auth = cls.auth)
100 if result.ok:
101 return result.json()['flows']
102 return None
103
104 @classmethod
105 def cord_olt_config(cls, olt_device_data = None):
106 '''Configures OLT data for existing devices/switches'''
107 if olt_device_data is None:
108 return
109 did_dict = {}
110 config = { 'devices' : did_dict }
111 devices = cls.get_devices()
112 if not devices:
113 return
114 device_ids = map(lambda d: d['id'], devices)
115 for did in device_ids:
116 access_device_dict = {}
117 access_device_dict['accessDevice'] = olt_device_data
118 did_dict[did] = access_device_dict
119
120 ##configure the device list with access information
121 return cls.config(config)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700122
123 @classmethod
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700124 def install_app(cls, app_file, onos_ip = None):
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700125 params = {'activate':'true'}
126 headers = {'content-type':'application/octet-stream'}
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700127 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 -0700128 with open(app_file, 'rb') as payload:
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700129 result = requests.post(url, auth = cls.auth,
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700130 params = params, headers = headers,
131 data = payload)
132 return result.ok, result.status_code
A R Karthickb7e80902016-05-17 09:38:31 -0700133
134 @classmethod
A.R Karthick95d044e2016-06-10 18:44:36 -0700135 def install_app_from_url(cls, app_name, app_version, app_url = None, onos_ip = None):
136 params = {'activate':'true'}
137 headers = {'content-type':'application/json'}
138 if app_url is None:
139 app_oar_file = '{}-{}.oar'.format(app_name, app_version)
140 app_url = '{0}/{1}/{2}/{3}'.format(cls.maven_repo, app_name, app_version, app_oar_file)
141 params['url'] = app_url
142 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
143 result = requests.post(url, auth = cls.auth,
144 json = params, headers = headers)
145 return result.ok, result.status_code
146
147 @classmethod
A R Karthickb7e80902016-05-17 09:38:31 -0700148 def uninstall_app(cls, app_name, onos_ip = None):
149 params = {'activate':'true'}
150 headers = {'content-type':'application/octet-stream'}
151 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
152 app_url = '{}/{}'.format(url, app_name)
153 resp = requests.delete(app_url, auth = cls.auth)
154 return resp.ok, resp.status_code
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700155
156 @classmethod
157 def host_config(cls, config):
158 if config:
159 json_data = json.dumps(config)
160 resp = requests.post(cls.host_cfg_url, auth = cls.auth, data = json_data)
161 return resp.ok, resp.status_code
162 return False, 400