blob: 2845721de1fe4a34c60b01c73305c34126fdc849 [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
Matteo Scandolof6337eb2018-04-05 15:58:37 -070024from helpers import Helpers
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080025
26log = create_logger(Config().get('logging'))
27
28class SyncOLTDevice(SyncStep):
29 provides = [OLTDevice]
30
31 observes = OLTDevice
32
33 @staticmethod
Matteo Scandolof6337eb2018-04-05 15:58:37 -070034 def get_ids_from_logical_device(o):
35 voltha_url = Helpers.get_voltha_info(o.volt_service)['url']
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080036
37 r = requests.get(voltha_url + "/api/v1/logical_devices")
38
39 if r.status_code != 200:
40 raise Exception("Failed to retrieve logical devices from VOLTHA: %s" % r.text)
41
42 res = r.json()
43
44 for ld in res["items"]:
45 if ld["root_device_id"] == o.device_id:
Matteo Scandolof6337eb2018-04-05 15:58:37 -070046 o.of_id = ld["id"]
47 o.dp_id = "of:" + Helpers.datapath_id_to_hex(ld["datapath_id"]) # convert to hex
48 return o
49
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080050 raise Exception("Can't find a logical device for device id: %s" % o.device_id)
51
52
53 def sync_record(self, o):
54 log.info("sync'ing device", object=str(o), **o.tologdict())
55
Matteo Scandolob8621cd2018-04-04 17:12:37 -070056 # If the device has feedback_state is already present in voltha
57 if not o.device_id and not o.admin_state and not o.oper_status and not o.of_id:
58 log.info("Pushing device to VOLTHA", object=str(o), **o.tologdict())
Matteo Scandolof6337eb2018-04-05 15:58:37 -070059 voltha_url = Helpers.get_voltha_info(o.volt_service)['url']
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080060
Matteo Scandolob8621cd2018-04-04 17:12:37 -070061 data = {
62 "type": o.device_type,
63 "host_and_port": "%s:%s" % (o.host, o.port)
64 }
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080065
Matteo Scandolob8621cd2018-04-04 17:12:37 -070066 if o.device_type == 'simulated_olt':
67 # simulated devices won't accept host and port, for testing only
68 data.pop('host_and_port')
69 data['mac_address'] = "00:0c:e2:31:40:00"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080070
Matteo Scandolob8621cd2018-04-04 17:12:37 -070071 log.info("pushing olt to voltha", data=data)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080072
Matteo Scandolob8621cd2018-04-04 17:12:37 -070073 r = requests.post(voltha_url + "/api/v1/devices", json=data)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080074
Matteo Scandolob8621cd2018-04-04 17:12:37 -070075 if r.status_code != 200:
76 raise Exception("Failed to add device: %s" % r.text)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080077
Matteo Scandolob8621cd2018-04-04 17:12:37 -070078 log.info("add device response", text=r.text)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080079
Matteo Scandolob8621cd2018-04-04 17:12:37 -070080 res = r.json()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080081
Matteo Scandolob8621cd2018-04-04 17:12:37 -070082 log.info("add device json res", res=res)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080083
Matteo Scandolob8621cd2018-04-04 17:12:37 -070084 if not res['id']:
85 raise Exception('VOLTHA Device Id is empty, this probably means that the device is already provisioned in VOLTHA')
86 else:
87 o.device_id = res['id'];
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080088
Matteo Scandolob8621cd2018-04-04 17:12:37 -070089 # enable device
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080090
Matteo Scandolob8621cd2018-04-04 17:12:37 -070091 r = requests.post(voltha_url + "/api/v1/devices/" + o.device_id + "/enable")
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080092
Matteo Scandolob8621cd2018-04-04 17:12:37 -070093 if r.status_code != 200:
94 raise Exception("Failed to enable device: %s" % r.text)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080095
Matteo Scandolob8621cd2018-04-04 17:12:37 -070096 # read state
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080097 r = requests.get(voltha_url + "/api/v1/devices/" + o.device_id).json()
Matteo Scandolob8621cd2018-04-04 17:12:37 -070098 while r['oper_status'] == "ACTIVATING":
99 log.info("Waiting for device %s (%s) to activate" % (o.name, o.device_id))
100 sleep(5)
101 r = requests.get(voltha_url + "/api/v1/devices/" + o.device_id).json()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800102
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700103 o.admin_state = r['admin_state']
104 o.oper_status = r['oper_status']
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800105
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700106 # find of_id of device
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700107 o = self.get_ids_from_logical_device(o)
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700108 o.save()
109 else:
110 log.info("Device already exists in VOLTHA", object=str(o), **o.tologdict())
111
112
113 # NOTE do we need to move this synchronization in a PON_PORT specific step?
114 # for now we assume that each OLT has only one Port
115 vlan = o.ports.all()[0].s_tag
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800116
117 # add device info to P-ONOS
118 data = {
119 "devices": {
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700120 o.dp_id: {
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800121 "basic": {
122 "driver": o.driver
123 },
124 "accessDevice": {
125 "uplink": o.uplink,
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700126 "vlan": vlan
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800127 }
128 }
129 }
130 }
131
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700132 onos= Helpers.get_p_onos_info(o.volt_service)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800133
134 r = requests.post(onos['url'] + '/onos/v1/network/configuration/', data=json.dumps(data), auth=HTTPBasicAuth(onos['user'], onos['pass']))
135
136 if r.status_code != 200:
137 log.error(r.text)
138 raise Exception("Failed to add device %s into ONOS" % o.name)
139 else:
140 try:
141 print r.json()
142 except Exception:
143 print r.text
144
145 def delete_record(self, o):
146
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700147 voltha_url = Helpers.get_voltha_info(o.volt_service)['url']
148 onos = Helpers.get_p_onos_info(o.volt_service)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800149 if not o.device_id:
150 log.error("Device %s has no device_id" % o.name)
151
152 else:
153
154 # remove the device from ONOS
155 r = requests.delete(onos['url'] + '/onos/v1/network/configuration/devices/' + o.of_id, auth=HTTPBasicAuth(onos['user'], onos['pass']))
156
157 if r.status_code != 200:
158 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)
159 raise Exception("Failed to remove device in ONOS")
160
161 # disable the device
162 r = requests.post(voltha_url + "/api/v1/devices/" + o.device_id + "/disable")
163
164 if r.status_code != 200:
165 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)
166 raise Exception("Failed to disable device in VOLTHA")
167
168 # delete the device
169 r = requests.delete(voltha_url + "/api/v1/devices/" + o.device_id + "/delete")
170
171 if r.status_code != 200:
172 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)
173 raise Exception("Failed to delete device in VOLTHA")