blob: 6d2c2dd42d454303c414d0ca40df095fc24f2ffd [file] [log] [blame]
Chip Boling6e27b352020-02-14 09:10:01 -06001/*
2 * Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
Andrea Campanella7167ebb2020-02-24 09:56:38 +01003 * Copyright 2020-present Open Networking Foundation
4
Chip Boling6e27b352020-02-14 09:10:01 -06005 * 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
Andrea Campanella7167ebb2020-02-24 09:56:38 +01008
Chip Boling6e27b352020-02-14 09:10:01 -06009 * http://www.apache.org/licenses/LICENSE-2.0
Andrea Campanella7167ebb2020-02-24 09:56:38 +010010
Chip Boling6e27b352020-02-14 09:10:01 -060011 * 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 */
Chip Boling34ebcb62021-02-02 12:13:58 -060017/*
Chip Boling6e27b352020-02-14 09:10:01 -060018 * 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 "errors"
28 "fmt"
29 "github.com/deckarep/golang-set"
30 "github.com/google/gopacket"
31 "math/bits"
32)
33
34// ManagedEntityDefinition defines a Manage Entity
35type ManagedEntityDefinition struct {
Chip Boling34ebcb62021-02-02 12:13:58 -060036 Name string
37 ClassID ClassID
38 MessageTypes mapset.Set // Mandatory
Chip Boling6e27b352020-02-14 09:10:01 -060039 AllowedAttributeMask uint16
40 AttributeDefinitions AttributeDefinitionMap
41 Access ClassAccess
42 Support ClassSupport
Chip Boling34ebcb62021-02-02 12:13:58 -060043 Alarms AlarmMap // AlarmBit -> AlarmName
Chip Boling6e27b352020-02-14 09:10:01 -060044}
45
46func (bme *ManagedEntityDefinition) String() string {
47 return fmt.Sprintf("Definition: %s: CID: %v, Attributes: %v",
48 bme.Name, bme.ClassID, bme.AttributeDefinitions)
49}
50
51// GetName retrieves the name of a managed entity from a ME Definition
52func (bme ManagedEntityDefinition) GetName() string {
53 return bme.Name
54}
55
56// GetClassID retrieves the 16-bit class ID of a managed entity from a ME Definition
57func (bme ManagedEntityDefinition) GetClassID() ClassID {
58 return bme.ClassID
59}
60
Chip Boling8a5379a2020-11-17 12:59:40 -060061// SetClassID assigns the 16-bit class ID of a managed entity from a ME Definition
62func (bme *ManagedEntityDefinition) SetClassID(classID ClassID) {
63 bme.ClassID = classID
64}
65
Chip Boling6e27b352020-02-14 09:10:01 -060066// GetMessageTypes retrieves the OMCI Message Types supporte3d by a managed entity from a ME Definition
67func (bme ManagedEntityDefinition) GetMessageTypes() mapset.Set {
68 return bme.MessageTypes
69}
70
71// GetAllowedAttributeMask retrieves the allowed/valid 16-bit attribute mask of a managed entity
72// from a ME Definition
73func (bme ManagedEntityDefinition) GetAllowedAttributeMask() uint16 {
74 return bme.AllowedAttributeMask
75}
76
77// GetAttributeDefinitions retrieves the attribute definitions of a managed entity from a ME Definition
78func (bme ManagedEntityDefinition) GetAttributeDefinitions() AttributeDefinitionMap {
79 return bme.AttributeDefinitions
80}
81
Chip Boling34ebcb62021-02-02 12:13:58 -060082// GetClassSupport returns ONUs support of this class
83func (bme ManagedEntityDefinition) GetClassSupport() ClassSupport {
84 return bme.Support
85}
86
87// GetAlarmMap returns the Alarm bit number to name map
88func (bme ManagedEntityDefinition) GetAlarmMap() AlarmMap {
89 return bme.Alarms
90}
91
Chip Boling6e27b352020-02-14 09:10:01 -060092func (bme ManagedEntityDefinition) DecodeAttributes(mask uint16, data []byte, p gopacket.PacketBuilder, msgType byte) (AttributeValueMap, error) {
Chip Boling934e1052021-09-27 15:12:06 -050093 badMask := (mask | bme.GetAllowedAttributeMask()) ^ bme.GetAllowedAttributeMask()
94
95 var maskErr error
96 if badMask != 0 {
97 maskErr = fmt.Errorf("unsupported attribute mask %#x, valid: %#x for ME %v (Class ID: %d)",
Chip Boling610117d2021-09-09 11:24:34 -050098 mask, bme.GetAllowedAttributeMask(), bme.GetName(), bme.ClassID)
Chip Boling934e1052021-09-27 15:12:06 -050099 mask &= bme.GetAllowedAttributeMask()
Chip Boling6e27b352020-02-14 09:10:01 -0600100 }
Chip Boling934e1052021-09-27 15:12:06 -0500101 // Process known attributes
Chip Boling6e27b352020-02-14 09:10:01 -0600102 keyList := GetAttributeDefinitionMapKeys(bme.AttributeDefinitions)
103
104 attrMap := make(AttributeValueMap, bits.OnesCount16(mask))
105 for _, index := range keyList {
106 if index == 0 {
107 continue // Skip Entity ID
108 }
109 attrDef := bme.AttributeDefinitions[index]
110 name := attrDef.GetName()
111
112 if mask&attrDef.Mask != 0 {
113 value, err := attrDef.Decode(data, p, msgType)
114 if err != nil {
115 return nil, err
116 }
117 if attrDef.IsTableAttribute() {
118 switch msgType {
119 default:
120 return nil, fmt.Errorf("unsupported Message Type '%v' for table serialization", msgType)
121
122 case byte(Get) | AK: // Get Response
123 attrMap[name] = value
124 data = data[4:]
125
126 case byte(GetNext) | AK: // Get Next Response
127 // Value is a partial octet buffer we need to collect and at
128 // the end (last segment) pull it up into more appropriate table
129 // rows
130 valueBuffer, ok := value.([]byte)
131 if !ok {
132 panic("unexpected type already returned as get-next-response attribute data")
133 }
134 if existing, found := attrMap[name]; found {
135 prev, ok := existing.([]byte)
136 if !ok {
137 panic("unexpected type already in attribute value map")
138 }
139 attrMap[name] = append(prev, valueBuffer...)
140 } else {
141 attrMap[name] = valueBuffer
142 }
143 if size := attrDef.GetSize(); size != 0 && size > len(valueBuffer) {
144 panic("unexpected size difference")
145 }
146 data = data[len(valueBuffer):]
147
148 case byte(Set) | AR: // Set Request
Chip Boling157c9b92021-04-21 09:58:36 -0500149 // TODO: No support at this time
Chip Boling6e27b352020-02-14 09:10:01 -0600150
151 case byte(SetTable) | AR: // Set Table Request
Chip Boling610117d2021-09-09 11:24:34 -0500152 attrMap[name] = value
153 data = data[len(data):]
Chip Boling6e27b352020-02-14 09:10:01 -0600154 }
155 } else {
156 attrMap[name] = value
157 data = data[attrDef.GetSize():]
158 }
159 }
160 }
Chip Boling934e1052021-09-27 15:12:06 -0500161 // If badMask is non-zero. Handle it by re-encoding the error as a custom relaxed
162 // decode error that the caller of this decode can process if they wish to relax
163 // the decoding
164 if badMask != 0 {
165 maskErr = NewUnknownAttributeDecodeError(maskErr.Error(), badMask, data)
166 }
167 return attrMap, maskErr
Chip Boling6e27b352020-02-14 09:10:01 -0600168}
169
170func (bme ManagedEntityDefinition) SerializeAttributes(attr AttributeValueMap, mask uint16,
171 b gopacket.SerializeBuffer, msgType byte, bytesAvailable int, packData bool) (error, uint16) {
172
173 if (mask | bme.GetAllowedAttributeMask()) != bme.GetAllowedAttributeMask() {
174 // TODO: Provide custom error code so a response 'result' can properly be coded
175 return errors.New("unsupported attribute mask"), 0
176 }
177 // TODO: Need to limit number of bytes appended to not exceed packet size
178 // Is there space/metadata info in 'b' parameter to allow this?
179 keyList := GetAttributeDefinitionMapKeys(bme.AttributeDefinitions)
180 var failedMask uint16
181
182 for _, index := range keyList {
183 if index == 0 {
184 continue // Skip Entity ID
185 }
186 attrDef := bme.AttributeDefinitions[index]
187
188 if mask&attrDef.Mask != 0 {
189 value, ok := attr[attrDef.GetName()]
190 if !ok {
191 msg := fmt.Sprintf("attribute not found: '%v'", attrDef.GetName())
192 return errors.New(msg), failedMask
193 }
194 size, err := attrDef.SerializeTo(value, b, msgType, bytesAvailable)
195 if err != nil {
196 failedMask |= attrDef.Mask
197 if packData {
198 continue
199 }
200 return err, failedMask
201 }
202 bytesAvailable -= size
203 }
204 }
205 return nil, failedMask
206}