blob: b835817f6ad3b3d1ca601afc8ab681d85f78e638 [file] [log] [blame]
Chip Boling934e1052021-09-27 15:12:06 -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 "fmt"
22 "github.com/google/gopacket"
23 "github.com/google/gopacket/layers"
Andrea Campanellae0cd8232021-10-15 15:10:23 +020024 me "github.com/opencord/omci-lib-go/v2/generated"
Chip Boling934e1052021-09-27 15:12:06 -050025)
26
27type UnknownAttributeInfo struct {
28 EntityClass me.ClassID
29 EntityInstance uint16
30 AttributeMask uint16
31 AttributeData []byte
32}
33type UnknownAttributes struct {
34 // Each Attributes entry relates one or more unknown attributes to a specific managed
35 // entity. For message types such as MIB Upload Next responses, there may be multiple
36 // Managed Entities in a single response if the Extended Message set is being used.
37 Attributes []UnknownAttributeInfo
38
39 gopacket.Layer
40 layers.BaseLayer
41 MsgLayerType gopacket.LayerType
42}
43
44// SerializeTo provides serialization of an Get Next Message Type Response
45func (msg *UnknownAttributes) String() string {
46 return fmt.Sprintf("Unknown Attributes, %v Managed Entities", len(msg.Attributes))
47}
48
49// LayerType returns LayerTypeGetNextResponse
50func (msg *UnknownAttributes) LayerType() gopacket.LayerType {
51 return LayerTypeUnknownAttributes
52}
53
54// CanDecode returns the set of layer types that this DecodingLayer can decode
55func (msg *UnknownAttributes) CanDecode() gopacket.LayerClass {
56 return LayerTypeUnknownAttributes
57}
58
59// LayerContents returns the bytes of the packet layer.
60func (msg *UnknownAttributes) LayerContents() []byte {
61 return msg.Contents
62}
63
64// LayerPayload returns the bytes contained within the packet layer
65func (msg *UnknownAttributes) LayerPayload() []byte {
66 return msg.Payload
67}
68
69// NextLayerType returns the layer type contained by this DecodingLayer.
70func (msg *UnknownAttributes) NextLayerType() gopacket.LayerType {
71 return gopacket.LayerTypeZero
72}
73
74// DecodeFromBytes decodes the given bytes of a Get Next Response into this layer
75func (msg *UnknownAttributes) DecodeFromBytes(_ []byte, _ gopacket.PacketBuilder) error {
76 // This is not a real layer. It is used to pass on relaxed decode error information
77 // as an ErrorLayer
78 return fmt.Errorf("This function is never called. This is an error layer that gets assigned")
79}
80
81func decodeUnknownAttributes(_ []byte, _ gopacket.PacketBuilder) error {
82 return fmt.Errorf("This function is never called. This is an error layer that gets assigned")
83}
84
85func (msg *UnknownAttributes) Error() error {
86 return fmt.Errorf("%v managed entities with Unknown Attributes detected during decode",
87 len(msg.Attributes))
88}
89
90func newUnknownAttributesLayer(prevLayer gopacket.Layer, errInfo []me.IRelaxedDecodeError, p gopacket.PacketBuilder) error {
91 // Add the previous layer
92 p.AddLayer(prevLayer)
93
94 // Append unknown attributes layer and also set ErrorLayer
95
96 errLayer := &UnknownAttributes{
97 Attributes: make([]UnknownAttributeInfo, 0),
98 MsgLayerType: LayerTypeUnknownAttributes,
99 }
100 for _, item := range errInfo {
101 unknown, ok := item.(*me.UnknownAttributeDecodeError)
102 if !ok {
103 return fmt.Errorf("only UnknownAttributeDecodeError information can be encoded. Found %T",
104 unknown)
105 }
106 data := UnknownAttributeInfo{
107 EntityClass: unknown.EntityClass,
108 EntityInstance: unknown.EntityInstance,
109 AttributeMask: unknown.AttributeMask,
110 }
111 if unknown.Contents != nil {
112 data.AttributeData = make([]byte, len(unknown.Contents))
113 copy(data.AttributeData, unknown.Contents)
114 }
115 errLayer.Attributes = append(errLayer.Attributes, data)
116 }
117 p.AddLayer(errLayer)
118 p.SetErrorLayer(errLayer)
119
120 // Return a valid error so that packet decoding stops
121 return errLayer.Error()
122}