Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 15 | #from __future__ import absolute_import |
| 16 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 17 | import requests |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 18 | import urllib |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 19 | from requests.auth import HTTPBasicAuth |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 20 | from xossynchronizer.steps.syncstep import SyncStep |
| 21 | from xossynchronizer.modelaccessor import SwitchPort, PortInterface, FabricIpAddress, model_accessor |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 22 | |
| 23 | from xosconfig import Config |
| 24 | from multistructlog import create_logger |
| 25 | |
Matteo Scandolo | 04e5e12 | 2018-05-04 16:21:53 -0700 | [diff] [blame] | 26 | from helpers import Helpers |
| 27 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 28 | log = create_logger(Config().get('logging')) |
| 29 | |
| 30 | class SyncFabricPort(SyncStep): |
| 31 | provides = [SwitchPort] |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 32 | observes = [SwitchPort, PortInterface, FabricIpAddress] |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 33 | |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 34 | def sync_record(self, model): |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 35 | |
| 36 | if model.leaf_model_name == "PortInterface": |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 37 | log.info("Received update for PortInterface", port=model.port.portId, interface=model) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 38 | return self.sync_record(model.port) |
| 39 | |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 40 | if model.leaf_model_name == "FabricIpAddress": |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 41 | log.info("Received update for FabricIpAddress", |
| 42 | port=model.interface.port.portId, |
| 43 | interface=model.interface.name, |
| 44 | ip=model.ip) |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 45 | return self.sync_record(model.interface.port) |
| 46 | |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 47 | 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] | 48 | interfaces = [] |
| 49 | for intf in model.interfaces.all(): |
| 50 | i = { |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 51 | "name": intf.name, |
| 52 | "ips": [i.ip for i in intf.ips.all()] |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 53 | } |
| 54 | if intf.vlanUntagged: |
| 55 | i["vlan-untagged"] = intf.vlanUntagged |
| 56 | interfaces.append(i) |
| 57 | |
| 58 | # Send port config to onos-fabric netcfg |
| 59 | data = { |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame] | 60 | "ports": { |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 61 | "%s/%s" % (model.switch.ofId, model.portId): { |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame] | 62 | "interfaces": interfaces, |
| 63 | "hostLearning": { |
| 64 | "enabled": model.host_learning |
| 65 | } |
| 66 | } |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 67 | } |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame] | 70 | log.debug("Port %s/%s data" % (model.switch.ofId, model.portId), data=data) |
| 71 | |
Himanshu Bhandari | 3d2d341 | 2020-04-29 12:08:09 +0530 | [diff] [blame] | 72 | onos = Helpers.get_onos(model.switch, self.model_accessor) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 73 | url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port) |
| 74 | r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
Luca Prete | b601c38 | 2018-04-30 16:10:43 -0700 | [diff] [blame] | 75 | if r.status_code != 200: |
| 76 | log.error(r.text) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 77 | 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] | 78 | else: |
| 79 | try: |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame] | 80 | 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] | 81 | except Exception: |
Matteo Scandolo | dfe75ff | 2018-06-15 10:52:09 -0700 | [diff] [blame] | 82 | log.info("Port %s/%s response" % (model.switch.ofId, model.portId), text=r.text) |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 83 | # Now set the port's administrative state. |
Himanshu Bhandari | 3d2d341 | 2020-04-29 12:08:09 +0530 | [diff] [blame] | 84 | # TODO(smbaker): See if netcfg allows us to specify the portstate instead of using a separate REST call. |
| 85 | url = 'http://%s:%s/onos/v1/devices/%s/portstate/%s' % (onos.rest_hostname, onos.rest_port, model.switch.ofId, model.portId) |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 86 | data = {"enabled": True if model.admin_state == "enabled" else False} |
| 87 | log.debug("Sending portstate %s to %s/%s" % (data, model.switch.ofId, model.portId)) |
| 88 | r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
Scott Baker | 91bf91b | 2019-03-15 16:13:51 -0700 | [diff] [blame] | 89 | if r.status_code != 200: |
| 90 | log.error(r.text) |
| 91 | raise Exception("Failed to set portstate %s/%s into ONOS" % (model.switch.ofId, model.portId)) |
| 92 | else: |
| 93 | try: |
| 94 | log.info("Portstate %s/%s response" % (model.switch.ofId, model.portId), json=r.json()) |
| 95 | except Exception: |
| 96 | log.info("Portstate %s/%s response" % (model.switch.ofId, model.portId), text=r.text) |
| 97 | |
Himanshu Bhandari | 3d2d341 | 2020-04-29 12:08:09 +0530 | [diff] [blame] | 98 | def delete_netcfg_item(self, partial_url, switch, model_accessor): |
| 99 | onos = Helpers.get_onos(switch, model_accessor) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 100 | 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] | 101 | r = requests.delete(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password)) |
Matteo Scandolo | 6739b51 | 2018-05-30 18:55:29 -0700 | [diff] [blame] | 102 | if r.status_code != 204: |
| 103 | log.error(r.text) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 104 | raise Exception("Failed to %s port %s from ONOS" % url) |
| 105 | |
| 106 | def delete_record(self, model): |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 107 | if model.leaf_model_name == "PortInterface": |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 108 | 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] | 109 | log.info("Removing port interface %s from port %s/%s in onos-fabric" % |
| 110 | (model.name, model.port.switch.ofId, model.port.portId)) |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 111 | |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 112 | # resync the existing interfaces |
| 113 | return self.sync_record(model.port) |
| 114 | |
Luca Prete | 1537787 | 2018-09-11 17:32:55 -0700 | [diff] [blame] | 115 | if model.leaf_model_name == "FabricIpAddress": |
| 116 | # TODO add unit tests |
Scott Baker | e985501 | 2019-04-01 15:01:34 -0700 | [diff] [blame] | 117 | log.info( |
| 118 | "Received update for FabricIpAddress", |
| 119 | port=model.interface.port.portId, |
| 120 | interface=model.interface.name, |
| 121 | ip=model.ip) |
| 122 | log.info("Removing IP %s from interface %s, on port %s/%s in onos-fabric" % |
| 123 | (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] | 124 | |
| 125 | # resync the existing interfaces |
| 126 | return self.sync_record(model.interface.port) |
| 127 | |
Matteo Scandolo | db7adc3 | 2018-06-15 16:05:19 -0700 | [diff] [blame] | 128 | log.info("Removing port %s/%s from onos-fabric" % (model.switch.ofId, model.portId)) |
| 129 | |
| 130 | key = "%s/%s" % (model.switch.ofId, model.portId) |
| 131 | key = urllib.quote(key, safe='') |
| 132 | |
| 133 | # deleting the port |
Himanshu Bhandari | 3d2d341 | 2020-04-29 12:08:09 +0530 | [diff] [blame] | 134 | self.delete_netcfg_item(key, model.switch, self.model_accessor) |