blob: 021490718e007ac377dc2d5f70b5b215255eeaed [file] [log] [blame]
Chip Boling610117d2021-09-09 11:24:34 -05001/*
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
18package omci
19
20import (
21 "encoding/binary"
22 "errors"
23 "fmt"
24 "github.com/google/gopacket"
25 me "github.com/opencord/omci-lib-go/generated"
26)
27
28type GetRequest struct {
29 MeBasePacket
30 AttributeMask uint16
31}
32
33func (omci *GetRequest) String() string {
34 return fmt.Sprintf("%v, Mask: %#x",
35 omci.MeBasePacket.String(), omci.AttributeMask)
36}
37
38// LayerType returns LayerTypeGetRequest
39func (omci *GetRequest) LayerType() gopacket.LayerType {
40 return LayerTypeGetRequest
41}
42
43// CanDecode returns the set of layer types that this DecodingLayer can decode
44func (omci *GetRequest) CanDecode() gopacket.LayerClass {
45 return LayerTypeGetRequest
46}
47
48// NextLayerType returns the layer type contained by this DecodingLayer.
49func (omci *GetRequest) NextLayerType() gopacket.LayerType {
50 return gopacket.LayerTypePayload
51}
52
53// DecodeFromBytes decodes the given bytes of a Get Request into this layer
54func (omci *GetRequest) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
55 // Common ClassID/EntityID decode in msgBase
56 var hdrSize int
57 if omci.Extended {
58 hdrSize = 6 + 2
59 } else {
60 hdrSize = 4 + 2
61 }
62 err := omci.MeBasePacket.DecodeFromBytes(data, p, hdrSize)
63 if err != nil {
64 return err
65 }
66 meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
67 me.ParamData{EntityID: omci.EntityInstance})
68 if omciErr.StatusCode() != me.Success {
69 return omciErr.GetError()
70 }
71 // ME needs to support Get
72 if !me.SupportsMsgType(meDefinition, me.Get) {
73 return me.NewProcessingError("managed entity does not support Get Message-Type")
74 }
75 if omci.Extended {
76 if len(data) < 8 {
77 p.SetTruncated()
78 return errors.New("frame too small")
79 }
80 omci.AttributeMask = binary.BigEndian.Uint16(data[6:])
81 } else {
82 omci.AttributeMask = binary.BigEndian.Uint16(data[4:])
83 }
84 return nil
85}
86
87func decodeGetRequest(data []byte, p gopacket.PacketBuilder) error {
88 omci := &GetRequest{}
89 omci.MsgLayerType = LayerTypeGetRequest
90 return decodingLayerDecoder(omci, data, p)
91}
92
93func decodeGetRequestExtended(data []byte, p gopacket.PacketBuilder) error {
94 omci := &GetRequest{}
95 omci.MsgLayerType = LayerTypeGetRequest
96 omci.Extended = true
97 return decodingLayerDecoder(omci, data, p)
98}
99
100// SerializeTo provides serialization of an Get Request message
101func (omci *GetRequest) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error {
102 // Basic (common) OMCI Header is 8 octets, 10
103 err := omci.MeBasePacket.SerializeTo(b)
104 if err != nil {
105 return err
106 }
107 meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
108 me.ParamData{EntityID: omci.EntityInstance})
109 if omciErr.StatusCode() != me.Success {
110 return omciErr.GetError()
111 }
112 // ME needs to support Set
113 if !me.SupportsMsgType(meDefinition, me.Get) {
114 return me.NewProcessingError("managed entity does not support Get Message-Type")
115 }
116 maskOffset := 0
117 if omci.Extended {
118 maskOffset = 2
119 }
120 bytes, err := b.AppendBytes(2 + maskOffset)
121 if err != nil {
122 return err
123 }
124 if omci.Extended {
125 binary.BigEndian.PutUint16(bytes, uint16(2))
126 }
127 binary.BigEndian.PutUint16(bytes[maskOffset:], omci.AttributeMask)
128 return nil
129}
130
131type GetResponse struct {
132 MeBasePacket
133 Result me.Results
134 AttributeMask uint16
135 Attributes me.AttributeValueMap
136 UnsupportedAttributeMask uint16
137 FailedAttributeMask uint16
138}
139
140func (omci *GetResponse) String() string {
141 return fmt.Sprintf("%v, Result: %d (%v), Mask: %#x, Unsupported: %#x, Failed: %#x, attributes: %v",
142 omci.MeBasePacket.String(), omci.Result, omci.Result, omci.AttributeMask,
143 omci.UnsupportedAttributeMask, omci.FailedAttributeMask, omci.Attributes)
144}
145
146// LayerType returns LayerTypeGetResponse
147func (omci *GetResponse) LayerType() gopacket.LayerType {
148 return LayerTypeGetResponse
149}
150
151// CanDecode returns the set of layer types that this DecodingLayer can decode
152func (omci *GetResponse) CanDecode() gopacket.LayerClass {
153 return LayerTypeGetResponse
154}
155
156// NextLayerType returns the layer type contained by this DecodingLayer.
157func (omci *GetResponse) NextLayerType() gopacket.LayerType {
158 return gopacket.LayerTypePayload
159}
160
161// DecodeFromBytes decodes the given bytes of a Get Response into this layer
162func (omci *GetResponse) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
163 // Common ClassID/EntityID decode in msgBase
164 err := omci.MeBasePacket.DecodeFromBytes(data, p, 4+3)
165 if err != nil {
166 return err
167 }
168 meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
169 me.ParamData{EntityID: omci.EntityInstance})
170 if omciErr.StatusCode() != me.Success {
171 return omciErr.GetError()
172 }
173 // ME needs to support Get
174 if !me.SupportsMsgType(meDefinition, me.Get) {
175 return me.NewProcessingError("managed entity does not support Get Message-Type")
176 }
177 if omci.Extended {
178 if len(data) < 13 {
179 p.SetTruncated()
180 return errors.New("frame too small")
181 }
182 omci.Result = me.Results(data[6])
183 omci.AttributeMask = binary.BigEndian.Uint16(data[7:])
184
185 // If Attribute failed or Unknown, decode optional attribute mask
186 if omci.Result == me.AttributeFailure {
187 omci.UnsupportedAttributeMask = binary.BigEndian.Uint16(data[9:])
188 omci.FailedAttributeMask = binary.BigEndian.Uint16(data[11:])
189 }
190 } else {
191 omci.Result = me.Results(data[4])
192 omci.AttributeMask = binary.BigEndian.Uint16(data[5:])
193
194 // If Attribute failed or Unknown, decode optional attribute mask
195 if omci.Result == me.AttributeFailure {
196 omci.UnsupportedAttributeMask = binary.BigEndian.Uint16(data[32:34])
197 omci.FailedAttributeMask = binary.BigEndian.Uint16(data[34:36])
198 }
199 }
200 // Attribute decode. Note that the ITU-T G.988 specification states that the
201 // Unsupported and Failed attribute masks are always present
202 // but only valid if the status code== 9. However some XGS
203 // ONUs (T&W and Alpha, perhaps more) will use these last 4
204 // octets for data if the status code == 0. So accommodate
205 // this behaviour in favor of greater interoperability.
206 firstOctet := 7
207 lastOctet := 36
208 if omci.Extended {
209 firstOctet = 13
210 lastOctet = len(data)
211 }
212
213 switch omci.Result {
214 case me.ProcessingError, me.NotSupported, me.UnknownEntity, me.UnknownInstance, me.DeviceBusy:
215 return nil // Done (do not try and decode attributes)
216
217 case me.AttributeFailure:
218 if !omci.Extended {
219 lastOctet = 32
220 }
221 }
222 omci.Attributes, err = meDefinition.DecodeAttributes(omci.AttributeMask,
223 data[firstOctet:lastOctet], p, byte(GetResponseType))
224 if err != nil {
225 return err
226 }
227 // Validate all attributes support read
228 for attrName := range omci.Attributes {
229 attr, err := me.GetAttributeDefinitionByName(meDefinition.GetAttributeDefinitions(), attrName)
230 if err != nil {
231 return err
232 }
233 if attr.Index != 0 && !me.SupportsAttributeAccess(*attr, me.Read) {
234 msg := fmt.Sprintf("attribute '%v' does not support read access", attrName)
235 return me.NewProcessingError(msg)
236 }
237 }
238 if eidDef, eidDefOK := meDefinition.GetAttributeDefinitions()[0]; eidDefOK {
239 omci.Attributes[eidDef.GetName()] = omci.EntityInstance
240 return nil
241 }
242 return errors.New("all Managed Entities have an EntityID attribute")
243}
244
245func decodeGetResponse(data []byte, p gopacket.PacketBuilder) error {
246 omci := &GetResponse{}
247 omci.MsgLayerType = LayerTypeGetResponse
248 return decodingLayerDecoder(omci, data, p)
249}
250
251func decodeGetResponseExtended(data []byte, p gopacket.PacketBuilder) error {
252 omci := &GetResponse{}
253 omci.MsgLayerType = LayerTypeGetResponse
254 omci.Extended = true
255 return decodingLayerDecoder(omci, data, p)
256}
257
258// SerializeTo provides serialization of an Get Response message
259func (omci *GetResponse) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
260 // Basic (common) OMCI Header is 8 octets, 10
261 if err := omci.MeBasePacket.SerializeTo(b); err != nil {
262 return err
263 }
264 meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
265 me.ParamData{EntityID: omci.EntityInstance})
266
267 if omciErr.StatusCode() != me.Success {
268 return omciErr.GetError()
269 }
270 // ME needs to support Get
271 if !me.SupportsMsgType(meDefinition, me.Get) {
272 return me.NewProcessingError("managed entity does not support the Get Message-Type")
273 }
274 resultOffset := 0
275 attributeErrExtra := 0
276
277 if omci.Extended {
278 resultOffset = 2
279 attributeErrExtra = 4 // Attribute mask + attribute error masks
280 }
281 // Space for result + mask (both types) + (len & error masks if extended)
282 buffer, err := b.AppendBytes(3 + resultOffset + attributeErrExtra)
283 if err != nil {
284 return err
285 }
286 // Save result and initial mask. Other header fields updated after
287 // attribute copy
288 buffer[resultOffset] = byte(omci.Result)
289 binary.BigEndian.PutUint16(buffer[resultOffset+1:], omci.AttributeMask)
290
291 // Validate all attributes requested support read
292 for attrName := range omci.Attributes {
293 var attr *me.AttributeDefinition
294 attr, err = me.GetAttributeDefinitionByName(meDefinition.GetAttributeDefinitions(), attrName)
295 if err != nil {
296 return err
297 }
298 if attr.Index != 0 && (attr.Mask&omci.AttributeMask != 0) && !me.SupportsAttributeAccess(*attr, me.Read) {
299 msg := fmt.Sprintf("attribute '%v' does not support read access", attrName)
300 return me.NewProcessingError(msg)
301 }
302 }
303 // Attribute serialization
304 switch omci.Result {
305 default:
306 if omci.Extended {
307 // Minimum length is 7 for extended an need to write error masks
308 binary.BigEndian.PutUint16(buffer, uint16(7))
309 binary.BigEndian.PutUint32(buffer[resultOffset+3:], 0)
310 }
311 break
312
313 case me.Success, me.AttributeFailure:
314 // TODO: Baseline only supported at this time)
315 var available int
316 if omci.Extended {
317 available = MaxExtendedLength - 18 - 4 // Less: header, mic
318 } else {
319 available = MaxBaselineLength - 11 - 4 - 8 // Less: header, failed attributes, length, mic
320 }
321 // Serialize to temporary buffer if we may need to reset values due to
322 // recoverable truncation errors
323 attributeBuffer := gopacket.NewSerializeBuffer()
324 var failedMask uint16
325 err, failedMask = meDefinition.SerializeAttributes(omci.Attributes, omci.AttributeMask,
326 attributeBuffer, byte(GetResponseType), available, opts.FixLengths)
327
328 if err != nil {
329 return err
330 }
331 if failedMask != 0 {
332 // Not all attributes would fit
333 omci.FailedAttributeMask |= failedMask
334 omci.AttributeMask &= ^failedMask
335 omci.Result = me.AttributeFailure
336
337 // Adjust already recorded values
338 buffer[resultOffset] = byte(omci.Result)
339 binary.BigEndian.PutUint16(buffer[resultOffset+1:], omci.AttributeMask)
340 }
341 if omci.Extended {
342 // Set length and any failure masks
343 binary.BigEndian.PutUint16(buffer, uint16(len(attributeBuffer.Bytes())+7))
344
345 if omci.Result == me.AttributeFailure {
346 binary.BigEndian.PutUint16(buffer[resultOffset+3:], omci.UnsupportedAttributeMask)
347 binary.BigEndian.PutUint16(buffer[resultOffset+5:], omci.FailedAttributeMask)
348 } else {
349 binary.BigEndian.PutUint32(buffer[resultOffset+3:], 0)
350 }
351 }
352 // Copy over attributes to the original serialization buffer
353 var newSpace []byte
354
355 newSpace, err = b.AppendBytes(len(attributeBuffer.Bytes()))
356 if err != nil {
357 return err
358 }
359 copy(newSpace, attributeBuffer.Bytes())
360
361 if !omci.Extended {
362 // Calculate space left. Max - msgType header - OMCI trailer - spacedUsedSoFar
363 bytesLeft := MaxBaselineLength - 4 - 8 - len(b.Bytes())
364
365 var remainingBytes []byte
366 remainingBytes, err = b.AppendBytes(bytesLeft + 4)
367
368 if err != nil {
369 return me.NewMessageTruncatedError(err.Error())
370 }
371 copy(remainingBytes, lotsOfZeros[:])
372
373 if omci.Result == me.AttributeFailure {
374 binary.BigEndian.PutUint16(remainingBytes[bytesLeft-4:bytesLeft-2], omci.UnsupportedAttributeMask)
375 binary.BigEndian.PutUint16(remainingBytes[bytesLeft-2:bytesLeft], omci.FailedAttributeMask)
376 }
377 }
378 }
379 return nil
380}