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 |
| 18 | from synchronizers.new_base.syncstep import SyncStep, DeferredException |
| 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): |
| 33 | # Send device info to onos-fabric netcfg |
| 34 | data = { |
| 35 | "devices": { |
| 36 | model.ofId: { |
| 37 | "basic": { |
| 38 | "name": model.name, |
| 39 | "driver": model.driver |
| 40 | }, |
| 41 | "segmentrouting" : { |
| 42 | "name" : model.name, |
| 43 | "ipv4NodeSid" : model.ipv4NodeSid, |
| 44 | "ipv4Loopback" : model.ipv4Loopback, |
| 45 | "routerMac" : model.routerMac, |
| 46 | "isEdgeRouter" : model.isEdgeRouter, |
| 47 | "adjacencySids" : [] |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
Matteo Scandolo | 04e5e12 | 2018-05-04 16:21:53 -0700 | [diff] [blame^] | 53 | onos = Helpers.get_onos_fabric_service() |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 54 | |
| 55 | url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port) |
| 56 | r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
| 57 | |
| 58 | if r.status_code != 200: |
| 59 | log.error(r.text) |
| 60 | raise Exception("Failed to add device %s into ONOS" % model.name) |
| 61 | else: |
| 62 | try: |
| 63 | print r.json() |
| 64 | except Exception: |
| 65 | print r.text |
| 66 | |
| 67 | def delete_record(self, switch): |
| 68 | pass |