Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 16 | #from __future__ import absolute_import |
| 17 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 18 | import requests |
| 19 | from requests.auth import HTTPBasicAuth |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 20 | from xossynchronizer.steps.syncstep import SyncStep |
| 21 | from xossynchronizer.modelaccessor import FabricService, Switch, model_accessor |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 22 | |
| 23 | from xosconfig import Config |
| 24 | from multistructlog import create_logger |
| 25 | |
Matteo Scandolo | 04e5e12 | 2018-05-04 16:21:53 -0700 | [diff] [blame] | 26 | from helpers import Helpers |
| 27 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 28 | log = create_logger(Config().get('logging')) |
| 29 | |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 30 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 31 | class SyncFabricSwitch(SyncStep): |
| 32 | provides = [Switch] |
| 33 | observes = Switch |
| 34 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 35 | def sync_record(self, model): |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 36 | log.info("Adding switch %s to onos-fabric" % model.name) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 37 | # Send device info to onos-fabric netcfg |
| 38 | data = { |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 39 | "devices": { |
| 40 | model.ofId: { |
| 41 | "basic": { |
| 42 | "name": model.name, |
| 43 | "driver": model.driver |
| 44 | }, |
| 45 | "segmentrouting": { |
| 46 | "name": model.name, |
| 47 | "ipv4NodeSid": model.ipv4NodeSid, |
| 48 | "ipv4Loopback": model.ipv4Loopback, |
| 49 | "routerMac": model.routerMac, |
| 50 | "isEdgeRouter": model.isEdgeRouter, |
| 51 | "adjacencySids": [] |
| 52 | } |
| 53 | } |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 54 | } |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 57 | onos = Helpers.get_onos_fabric_service(model_accessor=self.model_accessor) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 58 | |
| 59 | url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port) |
| 60 | r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
| 61 | |
| 62 | if r.status_code != 200: |
| 63 | log.error(r.text) |
| 64 | raise Exception("Failed to add device %s into ONOS" % model.name) |
| 65 | else: |
| 66 | try: |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 67 | log.info("result", json=r.json()) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 68 | except Exception: |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 69 | log.info("result", text=r.text) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 70 | |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 71 | def delete_record(self, model): |
| 72 | log.info("Removing switch %s from onos-fabric" % model.name) |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 73 | onos = Helpers.get_onos_fabric_service(model_accessor=self.model_accessor) |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 74 | url = 'http://%s:%s/onos/v1/network/configuration/devices/%s' % ( |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 75 | onos.rest_hostname, onos.rest_port, model.ofId) |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 76 | |
| 77 | r = requests.delete(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
| 78 | |
| 79 | if r.status_code != 204: |
| 80 | log.error(r.text) |
| 81 | raise Exception("Failed to remove switch %s from ONOS" % model.name) |