blob: 4a42ec16a565faec0f43e08adf8d51591190b7a7 [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]
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080030 observes = OLTDevice
31
32 @staticmethod
Matteo Scandolof6337eb2018-04-05 15:58:37 -070033 def get_ids_from_logical_device(o):
Luca Preteca974c82018-05-01 18:06:16 -070034 voltha = Helpers.get_voltha_info(o.volt_service)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080035
Luca Preteca974c82018-05-01 18:06:16 -070036 request = requests.get("%s:%d/api/v1/logical_devices" % (voltha['url'], voltha['port']))
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080037
Luca Preteca974c82018-05-01 18:06:16 -070038 if request.status_code != 200:
39 raise Exception("Failed to retrieve logical devices from VOLTHA: %s" % request.text)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080040
Luca Preteca974c82018-05-01 18:06:16 -070041 response = request.json()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080042
Luca Preteca974c82018-05-01 18:06:16 -070043 for ld in response["items"]:
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080044 if ld["root_device_id"] == o.device_id:
Matteo Scandolof6337eb2018-04-05 15:58:37 -070045 o.of_id = ld["id"]
Luca Preteca974c82018-05-01 18:06:16 -070046 o.dp_id = "of:%s" % (Helpers.datapath_id_to_hex(ld["datapath_id"])) # Convert to hex
Matteo Scandolof6337eb2018-04-05 15:58:37 -070047 return o
48
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080049 raise Exception("Can't find a logical device for device id: %s" % o.device_id)
50
51
52 def sync_record(self, o):
Luca Preteca974c82018-05-01 18:06:16 -070053 log.info("Synching device", object=str(o), **o.tologdict())
54
55 voltha = Helpers.get_voltha_info(o.volt_service)
56 onos_voltha = Helpers.get_onos_voltha_info(o.volt_service)
57 onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass'])
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080058
Matteo Scandolob8621cd2018-04-04 17:12:37 -070059 # If the device has feedback_state is already present in voltha
60 if not o.device_id and not o.admin_state and not o.oper_status and not o.of_id:
61 log.info("Pushing device to VOLTHA", object=str(o), **o.tologdict())
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080062
Matteo Scandolob8621cd2018-04-04 17:12:37 -070063 data = {
64 "type": o.device_type,
65 "host_and_port": "%s:%s" % (o.host, o.port)
66 }
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080067
Matteo Scandolob8621cd2018-04-04 17:12:37 -070068 if o.device_type == 'simulated_olt':
Luca Preteca974c82018-05-01 18:06:16 -070069 # Simulated devices will not accept host and port. This is for test only
Matteo Scandolob8621cd2018-04-04 17:12:37 -070070 data.pop('host_and_port')
71 data['mac_address'] = "00:0c:e2:31:40:00"
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080072
Luca Preteca974c82018-05-01 18:06:16 -070073 log.info("Pushing OLT to Voltha", data=data)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080074
Luca Preteca974c82018-05-01 18:06:16 -070075 request = requests.post("%s:%d/api/v1/devices" % (voltha['url'], voltha['port']), json=data)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080076
Luca Preteca974c82018-05-01 18:06:16 -070077 if request.status_code != 200:
78 raise Exception("Failed to add device: %s" % request.text)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080079
Luca Preteca974c82018-05-01 18:06:16 -070080 log.info("Add device response", text=request.text)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080081
Luca Preteca974c82018-05-01 18:06:16 -070082 res = request.json()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080083
Luca Preteca974c82018-05-01 18:06:16 -070084 log.info("Add device json res", res=res)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080085
Matteo Scandolob8621cd2018-04-04 17:12:37 -070086 if not res['id']:
87 raise Exception('VOLTHA Device Id is empty, this probably means that the device is already provisioned in VOLTHA')
88 else:
89 o.device_id = res['id'];
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080090
Luca Preteca974c82018-05-01 18:06:16 -070091 # Enable device
92 request = requests.post("%s:%d/api/v1/devices/%s/enable" % (voltha['url'], voltha['port'], o.device_id))
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080093
Luca Preteca974c82018-05-01 18:06:16 -070094 if request.status_code != 200:
95 raise Exception("Failed to enable device: %s" % request.text)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -080096
Luca Preteca974c82018-05-01 18:06:16 -070097 # Read state
98 request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], o.device_id)).json()
99 while request['oper_status'] == "ACTIVATING":
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700100 log.info("Waiting for device %s (%s) to activate" % (o.name, o.device_id))
101 sleep(5)
Luca Preteca974c82018-05-01 18:06:16 -0700102 request = requests.get("%s:%d/api/v1/devices/%s" % (voltha['url'], voltha['port'], o.device_id)).json()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800103
Luca Preteca974c82018-05-01 18:06:16 -0700104 o.admin_state = request['admin_state']
105 o.oper_status = request['oper_status']
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800106
Luca Preteca974c82018-05-01 18:06:16 -0700107 # Find the of_id of the device
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700108 o = self.get_ids_from_logical_device(o)
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700109 o.save()
110 else:
111 log.info("Device already exists in VOLTHA", object=str(o), **o.tologdict())
112
113
Luca Preteca974c82018-05-01 18:06:16 -0700114 # Do we need to move this synchronization in a PON_PORT specific step?
115 # For now, we assume that each OLT has only one port
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700116 vlan = o.ports.all()[0].s_tag
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800117
Luca Preteca974c82018-05-01 18:06:16 -0700118 # Add device info to onos-voltha
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800119 data = {
120 "devices": {
Matteo Scandolof6337eb2018-04-05 15:58:37 -0700121 o.dp_id: {
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800122 "basic": {
123 "driver": o.driver
124 },
125 "accessDevice": {
126 "uplink": o.uplink,
Matteo Scandolob8621cd2018-04-04 17:12:37 -0700127 "vlan": vlan
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800128 }
129 }
130 }
131 }
132
Luca Preteca974c82018-05-01 18:06:16 -0700133 request = requests.post("%s:%d/onos/v1/network/configuration/" % (onos_voltha['url'], onos_voltha['port']), data=json.dumps(data), auth=onos_voltha_basic_auth)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800134
Luca Preteca974c82018-05-01 18:06:16 -0700135 if request.status_code != 200:
136 log.error(request.text)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800137 raise Exception("Failed to add device %s into ONOS" % o.name)
138 else:
139 try:
Luca Preteca974c82018-05-01 18:06:16 -0700140 print request.json()
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800141 except Exception:
Luca Preteca974c82018-05-01 18:06:16 -0700142 print request.text
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800143
144 def delete_record(self, o):
Luca Preteca974c82018-05-01 18:06:16 -0700145 log.info("Deleting device", object=str(o), **o.tologdict())
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800146
Luca Preteca974c82018-05-01 18:06:16 -0700147 voltha = Helpers.get_voltha_info(o.volt_service)
148 onos_voltha = Helpers.get_onos_voltha_info(o.volt_service)
149 onos_voltha_basic_auth = HTTPBasicAuth(onos_voltha['user'], onos_voltha['pass'])
150
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800151 if not o.device_id:
152 log.error("Device %s has no device_id" % o.name)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800153 else:
Luca Preteca974c82018-05-01 18:06:16 -0700154 # Remove the device from ONOS
155 request = requests.delete("%s:%d/onos/v1/network/configuration/devices/%s" % (onos_voltha['url'], onos_voltha['port'], o.of_id), auth=onos_voltha_basic_auth)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800156
Luca Preteca974c82018-05-01 18:06:16 -0700157 if request.status_code != 200:
158 log.error("Failed to remove device from ONOS: %s - %s" % (o.name, o.of_id), rest_responese=request.text, rest_status_code=request.status_code)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800159 raise Exception("Failed to remove device in ONOS")
160
Luca Preteca974c82018-05-01 18:06:16 -0700161 # Disable the device
162 request = requests.post("%s:%d/api/v1/devices/%s/disable" % (voltha['url'], voltha['port'], o.device_id))
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800163
Luca Preteca974c82018-05-01 18:06:16 -0700164 if request.status_code != 200:
165 log.error("Failed to disable device in VOLTHA: %s - %s" % (o.name, o.device_id), rest_responese=request.text, rest_status_code=request.status_code)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800166 raise Exception("Failed to disable device in VOLTHA")
167
Luca Preteca974c82018-05-01 18:06:16 -0700168 # Delete the device
169 request = requests.delete("%s:%d/api/v1/devices/%s/delete" % (voltha['url'], voltha['port'], o.device_id))
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800170
Luca Preteca974c82018-05-01 18:06:16 -0700171 if request.status_code != 200:
172 log.error("Failed to delete device in VOLTHA: %s - %s" % (o.name, o.device_id), rest_responese=request.text, rest_status_code=request.status_code)
Matteo Scandolo4a8b4d62018-03-06 17:18:46 -0800173 raise Exception("Failed to delete device in VOLTHA")