blob: 199f8cf1c1171dcbf35f1bee8efed013f87946c9 [file] [log] [blame]
Luca Preteb601c382018-04-30 16:10:43 -07001
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
16import requests
Matteo Scandolodb7adc32018-06-15 16:05:19 -070017import urllib
Luca Preteb601c382018-04-30 16:10:43 -070018from requests.auth import HTTPBasicAuth
Matteo Scandolo6739b512018-05-30 18:55:29 -070019from synchronizers.new_base.syncstep import SyncStep, DeferredException, model_accessor
Matteo Scandolodb7adc32018-06-15 16:05:19 -070020from synchronizers.new_base.modelaccessor import FabricService, SwitchPort, PortInterface
Luca Preteb601c382018-04-30 16:10:43 -070021
22from xosconfig import Config
23from multistructlog import create_logger
24
Matteo Scandolo04e5e122018-05-04 16:21:53 -070025from helpers import Helpers
26
Luca Preteb601c382018-04-30 16:10:43 -070027log = create_logger(Config().get('logging'))
28
29class SyncFabricPort(SyncStep):
30 provides = [SwitchPort]
Matteo Scandolodb7adc32018-06-15 16:05:19 -070031 observes = [SwitchPort, PortInterface]
Luca Preteb601c382018-04-30 16:10:43 -070032
Luca Preteb601c382018-04-30 16:10:43 -070033 def sync_record(self, model):
Matteo Scandolodb7adc32018-06-15 16:05:19 -070034
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 Scandolo6739b512018-05-30 18:55:29 -070039 log.info("Adding port %s/%s to onos-fabric" % (model.switch.ofId, model.portId))
Luca Preteb601c382018-04-30 16:10:43 -070040 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 Scandolodfe75ff2018-06-15 10:52:09 -070052 "ports": {
53 "%s/%s" % (model.switch.ofId, model.portId) : {
54 "interfaces": interfaces,
55 "hostLearning": {
56 "enabled": model.host_learning
57 }
58 }
Luca Preteb601c382018-04-30 16:10:43 -070059 }
Luca Preteb601c382018-04-30 16:10:43 -070060 }
61
Matteo Scandolodfe75ff2018-06-15 10:52:09 -070062 log.debug("Port %s/%s data" % (model.switch.ofId, model.portId), data=data)
63
Matteo Scandolo04e5e122018-05-04 16:21:53 -070064 onos = Helpers.get_onos_fabric_service()
Luca Preteb601c382018-04-30 16:10:43 -070065
66 url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port)
Matteo Scandolo6739b512018-05-30 18:55:29 -070067
Luca Preteb601c382018-04-30 16:10:43 -070068 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 Scandolodb7adc32018-06-15 16:05:19 -070072 raise Exception("Failed to add port %s/%s into ONOS" % (model.switch.ofId, model.portId))
Luca Preteb601c382018-04-30 16:10:43 -070073 else:
74 try:
Matteo Scandolodfe75ff2018-06-15 10:52:09 -070075 log.info("Port %s/%s response" % (model.switch.ofId, model.portId), json=r.json())
Luca Preteb601c382018-04-30 16:10:43 -070076 except Exception:
Matteo Scandolodfe75ff2018-06-15 10:52:09 -070077 log.info("Port %s/%s response" % (model.switch.ofId, model.portId), text=r.text)
Luca Preteb601c382018-04-30 16:10:43 -070078
Matteo Scandolodb7adc32018-06-15 16:05:19 -070079 def delete_netcfg_item(self, partial_url):
Matteo Scandolo6739b512018-05-30 18:55:29 -070080 onos = Helpers.get_onos_fabric_service()
Matteo Scandolodb7adc32018-06-15 16:05:19 -070081 url = 'http://%s:%s/onos/v1/network/configuration/ports/%s' % (onos.rest_hostname, onos.rest_port, partial_url)
Matteo Scandolo6739b512018-05-30 18:55:29 -070082
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 Scandolodb7adc32018-06-15 16:05:19 -070087 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)