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 |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 17 | import urllib |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 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, DeferredException |
| 20 | from xossynchronizer.modelaccessor import FabricService, SwitchPort, PortInterface, FabricIpAddress, 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 SyncFabricPort(SyncStep): |
| 30 | provides = [SwitchPort] |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 31 | observes = [SwitchPort, PortInterface, FabricIpAddress] |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 32 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 33 | def sync_record(self, model): |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 34 | |
| 35 | if model.leaf_model_name == "PortInterface": |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 36 | log.info("Receivent update for PortInterface", port=model.port.portId, interface=model) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 37 | return self.sync_record(model.port) |
| 38 | |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 39 | if model.leaf_model_name == "FabricIpAddress": |
| 40 | log.info("Receivent update for FabricIpAddress", port=model.interface.port.portId, interface=model.interface.name, ip=model.ip) |
| 41 | return self.sync_record(model.interface.port) |
| 42 | |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 43 | 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] | 44 | interfaces = [] |
| 45 | for intf in model.interfaces.all(): |
| 46 | i = { |
| 47 | "name" : intf.name, |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 48 | "ips" : [ i.ip for i in intf.ips.all() ] |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 49 | } |
| 50 | if intf.vlanUntagged: |
| 51 | i["vlan-untagged"] = intf.vlanUntagged |
| 52 | interfaces.append(i) |
| 53 | |
| 54 | # Send port config to onos-fabric netcfg |
| 55 | data = { |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame] | 56 | "ports": { |
| 57 | "%s/%s" % (model.switch.ofId, model.portId) : { |
| 58 | "interfaces": interfaces, |
| 59 | "hostLearning": { |
| 60 | "enabled": model.host_learning |
| 61 | } |
| 62 | } |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 63 | } |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame] | 66 | log.debug("Port %s/%s data" % (model.switch.ofId, model.portId), data=data) |
| 67 | |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 68 | onos = Helpers.get_onos_fabric_service(self.model_accessor) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 69 | |
| 70 | 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] | 71 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 72 | r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
| 73 | |
| 74 | if r.status_code != 200: |
| 75 | log.error(r.text) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 76 | raise Exception("Failed to add port %s/%s into ONOS" % (model.switch.ofId, model.portId)) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 77 | else: |
| 78 | try: |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame] | 79 | 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] | 80 | except Exception: |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame] | 81 | 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] | 82 | |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 83 | def delete_netcfg_item(self, partial_url): |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 84 | onos = Helpers.get_onos_fabric_service(self.model_accessor) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 85 | url = 'http://%s:%s/onos/v1/network/configuration/ports/%s' % (onos.rest_hostname, onos.rest_port, partial_url) |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 86 | |
| 87 | r = requests.delete(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
| 88 | |
| 89 | if r.status_code != 204: |
| 90 | log.error(r.text) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 91 | raise Exception("Failed to %s port %s from ONOS" % url) |
| 92 | |
| 93 | def delete_record(self, model): |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 94 | if model.leaf_model_name == "PortInterface": |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 95 | log.info("Received update for PortInterface", port=model.port.portId, interface=model.name) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 96 | log.info("Removing port interface %s from port %s/%s in onos-fabric" % (model.name, model.port.switch.ofId, model.port.portId)) |
| 97 | |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 98 | # resync the existing interfaces |
| 99 | return self.sync_record(model.port) |
| 100 | |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 101 | if model.leaf_model_name == "FabricIpAddress": |
| 102 | # TODO add unit tests |
| 103 | log.info("Received update for FabricIpAddress", port=model.interface.port.portId, interface=model.interface.name, ip=model.ip) |
| 104 | log.info("Removing IP %s from interface %s, on port %s/%s in onos-fabric" % (model.ip, model.interface.name, model.interface.port.switch.ofId, model.interface.port.portId)) |
| 105 | |
| 106 | # resync the existing interfaces |
| 107 | return self.sync_record(model.interface.port) |
| 108 | |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 109 | log.info("Removing port %s/%s from onos-fabric" % (model.switch.ofId, model.portId)) |
| 110 | |
| 111 | key = "%s/%s" % (model.switch.ofId, model.portId) |
| 112 | key = urllib.quote(key, safe='') |
| 113 | |
| 114 | # deleting the port |
| 115 | self.delete_netcfg_item(key) |