blob: 0615b0f92b5bf7482d3dca002875a0c7a379f621 [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
24log = create_logger(Config().get('logging'))
25
26class SyncFabricPort(SyncStep):
27 provides = [SwitchPort]
28 observes = SwitchPort
29
30 def get_fabric_onos_service_internal(self):
31 # There will be a ServiceInstanceLink from the FabricService to the Fabric ONOS App
32 fs = FabricService.objects.first()
33 for link in fs.subscribed_links.all():
34 if link.provider_service_instance:
35 # Cast from ServiceInstance to ONOSApp
36 service_instance = link.provider_service_instance.leaf_model
37 # Cast from Service to ONOSService
38 return service_instance.owner.leaf_model
39
40 return None
41
42 def get_fabric_onos_service(self):
43 fos = self.get_fabric_onos_service_internal()
44 if not fos:
45 raise Exception("Fabric ONOS service not found")
46 return fos
47
48 def sync_record(self, model):
49 interfaces = []
50 for intf in model.interfaces.all():
51 i = {
52 "name" : intf.name,
53 "ips" : [ intf.ips ]
54 }
55 if intf.vlanUntagged:
56 i["vlan-untagged"] = intf.vlanUntagged
57 interfaces.append(i)
58
59 # Send port config to onos-fabric netcfg
60 data = {
61 "ports": {
62 "%s/%s" % (model.switch.ofId, model.portId) : {
63 "interfaces" : interfaces
64 }
65 }
66 }
67
68 onos = self.get_fabric_onos_service()
69
70 url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port)
71 r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
72
73 if r.status_code != 200:
74 log.error(r.text)
75 raise Exception("Failed to add port %s into ONOS" % model.name)
76 else:
77 try:
78 print r.json()
79 except Exception:
80 print r.text
81
82 def delete_record(self, switch):
83 pass