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