Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -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 ( |
Matteo Scandolo | 5863f00 | 2021-02-08 08:08:14 -0800 | [diff] [blame] | 20 | "encoding/hex" |
| 21 | "fmt" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 22 | "github.com/google/gopacket" |
| 23 | "github.com/opencord/omci-lib-go" |
| 24 | me "github.com/opencord/omci-lib-go/generated" |
Matteo Scandolo | 5863f00 | 2021-02-08 08:08:14 -0800 | [diff] [blame] | 25 | "github.com/opencord/voltha-protos/v4/go/openolt" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 26 | "gotest.tools/assert" |
| 27 | "testing" |
| 28 | ) |
| 29 | |
| 30 | func omciBytesToMsg(t *testing.T, data []byte) (*omci.OMCI, *gopacket.Packet) { |
| 31 | packet := gopacket.NewPacket(data, omci.LayerTypeOMCI, gopacket.NoCopy) |
| 32 | if packet == nil { |
| 33 | t.Fatal("could not decode rxMsg as OMCI") |
| 34 | } |
| 35 | omciLayer := packet.Layer(omci.LayerTypeOMCI) |
| 36 | if omciLayer == nil { |
| 37 | t.Fatal("could not decode omci layer") |
| 38 | } |
| 39 | omciMsg, ok := omciLayer.(*omci.OMCI) |
| 40 | if !ok { |
| 41 | t.Fatal("could not assign omci layer") |
| 42 | } |
| 43 | return omciMsg, &packet |
| 44 | } |
| 45 | |
| 46 | func omciToGetResponse(t *testing.T, omciPkt *gopacket.Packet) *omci.GetResponse { |
| 47 | msgLayer := (*omciPkt).Layer(omci.LayerTypeGetResponse) |
| 48 | if msgLayer == nil { |
| 49 | t.Fatal("omci Msg layer could not be detected for GetResponse - handling of MibSyncChan stopped") |
| 50 | } |
| 51 | msgObj, msgOk := msgLayer.(*omci.GetResponse) |
| 52 | if !msgOk { |
| 53 | t.Fatal("omci Msg layer could not be assigned for GetResponse - handling of MibSyncChan stopped") |
| 54 | } |
| 55 | return msgObj |
| 56 | } |
| 57 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 58 | type getArgs struct { |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 59 | generatedPkt *omci.GetResponse |
| 60 | transactionId uint16 |
| 61 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 62 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 63 | type getWant struct { |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 64 | transactionId uint16 |
| 65 | attributes map[string]interface{} |
| 66 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 67 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 68 | func TestGetResponse(t *testing.T) { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 69 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 70 | // NOTE that we're not testing the SerialNumber attribute part of the ONU-G |
| 71 | // response here as it is a special case and it requires transformation. |
| 72 | // we specifically test that in TestCreateOnugResponse |
| 73 | sn := &openolt.SerialNumber{ |
| 74 | VendorId: []byte("BBSM"), |
| 75 | VendorSpecific: []byte{0, byte(1 % 256), byte(1), byte(1)}, |
| 76 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 77 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 78 | tests := []struct { |
| 79 | name string |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 80 | args getArgs |
| 81 | want getWant |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 82 | }{ |
| 83 | {"getOnu2gResponse", |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 84 | getArgs{createOnu2gResponse(57344, 10), 1}, |
| 85 | getWant{1, map[string]interface{}{"OpticalNetworkUnitManagementAndControlChannelOmccVersion": uint8(180)}}, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 86 | }, |
| 87 | {"getOnugResponse", |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 88 | getArgs{createOnugResponse(40960, 10, sn), 1}, |
| 89 | getWant{1, map[string]interface{}{}}, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 90 | }, |
| 91 | {"getOnuDataResponse", |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 92 | getArgs{createOnuDataResponse(32768, 10, 129), 2}, |
| 93 | getWant{2, map[string]interface{}{"MibDataSync": uint8(129)}}, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 94 | }, |
Matteo Scandolo | 78d5ee1 | 2021-04-14 11:11:32 -0700 | [diff] [blame] | 95 | {"getGemPortNetworkCtpPerformanceMonitoringHistoryDataResponse", |
| 96 | getArgs{createGemPortNetworkCtpPerformanceMonitoringHistoryData(32768, 10), 2}, |
| 97 | getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}}, |
| 98 | }, |
| 99 | {"getEthernetFramePerformanceMonitoringHistoryDataUpstreamResponse", |
| 100 | getArgs{createEthernetFramePerformanceMonitoringHistoryDataUpstreamResponse(32768, 10), 2}, |
| 101 | getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}}, |
| 102 | }, |
| 103 | {"getEthernetFramePerformanceMonitoringHistoryDataDownstreamResponse", |
| 104 | getArgs{createEthernetFramePerformanceMonitoringHistoryDataDownstreamResponse(32768, 10), 2}, |
| 105 | getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}}, |
| 106 | }, |
| 107 | {"getEthernetPerformanceMonitoringHistoryDataResponse", |
| 108 | getArgs{createEthernetPerformanceMonitoringHistoryDataResponse(32768, 10), 2}, |
| 109 | getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}}, |
| 110 | }, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 111 | } |
| 112 | for _, tt := range tests { |
| 113 | t.Run(tt.name, func(t *testing.T) { |
| 114 | |
Matteo Scandolo | 78d5ee1 | 2021-04-14 11:11:32 -0700 | [diff] [blame] | 115 | data, err := Serialize(omci.GetResponseType, tt.args.generatedPkt, tt.args.transactionId) |
| 116 | if err != nil { |
| 117 | t.Fatal("cannot-serial-omci-packet", err) |
| 118 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 119 | |
| 120 | // emulate the openonu-go behavior: |
| 121 | // omci_cc.receiveMessage process the message (creates a gopacket and extracts the OMCI layer) and invokes a callback |
| 122 | // in the GetResponse case omci_cc.receiveOmciResponse |
| 123 | // then the OmciMessage (gopacket + OMIC layer) is is published on a channel |
| 124 | omciMsg, omciPkt := omciBytesToMsg(t, data) |
| 125 | |
| 126 | assert.Equal(t, omciMsg.MessageType, omci.GetResponseType) |
| 127 | assert.Equal(t, omciMsg.TransactionID, tt.want.transactionId) |
| 128 | |
| 129 | // that is read by myb_sync.processMibSyncMessages |
| 130 | // the myb_sync.handleOmciMessage is called and then |
| 131 | // myb_sync.handleOmciGetResponseMessage where we extract the GetResponse layer |
| 132 | getResponseLayer := omciToGetResponse(t, omciPkt) |
| 133 | |
| 134 | assert.Equal(t, getResponseLayer.Result, me.Success) |
| 135 | |
| 136 | for k, v := range tt.want.attributes { |
| 137 | attr := getResponseLayer.Attributes[k] |
| 138 | assert.Equal(t, attr, v) |
| 139 | } |
| 140 | }) |
| 141 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | func TestCreateOnugResponse(t *testing.T) { |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 145 | |
Matteo Scandolo | 5863f00 | 2021-02-08 08:08:14 -0800 | [diff] [blame] | 146 | sn := &openolt.SerialNumber{ |
| 147 | VendorId: []byte("BBSM"), |
| 148 | VendorSpecific: []byte{0, byte(1 % 256), byte(1), byte(1)}, |
| 149 | } |
| 150 | response := createOnugResponse(40960, 1, sn) |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 151 | data, _ := Serialize(omci.GetResponseType, response, 1) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 152 | |
| 153 | omciMsg, omciPkt := omciBytesToMsg(t, data) |
| 154 | |
| 155 | assert.Equal(t, omciMsg.MessageType, omci.GetResponseType) |
| 156 | |
| 157 | getResponseLayer := omciToGetResponse(t, omciPkt) |
| 158 | |
| 159 | assert.Equal(t, getResponseLayer.Result, me.Success) |
Matteo Scandolo | 5863f00 | 2021-02-08 08:08:14 -0800 | [diff] [blame] | 160 | snBytes := (getResponseLayer.Attributes["SerialNumber"]).([]byte) |
| 161 | snVendorPart := fmt.Sprintf("%s", snBytes[:4]) |
| 162 | snNumberPart := hex.EncodeToString(snBytes[4:]) |
| 163 | serialNumber := snVendorPart + snNumberPart |
| 164 | assert.Equal(t, serialNumber, "BBSM00010101") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 165 | } |