[Git][NTPsec/ntpsec][master] Added MIB tree flattening and inflation functions

Ian Bruene gitlab at mg.gitlab.com
Wed Oct 18 00:10:22 UTC 2017


Ian Bruene pushed to branch master at NTPsec / ntpsec


Commits:
230cf6e9 by Ian Bruene at 2017-10-17T19:09:15-05:00
Added MIB tree flattening and inflation functions

- - - - -


2 changed files:

- pylib/agentx.py
- tests/pylib/test_agentx.py


Changes:

=====================================
pylib/agentx.py
=====================================
--- a/pylib/agentx.py
+++ b/pylib/agentx.py
@@ -1255,6 +1255,37 @@ def decode_packet(data):
         parsedPkt = decoder(packetSlice, header)
     return parsedPkt, newData
 
+def mibTree2List(mibtree, currentPath=()):
+    "Takes a tree of nested dicts representing OIDs and flattens it to a list"
+    if (mibtree is None) or (mibtree == {}):
+        return ()  # Empty tree
+    paths = []
+    branches = mibtree.keys()
+    branches.sort()
+    for branch in branches:
+        paths.append(currentPath + (branch,))
+        branchPath = currentPath + (branch,)
+        paths += mibTree2List(mibtree[branch], branchPath)
+    return tuple(paths)
+
+def mibList2Tree(miblist):
+    "Takes a list of OIDs and inflates it into a tree"
+    tree = {}
+    for node in miblist:
+        branch = tree
+        nodePos = 0
+        nodeSize = len(node)
+        maxNode = nodeSize - 1
+        while nodePos < nodeSize:
+            subid = node[nodePos]
+            if subid not in branch:  # First time at this position
+                branch[subid] = None  # might be a leaf
+            elif branch[subid] is None:  # It isn't a leaf
+                branch[subid] = {}
+            branch = branch[subid]
+            nodePos += 1
+    return tree
+
 
 # Value types
 VALUE_INTEGER = 2


=====================================
tests/pylib/test_agentx.py
=====================================
--- a/tests/pylib/test_agentx.py
+++ b/tests/pylib/test_agentx.py
@@ -2449,6 +2449,42 @@ class TestNtpclientsNtpsnmpd(unittest.TestCase):
             fail = True
         self.assertEqual(fail, True)
 
+    def test_mibTree2List(self):
+        f = ntp.agentx.mibTree2List
+
+        # Test empty tree
+        self.assertEqual(f({}), ())
+        # Test flat tree
+        self.assertEqual(f({0: None, 1: None, 3: None, 4: None}),
+                         ((0,), (1,), (3,), (4,)))
+        # Test nested tree
+        self.assertEqual(f({0: None,
+                            2: {0: None, 1: None},
+                            3: {5: {0: None, 2: None}, 6: None},
+                            4: None}),
+                         ((0,),
+                          (2,), (2, 0), (2, 1),
+                          (3,), (3, 5), (3, 5, 0), (3, 5, 2), (3, 6),
+                          (4,)))
+
+    def test_mibList2Tree(self):
+        f = ntp.agentx.mibList2Tree
+
+        # Test empty tree
+        self.assertEqual(f(tuple()), {})
+        # Test flat tree
+        self.assertEqual(f(((0,), (1,), (3,), (4,))),
+                         {0: None, 1: None, 3: None, 4:None})
+        # Test nested tree
+        self.assertEqual(f(((0,),
+                            (2,), (2, 0), (2, 1),
+                            (3,), (3, 5), (3, 5, 0), (3, 5, 2), (3, 6),
+                            (4,))),
+                         {0: None,
+                          2: {0: None, 1: None},
+                          3: {5: {0: None, 2: None}, 6: None},
+                          4: None})
+
 
 if __name__ == "__main__":
     unittest.main()



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/230cf6e9ab97eccdf57b50f605ddcda56371fcbd

---
View it on GitLab: https://gitlab.com/NTPsec/ntpsec/commit/230cf6e9ab97eccdf57b50f605ddcda56371fcbd
You're receiving this email because of your account on gitlab.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20171018/8dc06819/attachment.html>


More information about the vc mailing list