blob: b29ec91e399c9d53c4d851340d3dadacceee2acb [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, Switch
20
21from xosconfig import Config
22from multistructlog import create_logger
23
24log = create_logger(Config().get('logging'))
25
26class SyncFabricSwitch(SyncStep):
27 provides = [Switch]
28 observes = Switch
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 # Send device info to onos-fabric netcfg
50 data = {
51 "devices": {
52 model.ofId: {
53 "basic": {
54 "name": model.name,
55 "driver": model.driver
56 },
57 "segmentrouting" : {
58 "name" : model.name,
59 "ipv4NodeSid" : model.ipv4NodeSid,
60 "ipv4Loopback" : model.ipv4Loopback,
61 "routerMac" : model.routerMac,
62 "isEdgeRouter" : model.isEdgeRouter,
63 "adjacencySids" : []
64 }
65 }
66 }
67 }
68
69 onos = self.get_fabric_onos_service()
70
71 url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port)
72 r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
73
74 if r.status_code != 200:
75 log.error(r.text)
76 raise Exception("Failed to add device %s into ONOS" % model.name)
77 else:
78 try:
79 print r.json()
80 except Exception:
81 print r.text
82
83 def delete_record(self, switch):
84 pass