blob: 74da3b089e40647b85ec3bdee7f1eabc12c72da3 [file] [log] [blame]
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -08001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import json
16from synchronizers.new_base.SyncInstanceUsingAnsible import SyncStep
17from synchronizers.new_base.modelaccessor import OLTDevice
18
19from xosconfig import Config
20from multistructlog import create_logger
21from time import sleep
22import requests
23from requests.auth import HTTPBasicAuth
24
25log = create_logger(Config().get('logging'))
26
27class SyncOLTDevice(SyncStep):
28 provides = [OLTDevice]
29
30 observes = OLTDevice
31
32 @staticmethod
33 def format_url(url):
34 if 'http' in url:
35 return url
36 else:
37 return 'http://%s' % url
38
39 @staticmethod
40 def get_voltha_info(o):
41 return {
42 'url': SyncOLTDevice.format_url(o.volt_service.voltha_url),
43 'user': o.volt_service.voltha_user,
44 'pass': o.volt_service.voltha_pass
45 }
46
47 @staticmethod
48 def get_p_onos_info(o):
49 return {
50 'url': SyncOLTDevice.format_url(o.volt_service.p_onos_url),
51 'user': o.volt_service.p_onos_user,
52 'pass': o.volt_service.p_onos_pass
53 }
54
55 @staticmethod
56 def get_of_id_from_device(o):
57 voltha_url = SyncOLTDevice.get_voltha_info(o)['url']
58
59 r = requests.get(voltha_url + "/api/v1/logical_devices")
60
61 if r.status_code != 200:
62 raise Exception("Failed to retrieve logical devices from VOLTHA: %s" % r.text)
63
64 res = r.json()
65
66 for ld in res["items"]:
67 if ld["root_device_id"] == o.device_id:
68 return ld["id"]
69 raise Exception("Can't find a logical device for device id: %s" % o.device_id)
70
71
72 def sync_record(self, o):
73 log.info("sync'ing device", object=str(o), **o.tologdict())
74
Matteo Scandolob8621cd2018-04-04 17:12:37 -070075 # If the device has feedback_state is already present in voltha
76 if not o.device_id and not o.admin_state and not o.oper_status and not o.of_id:
77 log.info("Pushing device to VOLTHA", object=str(o), **o.tologdict())
78 voltha_url = self.get_voltha_info(o)['url']
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080079
Matteo Scandolob8621cd2018-04-04 17:12:37 -070080 data = {
81 "type": o.device_type,
82 "host_and_port": "%s:%s" % (o.host, o.port)
83 }
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080084
Matteo Scandolob8621cd2018-04-04 17:12:37 -070085 if o.device_type == 'simulated_olt':
86 # simulated devices won't accept host and port, for testing only
87 data.pop('host_and_port')
88 data['mac_address'] = "00:0c:e2:31:40:00"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080089
Matteo Scandolob8621cd2018-04-04 17:12:37 -070090 log.info("pushing olt to voltha", data=data)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080091
Matteo Scandolob8621cd2018-04-04 17:12:37 -070092 r = requests.post(voltha_url + "/api/v1/devices", json=data)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080093
Matteo Scandolob8621cd2018-04-04 17:12:37 -070094 if r.status_code != 200:
95 raise Exception("Failed to add device: %s" % r.text)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080096
Matteo Scandolob8621cd2018-04-04 17:12:37 -070097 log.info("add device response", text=r.text)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080098
Matteo Scandolob8621cd2018-04-04 17:12:37 -070099 res = r.json()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800100
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700101 log.info("add device json res", res=res)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800102
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700103 if not res['id']:
104 raise Exception('VOLTHA Device Id is empty, this probably means that the device is already provisioned in VOLTHA')
105 else:
106 o.device_id = res['id'];
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800107
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700108 # enable device
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800109
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700110 r = requests.post(voltha_url + "/api/v1/devices/" + o.device_id + "/enable")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800111
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700112 if r.status_code != 200:
113 raise Exception("Failed to enable device: %s" % r.text)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800114
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700115 # read state
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800116 r = requests.get(voltha_url + "/api/v1/devices/" + o.device_id).json()
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700117 while r['oper_status'] == "ACTIVATING":
118 log.info("Waiting for device %s (%s) to activate" % (o.name, o.device_id))
119 sleep(5)
120 r = requests.get(voltha_url + "/api/v1/devices/" + o.device_id).json()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800121
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700122 o.admin_state = r['admin_state']
123 o.oper_status = r['oper_status']
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800124
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700125 # find of_id of device
126 o.of_id = self.get_of_id_from_device(o)
127 o.save()
128 else:
129 log.info("Device already exists in VOLTHA", object=str(o), **o.tologdict())
130
131
132 # NOTE do we need to move this synchronization in a PON_PORT specific step?
133 # for now we assume that each OLT has only one Port
134 vlan = o.ports.all()[0].s_tag
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800135
136 # add device info to P-ONOS
137 data = {
138 "devices": {
139 o.of_id: {
140 "basic": {
141 "driver": o.driver
142 },
143 "accessDevice": {
144 "uplink": o.uplink,
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700145 "vlan": vlan
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800146 }
147 }
148 }
149 }
150
151 onos= self.get_p_onos_info(o)
152
153 r = requests.post(onos['url'] + '/onos/v1/network/configuration/', data=json.dumps(data), auth=HTTPBasicAuth(onos['user'], onos['pass']))
154
155 if r.status_code != 200:
156 log.error(r.text)
157 raise Exception("Failed to add device %s into ONOS" % o.name)
158 else:
159 try:
160 print r.json()
161 except Exception:
162 print r.text
163
164 def delete_record(self, o):
165
166 voltha_url = self.get_voltha_info(o)['url']
167 onos = self.get_p_onos_info(o)
168 if not o.device_id:
169 log.error("Device %s has no device_id" % o.name)
170
171 else:
172
173 # remove the device from ONOS
174 r = requests.delete(onos['url'] + '/onos/v1/network/configuration/devices/' + o.of_id, auth=HTTPBasicAuth(onos['user'], onos['pass']))
175
176 if r.status_code != 200:
177 log.error("Failed to remove device from ONOS: %s - %s" % (o.name, o.of_id), rest_responese=r.text, rest_status_code=r.status_code)
178 raise Exception("Failed to remove device in ONOS")
179
180 # disable the device
181 r = requests.post(voltha_url + "/api/v1/devices/" + o.device_id + "/disable")
182
183 if r.status_code != 200:
184 log.error("Failed to disable device in VOLTHA: %s - %s" % (o.name, o.device_id), rest_responese=r.text, rest_status_code=r.status_code)
185 raise Exception("Failed to disable device in VOLTHA")
186
187 # delete the device
188 r = requests.delete(voltha_url + "/api/v1/devices/" + o.device_id + "/delete")
189
190 if r.status_code != 200:
191 log.error("Failed to delete device in VOLTHA: %s - %s" % (o.name, o.device_id), rest_responese=r.text, rest_status_code=r.status_code)
192 raise Exception("Failed to delete device in VOLTHA")