Implemented match all operator for HPA 57/36857/5
authorDileep Ranganathan <dileep.ranganathan@intel.com>
Mon, 19 Mar 2018 15:26:42 +0000 (08:26 -0700)
committerDileep Ranganathan <dileep.ranganathan@intel.com>
Sun, 25 Mar 2018 19:36:18 +0000 (12:36 -0700)
Implemented match all operator for list inputs.
For eg: InstructionSetExetensions which checks for a
input set of requirements in the available capability list.

Change-Id: I3784ae2f85309236f790a0d6a5927ab3ec535b97
Issue-ID: OPTFRA-181
Signed-off-by: Dileep Ranganathan <dileep.ranganathan@intel.com>
conductor/conductor/data/plugins/inventory_provider/hpa_utils.py [new file with mode: 0644]
conductor/conductor/tests/unit/data/plugins/inventory_provider/test_hpa_utils.py [new file with mode: 0644]

diff --git a/conductor/conductor/data/plugins/inventory_provider/hpa_utils.py b/conductor/conductor/data/plugins/inventory_provider/hpa_utils.py
new file mode 100644 (file)
index 0000000..6ed622c
--- /dev/null
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+#
+# -------------------------------------------------------------------------
+#   Copyright (c) 2018 Intel Corporation Intellectual Property
+#
+#   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.
+#
+# -------------------------------------------------------------------------
+#
+
+'''Utility functions for
+   Hardware Platform Awareness (HPA) constraint plugin'''
+
+# python imports
+
+# Conductor imports
+
+# Third-party library imports
+from oslo_log import log
+
+LOG = log.getLogger(__name__)
+
+
+def  match_all_operator(big_list, small_list):
+    '''
+    Match ALL operator for HPA
+    Check if smaller list is a subset of bigger list
+    :param big_list: bigger list
+    :param small_list: smaller list
+    :return: True or False
+    '''
+    if not big_list or not small_list:
+        return False
+
+    big_set = set(big_list)
+    small_set = set(small_list)
+
+    return small_set.issubset(big_set)
diff --git a/conductor/conductor/tests/unit/data/plugins/inventory_provider/test_hpa_utils.py b/conductor/conductor/tests/unit/data/plugins/inventory_provider/test_hpa_utils.py
new file mode 100644 (file)
index 0000000..f5e36c9
--- /dev/null
@@ -0,0 +1,65 @@
+#
+# -------------------------------------------------------------------------
+#   Copyright (c) 2018 Intel Corporation Intellectual Property
+#
+#   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 conductor.data.plugins.inventory_provider import hpa_utils
+
+
+class TestHPAUtils(unittest.TestCase):
+
+    def test_match_all_operator(self):
+        self.assertEqual(False, hpa_utils.match_all_operator(None, None))
+        self.assertEqual(False, hpa_utils.match_all_operator([], None))
+        self.assertEqual(False, hpa_utils.match_all_operator(None, []))
+        self.assertEqual(False, hpa_utils.match_all_operator([], []))
+
+        big_list = ['aes', 'sse', 'avx', 'smt']
+        small_list = ['avx', 'aes']
+        self.assertEqual(True,
+                         hpa_utils.match_all_operator(big_list, small_list))
+
+        big_list = ['aes', 'sse', 'avx', 'smt']
+        small_list = ['avx', 'pcmulqdq']
+        self.assertEqual(False,
+                         hpa_utils.match_all_operator(big_list, small_list))
+
+        big_list = ['aes', 'sse', 'avx', 'smt']
+        small_list = ['aes', 'sse', 'avx', 'smt']
+        self.assertEqual(True,
+                         hpa_utils.match_all_operator(big_list, small_list))
+
+        big_list = ['aes', 'sse', 'avx', 'smt']
+        small_list = ['smt']
+        self.assertEqual(True,
+                         hpa_utils.match_all_operator(big_list, small_list))
+
+        big_list = ['aes']
+        small_list = ['smt']
+        self.assertEqual(False,
+                         hpa_utils.match_all_operator(big_list, small_list))
+
+        # check the order of elements
+        big_list = ['aes', 'sse', 'avx', 'smt']
+        small_list = sorted(big_list)
+        self.assertEqual(True,
+                         hpa_utils.match_all_operator(big_list, small_list))
+
+
+if __name__ == "__main__":
+    unittest.main()