Chip Boling | 610117d | 2021-09-09 11:24:34 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
| 18 | package omci |
| 19 | |
| 20 | import ( |
| 21 | "encoding/binary" |
| 22 | "errors" |
| 23 | "fmt" |
| 24 | "github.com/google/gopacket" |
| 25 | me "github.com/opencord/omci-lib-go/generated" |
| 26 | ) |
| 27 | |
| 28 | type MibResetRequest struct { |
| 29 | MeBasePacket |
| 30 | } |
| 31 | |
| 32 | func (omci *MibResetRequest) String() string { |
| 33 | return fmt.Sprintf("%v", omci.MeBasePacket.String()) |
| 34 | } |
| 35 | |
| 36 | // LayerType returns LayerTypeMibResetRequest |
| 37 | func (omci *MibResetRequest) LayerType() gopacket.LayerType { |
| 38 | return LayerTypeMibResetRequest |
| 39 | } |
| 40 | |
| 41 | // CanDecode returns the set of layer types that this DecodingLayer can decode |
| 42 | func (omci *MibResetRequest) CanDecode() gopacket.LayerClass { |
| 43 | return LayerTypeMibResetRequest |
| 44 | } |
| 45 | |
| 46 | // NextLayerType returns the layer type contained by this DecodingLayer. |
| 47 | func (omci *MibResetRequest) NextLayerType() gopacket.LayerType { |
| 48 | return gopacket.LayerTypePayload |
| 49 | } |
| 50 | |
| 51 | // DecodeFromBytes decodes the given bytes of a MIB Reset Request into this layer |
| 52 | func (omci *MibResetRequest) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error { |
| 53 | // Common ClassID/EntityID decode in msgBase |
| 54 | var hdrSize int |
| 55 | if omci.Extended { |
| 56 | hdrSize = 6 |
| 57 | } else { |
| 58 | hdrSize = 4 |
| 59 | } |
| 60 | err := omci.MeBasePacket.DecodeFromBytes(data, p, hdrSize) |
| 61 | if err != nil { |
| 62 | return err |
| 63 | } |
| 64 | meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass, |
| 65 | me.ParamData{EntityID: omci.EntityInstance}) |
| 66 | if omciErr.StatusCode() != me.Success { |
| 67 | return omciErr.GetError() |
| 68 | } |
| 69 | // ME needs to support MIB reset |
| 70 | if !me.SupportsMsgType(meDefinition, me.MibReset) { |
| 71 | return me.NewProcessingError("managed entity does not support MIB Reset Message-Type") |
| 72 | } |
| 73 | // Entity Class are always ONU DATA (2) and Entity Instance of 0 |
| 74 | if omci.EntityClass != me.OnuDataClassID { |
| 75 | msg := fmt.Sprintf("invalid Entity Class for MIB Reset request: %v", |
| 76 | omci.EntityClass) |
| 77 | return me.NewProcessingError(msg) |
| 78 | } |
| 79 | if omci.EntityInstance != 0 { |
| 80 | msg := fmt.Sprintf("invalid Entity Instance for MIB Reset request: %v", |
| 81 | omci.EntityInstance) |
| 82 | return me.NewUnknownInstanceError(msg) |
| 83 | } |
| 84 | return nil |
| 85 | } |
| 86 | |
| 87 | func decodeMibResetRequest(data []byte, p gopacket.PacketBuilder) error { |
| 88 | omci := &MibResetRequest{} |
| 89 | omci.MsgLayerType = LayerTypeMibResetRequest |
| 90 | return decodingLayerDecoder(omci, data, p) |
| 91 | } |
| 92 | |
| 93 | func decodeMibResetRequestExtended(data []byte, p gopacket.PacketBuilder) error { |
| 94 | omci := &MibResetRequest{} |
| 95 | omci.MsgLayerType = LayerTypeMibResetRequest |
| 96 | omci.Extended = true |
| 97 | return decodingLayerDecoder(omci, data, p) |
| 98 | } |
| 99 | |
| 100 | // SerializeTo provides serialization of an MIB Reset Request message |
| 101 | func (omci *MibResetRequest) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error { |
| 102 | // MibReset Entity Class are always ONU DATA (2) and Entity Instance of 0 |
| 103 | if omci.EntityClass != me.OnuDataClassID { |
| 104 | return me.NewProcessingError("invalid Entity Class for MIB Reset request") |
| 105 | } |
| 106 | if omci.EntityInstance != 0 { |
| 107 | return me.NewUnknownInstanceError("invalid Entity Instance for MIB Reset request") |
| 108 | } |
| 109 | err := omci.MeBasePacket.SerializeTo(b) |
| 110 | if err != nil { |
| 111 | return err |
| 112 | } |
| 113 | // Add length if extended ident |
| 114 | if omci.Extended { |
| 115 | bytes, err := b.AppendBytes(2) |
| 116 | if err != nil { |
| 117 | return err |
| 118 | } |
| 119 | binary.BigEndian.PutUint16(bytes, 0) |
| 120 | } |
| 121 | return nil |
| 122 | } |
| 123 | |
| 124 | type MibResetResponse struct { |
| 125 | MeBasePacket |
| 126 | Result me.Results |
| 127 | } |
| 128 | |
| 129 | func (omci *MibResetResponse) String() string { |
| 130 | return fmt.Sprintf("%v, Result: %d (%v)", |
| 131 | omci.MeBasePacket.String(), omci.Result, omci.Result) |
| 132 | } |
| 133 | |
| 134 | // LayerType returns LayerTypeMibResetResponse |
| 135 | func (omci *MibResetResponse) LayerType() gopacket.LayerType { |
| 136 | return LayerTypeMibResetResponse |
| 137 | } |
| 138 | |
| 139 | // CanDecode returns the set of layer types that this DecodingLayer can decode |
| 140 | func (omci *MibResetResponse) CanDecode() gopacket.LayerClass { |
| 141 | return LayerTypeMibResetResponse |
| 142 | } |
| 143 | |
| 144 | // NextLayerType returns the layer type contained by this DecodingLayer. |
| 145 | func (omci *MibResetResponse) NextLayerType() gopacket.LayerType { |
| 146 | return gopacket.LayerTypePayload |
| 147 | } |
| 148 | |
| 149 | // DecodeFromBytes decodes the given bytes of a MIB Reset Response into this layer |
| 150 | func (omci *MibResetResponse) DecodeFromBytes(data []byte, p gopacket.PacketBuilder) error { |
| 151 | // Common ClassID/EntityID decode in msgBase |
| 152 | var hdrSize int |
| 153 | if omci.Extended { |
| 154 | hdrSize = 6 + 1 |
| 155 | } else { |
| 156 | hdrSize = 4 + 1 |
| 157 | } |
| 158 | err := omci.MeBasePacket.DecodeFromBytes(data, p, hdrSize) |
| 159 | if err != nil { |
| 160 | return err |
| 161 | } |
| 162 | meDefinition, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass, |
| 163 | me.ParamData{EntityID: omci.EntityInstance}) |
| 164 | if omciErr.StatusCode() != me.Success { |
| 165 | return omciErr.GetError() |
| 166 | } |
| 167 | // ME needs to support MIB reset |
| 168 | if !me.SupportsMsgType(meDefinition, me.MibReset) { |
| 169 | return me.NewProcessingError("managed entity does not support MIB Reset Message-Type") |
| 170 | } |
| 171 | // MIB Reset Response Entity Class always ONU DATA (2) and |
| 172 | // Entity Instance of 0 |
| 173 | if omci.EntityClass != me.OnuDataClassID { |
| 174 | return me.NewProcessingError("invalid Entity Class for MIB Reset Response") |
| 175 | } |
| 176 | if omci.EntityInstance != 0 { |
| 177 | return me.NewUnknownInstanceError("invalid Entity Instance for MIB Reset Response") |
| 178 | } |
| 179 | offset := hdrSize - 1 |
| 180 | omci.Result = me.Results(data[offset]) |
| 181 | if omci.Result > me.DeviceBusy { |
| 182 | msg := fmt.Sprintf("invalid results code: %v, must be 0..6", omci.Result) |
| 183 | return errors.New(msg) |
| 184 | } |
| 185 | return nil |
| 186 | } |
| 187 | |
| 188 | func decodeMibResetResponse(data []byte, p gopacket.PacketBuilder) error { |
| 189 | omci := &MibResetResponse{} |
| 190 | omci.MsgLayerType = LayerTypeMibResetResponse |
| 191 | return decodingLayerDecoder(omci, data, p) |
| 192 | } |
| 193 | |
| 194 | func decodeMibResetResponseExtended(data []byte, p gopacket.PacketBuilder) error { |
| 195 | omci := &MibResetResponse{} |
| 196 | omci.MsgLayerType = LayerTypeMibResetResponse |
| 197 | omci.Extended = true |
| 198 | return decodingLayerDecoder(omci, data, p) |
| 199 | } |
| 200 | |
| 201 | // SerializeTo provides serialization of an MIB Reset Response message |
| 202 | func (omci *MibResetResponse) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error { |
| 203 | // Basic (common) OMCI Header is 8 octets, 10 |
| 204 | err := omci.MeBasePacket.SerializeTo(b) |
| 205 | if err != nil { |
| 206 | return err |
| 207 | } |
| 208 | entity, omciErr := me.LoadManagedEntityDefinition(omci.EntityClass, |
| 209 | me.ParamData{EntityID: omci.EntityInstance}) |
| 210 | if omciErr.StatusCode() != me.Success { |
| 211 | return omciErr.GetError() |
| 212 | } |
| 213 | // ME needs to support Set |
| 214 | if !me.SupportsMsgType(entity, me.MibReset) { |
| 215 | return me.NewProcessingError("managed entity does not support the MIB Reset Message-Type") |
| 216 | } |
| 217 | var offset int |
| 218 | if omci.Extended { |
| 219 | offset = 2 |
| 220 | } |
| 221 | bytes, err := b.AppendBytes(offset + 1) |
| 222 | if err != nil { |
| 223 | return err |
| 224 | } |
| 225 | // Add length if extended ident |
| 226 | if omci.Extended { |
| 227 | binary.BigEndian.PutUint16(bytes, 1) |
| 228 | } |
| 229 | bytes[offset] = byte(omci.Result) |
| 230 | return nil |
| 231 | } |