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