| # Copyright 2017-present Open Networking Foundation |
| # |
| # Licensed under the Apache License, Version 2.0 (the "License"); |
| # you may not use this file except in compliance with the License. |
| # You may obtain a copy of the License at |
| # |
| # http://www.apache.org/licenses/LICENSE-2.0 |
| # |
| # Unless required by applicable law or agreed to in writing, software |
| # distributed under the License is distributed on an "AS IS" BASIS, |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| # See the License for the specific language governing permissions and |
| # limitations under the License. |
| |
| import os, sys |
| sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| |
| from helpers import Helpers |
| |
| import requests |
| from multistructlog import create_logger |
| from requests.auth import HTTPBasicAuth |
| from xossynchronizer.modelaccessor import TechnologyProfile, model_accessor |
| from xossynchronizer.steps.syncstep import SyncStep |
| from xosconfig import Config |
| |
| import etcd3 |
| |
| # TODO store ETCD_HOST_URL and ETCD_PORT in the vOLT Service model |
| ETCD_HOST_URL = 'etcd-cluster.default.svc.cluster.local' |
| ETCD_PORT = 2379 |
| PREFIX = "service/voltha/technology_profiles" |
| |
| log = create_logger(Config().get("logging")) |
| |
| class SyncTechnologyProfile(SyncStep): |
| provides = [TechnologyProfile] |
| |
| observes = TechnologyProfile |
| |
| def update_etcd(self, operation, key, value): |
| log.info('Update Etcd store: ', operation=operation, key=PREFIX + key, value=value) |
| |
| etcd = etcd3.client(host=ETCD_HOST_URL, port=ETCD_PORT) |
| if operation == 'PUT': |
| etcd.put(PREFIX + key, value) |
| log.info('Technology Profile [%s] saved successfully to Etcd store' % (PREFIX + key)) |
| elif operation == 'DELETE': |
| if False == etcd.delete(PREFIX + key): |
| log.error('Error while deleting Technology Profile [%s] from Etcd store' % key) |
| raise Exception('Failed to delete Technology Profile') |
| else: |
| log.info('Technology Profile [%s] deleted successfully from Etcd store' % key) |
| else: |
| log.warning('Invalid or unsupported Etcd operation: %s' % operation) |
| |
| def sync_record(self, model): |
| |
| log.info('Synching TechnologyProfile', object=str(model), **model.tologdict()) |
| |
| log.info('TechnologyProfile: %s : %s' % (model.technology, model.profile_id)) |
| |
| tp_key = u'/' + model.technology + u'/' + str(model.profile_id) |
| self.update_etcd('PUT', tp_key, model.profile_value) |
| |
| def delete_record(self, model): |
| log.info('Deleting TechnologyProfile', object=str(model), **model.tologdict()) |
| |
| log.info('TechnologyProfile: %s : %s' % (model.technology, model.profile_id)) |
| |
| tp_key = u'/' + model.technology + u'/' + str(model.profile_id) |
| self.update_etcd('DELETE', tp_key, None) |
| |