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" |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 22 | "reflect" |
| 23 | "testing" |
| 24 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 25 | "github.com/google/gopacket" |
Andrea Campanella | 10426e2 | 2021-10-15 17:58:04 +0200 | [diff] [blame] | 26 | "github.com/opencord/omci-lib-go/v2" |
| 27 | me "github.com/opencord/omci-lib-go/v2/generated" |
David K. Bainbridge | c415efe | 2021-08-19 13:05:21 +0000 | [diff] [blame] | 28 | "github.com/opencord/voltha-protos/v5/go/openolt" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 29 | "gotest.tools/assert" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 30 | ) |
| 31 | |
| 32 | func omciBytesToMsg(t *testing.T, data []byte) (*omci.OMCI, *gopacket.Packet) { |
| 33 | packet := gopacket.NewPacket(data, omci.LayerTypeOMCI, gopacket.NoCopy) |
| 34 | if packet == nil { |
| 35 | t.Fatal("could not decode rxMsg as OMCI") |
| 36 | } |
| 37 | omciLayer := packet.Layer(omci.LayerTypeOMCI) |
| 38 | if omciLayer == nil { |
| 39 | t.Fatal("could not decode omci layer") |
| 40 | } |
| 41 | omciMsg, ok := omciLayer.(*omci.OMCI) |
| 42 | if !ok { |
| 43 | t.Fatal("could not assign omci layer") |
| 44 | } |
| 45 | return omciMsg, &packet |
| 46 | } |
| 47 | |
| 48 | func omciToGetResponse(t *testing.T, omciPkt *gopacket.Packet) *omci.GetResponse { |
| 49 | msgLayer := (*omciPkt).Layer(omci.LayerTypeGetResponse) |
| 50 | if msgLayer == nil { |
| 51 | t.Fatal("omci Msg layer could not be detected for GetResponse - handling of MibSyncChan stopped") |
| 52 | } |
| 53 | msgObj, msgOk := msgLayer.(*omci.GetResponse) |
| 54 | if !msgOk { |
| 55 | t.Fatal("omci Msg layer could not be assigned for GetResponse - handling of MibSyncChan stopped") |
| 56 | } |
| 57 | return msgObj |
| 58 | } |
| 59 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 60 | type getArgs struct { |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 61 | generatedPkt *omci.GetResponse |
| 62 | transactionId uint16 |
| 63 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 64 | |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 65 | type getWant struct { |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 66 | transactionId uint16 |
| 67 | attributes map[string]interface{} |
| 68 | } |
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 | func TestGetResponse(t *testing.T) { |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 71 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 72 | // NOTE that we're not testing the SerialNumber attribute part of the ONU-G |
| 73 | // response here as it is a special case and it requires transformation. |
| 74 | // we specifically test that in TestCreateOnugResponse |
| 75 | sn := &openolt.SerialNumber{ |
| 76 | VendorId: []byte("BBSM"), |
| 77 | VendorSpecific: []byte{0, byte(1 % 256), byte(1), byte(1)}, |
| 78 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 79 | tests := []struct { |
| 80 | name string |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 81 | args getArgs |
| 82 | want getWant |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 83 | }{ |
| 84 | {"getOnu2gResponse", |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 85 | getArgs{createOnu2gResponse(57344, 10), 1}, |
| 86 | getWant{1, map[string]interface{}{"OpticalNetworkUnitManagementAndControlChannelOmccVersion": uint8(180)}}, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 87 | }, |
| 88 | {"getOnugResponse", |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 89 | getArgs{createOnugResponse(40960, 10, sn), 1}, |
| 90 | getWant{1, map[string]interface{}{}}, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 91 | }, |
| 92 | {"getOnuDataResponse", |
Matteo Scandolo | 4b077aa | 2021-02-16 17:33:37 -0800 | [diff] [blame] | 93 | getArgs{createOnuDataResponse(32768, 10, 129), 2}, |
| 94 | getWant{2, map[string]interface{}{"MibDataSync": uint8(129)}}, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 95 | }, |
Matteo Scandolo | 78d5ee1 | 2021-04-14 11:11:32 -0700 | [diff] [blame] | 96 | {"getGemPortNetworkCtpPerformanceMonitoringHistoryDataResponse", |
| 97 | getArgs{createGemPortNetworkCtpPerformanceMonitoringHistoryData(32768, 10), 2}, |
| 98 | getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}}, |
| 99 | }, |
| 100 | {"getEthernetFramePerformanceMonitoringHistoryDataUpstreamResponse", |
| 101 | getArgs{createEthernetFramePerformanceMonitoringHistoryDataUpstreamResponse(32768, 10), 2}, |
| 102 | getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}}, |
| 103 | }, |
| 104 | {"getEthernetFramePerformanceMonitoringHistoryDataDownstreamResponse", |
| 105 | getArgs{createEthernetFramePerformanceMonitoringHistoryDataDownstreamResponse(32768, 10), 2}, |
| 106 | getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}}, |
| 107 | }, |
| 108 | {"getEthernetPerformanceMonitoringHistoryDataResponse", |
| 109 | getArgs{createEthernetPerformanceMonitoringHistoryDataResponse(32768, 10), 2}, |
| 110 | getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}}, |
| 111 | }, |
Matteo Scandolo | c00e97a | 2021-05-27 11:45:27 -0700 | [diff] [blame] | 112 | {"getSoftwareImageResponse", |
| 113 | getArgs{createSoftwareImageResponse(61440, 0, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2}, |
| 114 | getWant{2, map[string]interface{}{"IsCommitted": uint8(0), "IsActive": uint8(0)}}, |
| 115 | }, |
| 116 | {"getSoftwareImageResponseActiveCommitted", |
| 117 | getArgs{createSoftwareImageResponse(61440, 1, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2}, |
| 118 | getWant{2, map[string]interface{}{"IsCommitted": uint8(1), "IsActive": uint8(1)}}, |
| 119 | }, |
| 120 | {"getSoftwareImageResponseVersion", |
| 121 | getArgs{createSoftwareImageResponse(61440, 1, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2}, |
| 122 | getWant{2, map[string]interface{}{"Version": ToOctets("BBSM_IMG_00001", 14)}}, |
| 123 | }, |
| 124 | {"getSoftwareImageResponseProductCode", |
| 125 | getArgs{createSoftwareImageResponse(2048, 1, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2}, |
| 126 | getWant{2, map[string]interface{}{"ProductCode": ToOctets("BBSIM-ONU", 25)}}, |
| 127 | }, |
| 128 | {"getSoftwareImageResponseActiveImageHash", |
| 129 | getArgs{createSoftwareImageResponse(1024, 1, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2}, |
| 130 | getWant{2, map[string]interface{}{"ImageHash": ToOctets("BBSM_IMG_00001", 25)}}, |
| 131 | }, |
Himani Chawla | 908a1f5 | 2022-01-27 14:39:44 +0530 | [diff] [blame] | 132 | {"getEthernetFrameExtendedPMDataResponse", |
| 133 | getArgs{createEthernetFrameExtendedPmGetResponse(me.EthernetFrameExtendedPmClassID, 16128, 10), 2}, |
| 134 | getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10), |
| 135 | "DropEvents": uint32(100), |
| 136 | "Octets": uint32(101), |
| 137 | "Frames": uint32(102), |
| 138 | "BroadcastFrames": uint32(103), |
| 139 | "MulticastFrames": uint32(104), |
| 140 | "CrcErroredFrames": uint32(105)}}, |
| 141 | }, |
| 142 | {"getEthernetFrameExtendedPM64BitDataResponse", |
| 143 | getArgs{createEthernetFrameExtendedPmGetResponse(me.EthernetFrameExtendedPm64BitClassID, 3, 10), 2}, |
| 144 | getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10), |
| 145 | "Frames512To1023Octets": uint64(112), |
| 146 | "Frames1024To1518Octets": uint64(113)}}, |
| 147 | }, |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 148 | } |
| 149 | for _, tt := range tests { |
| 150 | t.Run(tt.name, func(t *testing.T) { |
| 151 | |
Matteo Scandolo | 78d5ee1 | 2021-04-14 11:11:32 -0700 | [diff] [blame] | 152 | data, err := Serialize(omci.GetResponseType, tt.args.generatedPkt, tt.args.transactionId) |
| 153 | if err != nil { |
| 154 | t.Fatal("cannot-serial-omci-packet", err) |
| 155 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 156 | |
| 157 | // emulate the openonu-go behavior: |
| 158 | // omci_cc.receiveMessage process the message (creates a gopacket and extracts the OMCI layer) and invokes a callback |
| 159 | // in the GetResponse case omci_cc.receiveOmciResponse |
| 160 | // then the OmciMessage (gopacket + OMIC layer) is is published on a channel |
| 161 | omciMsg, omciPkt := omciBytesToMsg(t, data) |
| 162 | |
| 163 | assert.Equal(t, omciMsg.MessageType, omci.GetResponseType) |
| 164 | assert.Equal(t, omciMsg.TransactionID, tt.want.transactionId) |
| 165 | |
| 166 | // that is read by myb_sync.processMibSyncMessages |
| 167 | // the myb_sync.handleOmciMessage is called and then |
| 168 | // myb_sync.handleOmciGetResponseMessage where we extract the GetResponse layer |
| 169 | getResponseLayer := omciToGetResponse(t, omciPkt) |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 170 | assert.Equal(t, getResponseLayer.Result, me.Success) |
| 171 | |
| 172 | for k, v := range tt.want.attributes { |
| 173 | attr := getResponseLayer.Attributes[k] |
Matteo Scandolo | c00e97a | 2021-05-27 11:45:27 -0700 | [diff] [blame] | 174 | attrValue := reflect.ValueOf(attr) |
| 175 | if attrValue.Kind() == reflect.Slice { |
| 176 | // it the attribute is a list, iterate and compare single values |
| 177 | expectedValue := reflect.ValueOf(v) |
| 178 | for i := 0; i < attrValue.Len(); i++ { |
| 179 | assert.Equal(t, attrValue.Index(i).Interface(), expectedValue.Index(i).Interface(), |
| 180 | fmt.Sprintf("Attribute %s does not match, expected: %s, received %s", k, v, attr)) |
| 181 | } |
| 182 | } else { |
| 183 | // if it's not a list just compare |
| 184 | assert.Equal(t, attr, v) |
| 185 | } |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 186 | } |
| 187 | }) |
| 188 | } |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | func TestCreateOnugResponse(t *testing.T) { |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 192 | |
Matteo Scandolo | 5863f00 | 2021-02-08 08:08:14 -0800 | [diff] [blame] | 193 | sn := &openolt.SerialNumber{ |
| 194 | VendorId: []byte("BBSM"), |
| 195 | VendorSpecific: []byte{0, byte(1 % 256), byte(1), byte(1)}, |
| 196 | } |
| 197 | response := createOnugResponse(40960, 1, sn) |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 198 | data, _ := Serialize(omci.GetResponseType, response, 1) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 199 | |
| 200 | omciMsg, omciPkt := omciBytesToMsg(t, data) |
| 201 | |
| 202 | assert.Equal(t, omciMsg.MessageType, omci.GetResponseType) |
| 203 | |
| 204 | getResponseLayer := omciToGetResponse(t, omciPkt) |
| 205 | |
| 206 | assert.Equal(t, getResponseLayer.Result, me.Success) |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 207 | snBytes := (getResponseLayer.Attributes[me.OnuG_SerialNumber]).([]byte) |
Matteo Scandolo | 5863f00 | 2021-02-08 08:08:14 -0800 | [diff] [blame] | 208 | snVendorPart := fmt.Sprintf("%s", snBytes[:4]) |
| 209 | snNumberPart := hex.EncodeToString(snBytes[4:]) |
| 210 | serialNumber := snVendorPart + snNumberPart |
| 211 | assert.Equal(t, serialNumber, "BBSM00010101") |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 212 | } |