blob: ad16f00880ba391f130a76c96558554bb7baf520 [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
Hardik Windlassda0d6c42019-05-13 16:00:01 +000019import requests
20from multistructlog import create_logger
21from requests.auth import HTTPBasicAuth
22from xossynchronizer.modelaccessor import TechnologyProfile, model_accessor
23from xossynchronizer.steps.syncstep import SyncStep
24from xosconfig import Config
Hardik Windlassda0d6c42019-05-13 16:00:01 +000025import etcd3
26
Matteo Scandolo82026252019-06-20 12:11:45 -070027# TODO store ETCD_HOST_URL and ETCD_PORT in the vOLT Service model
Hardik Windlassda0d6c42019-05-13 16:00:01 +000028ETCD_HOST_URL = 'etcd-cluster.default.svc.cluster.local'
29ETCD_PORT = 2379
Matteo Scandolo82026252019-06-20 12:11:45 -070030PREFIX = "service/voltha/technology_profiles"
Hardik Windlassda0d6c42019-05-13 16:00:01 +000031
32log = create_logger(Config().get("logging"))
33
Matteo Scandolo2044cec2019-06-27 15:30:34 -070034
Hardik Windlassda0d6c42019-05-13 16:00:01 +000035class SyncTechnologyProfile(SyncStep):
36 provides = [TechnologyProfile]
37
38 observes = TechnologyProfile
39
Matteo Scandolo2044cec2019-06-27 15:30:34 -070040 def update_etcd(self, operation, key, value=None):
Matteo Scandolo2b4c8472019-06-26 18:06:47 -070041 log.info('Update Etcd store: ', operation=operation, key=PREFIX + key, value=value)
Hardik Windlassda0d6c42019-05-13 16:00:01 +000042 etcd = etcd3.client(host=ETCD_HOST_URL, port=ETCD_PORT)
43 if operation == 'PUT':
Matteo Scandolo2044cec2019-06-27 15:30:34 -070044 etcd.put(PREFIX + key, value)
45 log.info('Technology Profile [%s] saved successfully to Etcd store' % (PREFIX + key))
46 elif operation == 'GET':
47 return etcd.get(PREFIX + key)
Hardik Windlassda0d6c42019-05-13 16:00:01 +000048 elif operation == 'DELETE':
Matteo Scandolo2044cec2019-06-27 15:30:34 -070049 if False == etcd.delete(PREFIX + key):
50 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)
Hardik Windlassda0d6c42019-05-13 16:00:01 +000054 else:
Matteo Scandolo2044cec2019-06-27 15:30:34 -070055 log.warning('Invalid or unsupported Etcd operation: %s' % operation)
Hardik Windlassda0d6c42019-05-13 16:00:01 +000056
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)
Matteo Scandolo2044cec2019-06-27 15:30:34 -070064 self.update_etcd('PUT', tp_key, value=model.profile_value)
Hardik Windlassda0d6c42019-05-13 16:00:01 +000065
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
Matteo Scandolo2044cec2019-06-27 15:30:34 -070071 tp_key = "/%s/%s" % (model.technology, model.profile_id)
72 [existing_tp, metadata] = self.update_etcd('GET', tp_key)
73 if existing_tp is not None:
74 self.update_etcd('DELETE', tp_key)