Luca Prete | 4259b99 | 2018-05-04 10:02:03 -0700 | [diff] [blame] | 1 | |
| 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 | |
| 16 | import requests |
| 17 | from requests.auth import HTTPBasicAuth |
| 18 | from synchronizers.new_base.syncstep import SyncStep, DeferredException |
Matteo Scandolo | 769a5a4 | 2018-05-31 16:43:39 -0700 | [diff] [blame] | 19 | from synchronizers.new_base.modelaccessor import VRouterStaticRoute, model_accessor |
Luca Prete | 4259b99 | 2018-05-04 10:02:03 -0700 | [diff] [blame] | 20 | |
| 21 | from helpers import Helpers |
| 22 | from multistructlog import create_logger |
| 23 | from xosconfig import Config |
| 24 | |
| 25 | log = create_logger(Config().get('logging')) |
| 26 | |
| 27 | class 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 Scandolo | 181140e | 2018-08-30 15:17:09 -0700 | [diff] [blame] | 34 | fabric_service = vrouter_service.provider_services[0] |
| 35 | onos_fabric_service = fabric_service.provider_services[0] |
Luca Prete | 4259b99 | 2018-05-04 10:02:03 -0700 | [diff] [blame] | 36 | |
| 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 |