-
Notifications
You must be signed in to change notification settings - Fork 1
/
smi.go
239 lines (216 loc) · 4.89 KB
/
smi.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright (c) 2019 Arista Networks, Inc.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package smi
import (
"fmt"
// Ensure mibs package doesn't get pruned by dependency management systems
_ "github.com/aristanetworks/cloudvision-go/provider/snmp/smi/mibs"
)
// Object describes an SMI object.
type Object struct {
Access Access
Description string
Indexes []string
Kind Kind
Module string
Name string
Oid string
Status Status
Parent *Object
Children []*Object
}
func (o *Object) String() string {
s := fmt.Sprintf("{Name: %s", o.Name)
s += fmt.Sprintf(", OID: %s", o.Oid)
s += fmt.Sprintf(", Module: %s", o.Module)
s += fmt.Sprintf(", Access: %s", o.Access)
if len(o.Indexes) > 0 {
s += fmt.Sprintf(", Indexes: %v", o.Indexes)
}
s += fmt.Sprintf(", Kind: %s", o.Kind)
return s + "}"
}
// Import describes an imported object and the module it's imported from.
type Import struct {
Object string
Module string
}
func (i Import) String() string {
return fmt.Sprintf("Object: %s, Module: %s", i.Object, i.Module)
}
// Module describes an SMI module. It contains an Object tree and a set
// of imports.
type Module struct {
Name string
ObjectTree []*Object
Imports []Import
}
// Kind describes whether an SMI object is a table, row, column,
// scalar, or something else.
type Kind int
// Kind of SMI object
const (
KindUnknown Kind = iota
KindObject Kind = 1 << (iota - 1)
KindScalar
KindTable
KindRow
KindColumn
KindNotification
KindGroup
KindCompliance
KindCapabilities
KindAny Kind = 0xffff
)
func (k Kind) String() string {
m := map[Kind]string{
KindUnknown: "Unknown",
KindObject: "Object",
KindScalar: "Scalar",
KindTable: "Table",
KindRow: "Row",
KindColumn: "Column",
KindNotification: "Notification",
KindGroup: "Group",
KindCompliance: "Compliance",
KindCapabilities: "Capabilities",
KindAny: "Any",
}
if p, ok := m[k]; ok {
return p
}
return "Unknown"
}
// Access describes an SMI object's access value.
type Access int
// Access types for SMI objects
const (
AccessUnknown Access = iota
AccessNotAccessible
AccessNotify
AccessReadOnly
AccessReadWrite
AccessReadCreate
)
func (a Access) String() string {
m := map[Access]string{
AccessUnknown: "unknown",
AccessNotAccessible: "not-accessible",
AccessNotify: "accessible-for-notify",
AccessReadOnly: "read-only",
AccessReadWrite: "read-write",
AccessReadCreate: "read-create",
}
if s, ok := m[a]; ok {
return s
}
return m[AccessUnknown]
}
func strToAccess(s string) Access {
m := map[string]Access{
"accessible-for-notify": AccessNotify,
"not-accessible": AccessNotAccessible,
"read-only": AccessReadOnly,
"read-write": AccessReadWrite,
"read-create": AccessReadCreate,
}
if a, ok := m[s]; ok {
return a
}
return AccessUnknown
}
// Status describes an SMI object's status value.
type Status int
// Status of SMI object
const (
StatusUnknown Status = iota
StatusCurrent
StatusDeprecated
StatusMandatory
StatusObsolete
StatusOptional
)
func (s Status) String() string {
m := map[Status]string{
StatusUnknown: "unknown",
StatusCurrent: "current",
StatusDeprecated: "deprecated",
StatusMandatory: "mandatory",
StatusObsolete: "obsolete",
StatusOptional: "optional",
}
if st, ok := m[s]; ok {
return st
}
return m[StatusUnknown]
}
func strToStatus(s string) Status {
m := map[string]Status{
"unknown": StatusUnknown,
"current": StatusCurrent,
"deprecated": StatusDeprecated,
"mandatory": StatusMandatory,
"obsolete": StatusObsolete,
"optional": StatusOptional,
}
if st, ok := m[s]; ok {
return st
}
return StatusUnknown
}
// parseObject is effectively an augmented Object. It contains all
// the information the parser needs for a given SMI object that
// doesn't make sense to include in the Object itself.
type parseObject struct {
object *Object
parent *parseObject
children []*parseObject
table bool
subidentifiers []string
decl decl
augments string
}
type parseModule struct {
name string
objectTree []*parseObject
orphans []*parseObject
imports []Import
}
type decl int
const (
declUnknown decl = iota
declImplicitType
declTypeAssignment
declImplSequenceOf
declValueAssignment
declObjectType
declObjectIdentity
declModuleIdentity
declNotificationType
declTrapType
declObjectGroup
declNotificationGroup
declModuleCompliance
declAgentCapabilities
declTextualConvention
declMacro
declComplGroup
declComplObject
declImplObject
declModule
declExtension
declTypedef
declObject
declScalar
declTable
declRow
declColumn
declNotification
declGroup
declCompliance
declIdentity
declClass
declAttribute
declEvent
)