blob: 328f6a6360c4b29257d83a1f451d46b18f278515 [file] [log] [blame]
Luca Prete4259b992018-05-04 10:02:03 -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
Scott Baker81389db2019-02-04 15:13:38 -080018from xossynchronizer.steps.syncstep import SyncStep, DeferredException
19from xossynchronizer.modelaccessor import VRouterStaticRoute, model_accessor
Luca Prete4259b992018-05-04 10:02:03 -070020
21from helpers import Helpers
22from multistructlog import create_logger
23from xosconfig import Config
24
25log = create_logger(Config().get('logging'))
26
27class SyncRoutes(SyncStep):
28 provides = [VRouterStaticRoute]
29 observes = VRouterStaticRoute
30
31 # Get fabric service info
32 def get_onos_fabric_service(self, model):
33 vrouter_service = model.vrouter.owner
Matteo Scandolo181140e2018-08-30 15:17:09 -070034 fabric_service = vrouter_service.provider_services[0]
35 onos_fabric_service = fabric_service.provider_services[0]
Luca Prete4259b992018-05-04 10:02:03 -070036
37 return onos_fabric_service
38
39 def sync_record(self, model):
40 onos_fabric_service = self.get_onos_fabric_service(model)
41
42 onos = Helpers.get_onos_info(onos_fabric_service.leaf_model)
43 onos_basic_auth = HTTPBasicAuth(onos['user'], onos['pass'])
44
45 data = {
46 "prefix": model.prefix,
47 "nextHop": model.next_hop
48 }
49
50 url = '%s:%s/onos/routeservice/routes' % (onos['url'], onos['port'])
51 request = requests.post(url, json=data, auth=onos_basic_auth)
52
53 if request.status_code != 204:
54 log.error("Request failed", response=request.text)
55 raise Exception("Failed to add static route %s via %s in ONOS" % (model.prefix, model.next_hop))
56 else:
57 try:
58 print request.json()
59 except Exception:
60 print request.text
61
62 def delete_record(self, model):
63 onos_fabric_service = self.get_onos_fabric_service(model)
64
65 onos = Helpers.get_onos_info(onos_fabric_service.leaf_model)
66 onos_basic_auth = HTTPBasicAuth(onos['user'], onos['pass'])
67
68 data = {
69 "prefix": model.prefix,
70 "nextHop": model.next_hop
71 }
72
73 url = '%s:%s/onos/routeservice/routes' % (onos['url'], onos['port'])
74 request = requests.delete(url, json=data, auth=onos_basic_auth)
75
76 if request.status_code != 204:
77 log.error("Request failed", response=request.text)
78 raise Exception("Failed to delete static route %s via %s in ONOS" % (model.prefix, model.next_hop))
79 else:
80 try:
81 print request.json()
82 except Exception:
83 print request.text