blob: 01e7da308dd25eb734e6014100bcb628af672b81 [file] [log] [blame]
Hardik Windlassda0d6c42019-05-13 16:00:01 +00001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os, sys
16sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
17
18from helpers import Helpers
19
20import requests
21from multistructlog import create_logger
22from requests.auth import HTTPBasicAuth
23from xossynchronizer.modelaccessor import TechnologyProfile, model_accessor
24from xossynchronizer.steps.syncstep import SyncStep
25from xosconfig import Config
26
27import etcd3
28
29ETCD_HOST_URL = 'etcd-cluster.default.svc.cluster.local'
30ETCD_PORT = 2379
31
32log = create_logger(Config().get("logging"))
33
34class SyncTechnologyProfile(SyncStep):
35 provides = [TechnologyProfile]
36
37 observes = TechnologyProfile
38
39 def update_etcd(self, operation, key, value):
40 log.info('Update Etcd store: ', operation=operation, key=key, value=value)
41
42 etcd = etcd3.client(host=ETCD_HOST_URL, port=ETCD_PORT)
43 if operation == 'PUT':
44 etcd.put(key, value)
45 log.info('Technology Profile [%s] saved successfully to Etcd store' % key)
46 elif operation == 'DELETE':
47 if False == etcd.delete(key):
48 log.error('Error while deleting Technology Profile [%s] from Etcd store' % key)
49 raise Exception('Failed to delete Technology Profile')
50 else:
51 log.info('Technology Profile [%s] deleted successfully from Etcd store' % key)
52 else:
53 log.warning('Invalid or unsupported Etcd operation: %s' % operation)
54
55 def sync_record(self, model):
56 log.info('Synching TechnologyProfile', object=str(model), **model.tologdict())
57
58 log.info('TechnologyProfile: %s : %s' % (model.technology, model.profile_id))
59
60 tp_key = u'/' + model.technology + u'/' + str(model.profile_id)
61 self.update_etcd('PUT', tp_key, model.profile_value)
62
63 def delete_record(self, model):
64 log.info('Deleting TechnologyProfile', object=str(model), **model.tologdict())
65
66 log.info('TechnologyProfile: %s : %s' % (model.technology, model.profile_id))
67
68 tp_key = u'/' + model.technology + u'/' + str(model.profile_id)
69 self.update_etcd('DELETE', tp_key, None)
70