blob: e2e47ce4d9718ffb3166edea72131d5cace7e278 [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
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 SyncFabricSwitch(SyncStep):
29 provides = [Switch]
30 observes = Switch
31
Luca Preteb601c382018-04-30 16:10:43 -070032 def sync_record(self, model):
33 # Send device info to onos-fabric netcfg
34 data = {
35 "devices": {
36 model.ofId: {
37 "basic": {
38 "name": model.name,
39 "driver": model.driver
40 },
41 "segmentrouting" : {
42 "name" : model.name,
43 "ipv4NodeSid" : model.ipv4NodeSid,
44 "ipv4Loopback" : model.ipv4Loopback,
45 "routerMac" : model.routerMac,
46 "isEdgeRouter" : model.isEdgeRouter,
47 "adjacencySids" : []
48 }
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)
56 r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
57
58 if r.status_code != 200:
59 log.error(r.text)
60 raise Exception("Failed to add device %s into ONOS" % model.name)
61 else:
62 try:
63 print r.json()
64 except Exception:
65 print r.text
66
67 def delete_record(self, switch):
68 pass