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