blob: 131522f5867044ce94c3df729c580d63c2e1b85f [file] [log] [blame]
Matteo Scandoloa6a3aee2019-11-26 13:30:14 -07001/*
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 */
20package generated
21
22import (
23 "encoding/binary"
24 "errors"
25 "fmt"
26 "github.com/deckarep/golang-set"
27 "github.com/google/gopacket"
28)
29
30type ManagedEntity struct {
31 definition *ManagedEntityDefinition
32 attributeMask uint16
33 attributes AttributeValueMap
34}
35
36// String provides a simple string that describes this struct
37func (entity *ManagedEntity) String() string {
38 return fmt.Sprintf("ManagedEntity: %v, EntityID: (%d/%#x): Attributes: %v",
39 entity.GetClassID(), entity.GetEntityID(), entity.GetEntityID(), entity.attributes)
40}
41
42func NewManagedEntity(definition *ManagedEntityDefinition, params ...ParamData) (*ManagedEntity, OmciErrors) {
43 entity := &ManagedEntity{
44 definition: definition,
45 attributes: make(map[string]interface{}),
46 }
47 if params != nil {
48 err := entity.setAttributes(params...)
49 if err != nil {
50 return nil, err
51 }
52 }
53 return entity, nil
54}
55
56func (entity *ManagedEntity) GetManagedEntityDefinition() *ManagedEntityDefinition {
57 return entity.definition
58}
59
60func (entity *ManagedEntity) SetManagedEntityDefinition(def *ManagedEntityDefinition) {
61 entity.definition = def
62}
63
64func (entity *ManagedEntity) GetName() string {
65 return entity.definition.GetName()
66}
67
68func (entity *ManagedEntity) GetClassID() ClassID {
69 return entity.definition.GetClassID()
70}
71
72func (entity *ManagedEntity) GetMessageTypes() mapset.Set {
73 return entity.definition.GetMessageTypes()
74}
75
76func (entity *ManagedEntity) GetAllowedAttributeMask() uint16 {
77 return entity.definition.GetAllowedAttributeMask()
78}
79
80func (entity *ManagedEntity) GetAttributeDefinitions() *AttributeDefinitionMap {
81 return entity.definition.GetAttributeDefinitions()
82}
83
84func (entity *ManagedEntity) DecodeAttributes(mask uint16, data []byte, p gopacket.PacketBuilder, msgType byte) (AttributeValueMap, error) {
85 return entity.definition.DecodeAttributes(mask, data, p, msgType)
86}
87
88func (entity *ManagedEntity) SerializeAttributes(attr AttributeValueMap, mask uint16,
89 b gopacket.SerializeBuffer, msgType byte, bytesAvailable int) error {
90 return entity.definition.SerializeAttributes(attr, mask, b, msgType, bytesAvailable)
91}
92
93func (entity *ManagedEntity) GetEntityID() uint16 {
94 if eid, err := entity.GetAttributeByIndex(0); err == nil {
95 return eid.(uint16)
96 }
97 return 0
98}
99
100func (entity *ManagedEntity) SetEntityID(eid uint16) error {
101 return entity.SetAttributeByIndex(0, eid)
102}
103
104func (entity *ManagedEntity) GetAttributeMask() uint16 {
105 return entity.attributeMask
106}
107
108func (entity *ManagedEntity) GetAttributeValueMap() *AttributeValueMap {
109 return &entity.attributes
110}
111
112func (entity *ManagedEntity) GetAttribute(name string) (interface{}, error) {
113 value, ok := entity.attributes[name]
114 if !ok {
115 return 0, errors.New(fmt.Sprintf("attribute '%v' not found", name))
116 }
117 return value, nil
118}
119
120func (entity *ManagedEntity) GetAttributeByIndex(index uint) (interface{}, error) {
121 if len(entity.attributes) == 0 {
122 return nil, errors.New("attributes have already been set")
123 }
124 if _, ok := entity.definition.AttributeDefinitions[index]; !ok {
125 return nil, errors.New(fmt.Sprintf("invalid attribute index: %d, should be 0..%d",
126 index, len(entity.definition.AttributeDefinitions)-1))
127 }
128 return entity.GetAttribute(entity.definition.AttributeDefinitions[index].Name)
129}
130
131func (entity *ManagedEntity) setAttributes(params ...ParamData) OmciErrors {
132 if entity.attributes == nil {
133 entity.attributes = make(map[string]interface{})
134 } else if len(entity.attributes) > 0 {
135 return NewNonStatusError("attributes have already been set")
136 }
137 eidName := entity.definition.AttributeDefinitions[0].Name
138 if len(params) == 0 {
139 entity.attributes[eidName] = uint16(0)
140 return nil
141 }
142 entity.attributes[eidName] = params[0].EntityID
143
144 for name, value := range params[0].Attributes {
145 if name == eidName {
146 continue
147 }
148 if err := entity.SetAttribute(name, value); err != nil {
149 return err
150 }
151 }
152 return nil
153}
154
155func (entity *ManagedEntity) SetAttribute(name string, value interface{}) OmciErrors {
156 attrDef, err := GetAttributeDefinitionByName(entity.definition.GetAttributeDefinitions(), name)
157 if err != nil {
158 return err
159 } else if entity.attributes == nil {
160 entity.attributes = make(map[string]interface{})
161 }
162 mask := uint16(1 << (16 - attrDef.GetIndex()))
163 // check any constraints
164 if constraintCheck := attrDef.GetConstraints(); constraintCheck != nil {
165 err = constraintCheck(value)
166 if err != nil {
167 return NewParameterError(mask, err)
168 }
169 }
170 entity.attributes[name] = value
171 entity.attributeMask |= mask
172 return nil
173}
174
175func (entity *ManagedEntity) SetAttributeByIndex(index uint, value interface{}) error {
176 attrDef, ok := entity.definition.AttributeDefinitions[index]
177 if !ok {
178 return errors.New(fmt.Sprintf("invalid attribute index: %d, should be 0..%d",
179 index, len(entity.definition.AttributeDefinitions)-1))
180 } else if entity.attributes == nil {
181 entity.attributes = make(map[string]interface{})
182 }
183 mask := uint16(1 << (16 - attrDef.GetIndex()))
184 // check any constraints
185 if constraintCheck := attrDef.GetConstraints(); constraintCheck != nil {
186 err := constraintCheck(value)
187 if err != nil {
188 return NewParameterError(mask, err)
189 }
190 }
191 entity.attributes[attrDef.Name] = value
192 entity.attributeMask |= mask
193 return nil
194}
195
196func (entity *ManagedEntity) DeleteAttribute(name string) error {
197 attrDef, err := GetAttributeDefinitionByName(entity.definition.GetAttributeDefinitions(), name)
198 if err != nil {
199 return err
200 }
201 if entity.attributes != nil {
202 delete(entity.attributes, name)
203 entity.attributeMask &= ^uint16(1 << (16 - attrDef.GetIndex()))
204 }
205 return nil
206}
207
208func (entity *ManagedEntity) DeleteAttributeByIndex(index uint) error {
209 attrDef, ok := entity.definition.AttributeDefinitions[index]
210 if !ok {
211 return errors.New(fmt.Sprintf("invalid attribute index: %d, should be 0..%d",
212 index, len(entity.definition.AttributeDefinitions)-1))
213 }
214 if entity.attributes != nil {
215 delete(entity.attributes, attrDef.Name)
216 entity.attributeMask &= ^uint16(1 << (16 - attrDef.GetIndex()))
217 }
218 return nil
219}
220
221func (entity *ManagedEntity) DecodeFromBytes(data []byte, p gopacket.PacketBuilder, msgType byte) error {
222 if len(data) < 6 {
223 p.SetTruncated()
224 return errors.New("frame too small")
225 }
226 classID := ClassID(binary.BigEndian.Uint16(data[0:2]))
227 entityID := binary.BigEndian.Uint16(data[2:4])
228 parameters := ParamData{EntityID: entityID}
229
230 meDefinition, omciErr := LoadManagedEntityDefinition(classID, parameters)
231 if omciErr != nil {
232 return omciErr.GetError()
233 }
234 entity.definition = meDefinition.definition
235 entity.attributeMask = binary.BigEndian.Uint16(data[4:6])
236 entity.attributes = make(map[string]interface{})
237 entity.SetEntityID(entityID)
238 packetAttributes, err := entity.DecodeAttributes(entity.GetAttributeMask(), data[6:], p, msgType)
239 if err != nil {
240 return err
241 }
242 for name, value := range packetAttributes {
243 entity.attributes[name] = value
244 }
245 return nil
246}
247
248func (entity *ManagedEntity) SerializeTo(b gopacket.SerializeBuffer, msgType byte, bytesAvailable int) error {
249 // Add class ID and entity ID
250 bytes, err := b.AppendBytes(6)
251 if err != nil {
252 return err
253 }
254 binary.BigEndian.PutUint16(bytes, uint16(entity.GetClassID()))
255 binary.BigEndian.PutUint16(bytes[2:], entity.GetEntityID())
256 binary.BigEndian.PutUint16(bytes[4:], entity.GetAttributeMask())
257
258 // TODO: Need to limit number of bytes appended to not exceed packet size
259 // Is there space/metadata info in 'b' parameter to allow this?
260 err = entity.SerializeAttributes(entity.attributes, entity.GetAttributeMask(), b, msgType, bytesAvailable)
261 return err
262}