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