blob: 32c4068c064620c0906e5d50250b8a8fafb474f1 [file] [log] [blame]
Chetan Gaonkercb122cc2016-05-10 10:58:34 -07001#!/usr/bin/env python
Chetan Gaonkercfcce782016-05-10 10:10:42 -07002#
3# Copyright 2016-present Ciena Corporation
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080017import json
18import requests
19import os,sys,time
20
21class OnosCtrl:
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070022
23 auth = ('karaf', 'karaf')
24 controller = os.getenv('ONOS_CONTROLLER_IP') or 'localhost'
25 cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(controller)
Chetan Gaonker93e302d2016-04-05 10:51:07 -070026 applications_url = 'http://%s:8181/onos/v1/applications' %(controller)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070027
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080028 def __init__(self, app, controller = None):
29 self.app = app
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070030 if controller is not None:
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080031 self.controller = controller
32 self.app_url = 'http://%s:8181/onos/v1/applications/%s' %(self.controller, self.app)
33 self.cfg_url = 'http://%s:8181/onos/v1/network/configuration/' %(self.controller)
34 self.auth = ('karaf', 'karaf')
35
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070036 @classmethod
37 def config(cls, config):
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080038 if config:
39 json_data = json.dumps(config)
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070040 resp = requests.post(cls.cfg_url, auth = cls.auth, data = json_data)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080041 return resp.ok, resp.status_code
42 return False, 400
43
44 def activate(self):
45 resp = requests.post(self.app_url + '/active', auth = self.auth)
46 return resp.ok, resp.status_code
47
48 def deactivate(self):
49 resp = requests.delete(self.app_url + '/active', auth = self.auth)
50 return resp.ok, resp.status_code
51
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070052 @classmethod
53 def get_devices(cls):
54 url = 'http://%s:8181/onos/v1/devices' %(cls.controller)
55 result = requests.get(url, auth = cls.auth)
56 if result.ok:
57 devices = result.json()['devices']
58 return filter(lambda d: d['available'], devices)
Chetan Gaonker4a25e2b2016-03-04 14:45:15 -080059
Chetan Gaonkera2b87df2016-03-31 15:41:31 -070060 return None
61
62 @classmethod
63 def get_flows(cls, device_id):
64 url = 'http://%s:8181/onos/v1/flows/' %(cls.controller) + device_id
65 result = requests.get(url, auth = cls.auth)
66 if result.ok:
67 return result.json()['flows']
68 return None
69
70 @classmethod
71 def cord_olt_config(cls, olt_device_data = None):
72 '''Configures OLT data for existing devices/switches'''
73 if olt_device_data is None:
74 return
75 did_dict = {}
76 config = { 'devices' : did_dict }
77 devices = cls.get_devices()
78 if not devices:
79 return
80 device_ids = map(lambda d: d['id'], devices)
81 for did in device_ids:
82 access_device_dict = {}
83 access_device_dict['accessDevice'] = olt_device_data
84 did_dict[did] = access_device_dict
85
86 ##configure the device list with access information
87 return cls.config(config)
Chetan Gaonker93e302d2016-04-05 10:51:07 -070088
89 @classmethod
Chetan Gaonker5209fe82016-04-19 10:09:53 -070090 def install_app(cls, app_file, onos_ip = None):
Chetan Gaonker93e302d2016-04-05 10:51:07 -070091 params = {'activate':'true'}
92 headers = {'content-type':'application/octet-stream'}
Chetan Gaonker5209fe82016-04-19 10:09:53 -070093 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 -070094 with open(app_file, 'rb') as payload:
Chetan Gaonker5209fe82016-04-19 10:09:53 -070095 result = requests.post(url, auth = cls.auth,
Chetan Gaonker93e302d2016-04-05 10:51:07 -070096 params = params, headers = headers,
97 data = payload)
98 return result.ok, result.status_code