blob: cac7ac565ef7eaebe3578c9e10c17e392df1a070 [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
Scott Bakere9855012019-04-01 15:01:34 -070016#from __future__ import absolute_import
17
Luca Preteb601c382018-04-30 16:10:43 -070018import requests
19from requests.auth import HTTPBasicAuth
Scott Baker382366d2019-02-04 10:58:43 -080020from xossynchronizer.steps.syncstep import SyncStep
21from xossynchronizer.modelaccessor import FabricService, Switch, model_accessor
Luca Preteb601c382018-04-30 16:10:43 -070022
23from xosconfig import Config
24from multistructlog import create_logger
25
Matteo Scandolo04e5e122018-05-04 16:21:53 -070026from helpers import Helpers
27
Luca Preteb601c382018-04-30 16:10:43 -070028log = create_logger(Config().get('logging'))
29
Scott Bakere9855012019-04-01 15:01:34 -070030
Luca Preteb601c382018-04-30 16:10:43 -070031class SyncFabricSwitch(SyncStep):
32 provides = [Switch]
33 observes = Switch
34
Luca Preteb601c382018-04-30 16:10:43 -070035 def sync_record(self, model):
Matteo Scandolo6739b512018-05-30 18:55:29 -070036 log.info("Adding switch %s to onos-fabric" % model.name)
Luca Preteb601c382018-04-30 16:10:43 -070037 # Send device info to onos-fabric netcfg
38 data = {
Scott Bakere9855012019-04-01 15:01:34 -070039 "devices": {
40 model.ofId: {
41 "basic": {
42 "name": model.name,
43 "driver": model.driver
44 },
45 "segmentrouting": {
46 "name": model.name,
47 "ipv4NodeSid": model.ipv4NodeSid,
48 "ipv4Loopback": model.ipv4Loopback,
49 "routerMac": model.routerMac,
50 "isEdgeRouter": model.isEdgeRouter,
51 "adjacencySids": []
52 }
53 }
Luca Preteb601c382018-04-30 16:10:43 -070054 }
Luca Preteb601c382018-04-30 16:10:43 -070055 }
56
Scott Baker382366d2019-02-04 10:58:43 -080057 onos = Helpers.get_onos_fabric_service(model_accessor=self.model_accessor)
Luca Preteb601c382018-04-30 16:10:43 -070058
59 url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port)
60 r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
61
62 if r.status_code != 200:
63 log.error(r.text)
64 raise Exception("Failed to add device %s into ONOS" % model.name)
65 else:
66 try:
Scott Bakere9855012019-04-01 15:01:34 -070067 log.info("result", json=r.json())
Luca Preteb601c382018-04-30 16:10:43 -070068 except Exception:
Scott Bakere9855012019-04-01 15:01:34 -070069 log.info("result", text=r.text)
Luca Preteb601c382018-04-30 16:10:43 -070070
Matteo Scandolo6739b512018-05-30 18:55:29 -070071 def delete_record(self, model):
72 log.info("Removing switch %s from onos-fabric" % model.name)
Scott Baker382366d2019-02-04 10:58:43 -080073 onos = Helpers.get_onos_fabric_service(model_accessor=self.model_accessor)
Matteo Scandolo6739b512018-05-30 18:55:29 -070074 url = 'http://%s:%s/onos/v1/network/configuration/devices/%s' % (
Scott Bakere9855012019-04-01 15:01:34 -070075 onos.rest_hostname, onos.rest_port, model.ofId)
Matteo Scandolo6739b512018-05-30 18:55:29 -070076
77 r = requests.delete(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
78
79 if r.status_code != 204:
80 log.error(r.text)
81 raise Exception("Failed to remove switch %s from ONOS" % model.name)