From dea5c7aaee182dda3825849ae39a847bce70aa8b Mon Sep 17 00:00:00 2001 From: Swapnil Wagh Date: Tue, 24 Jan 2017 15:28:04 +0530 Subject: [PATCH 1/2] add support for query_children with hierarchy and class_id as filter Signed-off-by: Swapnil Wagh --- imcsdk/imccoreutils.py | 8 ++++++++ imcsdk/imchandle.py | 10 ++++++---- tests/common/test_query_children.py | 13 +++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/imcsdk/imccoreutils.py b/imcsdk/imccoreutils.py index 47d5ec1f..e0c6bea6 100644 --- a/imcsdk/imccoreutils.py +++ b/imcsdk/imccoreutils.py @@ -326,6 +326,14 @@ def extract_molist_from_method_response(method_response, return mo_list +def filter_molist_on_class_id(mo_list, class_id=None): + if class_id is None: + return mo_list + + out_list = [mo for mo in mo_list if mo._class_id.lower() == class_id.lower()] + return out_list + + def write_mo_tree(mo, level=0, depth=None, show_level=[], print_tree=True, tree_dict={}, dn=None): """ diff --git a/imcsdk/imchandle.py b/imcsdk/imchandle.py index 6fa006c2..3d981f04 100644 --- a/imcsdk/imchandle.py +++ b/imcsdk/imchandle.py @@ -322,13 +322,12 @@ def query_children(self, in_mo=None, in_dn=None, class_id=None, elif in_dn: parent_dn = in_dn - if class_id: + meta_class_id = None + if class_id and not hierarchy: meta_class_id = imccoreutils.find_class_id_in_mo_meta_ignore_case( class_id) if not meta_class_id: meta_class_id = class_id - else: - meta_class_id = class_id elem = config_resolve_children(cookie=self.cookie, class_id=meta_class_id, @@ -342,7 +341,10 @@ def query_children(self, in_mo=None, in_dn=None, class_id=None, out_mo_list = imccoreutils.extract_molist_from_method_response(response, hierarchy ) - + if class_id and hierarchy: + out_mo_list = imccoreutils.filter_molist_on_class_id( + out_mo_list, + class_id=class_id) return out_mo_list def add_mo(self, mo, modify_present=True, timeout=None): diff --git a/tests/common/test_query_children.py b/tests/common/test_query_children.py index ea56fe75..5a7b0769 100644 --- a/tests/common/test_query_children.py +++ b/tests/common/test_query_children.py @@ -13,6 +13,7 @@ from nose.tools import assert_equal from ..connection.info import custom_setup, custom_teardown +from imcsdk.imccoreutils import IMC_PLATFORM handle = None @@ -30,3 +31,15 @@ def test_default(): mos = handle.query_children(in_dn="sys/user-ext", class_id="aaaUser") for mo in mos: assert_equal(mo._class_id, "AaaUser") + + +def test_hierarchy(): + mos = handle.query_children(in_dn="sys/svc-ext", hierarchy=True) + assert_equal(len(mos) > 30, True) + + +def test_hierarchy_with_class_id(): + mos = handle.query_children(in_dn="sys/svc-ext", class_id="commSnmpUser", + hierarchy=True) + for mo in mos: + assert_equal(mo._class_id, "CommSnmpUser") From 386a159dabbd70e0b06499eea8cdd24efef78193 Mon Sep 17 00:00:00 2001 From: Swapnil Wagh Date: Wed, 1 Feb 2017 12:04:13 +0530 Subject: [PATCH 2/2] add comments and an extra testcase Signed-off-by: Swapnil Wagh --- imcsdk/imccoreutils.py | 2 +- imcsdk/imchandle.py | 5 +++++ tests/common/test_query_children.py | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/imcsdk/imccoreutils.py b/imcsdk/imccoreutils.py index e0c6bea6..6ef00faf 100644 --- a/imcsdk/imccoreutils.py +++ b/imcsdk/imccoreutils.py @@ -327,7 +327,7 @@ def extract_molist_from_method_response(method_response, def filter_molist_on_class_id(mo_list, class_id=None): - if class_id is None: + if not class_id: return mo_list out_list = [mo for mo in mo_list if mo._class_id.lower() == class_id.lower()] diff --git a/imcsdk/imchandle.py b/imcsdk/imchandle.py index 3d981f04..627719d5 100644 --- a/imcsdk/imchandle.py +++ b/imcsdk/imchandle.py @@ -323,6 +323,11 @@ def query_children(self, in_mo=None, in_dn=None, class_id=None, parent_dn = in_dn meta_class_id = None + # Setting the default class-id to None + # When hierarchy and class-id are passed together to Cisco IMC, + # an empty response is received. + # Hence, passing the class-id only when hierarchy is not set + # When both hierarchy and class-id are set, do local filtering for class-id if class_id and not hierarchy: meta_class_id = imccoreutils.find_class_id_in_mo_meta_ignore_case( class_id) diff --git a/tests/common/test_query_children.py b/tests/common/test_query_children.py index 5a7b0769..36d862a6 100644 --- a/tests/common/test_query_children.py +++ b/tests/common/test_query_children.py @@ -38,6 +38,11 @@ def test_hierarchy(): assert_equal(len(mos) > 30, True) +def test_hierarchy_with_empty_class_id(): + mos = handle.query_children(in_dn="sys/svc-ext", hierarchy=True, class_id="") + assert_equal(len(mos) > 30, True) + + def test_hierarchy_with_class_id(): mos = handle.query_children(in_dn="sys/svc-ext", class_id="commSnmpUser", hierarchy=True)