blob: dcd5f5202d32bb99c7c1b9dd62daa4b77a0c7b10 [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):
Matteo Scandolo2b4c8472019-06-26 18:06:47 -070042 log.info('Update Etcd store: ', operation=operation, key=PREFIX + key, value=value)
Hardik Windlassda0d6c42019-05-13 16:00:01 +000043
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):
Matteo Scandolo2b4c8472019-06-26 18:06:47 -070058
Hardik Windlassda0d6c42019-05-13 16:00:01 +000059 log.info('Synching TechnologyProfile', object=str(model), **model.tologdict())
60
61 log.info('TechnologyProfile: %s : %s' % (model.technology, model.profile_id))
62
63 tp_key = u'/' + model.technology + u'/' + str(model.profile_id)
64 self.update_etcd('PUT', tp_key, model.profile_value)
65
66 def delete_record(self, model):
67 log.info('Deleting TechnologyProfile', object=str(model), **model.tologdict())
68
69 log.info('TechnologyProfile: %s : %s' % (model.technology, model.profile_id))
70
71 tp_key = u'/' + model.technology + u'/' + str(model.profile_id)
72 self.update_etcd('DELETE', tp_key, None)
73