blob: 8bec058d370c07929daf96e1500e652d53c12090 [file] [log] [blame]
Matteo Scandolof9d43412021-01-12 11:11:34 -08001/*
2 * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
3 * Copyright 2020-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17/*
18 * NOTE: This file was generated, manual edits will be overwritten!
19 *
20 * Generated by 'goCodeGenerator.py':
21 * https://github.com/cboling/OMCI-parser/README.md
22 */
23
24package generated
25
26import (
27 "encoding/binary"
28 "errors"
29 "fmt"
30 "github.com/deckarep/golang-set"
31 "github.com/google/gopacket"
32)
33
34// ManagedEntity provides a complete instance of a Managed Entity
35type ManagedEntity struct {
36 definition ManagedEntityDefinition
37 attributeMask uint16
38 requestedAttributeMask uint16
39 attributes AttributeValueMap
40}
41
42// String provides a simple string that describes this struct
43func (entity *ManagedEntity) String() string {
44 return fmt.Sprintf("ManagedEntity: %v, EntityID: (%d/%#x): Attributes: %v",
45 entity.GetClassID(), entity.GetEntityID(), entity.GetEntityID(), entity.attributes)
46}
47
48// NewManagedEntity creates a ManagedEntity given an ME Definition and parameter/attribute data
49func NewManagedEntity(definition ManagedEntityDefinition, params ...ParamData) (*ManagedEntity, OmciErrors) {
50 entity := &ManagedEntity{
51 definition: definition,
52 attributes: make(map[string]interface{}),
53 }
54 if params != nil {
55 if err := entity.setAttributes(params...); err.StatusCode() != Success {
56 return nil, err
57 }
58 }
59 return entity, NewOmciSuccess()
60}
61
62// GetManagedEntityDefinition provides the ME definition of a Managed Entity
63func (entity *ManagedEntity) GetManagedEntityDefinition() ManagedEntityDefinition {
64 return entity.definition
65}
66
67// GetName provides the ME Name of a Managed Entity
68func (entity ManagedEntity) GetName() string {
69 return entity.definition.GetName()
70}
71
72// GetClassID returns the 16-bit class ID of a Managed Entity
73func (entity ManagedEntity) GetClassID() ClassID {
74 return entity.definition.GetClassID()
75}
76
77// SetClassID assigns the 16-bit class ID of a Managed Entity
78func (entity *ManagedEntity) SetClassID(classID ClassID) {
79 entity.definition.SetClassID(classID)
80}
81
82// GetMessageTypes returns the OMCI message types that a Managed Entity supports
83func (entity ManagedEntity) GetMessageTypes() mapset.Set {
84 return entity.definition.GetMessageTypes()
85}
86
87// GetAllowedAttributeMask returns the 16-bit bitmask of attributes a Managed Entity supports
88func (entity ManagedEntity) GetAllowedAttributeMask() uint16 {
89 return entity.definition.GetAllowedAttributeMask()
90}
91
92// GetAttributeDefinitions returns the attribute definition map for a Managed Entity
93func (entity ManagedEntity) GetAttributeDefinitions() AttributeDefinitionMap {
94 return entity.definition.GetAttributeDefinitions()
95}
96
97// DecodeAttributes will decode the attributes portion of a Managed Entity frame/packet
98func (entity *ManagedEntity) DecodeAttributes(mask uint16, data []byte, p gopacket.PacketBuilder, msgType byte) (AttributeValueMap, error) {
99 return entity.definition.DecodeAttributes(mask, data, p, msgType)
100}
101
102// SerializeAttributes will serialize the attributes of a Managed Entity type
103func (entity *ManagedEntity) SerializeAttributes(attr AttributeValueMap, mask uint16,
104 b gopacket.SerializeBuffer, msgType byte, bytesAvailable int, packData bool) (error, uint16) {
105 return entity.definition.SerializeAttributes(attr, mask, b, msgType, bytesAvailable, packData)
106}
107
108// GetEntityID will return the Entity/Instance ID for a Managed Entity
109func (entity *ManagedEntity) GetEntityID() uint16 {
110 if eid, err := entity.GetAttributeByIndex(0); err == nil {
111 return eid.(uint16)
112 }
113 return 0
114}
115
116// SetEntityID will set the Entity/Instance ID for a Managed Entity
117func (entity *ManagedEntity) SetEntityID(eid uint16) error {
118 return entity.SetAttributeByIndex(0, eid)
119}
120
121// GetAttributeMask will return the 16-bit attribute mask of a Managed Entity
122func (entity *ManagedEntity) GetAttributeMask() uint16 {
123 return entity.attributeMask
124}
125
126// SetRequestedAttributeMask is used to initialize the requested attribute mask to a specific
127// value. This should only be done on "Get" type operations that need to fetch and attribute
128// and store it in the entity. For other operations (create, set, ...) you should specify
129// the attributes and values in the Params initialization or use the SetAttribute
130func (entity *ManagedEntity) SetRequestedAttributeMask(mask uint16) {
131 entity.requestedAttributeMask = mask
132}
133
134// GetRequestedAttributeMask will return the 16-bit requested attribute mask of a Managed Entity.
135// This is only specified for requests that perform a Get operation
136func (entity *ManagedEntity) GetRequestedAttributeMask() uint16 {
137 return entity.requestedAttributeMask
138}
139
140// GetAttributeValueMap will return the map of attributes of a Managed Entity
141func (entity *ManagedEntity) GetAttributeValueMap() AttributeValueMap {
142 return entity.attributes
143}
144
145// GetAttribute will return the value of a specific attribute for the specified attribute by name
146func (entity *ManagedEntity) GetAttribute(name string) (interface{}, error) {
147 value, ok := entity.attributes[name]
148 if !ok {
149 return 0, fmt.Errorf("attribute '%v' not found", name)
150 }
151 return value, nil
152}
153
154// GetAttributeByIndex will return the value of a specific attribute for the specified attribute by index
155func (entity *ManagedEntity) GetAttributeByIndex(index uint) (interface{}, error) {
156 if len(entity.attributes) == 0 {
157 return nil, errors.New("attributes have already been set")
158 }
159 if _, ok := entity.definition.AttributeDefinitions[index]; !ok {
160 return nil, fmt.Errorf("invalid attribute index: %d, should be 0..%d",
161 index, len(entity.definition.AttributeDefinitions)-1)
162 }
163 return entity.GetAttribute(entity.definition.AttributeDefinitions[index].Name)
164}
165
166func (entity *ManagedEntity) setAttributes(params ...ParamData) OmciErrors {
167 if entity.attributes == nil {
168 entity.attributes = make(map[string]interface{})
169 } else if len(entity.attributes) > 0 {
170 return NewNonStatusError("attributes have already been set")
171 }
172 eidName := entity.definition.AttributeDefinitions[0].Name
173 if len(params) == 0 {
174 entity.attributes[eidName] = uint16(0)
175 return NewOmciSuccess()
176 }
177 entity.attributes[eidName] = params[0].EntityID
178
179 for name, value := range params[0].Attributes {
180 if name == eidName {
181 continue
182 }
183 if err := entity.SetAttribute(name, value); err.StatusCode() != Success {
184 return err
185 }
186 }
187 return NewOmciSuccess()
188}
189
190// SetAttribute can be uses to set the value of a specific attribute by name
191func (entity *ManagedEntity) SetAttribute(name string, value interface{}) OmciErrors {
192 attrDef, err := GetAttributeDefinitionByName(entity.definition.GetAttributeDefinitions(), name)
193 if err != nil {
194 // Not found, which means not in the attribute definition map
195 return NewProcessingError(err.Error())
196 } else if entity.attributes == nil {
197 entity.attributes = make(map[string]interface{})
198 }
199 mask := attrDef.Mask
200 // check any constraints
201 if constraintCheck := attrDef.GetConstraints(); constraintCheck != nil {
202 err = constraintCheck(value)
203 if err != nil {
204 return NewParameterError(mask, entity.GetAttributeDefinitions(), err)
205 }
206 }
207 entity.attributes[name] = value
208 entity.attributeMask |= mask
209 return NewOmciSuccess()
210}
211
212// SetAttributeByIndex can be uses to set the value of a specific attribute by attribute index (0..15)
213func (entity *ManagedEntity) SetAttributeByIndex(index uint, value interface{}) error {
214 attrDef, ok := entity.definition.AttributeDefinitions[index]
215 if !ok {
216 return fmt.Errorf("invalid attribute index: %d, should be 0..%d",
217 index, len(entity.definition.AttributeDefinitions)-1)
218 } else if entity.attributes == nil {
219 entity.attributes = make(map[string]interface{})
220 }
221 mask := attrDef.Mask
222 // check any constraints
223 if constraintCheck := attrDef.GetConstraints(); constraintCheck != nil {
224 err := constraintCheck(value)
225 if err != nil {
226 return NewParameterError(mask, entity.GetAttributeDefinitions(), err)
227 }
228 }
229 entity.attributes[attrDef.Name] = value
230 entity.attributeMask |= mask
231 return nil
232}
233
234// DeleteAttribute is used to remove a specific attribute from a Managed Index by name
235func (entity *ManagedEntity) DeleteAttribute(name string) error {
236 attrDef, err := GetAttributeDefinitionByName(entity.definition.GetAttributeDefinitions(), name)
237 if err != nil {
238 return err
239 }
240 if entity.attributes != nil {
241 delete(entity.attributes, name)
242 entity.attributeMask &= ^attrDef.Mask
243 }
244 return nil
245}
246
247// DeleteAttributeByIndex is used to remove a specific attribute from a Managed Index by attribute index (0..15)
248func (entity *ManagedEntity) DeleteAttributeByIndex(index uint) error {
249 attrDef, ok := entity.definition.AttributeDefinitions[index]
250 if !ok {
251 return fmt.Errorf("invalid attribute index: %d, should be 0..%d",
252 index, len(entity.definition.AttributeDefinitions)-1)
253 }
254 if entity.attributes != nil {
255 delete(entity.attributes, attrDef.Name)
256 entity.attributeMask &= ^attrDef.Mask
257 }
258 return nil
259}
260
261// GetClassSupport returns the ONU support for this managed entity
262func (entity *ManagedEntity) GetClassSupport() ClassSupport {
263 return entity.definition.GetClassSupport()
264}
265
266// GetAlarmMap returns the Alarm Bit Number to Alarm Name (string) mapping. Nil is returned if
267// the managed entity does not support alarms
268func (entity *ManagedEntity) GetAlarmMap() AlarmMap {
269 return entity.definition.GetAlarmMap()
270}
271
272// DecodeFromBytes decodes a Managed Entity give an octet stream pointing to the ME within a frame
273func (entity *ManagedEntity) DecodeFromBytes(data []byte, p gopacket.PacketBuilder, msgType byte) error {
274 if len(data) < 6 {
275 p.SetTruncated()
276 return errors.New("frame too small")
277 }
278 classID := ClassID(binary.BigEndian.Uint16(data[0:2]))
279 entityID := binary.BigEndian.Uint16(data[2:4])
280 parameters := ParamData{EntityID: entityID}
281
282 meDefinition, omciErr := LoadManagedEntityDefinition(classID, parameters)
283 if omciErr.StatusCode() != Success {
284 return omciErr.GetError()
285 }
286 entity.definition = meDefinition.definition
287 entity.attributeMask = binary.BigEndian.Uint16(data[4:6])
288 entity.attributes = make(map[string]interface{})
289 entity.SetEntityID(entityID)
290 packetAttributes, err := entity.DecodeAttributes(entity.GetAttributeMask(), data[6:], p, msgType)
291 if err != nil {
292 return err
293 }
294 for name, value := range packetAttributes {
295 entity.attributes[name] = value
296 }
297 return nil
298}
299
300// SerializeTo serializes a Managed Entity into an octet stream
301func (entity *ManagedEntity) SerializeTo(b gopacket.SerializeBuffer, msgType byte, bytesAvailable int, opts gopacket.SerializeOptions) error {
302 // Add class ID and entity ID
303 bytes, err := b.AppendBytes(6)
304 if err != nil {
305 return err
306 }
307 binary.BigEndian.PutUint16(bytes, uint16(entity.GetClassID()))
308 binary.BigEndian.PutUint16(bytes[2:], entity.GetEntityID())
309 binary.BigEndian.PutUint16(bytes[4:], entity.GetAttributeMask())
310
311 // TODO: Need to limit number of bytes appended to not exceed packet size
312 // Is there space/metadata info in 'b' parameter to allow this?
313 err, _ = entity.SerializeAttributes(entity.attributes, entity.GetAttributeMask(), b,
314 msgType, bytesAvailable, opts.FixLengths)
315 return err
316}