Matteo Scandolo | 0b986e2 | 2018-06-04 14:07:33 -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 json |
| 17 | import requests |
| 18 | from requests.auth import HTTPBasicAuth |
Scott Baker | 806d4b7 | 2019-02-26 19:01:59 -0800 | [diff] [blame] | 19 | from xossynchronizer.steps.syncstep import SyncStep |
| 20 | from xossynchronizer.modelaccessor import ONOSService, Service, ServiceAttribute, model_accessor |
Matteo Scandolo | 0b986e2 | 2018-06-04 14:07:33 -0700 | [diff] [blame] | 21 | |
| 22 | from xosconfig import Config |
| 23 | from multistructlog import create_logger |
| 24 | |
| 25 | from helpers import Helpers |
| 26 | |
| 27 | log = create_logger(Config().get('logging')) |
| 28 | |
Scott Baker | 7848fa5 | 2019-04-01 17:54:36 -0700 | [diff] [blame] | 29 | |
Matteo Scandolo | 0b986e2 | 2018-06-04 14:07:33 -0700 | [diff] [blame] | 30 | class SyncONOSService(SyncStep): |
| 31 | provides = [ONOSService] |
| 32 | observes = [ONOSService, ServiceAttribute] |
| 33 | |
| 34 | def get_service_attribute(self, o): |
| 35 | # NOTE this method is defined in the core convenience methods for services |
| 36 | svc = Service.objects.get(id=o.id) |
| 37 | return svc.serviceattribute_dict |
| 38 | |
| 39 | def sync_record(self, o): |
| 40 | if hasattr(o, 'service'): |
| 41 | # this is a ServiceAttribute model |
| 42 | if 'ONOSService' in o.service.leaf_model.class_names: |
| 43 | print "sync ONOSService Attribute", o.service.leaf_model |
| 44 | return self.sync_record(o.service.leaf_model) |
Scott Baker | 7848fa5 | 2019-04-01 17:54:36 -0700 | [diff] [blame] | 45 | return # if it's not related to an ONOSService do nothing |
Matteo Scandolo | 0b986e2 | 2018-06-04 14:07:33 -0700 | [diff] [blame] | 46 | |
| 47 | onos_url = "%s:%s" % (Helpers.format_url(o.rest_hostname), o.rest_port) |
| 48 | onos_basic_auth = HTTPBasicAuth(o.rest_username, o.rest_password) |
| 49 | |
| 50 | configs = self.get_service_attribute(o) |
| 51 | for url, value in configs.iteritems(): |
| 52 | |
| 53 | if url[0] == "/": |
| 54 | # strip initial / |
| 55 | url = url[1:] |
| 56 | |
| 57 | url = '%s/%s' % (onos_url, url) |
| 58 | value = json.loads(value) |
| 59 | request = requests.post(url, json=value, auth=onos_basic_auth) |
| 60 | |
| 61 | if request.status_code != 200: |
| 62 | log.error("Request failed", response=request.text) |
| 63 | raise Exception("Failed to add config %s in ONOS" % url) |
| 64 | |
| 65 | def delete_record(self, o): |
| 66 | |
| 67 | if hasattr(o, 'service'): |
| 68 | # this is a ServiceAttribute model |
| 69 | if 'ONOSService' in o.service.leaf_model.class_names: |
| 70 | print "sync ONOSService Attribute", o.service.leaf_model |
| 71 | |
| 72 | log.info("Deleting config %s" % o.name) |
| 73 | # getting onos url and auth |
| 74 | onos_service = o.service.leaf_model |
| 75 | onos_url = "%s:%s" % ( |
Scott Baker | 7848fa5 | 2019-04-01 17:54:36 -0700 | [diff] [blame] | 76 | Helpers.format_url(onos_service.rest_hostname), onos_service.rest_port) |
Matteo Scandolo | 0b986e2 | 2018-06-04 14:07:33 -0700 | [diff] [blame] | 77 | onos_basic_auth = HTTPBasicAuth(onos_service.rest_username, |
| 78 | onos_service.rest_password) |
| 79 | |
| 80 | url = o.name |
| 81 | if url[0] == "/": |
| 82 | # strip initial / |
| 83 | url = url[1:] |
| 84 | |
| 85 | url = '%s/%s' % (onos_url, url) |
| 86 | request = requests.delete(url, auth=onos_basic_auth) |
| 87 | |
| 88 | if request.status_code != 204: |
| 89 | log.error("Request failed", response=request.text) |
| 90 | raise Exception("Failed to remove config %s from ONOS: %s" % (url, request.text)) |