blob: 3c36f361cc6c2b35b0a9e56f9c0deadc09308f6a [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
Matteo Scandolo82026252019-06-20 12:11:45 -070029# TODO store ETCD_HOST_URL and ETCD_PORT in the vOLT Service model
Hardik Windlassda0d6c42019-05-13 16:00:01 +000030ETCD_HOST_URL = 'etcd-cluster.default.svc.cluster.local'
31ETCD_PORT = 2379
Matteo Scandolo82026252019-06-20 12:11:45 -070032PREFIX = "service/voltha/technology_profiles"
Hardik Windlassda0d6c42019-05-13 16:00:01 +000033
34log = create_logger(Config().get("logging"))
35
36class SyncTechnologyProfile(SyncStep):
37 provides = [TechnologyProfile]
38
39 observes = TechnologyProfile
40
41 def update_etcd(self, operation, key, value):
42 log.info('Update Etcd store: ', operation=operation, key=key, value=value)
43
44 etcd = etcd3.client(host=ETCD_HOST_URL, port=ETCD_PORT)
45 if operation == 'PUT':
Matteo Scandolo82026252019-06-20 12:11:45 -070046 etcd.put(PREFIX + key, value)
47 log.info('Technology Profile [%s] saved successfully to Etcd store' % (PREFIX + key))
Hardik Windlassda0d6c42019-05-13 16:00:01 +000048 elif operation == 'DELETE':
Matteo Scandolo82026252019-06-20 12:11:45 -070049 if False == etcd.delete(PREFIX + key):
Hardik Windlassda0d6c42019-05-13 16:00:01 +000050 log.error('Error while deleting Technology Profile [%s] from Etcd store' % key)
51 raise Exception('Failed to delete Technology Profile')
52 else:
53 log.info('Technology Profile [%s] deleted successfully from Etcd store' % key)
54 else:
55 log.warning('Invalid or unsupported Etcd operation: %s' % operation)
56
57 def sync_record(self, model):
58 log.info('Synching TechnologyProfile', object=str(model), **model.tologdict())
59
60 log.info('TechnologyProfile: %s : %s' % (model.technology, model.profile_id))
61
62 tp_key = u'/' + model.technology + u'/' + str(model.profile_id)
63 self.update_etcd('PUT', tp_key, model.profile_value)
64
65 def delete_record(self, model):
66 log.info('Deleting TechnologyProfile', object=str(model), **model.tologdict())
67
68 log.info('TechnologyProfile: %s : %s' % (model.technology, model.profile_id))
69
70 tp_key = u'/' + model.technology + u'/' + str(model.profile_id)
71 self.update_etcd('DELETE', tp_key, None)
72