blob: 15031cfcfa657470b7c6e2b8c64000c808616f1b [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,
Daniele Morofe693152019-09-20 16:53:40 -070043 "driver": model.driver,
44 "pipeconf": model.pipeconf,
45 "managementAddress": model.managementAddress
Scott Bakere9855012019-04-01 15:01:34 -070046 },
47 "segmentrouting": {
48 "name": model.name,
49 "ipv4NodeSid": model.ipv4NodeSid,
50 "ipv4Loopback": model.ipv4Loopback,
51 "routerMac": model.routerMac,
52 "isEdgeRouter": model.isEdgeRouter,
Daniele Morofe693152019-09-20 16:53:40 -070053 "adjacencySids": [],
Scott Bakere9855012019-04-01 15:01:34 -070054 }
55 }
Luca Preteb601c382018-04-30 16:10:43 -070056 }
Luca Preteb601c382018-04-30 16:10:43 -070057 }
58
Scott Baker382366d2019-02-04 10:58:43 -080059 onos = Helpers.get_onos_fabric_service(model_accessor=self.model_accessor)
Luca Preteb601c382018-04-30 16:10:43 -070060
61 url = 'http://%s:%s/onos/v1/network/configuration/' % (onos.rest_hostname, onos.rest_port)
62 r = requests.post(url, json=data, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
63
64 if r.status_code != 200:
65 log.error(r.text)
Daniele Morofe693152019-09-20 16:53:40 -070066 raise Exception("Failed to add device %s into ONOS: %s" % (model.name, r.text))
Luca Preteb601c382018-04-30 16:10:43 -070067 else:
68 try:
Scott Bakere9855012019-04-01 15:01:34 -070069 log.info("result", json=r.json())
Luca Preteb601c382018-04-30 16:10:43 -070070 except Exception:
Scott Bakere9855012019-04-01 15:01:34 -070071 log.info("result", text=r.text)
Luca Preteb601c382018-04-30 16:10:43 -070072
Matteo Scandolo6739b512018-05-30 18:55:29 -070073 def delete_record(self, model):
74 log.info("Removing switch %s from onos-fabric" % model.name)
Scott Baker382366d2019-02-04 10:58:43 -080075 onos = Helpers.get_onos_fabric_service(model_accessor=self.model_accessor)
Matteo Scandolo6739b512018-05-30 18:55:29 -070076 url = 'http://%s:%s/onos/v1/network/configuration/devices/%s' % (
Scott Bakere9855012019-04-01 15:01:34 -070077 onos.rest_hostname, onos.rest_port, model.ofId)
Matteo Scandolo6739b512018-05-30 18:55:29 -070078
79 r = requests.delete(url, auth=HTTPBasicAuth(onos.rest_username, onos.rest_password))
80
81 if r.status_code != 204:
82 log.error(r.text)
83 raise Exception("Failed to remove switch %s from ONOS" % model.name)