From e97fd8ebc23aece9826ec713fa175b7a36adffc9 Mon Sep 17 00:00:00 2001 From: Timon Stampfli Date: Fri, 15 Nov 2024 10:43:21 +0100 Subject: [PATCH] Add GetNodeByOID which keeps index --- node.go | 9 +++++++++ smi/node.go | 19 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/node.go b/node.go index 84cf46c..9ae7861 100644 --- a/node.go +++ b/node.go @@ -97,3 +97,12 @@ func GetNodeByOID(oid types.Oid) (node SmiNode, err error) { } return CreateNode(smiNode), nil } + +func GetNodeByOIDWithIndex(oid types.Oid) (node SmiNode, index []types.SmiSubId, err error) { + smiNode, index := smi.GetNodeByOIDWithIndex(oid) + if smiNode == nil { + err = fmt.Errorf("Could not find node for OID %s", oid) + return + } + return CreateNode(smiNode), index, nil +} diff --git a/smi/node.go b/smi/node.go index af94665..8061591 100644 --- a/smi/node.go +++ b/smi/node.go @@ -48,6 +48,25 @@ func GetNodeByOID(oid types.Oid) *types.SmiNode { return nodePtr.FirstObject.GetSmiNode() } +func GetNodeByOIDWithIndex(oid types.Oid) (*types.SmiNode, []types.SmiSubId) { + if len(oid) == 0 || internal.Root() == nil { + return nil, nil + } + var parentPtr, nodePtr *internal.Node = nil, internal.Root() + i := 0 + for ; i < len(oid) && nodePtr != nil; i++ { + parentPtr, nodePtr = nodePtr, nodePtr.Children.Get(oid[i]) + } + if nodePtr == nil { + nodePtr = parentPtr + i-- + } + if nodePtr == nil || nodePtr.FirstObject == nil { + return nil, nil + } + return nodePtr.FirstObject.GetSmiNode(), oid[i:] +} + // SmiNode *smiGetFirstNode(SmiModule *smiModulePtr, SmiNodekind nodekind) func GetFirstNode(smiModulePtr *types.SmiModule, nodekind types.NodeKind) *types.SmiNode { if smiModulePtr == nil {