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 |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 19 | import urllib |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 20 | from requests.auth import HTTPBasicAuth |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 21 | from xossynchronizer.steps.syncstep import SyncStep |
| 22 | from xossynchronizer.modelaccessor import SwitchPort, PortInterface, FabricIpAddress, model_accessor |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 23 | |
| 24 | from xosconfig import Config |
| 25 | from multistructlog import create_logger |
| 26 | |
Matteo Scandolo | 04e5e12 | 2018-05-04 16:21:53 -0700 | [diff] [blame] | 27 | from helpers import Helpers |
| 28 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 29 | log = create_logger(Config().get('logging')) |
| 30 | |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 31 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 32 | class SyncFabricPort(SyncStep): |
| 33 | provides = [SwitchPort] |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 34 | observes = [SwitchPort, PortInterface, FabricIpAddress] |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 35 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 36 | def sync_record(self, model): |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 37 | |
| 38 | if model.leaf_model_name == "PortInterface": |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 39 | log.info("Received update for PortInterface", port=model.port.portId, interface=model) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 40 | return self.sync_record(model.port) |
| 41 | |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 42 | if model.leaf_model_name == "FabricIpAddress": |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 43 | log.info("Received update for FabricIpAddress", |
| 44 | port=model.interface.port.portId, |
| 45 | interface=model.interface.name, |
| 46 | ip=model.ip) |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 47 | return self.sync_record(model.interface.port) |
| 48 | |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 49 | 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] | 50 | interfaces = [] |
| 51 | for intf in model.interfaces.all(): |
| 52 | i = { |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 53 | "name": intf.name, |
| 54 | "ips": [i.ip for i in intf.ips.all()] |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 55 | } |
| 56 | if intf.vlanUntagged: |
| 57 | i["vlan-untagged"] = intf.vlanUntagged |
| 58 | interfaces.append(i) |
| 59 | |
| 60 | # Send port config to onos-fabric netcfg |
| 61 | data = { |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame] | 62 | "ports": { |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 63 | "%s/%s" % (model.switch.ofId, model.portId): { |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame] | 64 | "interfaces": interfaces, |
| 65 | "hostLearning": { |
| 66 | "enabled": model.host_learning |
| 67 | } |
| 68 | } |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 69 | } |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame] | 72 | log.debug("Port %s/%s data" % (model.switch.ofId, model.portId), data=data) |
| 73 | |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 74 | onos = Helpers.get_onos_fabric_service(self.model_accessor) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 75 | |
| 76 | 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] | 77 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 78 | r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
| 79 | |
| 80 | if r.status_code != 200: |
| 81 | log.error(r.text) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 82 | 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] | 83 | else: |
| 84 | try: |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame] | 85 | 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] | 86 | except Exception: |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame] | 87 | 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] | 88 | |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 89 | # Now set the port's administrative state. |
| 90 | # TODO(smbaker): See if netcfg allows us to specify the portstate instead of using a separate REST call |
| 91 | |
| 92 | url = 'http://%s:%s/onos/v1/devices/%s/portstate/%s' % (onos.rest_hostname, |
| 93 | onos.rest_port, |
| 94 | model.switch.ofId, |
| 95 | model.portId) |
| 96 | data = {"enabled": True if model.admin_state == "enabled" else False} |
| 97 | log.debug("Sending portstate %s to %s/%s" % (data, model.switch.ofId, model.portId)) |
| 98 | r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
| 99 | |
| 100 | if r.status_code != 200: |
| 101 | log.error(r.text) |
| 102 | raise Exception("Failed to set portstate %s/%s into ONOS" % (model.switch.ofId, model.portId)) |
| 103 | else: |
| 104 | try: |
| 105 | log.info("Portstate %s/%s response" % (model.switch.ofId, model.portId), json=r.json()) |
| 106 | except Exception: |
| 107 | log.info("Portstate %s/%s response" % (model.switch.ofId, model.portId), text=r.text) |
| 108 | |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 109 | def delete_netcfg_item(self, partial_url): |
Scott Baker | 382366d | 2019-02-04 10:58:43 -0800 | [diff] [blame] | 110 | onos = Helpers.get_onos_fabric_service(self.model_accessor) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 111 | 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] | 112 | |
| 113 | r = requests.delete(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
| 114 | |
| 115 | if r.status_code != 204: |
| 116 | log.error(r.text) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 117 | raise Exception("Failed to %s port %s from ONOS" % url) |
| 118 | |
| 119 | def delete_record(self, model): |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 120 | if model.leaf_model_name == "PortInterface": |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 121 | log.info("Received update for PortInterface", port=model.port.portId, interface=model.name) |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 122 | log.info("Removing port interface %s from port %s/%s in onos-fabric" % |
| 123 | (model.name, model.port.switch.ofId, model.port.portId)) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 124 | |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 125 | # resync the existing interfaces |
| 126 | return self.sync_record(model.port) |
| 127 | |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 128 | if model.leaf_model_name == "FabricIpAddress": |
| 129 | # TODO add unit tests |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 130 | log.info( |
| 131 | "Received update for FabricIpAddress", |
| 132 | port=model.interface.port.portId, |
| 133 | interface=model.interface.name, |
| 134 | ip=model.ip) |
| 135 | log.info("Removing IP %s from interface %s, on port %s/%s in onos-fabric" % |
| 136 | (model.ip, model.interface.name, model.interface.port.switch.ofId, model.interface.port.portId)) |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 137 | |
| 138 | # resync the existing interfaces |
| 139 | return self.sync_record(model.interface.port) |
| 140 | |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 141 | log.info("Removing port %s/%s from onos-fabric" % (model.switch.ofId, model.portId)) |
| 142 | |
| 143 | key = "%s/%s" % (model.switch.ofId, model.portId) |
| 144 | key = urllib.quote(key, safe='') |
| 145 | |
| 146 | # deleting the port |
| 147 | self.delete_netcfg_item(key) |