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, |
Daniele Moro | fe69315 | 2019-09-20 16:53:40 -0700 | [diff] [blame] | 43 | "driver": model.driver, |
| 44 | "pipeconf": model.pipeconf, |
| 45 | "managementAddress": model.managementAddress |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 46 | }, |
| 47 | "segmentrouting": { |
| 48 | "name": model.name, |
| 49 | "ipv4NodeSid": model.ipv4NodeSid, |
| 50 | "ipv4Loopback": model.ipv4Loopback, |
| 51 | "routerMac": model.routerMac, |
| 52 | "isEdgeRouter": model.isEdgeRouter, |
Daniele Moro | fe69315 | 2019-09-20 16:53:40 -0700 | [diff] [blame] | 53 | "adjacencySids": [], |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 54 | } |
| 55 | } |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 56 | } |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 59 | onos = Helpers.get_onos_fabric_service(model_accessor=self.model_accessor) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 60 | |
| 61 | url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port) |
| 62 | r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
| 63 | |
| 64 | if r.status_code != 200: |
| 65 | log.error(r.text) |
Daniele Moro | fe69315 | 2019-09-20 16:53:40 -0700 | [diff] [blame] | 66 | 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] | 67 | else: |
| 68 | try: |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 69 | log.info("result", json=r.json()) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 70 | except Exception: |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 71 | log.info("result", text=r.text) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 72 | |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 73 | def delete_record(self, model): |
| 74 | log.info("Removing switch %s from onos-fabric" % model.name) |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 75 | onos = Helpers.get_onos_fabric_service(model_accessor=self.model_accessor) |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 76 | url = 'http://%s:%s/onos/v1/network/configuration/devices/%s' % ( |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 77 | onos.rest_hostname, onos.rest_port, model.ofId) |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 78 | |
| 79 | r = requests.delete(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
| 80 | |
| 81 | if r.status_code != 204: |
| 82 | log.error(r.text) |
| 83 | raise Exception("Failed to remove switch %s from ONOS" % model.name) |