forked from sleepinggenius2/gosmi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
99 lines (85 loc) · 2.24 KB
/
node.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package gosmi
import (
"fmt"
"github.com/sleepinggenius2/gosmi/models"
"github.com/sleepinggenius2/gosmi/smi"
"github.com/sleepinggenius2/gosmi/types"
)
type SmiNode struct {
models.Node
smiNode *types.SmiNode
SmiType *SmiType
}
func (n SmiNode) GetModule() (module SmiModule) {
smiModule := smi.GetNodeModule(n.smiNode)
return CreateModule(smiModule)
}
func (n SmiNode) GetSubtree() (nodes []SmiNode) {
first := true
smiNode := n.smiNode
for oidlen := n.OidLen; smiNode != nil && (first || int(smiNode.OidLen) > oidlen); smiNode = smi.GetNextNode(smiNode, types.NodeAny) {
node := CreateNode(smiNode)
nodes = append(nodes, node)
first = false
}
return
}
func (n SmiNode) Render(flags types.Render) string {
return smi.RenderNode(n.smiNode, flags)
}
func (n SmiNode) RenderNumeric() string {
return smi.RenderOID(n.smiNode.Oid, types.RenderNumeric)
}
func (n SmiNode) RenderQualified() string {
return n.Render(types.RenderQualified)
}
func (n SmiNode) GetRaw() (node *types.SmiNode) {
return n.smiNode
}
func (n *SmiNode) SetRaw(smiNode *types.SmiNode) {
n.smiNode = smiNode
}
func CreateNode(smiNode *types.SmiNode) SmiNode {
node := SmiNode{
Node: models.Node{
Access: smiNode.Access,
Decl: smiNode.Decl,
Description: smiNode.Description,
Kind: smiNode.NodeKind,
Name: string(smiNode.Name),
OidLen: smiNode.OidLen,
Oid: smiNode.Oid,
Status: smiNode.Status,
},
smiNode: smiNode,
SmiType: CreateTypeFromNode(smiNode),
}
if node.SmiType != nil {
node.Type = &node.SmiType.Type
}
return node
}
func GetNode(name string, module ...SmiModule) (node SmiNode, err error) {
var smiModule *types.SmiModule
if len(module) > 0 {
smiModule = module[0].GetRaw()
}
smiNode := smi.GetNode(smiModule, name)
if smiNode == nil {
if len(module) > 0 {
err = fmt.Errorf("Could not find node named %s in module %s", name, module[0].Name)
} else {
err = fmt.Errorf("Could not find node named %s", name)
}
return
}
return CreateNode(smiNode), nil
}
func GetNodeByOID(oid types.Oid) (node SmiNode, err error) {
smiNode := smi.GetNodeByOID(oid)
if smiNode == nil {
err = fmt.Errorf("Could not find node for OID %s", oid)
return
}
return CreateNode(smiNode), nil
}