blob: 09cbeeee4fde5f38fc613a30313419c2f2e15795 [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
22def get_mac(iface = 'ovsbr0', pad = 4):
23 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
24 try:
25 info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytes(iface[:15])))
26 except:
27 info = ['0'] * 24
28 s.close()
29 sep = ''
30 if pad == 0:
31 sep = ':'
32 return '0'*pad + sep.join(['%02x' %ord(char) for char in info[18:24]])
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080033
34class OnosCtrl:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070035
36 auth = ('karaf', 'karaf')
37 controller = os.getenv('ONOS_CONTROLLER_IP') or 'localhost'
38 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
A.R Karthick95d044e2016-06-10 18:44:36 -070039 maven_repo = 'http://central.maven.org/maven2/org/onosproject'
Chetan Gaonker93e302d2016-04-05 10:51:07 -070040 applications_url = 'http://%s:8181/onos/v1/applications' %(controller)
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -070041 host_cfg_url = 'http://%s:8181/onos/v1/hosts/' %(controller)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070042
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080043 def __init__(self, app, controller = None):
44 self.app = app
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070045 if controller is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080046 self.controller = controller
47 self.app_url = 'http://%s:8181/onos/v1/applications/%s' %(self.controller, self.app)
48 self.cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(self.controller)
49 self.auth = ('karaf', 'karaf')
50
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070051 @classmethod
52 def config(cls, config):
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080053 if config:
54 json_data = json.dumps(config)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070055 resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080056 return resp.ok, resp.status_code
57 return False, 400
58
59 def activate(self):
60 resp = requests.post(self.app_url + '/active', auth = self.auth)
61 return resp.ok, resp.status_code
62
63 def deactivate(self):
64 resp = requests.delete(self.app_url + '/active', auth = self.auth)
65 return resp.ok, resp.status_code
66
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070067 @classmethod
68 def get_devices(cls):
69 url = 'http://%s:8181/onos/v1/devices' %(cls.controller)
70 result = requests.get(url, auth = cls.auth)
71 if result.ok:
72 devices = result.json()['devices']
73 return filter(lambda d: d['available'], devices)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080074
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070075 return None
76
77 @classmethod
A R Karthickb03cecd2016-07-27 10:27:55 -070078 def get_device_id(cls):
79 '''If running under olt, we get the first switch connected to onos'''
80 olt = OltConfig()
A R Karthick5bc7b5a2016-07-27 14:12:58 -070081 did = 'of:' + get_mac('ovsbr0')
A R Karthickb03cecd2016-07-27 10:27:55 -070082 if olt.on_olt():
83 devices = cls.get_devices()
84 if devices:
85 dids = map(lambda d: d['id'], devices)
86 if len(dids) == 1:
87 did = dids[0]
88 else:
89 ###If we have more than 1, then check for env before using first one
90 did = os.getenv('OLT_DEVICE_ID', dids[0])
A R Karthickb03cecd2016-07-27 10:27:55 -070091
92 return did
93
94 @classmethod
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070095 def get_flows(cls, device_id):
96 url = 'http://%s:8181/onos/v1/flows/' %(cls.controller) + device_id
97 result = requests.get(url, auth = cls.auth)
98 if result.ok:
99 return result.json()['flows']
100 return None
101
102 @classmethod
103 def cord_olt_config(cls, olt_device_data = None):
104 '''Configures OLT data for existing devices/switches'''
105 if olt_device_data is None:
106 return
107 did_dict = {}
108 config = { 'devices' : did_dict }
109 devices = cls.get_devices()
110 if not devices:
111 return
112 device_ids = map(lambda d: d['id'], devices)
113 for did in device_ids:
114 access_device_dict = {}
115 access_device_dict['accessDevice'] = olt_device_data
116 did_dict[did] = access_device_dict
117
118 ##configure the device list with access information
119 return cls.config(config)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700120
121 @classmethod
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700122 def install_app(cls, app_file, onos_ip = None):
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700123 params = {'activate':'true'}
124 headers = {'content-type':'application/octet-stream'}
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700125 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 -0700126 with open(app_file, 'rb') as payload:
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700127 result = requests.post(url, auth = cls.auth,
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700128 params = params, headers = headers,
129 data = payload)
130 return result.ok, result.status_code
A R Karthickb7e80902016-05-17 09:38:31 -0700131
132 @classmethod
A.R Karthick95d044e2016-06-10 18:44:36 -0700133 def install_app_from_url(cls, app_name, app_version, app_url = None, onos_ip = None):
134 params = {'activate':'true'}
135 headers = {'content-type':'application/json'}
136 if app_url is None:
137 app_oar_file = '{}-{}.oar'.format(app_name, app_version)
138 app_url = '{0}/{1}/{2}/{3}'.format(cls.maven_repo, app_name, app_version, app_oar_file)
139 params['url'] = app_url
140 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
141 result = requests.post(url, auth = cls.auth,
142 json = params, headers = headers)
143 return result.ok, result.status_code
144
145 @classmethod
A R Karthickb7e80902016-05-17 09:38:31 -0700146 def uninstall_app(cls, app_name, onos_ip = None):
147 params = {'activate':'true'}
148 headers = {'content-type':'application/octet-stream'}
149 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
150 app_url = '{}/{}'.format(url, app_name)
151 resp = requests.delete(app_url, auth = cls.auth)
152 return resp.ok, resp.status_code
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700153
154 @classmethod
155 def host_config(cls, config):
156 if config:
157 json_data = json.dumps(config)
158 resp = requests.post(cls.host_cfg_url, auth = cls.auth, data = json_data)
159 return resp.ok, resp.status_code
160 return False, 400