Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 1 | # 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 | |
| 15 | import os, sys |
| 16 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 17 | |
| 18 | from helpers import Helpers |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 19 | import requests |
| 20 | from multistructlog import create_logger |
| 21 | from requests.auth import HTTPBasicAuth |
| 22 | from xossynchronizer.modelaccessor import TechnologyProfile, model_accessor |
| 23 | from xossynchronizer.steps.syncstep import SyncStep |
| 24 | from xosconfig import Config |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 25 | import etcd3 |
| 26 | |
Matteo Scandolo | 8202625 | 2019-06-20 12:11:45 -0700 | [diff] [blame] | 27 | # TODO store ETCD_HOST_URL and ETCD_PORT in the vOLT Service model |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 28 | ETCD_HOST_URL = 'etcd-cluster.default.svc.cluster.local' |
| 29 | ETCD_PORT = 2379 |
Matteo Scandolo | 8202625 | 2019-06-20 12:11:45 -0700 | [diff] [blame] | 30 | PREFIX = "service/voltha/technology_profiles" |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 31 | |
| 32 | log = create_logger(Config().get("logging")) |
| 33 | |
Matteo Scandolo | 2044cec | 2019-06-27 15:30:34 -0700 | [diff] [blame^] | 34 | |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 35 | class SyncTechnologyProfile(SyncStep): |
| 36 | provides = [TechnologyProfile] |
| 37 | |
| 38 | observes = TechnologyProfile |
| 39 | |
Matteo Scandolo | 2044cec | 2019-06-27 15:30:34 -0700 | [diff] [blame^] | 40 | def update_etcd(self, operation, key, value=None): |
Matteo Scandolo | 2b4c847 | 2019-06-26 18:06:47 -0700 | [diff] [blame] | 41 | log.info('Update Etcd store: ', operation=operation, key=PREFIX + key, value=value) |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 42 | etcd = etcd3.client(host=ETCD_HOST_URL, port=ETCD_PORT) |
| 43 | if operation == 'PUT': |
Matteo Scandolo | 2044cec | 2019-06-27 15:30:34 -0700 | [diff] [blame^] | 44 | 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 Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 48 | elif operation == 'DELETE': |
Matteo Scandolo | 2044cec | 2019-06-27 15:30:34 -0700 | [diff] [blame^] | 49 | 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 Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 54 | else: |
Matteo Scandolo | 2044cec | 2019-06-27 15:30:34 -0700 | [diff] [blame^] | 55 | log.warning('Invalid or unsupported Etcd operation: %s' % operation) |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 56 | |
| 57 | def sync_record(self, model): |
Matteo Scandolo | 2b4c847 | 2019-06-26 18:06:47 -0700 | [diff] [blame] | 58 | |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 59 | 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 Scandolo | 2044cec | 2019-06-27 15:30:34 -0700 | [diff] [blame^] | 64 | self.update_etcd('PUT', tp_key, value=model.profile_value) |
Hardik Windlass | da0d6c4 | 2019-05-13 16:00:01 +0000 | [diff] [blame] | 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 | |
Matteo Scandolo | 2044cec | 2019-06-27 15:30:34 -0700 | [diff] [blame^] | 71 | 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) |