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