Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package omci |
| 18 | |
| 19 | import ( |
| 20 | "encoding/hex" |
| 21 | "errors" |
| 22 | "github.com/google/gopacket" |
| 23 | "github.com/opencord/omci-lib-go" |
| 24 | me "github.com/opencord/omci-lib-go/generated" |
| 25 | log "github.com/sirupsen/logrus" |
| 26 | "math" |
| 27 | "strconv" |
| 28 | ) |
| 29 | |
| 30 | func ParseStartSoftwareDownloadRequest(omciPkt gopacket.Packet) (*omci.StartSoftwareDownloadRequest, error) { |
| 31 | msgLayer := omciPkt.Layer(omci.LayerTypeStartSoftwareDownloadRequest) |
| 32 | if msgLayer == nil { |
| 33 | err := "omci Msg layer could not be detected for LayerTypeStartSoftwareDownloadRequest" |
| 34 | omciLogger.Error(err) |
| 35 | return nil, errors.New(err) |
| 36 | } |
| 37 | msgObj, msgOk := msgLayer.(*omci.StartSoftwareDownloadRequest) |
| 38 | if !msgOk { |
| 39 | err := "omci Msg layer could not be assigned for LayerTypeStartSoftwareDownloadRequest" |
| 40 | omciLogger.Error(err) |
| 41 | return nil, errors.New(err) |
| 42 | } |
| 43 | return msgObj, nil |
| 44 | } |
| 45 | |
| 46 | func ParseDownloadSectionRequest(omciPkt gopacket.Packet) (*omci.DownloadSectionRequest, error) { |
| 47 | msgLayer := omciPkt.Layer(omci.LayerTypeDownloadSectionRequest) |
| 48 | if msgLayer == nil { |
| 49 | err := "omci Msg layer could not be detected for LayerTypeDownloadSectionRequest" |
| 50 | omciLogger.Error(err) |
| 51 | return nil, errors.New(err) |
| 52 | } |
| 53 | msgObj, msgOk := msgLayer.(*omci.DownloadSectionRequest) |
| 54 | if !msgOk { |
| 55 | err := "omci Msg layer could not be assigned for LayerTypeDownloadSectionRequest" |
| 56 | omciLogger.Error(err) |
| 57 | return nil, errors.New(err) |
| 58 | } |
| 59 | return msgObj, nil |
| 60 | } |
| 61 | |
| 62 | func ParseEndSoftwareDownloadRequest(omciPkt gopacket.Packet) (*omci.EndSoftwareDownloadRequest, error) { |
| 63 | msgLayer := omciPkt.Layer(omci.LayerTypeEndSoftwareDownloadRequest) |
| 64 | if msgLayer == nil { |
| 65 | err := "omci Msg layer could not be detected for LayerTypeEndSoftwareDownloadRequest" |
| 66 | omciLogger.Error(err) |
| 67 | return nil, errors.New(err) |
| 68 | } |
| 69 | msgObj, msgOk := msgLayer.(*omci.EndSoftwareDownloadRequest) |
| 70 | if !msgOk { |
| 71 | err := "omci Msg layer could not be assigned for LayerTypeEndSoftwareDownloadRequest" |
| 72 | omciLogger.Error(err) |
| 73 | return nil, errors.New(err) |
| 74 | } |
| 75 | return msgObj, nil |
| 76 | } |
| 77 | |
| 78 | func ParseActivateSoftwareRequest(omciPkt gopacket.Packet) (*omci.ActivateSoftwareRequest, error) { |
| 79 | msgLayer := omciPkt.Layer(omci.LayerTypeActivateSoftwareRequest) |
| 80 | if msgLayer == nil { |
| 81 | err := "omci Msg layer could not be detected for LayerTypeActivateSoftwareRequest" |
| 82 | omciLogger.Error(err) |
| 83 | return nil, errors.New(err) |
| 84 | } |
| 85 | msgObj, msgOk := msgLayer.(*omci.ActivateSoftwareRequest) |
| 86 | if !msgOk { |
| 87 | err := "omci Msg layer could not be assigned for LayerTypeActivateSoftwareRequest" |
| 88 | omciLogger.Error(err) |
| 89 | return nil, errors.New(err) |
| 90 | } |
| 91 | return msgObj, nil |
| 92 | } |
| 93 | |
| 94 | func ParseCommitSoftwareRequest(omciPkt gopacket.Packet) (*omci.CommitSoftwareRequest, error) { |
| 95 | msgLayer := omciPkt.Layer(omci.LayerTypeCommitSoftwareRequest) |
| 96 | if msgLayer == nil { |
| 97 | err := "omci Msg layer could not be detected for LayerTypeCommitSoftwareRequest" |
| 98 | omciLogger.Error(err) |
| 99 | return nil, errors.New(err) |
| 100 | } |
| 101 | msgObj, msgOk := msgLayer.(*omci.CommitSoftwareRequest) |
| 102 | if !msgOk { |
| 103 | err := "omci Msg layer could not be assigned for LayerTypeCommitSoftwareRequest" |
| 104 | omciLogger.Error(err) |
| 105 | return nil, errors.New(err) |
| 106 | } |
| 107 | return msgObj, nil |
| 108 | } |
| 109 | |
| 110 | func CreateStartSoftwareDownloadResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI) ([]byte, error) { |
| 111 | responeCode := me.Success |
| 112 | msgObj, err := ParseStartSoftwareDownloadRequest(omciPkt) |
| 113 | if err != nil { |
| 114 | responeCode = me.ProcessingError |
| 115 | } |
| 116 | |
| 117 | omciLogger.WithFields(log.Fields{ |
| 118 | "OmciMsgType": omciMsg.MessageType, |
| 119 | "TransCorrId": omciMsg.TransactionID, |
| 120 | "EntityInstance": msgObj.EntityInstance, |
| 121 | "WindowSize": msgObj.WindowSize, |
| 122 | "ImageSize": msgObj.ImageSize, |
| 123 | "NumberOfCircuitPacks": msgObj.NumberOfCircuitPacks, |
| 124 | "CircuitPacks": msgObj.CircuitPacks, |
| 125 | }).Debug("received-start-software-download-request") |
| 126 | |
| 127 | response := &omci.StartSoftwareDownloadResponse{ |
| 128 | MeBasePacket: omci.MeBasePacket{ |
| 129 | EntityClass: msgObj.EntityClass, |
| 130 | EntityInstance: msgObj.EntityInstance, |
| 131 | }, |
| 132 | WindowSize: msgObj.WindowSize, |
| 133 | Result: responeCode, |
| 134 | NumberOfInstances: 0, // NOTE this can't be bigger than 0 this we can populate downloadResults |
| 135 | MeResults: []omci.DownloadResults{}, // FIXME downloadResults is not exported |
| 136 | } |
| 137 | |
| 138 | pkt, err := Serialize(omci.StartSoftwareDownloadResponseType, response, omciMsg.TransactionID) |
| 139 | if err != nil { |
| 140 | omciLogger.WithFields(log.Fields{ |
| 141 | "Err": err, |
| 142 | }).Error("cannot-Serialize-CreateResponse") |
| 143 | return nil, err |
| 144 | } |
| 145 | |
| 146 | log.WithFields(log.Fields{ |
| 147 | "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16), |
| 148 | "pkt": hex.EncodeToString(pkt), |
| 149 | }).Trace("omci-start-software-download-response") |
| 150 | |
| 151 | return pkt, nil |
| 152 | } |
| 153 | |
| 154 | func CreateDownloadSectionResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI) ([]byte, error) { |
| 155 | |
| 156 | msgObj, err := ParseDownloadSectionRequest(omciPkt) |
| 157 | |
| 158 | if err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | |
| 162 | omciLogger.WithFields(log.Fields{ |
| 163 | "OmciMsgType": omciMsg.MessageType, |
| 164 | "TransCorrId": omciMsg.TransactionID, |
| 165 | "EntityInstance": msgObj.EntityInstance, |
| 166 | "SectionNumber": msgObj.SectionNumber, |
| 167 | }).Debug("received-download-section-request") |
| 168 | |
| 169 | response := &omci.DownloadSectionResponse{ |
| 170 | MeBasePacket: omci.MeBasePacket{ |
| 171 | EntityClass: msgObj.EntityClass, |
| 172 | EntityInstance: msgObj.EntityInstance, |
| 173 | }, |
| 174 | Result: me.Success, |
| 175 | SectionNumber: msgObj.SectionNumber, |
| 176 | } |
| 177 | |
| 178 | pkt, err := Serialize(omci.DownloadSectionResponseType, response, omciMsg.TransactionID) |
| 179 | if err != nil { |
| 180 | return nil, err |
| 181 | } |
| 182 | |
| 183 | log.WithFields(log.Fields{ |
| 184 | "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16), |
| 185 | "pkt": hex.EncodeToString(pkt), |
| 186 | }).Trace("omci-download-section-with-response-response") |
| 187 | |
| 188 | return pkt, nil |
| 189 | } |
| 190 | |
| 191 | func CreateEndSoftwareDownloadResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI, status me.Results) ([]byte, error) { |
| 192 | |
| 193 | msgObj, err := ParseEndSoftwareDownloadRequest(omciPkt) |
| 194 | |
| 195 | if err != nil { |
| 196 | return nil, err |
| 197 | } |
| 198 | |
| 199 | omciLogger.WithFields(log.Fields{ |
| 200 | "OmciMsgType": omciMsg.MessageType, |
| 201 | "TransCorrId": omciMsg.TransactionID, |
| 202 | "EntityInstance": msgObj.EntityInstance, |
| 203 | "Crc32": msgObj.CRC32, |
| 204 | "ImageSize": msgObj.ImageSize, |
| 205 | "NumberOfInstances": msgObj.NumberOfInstances, |
| 206 | "ImageInstances": msgObj.ImageInstances, |
| 207 | }).Debug("received-end-software-download-request") |
| 208 | |
| 209 | response := &omci.EndSoftwareDownloadResponse{ |
| 210 | MeBasePacket: omci.MeBasePacket{ |
| 211 | EntityClass: msgObj.EntityClass, |
| 212 | EntityInstance: msgObj.EntityInstance, |
| 213 | }, |
| 214 | Result: status, |
| 215 | NumberOfInstances: 0, // NOTE this can't be bigger than 0 this we can populate downloadResults |
| 216 | //MeResults: []omci.downloadResults{}, // FIXME downloadResults is not exported |
| 217 | } |
| 218 | |
| 219 | pkt, err := Serialize(omci.EndSoftwareDownloadResponseType, response, omciMsg.TransactionID) |
| 220 | if err != nil { |
| 221 | return nil, err |
| 222 | } |
| 223 | |
| 224 | log.WithFields(log.Fields{ |
| 225 | "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16), |
| 226 | "pkt": hex.EncodeToString(pkt), |
| 227 | }).Trace("omci-end-software-download-response") |
| 228 | |
| 229 | return pkt, nil |
| 230 | } |
| 231 | |
| 232 | func CreateActivateSoftwareResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI) ([]byte, error) { |
| 233 | |
| 234 | msgObj, err := ParseActivateSoftwareRequest(omciPkt) |
| 235 | |
| 236 | if err != nil { |
| 237 | return nil, err |
| 238 | } |
| 239 | |
| 240 | omciLogger.WithFields(log.Fields{ |
| 241 | "OmciMsgType": omciMsg.MessageType, |
| 242 | "TransCorrId": omciMsg.TransactionID, |
| 243 | "EntityInstance": msgObj.EntityInstance, |
| 244 | "ActivateFlags": msgObj.ActivateFlags, |
| 245 | }).Debug("received-activate-software-request") |
| 246 | |
| 247 | response := &omci.ActivateSoftwareResponse{ |
| 248 | MeBasePacket: omci.MeBasePacket{ |
| 249 | EntityClass: msgObj.EntityClass, |
| 250 | EntityInstance: msgObj.EntityInstance, |
| 251 | }, |
| 252 | Result: me.Success, |
| 253 | } |
| 254 | |
| 255 | pkt, err := Serialize(omci.ActivateSoftwareResponseType, response, omciMsg.TransactionID) |
| 256 | if err != nil { |
| 257 | return nil, err |
| 258 | } |
| 259 | |
| 260 | log.WithFields(log.Fields{ |
| 261 | "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16), |
| 262 | "pkt": hex.EncodeToString(pkt), |
| 263 | }).Trace("omci-activate-software-response") |
| 264 | |
| 265 | return pkt, nil |
| 266 | } |
| 267 | |
| 268 | func CreateCommitSoftwareResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI) ([]byte, error) { |
| 269 | |
| 270 | msgObj, err := ParseCommitSoftwareRequest(omciPkt) |
| 271 | |
| 272 | if err != nil { |
| 273 | return nil, err |
| 274 | } |
| 275 | |
| 276 | omciLogger.WithFields(log.Fields{ |
| 277 | "OmciMsgType": omciMsg.MessageType, |
| 278 | "TransCorrId": omciMsg.TransactionID, |
| 279 | "EntityInstance": msgObj.EntityInstance, |
| 280 | }).Debug("received-commit-software-request") |
| 281 | |
| 282 | response := &omci.CommitSoftwareResponse{ |
| 283 | MeBasePacket: omci.MeBasePacket{ |
| 284 | EntityClass: msgObj.EntityClass, |
| 285 | EntityInstance: msgObj.EntityInstance, |
| 286 | }, |
| 287 | } |
| 288 | |
| 289 | pkt, err := Serialize(omci.CommitSoftwareResponseType, response, omciMsg.TransactionID) |
| 290 | if err != nil { |
| 291 | return nil, err |
| 292 | } |
| 293 | |
| 294 | log.WithFields(log.Fields{ |
| 295 | "TxID": strconv.FormatInt(int64(omciMsg.TransactionID), 16), |
| 296 | "pkt": hex.EncodeToString(pkt), |
| 297 | }).Trace("omci-commit-software-response") |
| 298 | |
| 299 | return pkt, nil |
| 300 | } |
| 301 | |
| 302 | func ComputeDownloadSectionsCount(pkt gopacket.Packet) int { |
| 303 | msgObj, err := ParseStartSoftwareDownloadRequest(pkt) |
| 304 | if err != nil { |
| 305 | omciLogger.Error("cannot-parse-start-software-download-request") |
| 306 | } |
| 307 | |
| 308 | return int(math.Ceil(float64(msgObj.ImageSize) / float64(msgObj.WindowSize))) |
| 309 | } |