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