blob: 240fc7cecf1e83b55aea3344c9c3b92823e8bc8e [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 "fmt"
23 "github.com/google/gopacket"
Andrea Campanellae0cd8232021-10-15 15:10:23 +020024 me "github.com/opencord/omci-lib-go/v2/generated"
Chip Boling610117d2021-09-09 11:24:34 -050025)
26
27type AttributeValueChangeMsg struct {
28 MeBasePacket
29 AttributeMask uint16
30 Attributes me.AttributeValueMap
31}
32
33func (omci *AttributeValueChangeMsg) String() string {
34 return fmt.Sprintf("%v, Mask: %#x, attributes: %v",
35 omci.MeBasePacket.String(), omci.AttributeMask, omci.Attributes)
36}
37
38// LayerType returns LayerTypeAttributeValueChange
39func (omci *AttributeValueChangeMsg) LayerType() gopacket.LayerType {
40 return LayerTypeAttributeValueChange
41}
42
43// CanDecode returns the set of layer types that this DecodingLayer can decode
44func (omci *AttributeValueChangeMsg) CanDecode() gopacket.LayerClass {
45 return LayerTypeAttributeValueChange
46}
47
48// NextLayerType returns the layer type contained by this DecodingLayer.
49func (omci *AttributeValueChangeMsg) NextLayerType() gopacket.LayerType {
50 return gopacket.LayerTypePayload
51}
52
53// DecodeFromBytes decodes the given bytes of an Attribute Value Change notification into this layer
54func (omci *AttributeValueChangeMsg) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error {
55 // Common ClassID/EntityID decode in msgBase
56 err := omci.MeBasePacket.DecodeFromBytes(data, p, 4+2)
57 if err != nil {
58 return err
59 }
60 meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
61 me.ParamData{EntityID: omci.EntityInstance})
62 if omciErr.StatusCode() != me.Success {
63 return omciErr.GetError()
64 }
65 // TODO: Support for encoding AVC into message type support not yet supported
66 //if !me.SupportsMsgType(meDefinition, me.AlarmNotification) {
67 // return me.NewProcessingError("managed entity does not support Alarm Notification Message-Type")
68 //}
69 maskOffset := 4
70 if omci.Extended {
71 maskOffset = 6
72 }
73 omci.AttributeMask = binary.BigEndian.Uint16(data[maskOffset:])
74 // Attribute decode
75 omci.Attributes, err = meDefinition.DecodeAttributes(omci.AttributeMask, data[maskOffset+2:],
76 p, byte(AttributeValueChangeType))
77 // TODO: Add support for attributes that can have an AVC associated with them and then add a check here
78 // Validate all attributes support AVC
79 //for attrName := range omci.attributes {
80 // attr, err := me.GetAttributeDefinitionByName(meDefinition.GetAttributeDefinitions(), attrName)
81 // if err != nil {
82 // return err
83 // }
84 // if attr.Index != 0 && !me.SupportsAttributeAVC(attr) {
85 // msg := fmt.Sprintf("attribute '%v' does not support AVC notifications", attrName)
86 // return me.NewProcessingError(msg)
87 // }
88 //}
89 return err
90}
91
92func decodeAttributeValueChange(data []byte, p gopacket.PacketBuilder) error {
93 omci := &AttributeValueChangeMsg{}
94 omci.MsgLayerType = LayerTypeAttributeValueChange
95 return decodingLayerDecoder(omci, data, p)
96}
97
98func decodeAttributeValueChangeExtended(data []byte, p gopacket.PacketBuilder) error {
99 omci := &AttributeValueChangeMsg{}
100 omci.MsgLayerType = LayerTypeAttributeValueChange
101 omci.Extended = true
102 return decodingLayerDecoder(omci, data, p)
103}
104
105// SerializeTo provides serialization of an Attribute Value Change Notification message
106func (omci *AttributeValueChangeMsg) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error {
107 // Basic (common) OMCI Header is 8 octets, 10
108 err := omci.MeBasePacket.SerializeTo(b)
109 if err != nil {
110 return err
111 }
112 meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass,
113 me.ParamData{EntityID: omci.EntityInstance})
114 if omciErr.StatusCode() != me.Success {
115 return omciErr.GetError()
116 }
117 // TODO: Add support for attributes that can have an AVC associated with them and then add a check here
118 // Validate all attributes support AVC
119 //for attrName := range omci.attributes {
120 // attr, err := me.GetAttributeDefinitionByName(meDefinition.GetAttributeDefinitions(), attrName)
121 // if err != nil {
122 // return err
123 // }
124 // if attr.Index != 0 && !me.SupportsAttributeAVC(attr) {
125 // msg := fmt.Sprintf("attribute '%v' does not support AVC notifications", attrName)
126 // return me.NewProcessingError(msg)
127 // }
128 //}
129 var maskOffset int
130 var bytesAvailable int
131 if omci.Extended {
132 maskOffset = 2
133 bytesAvailable = MaxExtendedLength - 12 - 4
134 } else {
135 maskOffset = 0
136 bytesAvailable = MaxBaselineLength - 10 - 8
137 }
138 bytes, err := b.AppendBytes(maskOffset + 2)
139 if err != nil {
140 return err
141 }
142 binary.BigEndian.PutUint16(bytes[maskOffset:], omci.AttributeMask)
143
144 // Attribute serialization
145 attributeBuffer := gopacket.NewSerializeBuffer()
146 if err, _ = meDefinition.SerializeAttributes(omci.Attributes, omci.AttributeMask,
147 attributeBuffer, byte(GetResponseType), bytesAvailable, false); err != nil {
148 return err
149 }
150
151 if omci.Extended {
152 binary.BigEndian.PutUint16(bytes, uint16(len(attributeBuffer.Bytes())+2))
153 }
154 bytes, err = b.AppendBytes(len(attributeBuffer.Bytes()))
155 if err != nil {
156 return err
157 }
158 copy(bytes, attributeBuffer.Bytes())
159 return nil
160}