blob: a88874b9b55e88e52feadc52bbf134f80eefcbc5 [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
18from synchronizers.new_base.syncstep import SyncStep, DeferredException
19from 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):
33 interfaces = []
34 for intf in model.interfaces.all():
35 i = {
36 "name" : intf.name,
37 "ips" : [ intf.ips ]
38 }
39 if intf.vlanUntagged:
40 i["vlan-untagged"] = intf.vlanUntagged
41 interfaces.append(i)
42
43 # Send port config to onos-fabric netcfg
44 data = {
45 "ports": {
46 "%s/%s" % (model.switch.ofId, model.portId) : {
47 "interfaces" : interfaces
48 }
49 }
50 }
51
Matteo Scandolo04e5e122018-05-04 16:21:53 -070052 onos = Helpers.get_onos_fabric_service()
Luca Preteb601c382018-04-30 16:10:43 -070053
54 url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port)
55 r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
56
57 if r.status_code != 200:
58 log.error(r.text)
59 raise Exception("Failed to add port %s into ONOS" % model.name)
60 else:
61 try:
62 print r.json()
63 except Exception:
64 print r.text
65
66 def delete_record(self, switch):
67 pass