Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 1 | # 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 | |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 15 | #from __future__ import absolute_import |
| 16 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 17 | import requests |
| 18 | from requests.auth import HTTPBasicAuth |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 19 | from xossynchronizer.steps.syncstep import SyncStep |
| 20 | from xossynchronizer.modelaccessor import FabricService, Switch, model_accessor |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 21 | |
| 22 | from xosconfig import Config |
| 23 | from multistructlog import create_logger |
| 24 | |
Matteo Scandolo | 04e5e12 | 2018-05-04 16:21:53 -0700 | [diff] [blame] | 25 | from helpers import Helpers |
| 26 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 27 | log = create_logger(Config().get('logging')) |
| 28 | |
| 29 | class SyncFabricSwitch(SyncStep): |
| 30 | provides = [Switch] |
| 31 | observes = Switch |
| 32 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 33 | def sync_record(self, model): |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 34 | log.info("Adding switch %s to onos-fabric" % model.name) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 35 | # Send device info to onos-fabric netcfg |
| 36 | data = { |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 37 | "devices": { |
| 38 | model.ofId: { |
| 39 | "basic": { |
| 40 | "name": model.name, |
Daniele Moro | fe69315 | 2019-09-20 16:53:40 -0700 | [diff] [blame] | 41 | "driver": model.driver, |
| 42 | "pipeconf": model.pipeconf, |
| 43 | "managementAddress": model.managementAddress |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 44 | }, |
| 45 | "segmentrouting": { |
| 46 | "name": model.name, |
| 47 | "ipv4NodeSid": model.ipv4NodeSid, |
| 48 | "ipv4Loopback": model.ipv4Loopback, |
| 49 | "routerMac": model.routerMac, |
| 50 | "isEdgeRouter": model.isEdgeRouter, |
Daniele Moro | fe69315 | 2019-09-20 16:53:40 -0700 | [diff] [blame] | 51 | "adjacencySids": [], |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 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 | |
Himanshu Bhandari | 3d2d341 | 2020-04-29 12:08:09 +0530 | [diff] [blame^] | 57 | onos = Helpers.get_onos(model, self.model_accessor) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 58 | url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port) |
| 59 | r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 60 | if r.status_code != 200: |
| 61 | log.error(r.text) |
Daniele Moro | fe69315 | 2019-09-20 16:53:40 -0700 | [diff] [blame] | 62 | raise Exception("Failed to add device %s into ONOS: %s" % (model.name, r.text)) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 63 | else: |
| 64 | try: |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 65 | log.info("result", json=r.json()) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 66 | except Exception: |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 67 | log.info("result", text=r.text) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 68 | |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 69 | def delete_record(self, model): |
| 70 | log.info("Removing switch %s from onos-fabric" % model.name) |
Himanshu Bhandari | 3d2d341 | 2020-04-29 12:08:09 +0530 | [diff] [blame^] | 71 | onos = Helpers.get_onos(model, self.model_accessor) |
| 72 | url = 'http://%s:%s/onos/v1/network/configuration/devices/%s' % (onos.rest_hostname, onos.rest_port, model.ofId) |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 73 | r = requests.delete(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 74 | if r.status_code != 204: |
| 75 | log.error(r.text) |
| 76 | raise Exception("Failed to remove switch %s from ONOS" % model.name) |