blob: fae13e7f641bbaeb570a3c1b792e6015ac14d897 [file] [log] [blame]
Matteo Scandolo0b986e22018-06-04 14:07:33 -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 json
17import requests
18from requests.auth import HTTPBasicAuth
Scott Baker806d4b72019-02-26 19:01:59 -080019from xossynchronizer.steps.syncstep import SyncStep
20from xossynchronizer.modelaccessor import ONOSService, Service, ServiceAttribute, model_accessor
Matteo Scandolo0b986e22018-06-04 14:07:33 -070021
22from xosconfig import Config
23from multistructlog import create_logger
24
25from helpers import Helpers
26
27log = create_logger(Config().get('logging'))
28
Scott Baker7848fa52019-04-01 17:54:36 -070029
Matteo Scandolo0b986e22018-06-04 14:07:33 -070030class 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 Baker7848fa52019-04-01 17:54:36 -070045 return # if it's not related to an ONOSService do nothing
Matteo Scandolo0b986e22018-06-04 14:07:33 -070046
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 Baker7848fa52019-04-01 17:54:36 -070076 Helpers.format_url(onos_service.rest_hostname), onos_service.rest_port)
Matteo Scandolo0b986e22018-06-04 14:07:33 -070077 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))