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, DeferredException, model_accessor |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 19 | from synchronizers.new_base.modelaccessor import FabricService, SwitchPort |
| 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 SyncFabricPort(SyncStep): |
| 29 | provides = [SwitchPort] |
| 30 | observes = SwitchPort |
| 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 port %s/%s to onos-fabric" % (model.switch.ofId, model.portId)) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 34 | interfaces = [] |
| 35 | for intf in model.interfaces.all(): |
| 36 | i = { |
| 37 | "name" : intf.name, |
| 38 | "ips" : [ intf.ips ] |
| 39 | } |
| 40 | if intf.vlanUntagged: |
| 41 | i["vlan-untagged"] = intf.vlanUntagged |
| 42 | interfaces.append(i) |
| 43 | |
| 44 | # Send port config to onos-fabric netcfg |
| 45 | data = { |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame^] | 46 | "ports": { |
| 47 | "%s/%s" % (model.switch.ofId, model.portId) : { |
| 48 | "interfaces": interfaces, |
| 49 | "hostLearning": { |
| 50 | "enabled": model.host_learning |
| 51 | } |
| 52 | } |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 53 | } |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame^] | 56 | log.debug("Port %s/%s data" % (model.switch.ofId, model.portId), data=data) |
| 57 | |
Matteo Scandolo | 04e5e12 | 2018-05-04 16:21:53 -0700 | [diff] [blame] | 58 | onos = Helpers.get_onos_fabric_service() |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 59 | |
| 60 | url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port) |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 61 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 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) |
| 66 | raise Exception("Failed to add port %s into ONOS" % model.name) |
| 67 | else: |
| 68 | try: |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame^] | 69 | log.info("Port %s/%s response" % (model.switch.ofId, model.portId), json=r.json()) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 70 | except Exception: |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame^] | 71 | log.info("Port %s/%s response" % (model.switch.ofId, model.portId), 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 port %s/%s from onos-fabric" % (model.switch.ofId, model.portId)) |
| 75 | onos = Helpers.get_onos_fabric_service() |
| 76 | url = 'http://%s:%s/onos/v1/network/configuration/ports/%s/%s' % (onos.rest_hostname, onos.rest_port, model.switch.ofId, model.portId) |
| 77 | |
| 78 | r = requests.delete(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
| 79 | |
| 80 | if r.status_code != 204: |
| 81 | log.error(r.text) |
| 82 | raise Exception("Failed to remove port %s from ONOS" % model.name) |