blob: 793bfd28f1ea6d06de19fb55c27266988e5481ce [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
17from requests.auth import HTTPBasicAuth
Matteo Scandolo6739b512018-05-30 18:55:29 -070018from synchronizers.new_base.syncstep import SyncStep, DeferredException, model_accessor
Luca Preteb601c382018-04-30 16:10:43 -070019from synchronizers.new_base.modelaccessor import FabricService, SwitchPort
20
21from xosconfig import Config
22from multistructlog import create_logger
23
Matteo Scandolo04e5e122018-05-04 16:21:53 -070024from helpers import Helpers
25
Luca Preteb601c382018-04-30 16:10:43 -070026log = create_logger(Config().get('logging'))
27
28class SyncFabricPort(SyncStep):
29 provides = [SwitchPort]
30 observes = SwitchPort
31
Luca Preteb601c382018-04-30 16:10:43 -070032 def sync_record(self, model):
Matteo Scandolo6739b512018-05-30 18:55:29 -070033 log.info("Adding port %s/%s to onos-fabric" % (model.switch.ofId, model.portId))
Luca Preteb601c382018-04-30 16:10:43 -070034 interfaces = []
35 for intf in model.interfaces.all():
36 i = {
37 "name" : intf.name,
38 "ips" : [ intf.ips ]
39 }
40 if intf.vlanUntagged:
41 i["vlan-untagged"] = intf.vlanUntagged
42 interfaces.append(i)
43
44 # Send port config to onos-fabric netcfg
45 data = {
46 "ports": {
47 "%s/%s" % (model.switch.ofId, model.portId) : {
48 "interfaces" : interfaces
49 }
50 }
51 }
52
Matteo Scandolo04e5e122018-05-04 16:21:53 -070053 onos = Helpers.get_onos_fabric_service()
Luca Preteb601c382018-04-30 16:10:43 -070054
55 url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port)
Matteo Scandolo6739b512018-05-30 18:55:29 -070056
Luca Preteb601c382018-04-30 16:10:43 -070057 r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
58
59 if r.status_code != 200:
60 log.error(r.text)
61 raise Exception("Failed to add port %s into ONOS" % model.name)
62 else:
63 try:
64 print r.json()
65 except Exception:
66 print r.text
67
Matteo Scandolo6739b512018-05-30 18:55:29 -070068 def delete_record(self, model):
69 log.info("Removing port %s/%s from onos-fabric" % (model.switch.ofId, model.portId))
70 onos = Helpers.get_onos_fabric_service()
71 url = 'http://%s:%s/onos/v1/network/configuration/ports/%s/%s' % (onos.rest_hostname, onos.rest_port, model.switch.ofId, model.portId)
72
73 r = requests.delete(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
74
75 if r.status_code != 204:
76 log.error(r.text)
77 raise Exception("Failed to remove port %s from ONOS" % model.name)