Girish Gowdra | a539f52 | 2021-02-15 23:00:45 -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 | "errors" |
| 21 | "github.com/google/gopacket" |
Andrea Campanella | 10426e2 | 2021-10-15 17:58:04 +0200 | [diff] [blame] | 22 | "github.com/opencord/omci-lib-go/v2" |
| 23 | me "github.com/opencord/omci-lib-go/v2/generated" |
Girish Gowdra | a539f52 | 2021-02-15 23:00:45 -0800 | [diff] [blame] | 24 | log "github.com/sirupsen/logrus" |
| 25 | ) |
| 26 | |
| 27 | func ParseSyncTimeRequest(omciPkt gopacket.Packet) (*omci.SynchronizeTimeRequest, error) { |
| 28 | msgLayer := omciPkt.Layer(omci.LayerTypeSynchronizeTimeRequest) |
| 29 | if msgLayer == nil { |
| 30 | err := "omci Msg layer could not be detected for LayerTypeSynchronizeTimeRequest" |
| 31 | omciLogger.Error(err) |
| 32 | return nil, errors.New(err) |
| 33 | } |
| 34 | msgObj, msgOk := msgLayer.(*omci.SynchronizeTimeRequest) |
| 35 | if !msgOk { |
| 36 | err := "omci Msg layer could not be assigned for LayerTypeSynchronizeTimeRequest" |
| 37 | omciLogger.Error(err) |
| 38 | return nil, errors.New(err) |
| 39 | } |
| 40 | return msgObj, nil |
| 41 | } |
| 42 | |
| 43 | func CreateSyncTimeResponse(omciPkt gopacket.Packet, omciMsg *omci.OMCI) ([]byte, error) { |
| 44 | |
| 45 | msgObj, err := ParseSyncTimeRequest(omciPkt) |
| 46 | |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | |
| 51 | omciLogger.WithFields(log.Fields{ |
| 52 | "EntityClass": msgObj.EntityClass, |
| 53 | "EntityInstance": msgObj.EntityInstance, |
| 54 | }).Trace("received-omci-sync-time-request") |
| 55 | |
| 56 | response := &omci.SynchronizeTimeResponse{ |
| 57 | MeBasePacket: omci.MeBasePacket{ |
| 58 | EntityClass: msgObj.EntityClass, |
| 59 | EntityInstance: msgObj.EntityInstance, |
| 60 | }, |
| 61 | Result: me.Success, |
| 62 | } |
| 63 | |
| 64 | pkt, err := Serialize(omci.SynchronizeTimeResponseType, response, omciMsg.TransactionID) |
| 65 | if err != nil { |
| 66 | omciLogger.WithFields(log.Fields{ |
| 67 | "Err": err, |
| 68 | }).Error("cannot-Serialize-SyncTimeResponse") |
| 69 | return nil, err |
| 70 | } |
| 71 | |
| 72 | return pkt, nil |
| 73 | |
| 74 | } |