blob: 427b02af1fdcf43a0badc17e94f212ffcba8147b [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
36 def get_onos_fabric_service(self):
37 # FIXME do not select by name but follow ServiceDependency
38 fabric_service = self.model_accessor.Service.objects.get(name="fabric")
39 onos_fabric_service = fabric_service.provider_services[0].leaf_model
40 return onos_fabric_service
41
42 def pull_records(self):
43 log.debug("[ONOS device pull step] pulling devices from ONOS")
44
45 onos = self.get_onos_fabric_service()
46
47 url = 'http://%s:%s/onos/v1/devices/' % (onos.rest_hostname, onos.rest_port)
48
49 r = requests.get(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
50
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)
59
60 for device in r.json()["devices"]:
61 if device["type"] != "SWITCH":
62 continue
63
64 xos_devices = self.model_accessor.Switch.objects.filter(ofId = device["id"])
65 if not xos_devices:
66 continue
67
68 xos_device = xos_devices[0]
69 changed = False
70
71 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
76
77 if changed:
78 xos_device.save_changed_fields()