blob: 16943e494591fe680f2cf3146bec0e69e3608a32 [file] [log] [blame]
Luca Preteb601c382018-04-30 16:10:43 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Scott Bakere9855012019-04-01 15:01:34 -070015#from __future__ import absolute_import
16
Luca Preteb601c382018-04-30 16:10:43 -070017import requests
18from requests.auth import HTTPBasicAuth
Scott Baker382366d2019-02-04 10:58:43 -080019from xossynchronizer.steps.syncstep import SyncStep
20from xossynchronizer.modelaccessor import FabricService, Switch, model_accessor
Luca Preteb601c382018-04-30 16:10:43 -070021
22from xosconfig import Config
23from multistructlog import create_logger
24
Matteo Scandolo04e5e122018-05-04 16:21:53 -070025from helpers import Helpers
26
Luca Preteb601c382018-04-30 16:10:43 -070027log = create_logger(Config().get('logging'))
28
29class SyncFabricSwitch(SyncStep):
30 provides = [Switch]
31 observes = Switch
32
Luca Preteb601c382018-04-30 16:10:43 -070033 def sync_record(self, model):
Matteo Scandolo6739b512018-05-30 18:55:29 -070034 log.info("Adding switch %s to onos-fabric" % model.name)
Luca Preteb601c382018-04-30 16:10:43 -070035 # Send device info to onos-fabric netcfg
36 data = {
Scott Bakere9855012019-04-01 15:01:34 -070037 "devices": {
38 model.ofId: {
39 "basic": {
40 "name": model.name,
Daniele Morofe693152019-09-20 16:53:40 -070041 "driver": model.driver,
42 "pipeconf": model.pipeconf,
43 "managementAddress": model.managementAddress
Scott Bakere9855012019-04-01 15:01:34 -070044 },
45 "segmentrouting": {
46 "name": model.name,
47 "ipv4NodeSid": model.ipv4NodeSid,
48 "ipv4Loopback": model.ipv4Loopback,
49 "routerMac": model.routerMac,
50 "isEdgeRouter": model.isEdgeRouter,
Daniele Morofe693152019-09-20 16:53:40 -070051 "adjacencySids": [],
Scott Bakere9855012019-04-01 15:01:34 -070052 }
53 }
Luca Preteb601c382018-04-30 16:10:43 -070054 }
Luca Preteb601c382018-04-30 16:10:43 -070055 }
56
Himanshu Bhandari3d2d3412020-04-29 12:08:09 +053057 onos = Helpers.get_onos(model, self.model_accessor)
Luca Preteb601c382018-04-30 16:10:43 -070058 url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port)
59 r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
Luca Preteb601c382018-04-30 16:10:43 -070060 if r.status_code != 200:
61 log.error(r.text)
Daniele Morofe693152019-09-20 16:53:40 -070062 raise Exception("Failed to add device %s into ONOS: %s" % (model.name, r.text))
Luca Preteb601c382018-04-30 16:10:43 -070063 else:
64 try:
Scott Bakere9855012019-04-01 15:01:34 -070065 log.info("result", json=r.json())
Luca Preteb601c382018-04-30 16:10:43 -070066 except Exception:
Scott Bakere9855012019-04-01 15:01:34 -070067 log.info("result", text=r.text)
Luca Preteb601c382018-04-30 16:10:43 -070068
Matteo Scandolo6739b512018-05-30 18:55:29 -070069 def delete_record(self, model):
70 log.info("Removing switch %s from onos-fabric" % model.name)
Himanshu Bhandari3d2d3412020-04-29 12:08:09 +053071 onos = Helpers.get_onos(model, self.model_accessor)
72 url = 'http://%s:%s/onos/v1/network/configuration/devices/%s' % (onos.rest_hostname, onos.rest_port, model.ofId)
Matteo Scandolo6739b512018-05-30 18:55:29 -070073 r = requests.delete(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
Matteo Scandolo6739b512018-05-30 18:55:29 -070074 if r.status_code != 204:
75 log.error(r.text)
76 raise Exception("Failed to remove switch %s from ONOS" % model.name)