-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsettable.go
302 lines (279 loc) · 11 KB
/
settable.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Copyright (c) 2018 - present. Boling Consulting Solutions (bcsw.net)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package omci
import (
"encoding/binary"
"errors"
"fmt"
me "github.com/cboling/omci/v2/generated"
"github.com/google/gopacket"
"math/bits"
)
type SetTableRequest struct {
MeBasePacket
AttributeMask uint16
// Attributes below should be a single attribute whose value is of type TableRows
Attributes me.AttributeValueMap
}
func (omci *SetTableRequest) String() string {
return fmt.Sprintf("%v", omci.MeBasePacket.String())
}
// LayerType returns LayerTypeSetTableRequest
func (omci *SetTableRequest) LayerType() gopacket.LayerType {
return LayerTypeSetTableRequest
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *SetTableRequest) CanDecode() gopacket.LayerClass {
return LayerTypeSetTableRequest
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *SetTableRequest) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of a Set Table Request into this layer
func (omci *SetTableRequest) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Only supported in the Extended message set
if !omci.Extended {
return me.NewNotSupportedError("baseline message set not supported by SetTable Message-Type")
}
// Common ClassID/EntityID decode in msgBase
hdrSize := 6 + 2
if len(data) < hdrSize {
p.SetTruncated()
return errors.New("frame too small")
} // Common ClassID/EntityID decode in msgBase
err := omci.MeBasePacket.DecodeFromBytes(data, p, 6+2)
if err != nil {
return err
}
meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support SetTable
if !me.SupportsMsgType(meDefinition, me.SetTable) {
return me.NewProcessingError("managed entity does not support SetTable Message-Type")
}
offset := hdrSize - 2
omci.AttributeMask = binary.BigEndian.Uint16(data[offset:])
// Only a single attribute bit can be set
if bits.OnesCount16(omci.AttributeMask) != 1 {
return me.NewProcessingError("only a single attribute can be specified for the SetTable Message-Type")
}
// Attribute decode
omci.Attributes, err = meDefinition.DecodeAttributes(omci.AttributeMask, data[hdrSize:], p, byte(SetTableRequestType))
if err != nil {
return err
}
// Validate that the selected attribute support write and is a table
for attrName := range omci.Attributes {
attr, err := me.GetAttributeDefinitionByName(meDefinition.GetAttributeDefinitions(), attrName)
if err != nil {
return err
}
if attr.Index != 0 && attr.Mask == omci.AttributeMask {
if !me.SupportsAttributeAccess(*attr, me.Write) {
msg := fmt.Sprintf("attribute '%v' does not support write access", attrName)
return me.NewProcessingError(msg)
}
if !attr.IsTableAttribute() {
msg := fmt.Sprintf("attribute '%v' must be a table attribute for a SetTable Message-Type", attrName)
return me.NewProcessingError(msg)
}
break
}
}
if eidDef, eidDefOK := meDefinition.GetAttributeDefinitions()[0]; eidDefOK {
omci.Attributes[eidDef.GetName()] = omci.EntityInstance
return nil
}
return me.NewProcessingError("All Managed Entities have an EntityID attribute")
}
func decodeSetTableRequest(data []byte, p gopacket.PacketBuilder) error {
return me.NewNotSupportedError("baseline message set not supported by SetTable Message-Type")
}
func decodeSetTableRequestExtended(data []byte, p gopacket.PacketBuilder) error {
omci := &SetTableRequest{}
omci.MsgLayerType = LayerTypeSetTableRequest
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Set Table Message Type Request
func (omci *SetTableRequest) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error {
// Only Extended message set is supported for this message type
if !omci.Extended {
return me.NewNotSupportedError("only Extended Message set support for the SetTable Message-Type")
}
// Basic (common) OMCI Header
err := omci.MeBasePacket.SerializeTo(b)
if err != nil {
return err
}
meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support SetTable
if !me.SupportsMsgType(meDefinition, me.SetTable) {
return me.NewProcessingError("managed entity does not support SetTable Message-Type")
}
// Only a single attribute bit can be set for this request
if bits.OnesCount16(omci.AttributeMask) != 1 {
return me.NewProcessingError("only a single attribute can be specified for the SetTable Message-Type")
}
// Find the attributes and make sure it supports a write
for attrName := range omci.Attributes {
attr, err := me.GetAttributeDefinitionByName(meDefinition.GetAttributeDefinitions(), attrName)
if err != nil {
return err
}
// Do not test for write of Entity ID in the attribute list
if attr.Index != 0 && attr.Mask == omci.AttributeMask {
// Must be a table attribute and support writes
if !me.SupportsAttributeAccess(*attr, me.Write) {
msg := fmt.Sprintf("attribute '%v' does not support write access", attrName)
return me.NewProcessingError(msg)
}
if !attr.IsTableAttribute() {
msg := fmt.Sprintf("attribute '%v' must be a table attribute for a SetTable Message-Type", attrName)
return me.NewProcessingError(msg)
}
break
}
}
// Attribute serialization
maskOffset := 1
maskOffset = 2
bytesAvailable := MaxExtendedLength - 12 - 4
attributeBuffer := gopacket.NewSerializeBuffer()
if attrErr, _ := meDefinition.SerializeAttributes(omci.Attributes, omci.AttributeMask, attributeBuffer,
byte(SetTableRequestType), bytesAvailable, false); attrErr != nil {
return attrErr
}
bytes, err := b.AppendBytes(maskOffset + 2 + len(attributeBuffer.Bytes()))
if err != nil {
return err
}
// Encode the length nd attribute mask
binary.BigEndian.PutUint16(bytes, uint16(len(attributeBuffer.Bytes())+2))
binary.BigEndian.PutUint16(bytes[maskOffset:], omci.AttributeMask)
copy(bytes[maskOffset+2:], attributeBuffer.Bytes())
return nil
}
type SetTableResponse struct {
MeBasePacket
Result me.Results
}
func (omci *SetTableResponse) String() string {
return fmt.Sprintf("%v", omci.MeBasePacket.String())
}
// LayerType returns LayerTypeSetTableResponse
func (omci *SetTableResponse) LayerType() gopacket.LayerType {
return LayerTypeSetTableResponse
}
// CanDecode returns the set of layer types that this DecodingLayer can decode
func (omci *SetTableResponse) CanDecode() gopacket.LayerClass {
return LayerTypeSetTableResponse
}
// NextLayerType returns the layer type contained by this DecodingLayer.
func (omci *SetTableResponse) NextLayerType() gopacket.LayerType {
return gopacket.LayerTypePayload
}
// DecodeFromBytes decodes the given bytes of a Set Table Response into this layer
func (omci *SetTableResponse) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
// Common ClassID/EntityID decode in msgBase
err := omci.MeBasePacket.DecodeFromBytes(data, p, 6+1)
if err != nil {
return err
}
entity, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support SetTable
if !me.SupportsMsgType(entity, me.SetTable) {
return me.NewProcessingError("managed entity does not support the SetTable Message-Type")
}
omci.Result = me.Results(data[6])
if omci.Result == 7 || omci.Result == 8 || omci.Result >= 9 {
msg := fmt.Sprintf("invalid SetTable results code: %v, must be 0..6, 9", omci.Result)
return errors.New(msg)
}
return nil
}
func decodeSetTableResponse(data []byte, p gopacket.PacketBuilder) error {
return me.NewNotSupportedError("baseline message set not supported by SetTable Message-Type")
}
func decodeSetTableResponseExtended(data []byte, p gopacket.PacketBuilder) error {
omci := &SetTableResponse{}
omci.MsgLayerType = LayerTypeSetTableResponse
omci.Extended = true
return decodingLayerDecoder(omci, data, p)
}
// SerializeTo provides serialization of an Set Table Message Type Response
func (omci *SetTableResponse) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error {
// Basic (common) OMCI Header
err := omci.MeBasePacket.SerializeTo(b)
if err != nil {
return err
}
entity, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
me.ParamData{EntityID: omci.EntityInstance})
if omciErr.StatusCode() != me.Success {
return omciErr.GetError()
}
// ME needs to support SetTable
if !me.SupportsMsgType(entity, me.SetTable) {
return me.NewProcessingError("managed entity does not support the SetTable Message-Type")
}
offset := 2
length := 1
bytes, err := b.AppendBytes(offset + length)
if err != nil {
return err
}
if omci.Result == 7 || omci.Result == 8 || omci.Result >= 9 {
msg := fmt.Sprintf("invalid SetTable results code: %v, must be 0..6, 9", omci.Result)
return errors.New(msg)
}
// TODO: Section A.1.1 (page 505) of ITU-G.988-202003 specifies that:
// When the result-reason code in a response message indicates an exception (i.e., its
// value is not 0), the response message is permitted to include vendor-specific
// additional information. The rules for additional error information are as follows.
//
// 1. Additional error information is optional for the ONU to insert.
// 2. Additional information may or may not be represented in textual form.
// 3. The semantics of additional error information are specific to the ONU vendor.
// 4. The ONU must not rely on the OLT being able to detect or interpret additional
// error information.
// 5. Additional error information may occupy only padding bytes (baseline message set)
// or only uncommitted trailing bytes (extended message set).
// 6. In get, get current data and get next responses, the attribute mask controls the
// padding definition.
// 7. No additional error information is permitted in responses to start download and
// end download messages that are directed to multiple target MEs, as indicated by
// 0xFFFF in the target ME identifier.
//
// TODO: Add this capability to all appropriate response serializations and validate for
// decodes the information is available through the Payload() function of the message-type
binary.BigEndian.PutUint16(bytes, uint16(1))
bytes[offset] = byte(omci.Result)
return nil
}