blob: 9a343fffdb6cae409c4498c97a704554def521d9 [file] [log] [blame]
Scott Bakercabc3de2019-04-03 17:23:08 -07001# 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
15from xossynchronizer.pull_steps.pullstep import PullStep
16
17from xosconfig import Config
18from multistructlog import create_logger
19
20import requests
21from requests import ConnectionError
22from requests.auth import HTTPBasicAuth
23from requests.models import InvalidURL
24
25import os, sys
26sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
27
28from helpers import Helpers
29
30log = create_logger(Config().get('logging'))
31
32class ONOSDevicePullStep(PullStep):
33 def __init__(self, model_accessor):
34 super(ONOSDevicePullStep, self).__init__(model_accessor=model_accessor)
35
Himanshu Bhandaribb721952020-05-02 00:59:45 +053036 def get_onos_fabric_service(self, fabric):
37 fabric_service = self.model_accessor.Service.objects.get(id=fabric.id)
Scott Bakercabc3de2019-04-03 17:23:08 -070038 onos_fabric_service = fabric_service.provider_services[0].leaf_model
39 return onos_fabric_service
40
41 def pull_records(self):
42 log.debug("[ONOS device pull step] pulling devices from ONOS")
43
Himanshu Bhandaribb721952020-05-02 00:59:45 +053044 fabric_service=self.model_accessor.FabricService.objects.all()
45 for fabric in fabric_service:
46 onos = self.get_onos_fabric_service(fabric)
47 log.info("onos_fabric: %s" % onos)
Scott Bakercabc3de2019-04-03 17:23:08 -070048
Himanshu Bhandaribb721952020-05-02 00:59:45 +053049 url = 'http://%s:%s/onos/v1/devices/' % (onos.rest_hostname, onos.rest_port)
50 r = requests.get(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
51 if r.status_code != 200:
52 log.error(r.text)
53 raise Exception("Failed to get onos devices")
54 else:
55 try:
56 log.info("Get devices response", json=r.json())
57 except Exception:
58 log.info("Get devices exception response", text=r.text)
Scott Bakercabc3de2019-04-03 17:23:08 -070059
Himanshu Bhandaribb721952020-05-02 00:59:45 +053060 for device in r.json()["devices"]:
61 if device["type"] != "SWITCH":
62 continue
Scott Bakercabc3de2019-04-03 17:23:08 -070063
Himanshu Bhandaribb721952020-05-02 00:59:45 +053064 xos_devices = self.model_accessor.Switch.objects.filter(ofId = device["id"])
65 if not xos_devices:
66 continue
Scott Bakercabc3de2019-04-03 17:23:08 -070067
Himanshu Bhandaribb721952020-05-02 00:59:45 +053068 xos_device = xos_devices[0]
69 changed = False
Scott Bakercabc3de2019-04-03 17:23:08 -070070
Himanshu Bhandaribb721952020-05-02 00:59:45 +053071 managementAddress = device.get("annotations", {}).get("managementAddress")
72 if (xos_device.managementAddress != managementAddress):
73 log.info("Setting managementAddress on switch %s to %s" % (xos_device.id, managementAddress))
74 xos_device.managementAddress = managementAddress
75 changed = True
Scott Bakercabc3de2019-04-03 17:23:08 -070076
Himanshu Bhandaribb721952020-05-02 00:59:45 +053077 if changed:
78 xos_device.save_changed_fields()