blob: 9c73b4a399f77d79abe677f9c30f36a8d826dd72 [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()
81 if olt.on_olt():
82 devices = cls.get_devices()
83 if devices:
84 dids = map(lambda d: d['id'], devices)
85 if len(dids) == 1:
86 did = dids[0]
87 else:
88 ###If we have more than 1, then check for env before using first one
89 did = os.getenv('OLT_DEVICE_ID', dids[0])
90 else:
91 did = 'of:' + get_mac('ovsbr0')
92
93 return did
94
95 @classmethod
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070096 def get_flows(cls, device_id):
97 url = 'http://%s:8181/onos/v1/flows/' %(cls.controller) + device_id
98 result = requests.get(url, auth = cls.auth)
99 if result.ok:
100 return result.json()['flows']
101 return None
102
103 @classmethod
104 def cord_olt_config(cls, olt_device_data = None):
105 '''Configures OLT data for existing devices/switches'''
106 if olt_device_data is None:
107 return
108 did_dict = {}
109 config = { 'devices' : did_dict }
110 devices = cls.get_devices()
111 if not devices:
112 return
113 device_ids = map(lambda d: d['id'], devices)
114 for did in device_ids:
115 access_device_dict = {}
116 access_device_dict['accessDevice'] = olt_device_data
117 did_dict[did] = access_device_dict
118
119 ##configure the device list with access information
120 return cls.config(config)
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700121
122 @classmethod
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700123 def install_app(cls, app_file, onos_ip = None):
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700124 params = {'activate':'true'}
125 headers = {'content-type':'application/octet-stream'}
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700126 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 -0700127 with open(app_file, 'rb') as payload:
Chetan Gaonker5209fe82016-04-19 10:09:53 -0700128 result = requests.post(url, auth = cls.auth,
Chetan Gaonker93e302d2016-04-05 10:51:07 -0700129 params = params, headers = headers,
130 data = payload)
131 return result.ok, result.status_code
A R Karthickb7e80902016-05-17 09:38:31 -0700132
133 @classmethod
A.R Karthick95d044e2016-06-10 18:44:36 -0700134 def install_app_from_url(cls, app_name, app_version, app_url = None, onos_ip = None):
135 params = {'activate':'true'}
136 headers = {'content-type':'application/json'}
137 if app_url is None:
138 app_oar_file = '{}-{}.oar'.format(app_name, app_version)
139 app_url = '{0}/{1}/{2}/{3}'.format(cls.maven_repo, app_name, app_version, app_oar_file)
140 params['url'] = app_url
141 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
142 result = requests.post(url, auth = cls.auth,
143 json = params, headers = headers)
144 return result.ok, result.status_code
145
146 @classmethod
A R Karthickb7e80902016-05-17 09:38:31 -0700147 def uninstall_app(cls, app_name, onos_ip = None):
148 params = {'activate':'true'}
149 headers = {'content-type':'application/octet-stream'}
150 url = cls.applications_url if onos_ip is None else 'http://{0}:8181/onos/v1/applications'.format(onos_ip)
151 app_url = '{}/{}'.format(url, app_name)
152 resp = requests.delete(app_url, auth = cls.auth)
153 return resp.ok, resp.status_code
ChetanGaonkerf9c2f8b2016-07-19 15:49:41 -0700154
155 @classmethod
156 def host_config(cls, config):
157 if config:
158 json_data = json.dumps(config)
159 resp = requests.post(cls.host_cfg_url, auth = cls.auth, data = json_data)
160 return resp.ok, resp.status_code
161 return False, 400