Removing a tech-profile from the data-model if that's not found in etcd
Change-Id: Ibd2848cf417eed8a06881c6d9d080728db5965e9
diff --git a/xos/synchronizer/steps/sync_tech_profile.py b/xos/synchronizer/steps/sync_tech_profile.py
index dcd5f52..ad16f00 100644
--- a/xos/synchronizer/steps/sync_tech_profile.py
+++ b/xos/synchronizer/steps/sync_tech_profile.py
@@ -16,14 +16,12 @@
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
@@ -33,26 +31,28 @@
log = create_logger(Config().get("logging"))
+
class SyncTechnologyProfile(SyncStep):
provides = [TechnologyProfile]
observes = TechnologyProfile
- def update_etcd(self, operation, key, value):
+ def update_etcd(self, operation, key, value=None):
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))
+ etcd.put(PREFIX + key, value)
+ log.info('Technology Profile [%s] saved successfully to Etcd store' % (PREFIX + key))
+ elif operation == 'GET':
+ return etcd.get(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)
+ 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)
+ log.warning('Invalid or unsupported Etcd operation: %s' % operation)
def sync_record(self, model):
@@ -61,13 +61,14 @@
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)
+ self.update_etcd('PUT', tp_key, value=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)
-
+ tp_key = "/%s/%s" % (model.technology, model.profile_id)
+ [existing_tp, metadata] = self.update_etcd('GET', tp_key)
+ if existing_tp is not None:
+ self.update_etcd('DELETE', tp_key)
diff --git a/xos/synchronizer/steps/test_sync_onu_device.py b/xos/synchronizer/steps/test_sync_onu_device.py
index aba57aa..bf85bde 100644
--- a/xos/synchronizer/steps/test_sync_onu_device.py
+++ b/xos/synchronizer/steps/test_sync_onu_device.py
@@ -20,7 +20,7 @@
test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
-class TestSyncVOLTServiceInstance(unittest.TestCase):
+class TestSyncONUDevice(unittest.TestCase):
def setUp(self):
self.sys_path_save = sys.path
diff --git a/xos/synchronizer/steps/test_sync_tech_profile.py b/xos/synchronizer/steps/test_sync_tech_profile.py
new file mode 100644
index 0000000..38e4202
--- /dev/null
+++ b/xos/synchronizer/steps/test_sync_tech_profile.py
@@ -0,0 +1,99 @@
+# 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 unittest
+from mock import patch, call, Mock, PropertyMock
+import requests_mock
+
+import os, sys
+
+test_path=os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
+
+class TestSyncTechProfile(unittest.TestCase):
+ def setUp(self):
+
+ self.mock_etcd = Mock(name="etcd-client")
+ etcd = Mock(name="etcd-mocked-lib")
+ etcd.client.return_value = self.mock_etcd
+ modules = {
+ 'etcd3': etcd
+ }
+ self.module_patcher = patch.dict('sys.modules', modules)
+ self.module_patcher.start()
+
+ self.sys_path_save = sys.path
+
+ # Setting up the config module
+ from xosconfig import Config
+ config = os.path.join(test_path, "../test_config.yaml")
+ Config.clear()
+ Config.init(config, "synchronizer-config-schema.yaml")
+ # END Setting up the config module
+
+ from xossynchronizer.mock_modelaccessor_build import mock_modelaccessor_config
+ mock_modelaccessor_config(test_path, [("olt-service", "volt.xproto"),
+ ("rcord", "rcord.xproto")])
+
+ import xossynchronizer.modelaccessor
+ reload(xossynchronizer.modelaccessor) # in case nose2 loaded it in a previous test
+
+ from xossynchronizer.modelaccessor import model_accessor
+ self.model_accessor = model_accessor
+
+ from xossynchronizer.steps.syncstep import DeferredException
+ from sync_tech_profile import SyncTechnologyProfile, model_accessor
+
+ # import all class names to globals
+ for (k, v) in model_accessor.all_model_classes.items():
+ globals()[k] = v
+
+ self.sync_step = SyncTechnologyProfile
+
+ self.o = Mock()
+ self.o.technology = "test_technology"
+ self.o.profile_id = 64
+ self.o.profile_value = '{"test":"profile"}'
+
+ self.o.tologdict.return_value = {'name': "mock-tp"}
+
+ def tearDown(self):
+ self.o = None
+ sys.path = self.sys_path_save
+ self.module_patcher.stop()
+
+ def test_sync(self):
+
+ self.sync_step(model_accessor=self.model_accessor).sync_record(self.o)
+ self.mock_etcd.put.assert_called_with('service/voltha/technology_profiles/test_technology/64',
+ '{"test":"profile"}')
+
+ def test_delete(self):
+
+ self.mock_etcd.get.return_value = [self.o.profile_value, "response from mock-etcd"]
+
+ self.sync_step(model_accessor=self.model_accessor).delete_record(self.o)
+ self.mock_etcd.get.assert_called_with('service/voltha/technology_profiles/test_technology/64')
+ self.mock_etcd.delete.assert_called_with('service/voltha/technology_profiles/test_technology/64')
+
+ def test_delete_missing_object(self):
+
+ self.mock_etcd.get.return_value = [None, "response from mock-etcd"]
+
+ self.sync_step(model_accessor=self.model_accessor).delete_record(self.o)
+ self.mock_etcd.get.assert_called_with('service/voltha/technology_profiles/test_technology/64')
+ self.mock_etcd.delete.assert_not_called()
+
+
+if __name__ == "__main__":
+ unittest.main()