blob: bd28d12252c38650c8c0966979deaa2233f668a3 [file] [log] [blame]
Matteo Scandolof9d43412021-01-12 11:11:34 -08001/*
Joey Armstrong2c039362024-02-04 18:51:52 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandolof9d43412021-01-12 11:11:34 -08003
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
17package omci
18
19import (
Matteo Scandolo5863f002021-02-08 08:08:14 -080020 "encoding/hex"
21 "fmt"
Elia Battiston9bfe1102022-02-03 10:38:03 +010022 "reflect"
23 "testing"
24
Matteo Scandolof9d43412021-01-12 11:11:34 -080025 "github.com/google/gopacket"
Holger Hildebrandtb9a3dd02022-05-12 08:20:41 +000026 "github.com/opencord/bbsim/internal/common"
Andrea Campanella10426e22021-10-15 17:58:04 +020027 "github.com/opencord/omci-lib-go/v2"
28 me "github.com/opencord/omci-lib-go/v2/generated"
David K. Bainbridgec415efe2021-08-19 13:05:21 +000029 "github.com/opencord/voltha-protos/v5/go/openolt"
Matteo Scandolof9d43412021-01-12 11:11:34 -080030 "gotest.tools/assert"
Matteo Scandolof9d43412021-01-12 11:11:34 -080031)
32
33func 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
49func 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 Scandolo4b077aa2021-02-16 17:33:37 -080061type getArgs struct {
Matteo Scandolo992a23e2021-02-04 15:35:04 -080062 generatedPkt *omci.GetResponse
63 transactionId uint16
64}
Matteo Scandolof9d43412021-01-12 11:11:34 -080065
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080066type getWant struct {
Matteo Scandolo992a23e2021-02-04 15:35:04 -080067 transactionId uint16
68 attributes map[string]interface{}
69}
Matteo Scandolof9d43412021-01-12 11:11:34 -080070
Matteo Scandolo992a23e2021-02-04 15:35:04 -080071func TestGetResponse(t *testing.T) {
Matteo Scandolof9d43412021-01-12 11:11:34 -080072
Holger Hildebrandtb9a3dd02022-05-12 08:20:41 +000073 common.Config = common.GetDefaultOps()
74
Matteo Scandolo992a23e2021-02-04 15:35:04 -080075 // 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 Scandolo992a23e2021-02-04 15:35:04 -080082 tests := []struct {
83 name string
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080084 args getArgs
85 want getWant
Matteo Scandolo992a23e2021-02-04 15:35:04 -080086 }{
87 {"getOnu2gResponse",
Holger Hildebrandt02101a62022-04-06 13:00:51 +000088 getArgs{createOnu2gResponse(false, 57344, 10), 1},
Holger Hildebrandtb9a3dd02022-05-12 08:20:41 +000089 getWant{1, map[string]interface{}{"OpticalNetworkUnitManagementAndControlChannelOmccVersion": uint8(163)}},
Matteo Scandolo992a23e2021-02-04 15:35:04 -080090 },
91 {"getOnugResponse",
Holger Hildebrandt02101a62022-04-06 13:00:51 +000092 getArgs{createOnugResponse(false, 40960, 10, sn), 1},
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080093 getWant{1, map[string]interface{}{}},
Matteo Scandolo992a23e2021-02-04 15:35:04 -080094 },
95 {"getOnuDataResponse",
Holger Hildebrandt02101a62022-04-06 13:00:51 +000096 getArgs{createOnuDataResponse(false, 32768, 10, 129), 2},
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080097 getWant{2, map[string]interface{}{"MibDataSync": uint8(129)}},
Matteo Scandolo992a23e2021-02-04 15:35:04 -080098 },
Matteo Scandolo78d5ee12021-04-14 11:11:32 -070099 {"getGemPortNetworkCtpPerformanceMonitoringHistoryDataResponse",
Holger Hildebrandt02101a62022-04-06 13:00:51 +0000100 getArgs{createGemPortNetworkCtpPerformanceMonitoringHistoryData(false, 32768, 10), 2},
Matteo Scandolo78d5ee12021-04-14 11:11:32 -0700101 getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}},
102 },
103 {"getEthernetFramePerformanceMonitoringHistoryDataUpstreamResponse",
Holger Hildebrandt02101a62022-04-06 13:00:51 +0000104 getArgs{createEthernetFramePerformanceMonitoringHistoryDataUpstreamResponse(false, 32768, 10), 2},
Matteo Scandolo78d5ee12021-04-14 11:11:32 -0700105 getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}},
106 },
107 {"getEthernetFramePerformanceMonitoringHistoryDataDownstreamResponse",
Holger Hildebrandt02101a62022-04-06 13:00:51 +0000108 getArgs{createEthernetFramePerformanceMonitoringHistoryDataDownstreamResponse(false, 32768, 10), 2},
Matteo Scandolo78d5ee12021-04-14 11:11:32 -0700109 getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}},
110 },
111 {"getEthernetPerformanceMonitoringHistoryDataResponse",
Holger Hildebrandt02101a62022-04-06 13:00:51 +0000112 getArgs{createEthernetPerformanceMonitoringHistoryDataResponse(false, 32768, 10), 2},
Matteo Scandolo78d5ee12021-04-14 11:11:32 -0700113 getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10)}},
114 },
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700115 {"getSoftwareImageResponse",
Holger Hildebrandt02101a62022-04-06 13:00:51 +0000116 getArgs{createSoftwareImageResponse(false, 61440, 0, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2},
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700117 getWant{2, map[string]interface{}{"IsCommitted": uint8(0), "IsActive": uint8(0)}},
118 },
119 {"getSoftwareImageResponseActiveCommitted",
Holger Hildebrandt02101a62022-04-06 13:00:51 +0000120 getArgs{createSoftwareImageResponse(false, 61440, 1, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2},
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700121 getWant{2, map[string]interface{}{"IsCommitted": uint8(1), "IsActive": uint8(1)}},
122 },
123 {"getSoftwareImageResponseVersion",
Holger Hildebrandt02101a62022-04-06 13:00:51 +0000124 getArgs{createSoftwareImageResponse(false, 61440, 1, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2},
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700125 getWant{2, map[string]interface{}{"Version": ToOctets("BBSM_IMG_00001", 14)}},
126 },
127 {"getSoftwareImageResponseProductCode",
Holger Hildebrandt02101a62022-04-06 13:00:51 +0000128 getArgs{createSoftwareImageResponse(false, 2048, 1, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2},
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700129 getWant{2, map[string]interface{}{"ProductCode": ToOctets("BBSIM-ONU", 25)}},
130 },
131 {"getSoftwareImageResponseActiveImageHash",
Holger Hildebrandt02101a62022-04-06 13:00:51 +0000132 getArgs{createSoftwareImageResponse(false, 1024, 1, 1, 1, "BBSM_IMG_00000", "BBSM_IMG_00001", "BBSM_IMG_00001"), 2},
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700133 getWant{2, map[string]interface{}{"ImageHash": ToOctets("BBSM_IMG_00001", 25)}},
134 },
Himani Chawla908a1f52022-01-27 14:39:44 +0530135 {"getEthernetFrameExtendedPMDataResponse",
Holger Hildebrandt02101a62022-04-06 13:00:51 +0000136 getArgs{createEthernetFrameExtendedPmGetResponse(false, me.EthernetFrameExtendedPmClassID, 16128, 10), 2},
Himani Chawla908a1f52022-01-27 14:39:44 +0530137 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 Hildebrandt02101a62022-04-06 13:00:51 +0000146 getArgs{createEthernetFrameExtendedPmGetResponse(false, me.EthernetFrameExtendedPm64BitClassID, 3, 10), 2},
Himani Chawla908a1f52022-01-27 14:39:44 +0530147 getWant{2, map[string]interface{}{"ManagedEntityId": uint16(10),
148 "Frames512To1023Octets": uint64(112),
149 "Frames1024To1518Octets": uint64(113)}},
150 },
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800151 }
152 for _, tt := range tests {
153 t.Run(tt.name, func(t *testing.T) {
154
Matteo Scandolo78d5ee12021-04-14 11:11:32 -0700155 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 Scandolo992a23e2021-02-04 15:35:04 -0800159
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 Scandolo992a23e2021-02-04 15:35:04 -0800173 assert.Equal(t, getResponseLayer.Result, me.Success)
174
175 for k, v := range tt.want.attributes {
176 attr := getResponseLayer.Attributes[k]
Matteo Scandoloc00e97a2021-05-27 11:45:27 -0700177 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 Scandolo992a23e2021-02-04 15:35:04 -0800189 }
190 })
191 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800192}
193
194func TestCreateOnugResponse(t *testing.T) {
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800195
Matteo Scandolo5863f002021-02-08 08:08:14 -0800196 sn := &openolt.SerialNumber{
197 VendorId: []byte("BBSM"),
198 VendorSpecific: []byte{0, byte(1 % 256), byte(1), byte(1)},
199 }
Holger Hildebrandt02101a62022-04-06 13:00:51 +0000200 response := createOnugResponse(false, 40960, 1, sn)
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800201 data, _ := Serialize(omci.GetResponseType, response, 1)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800202
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 Battiston9bfe1102022-02-03 10:38:03 +0100210 snBytes := (getResponseLayer.Attributes[me.OnuG_SerialNumber]).([]byte)
Matteo Scandolo5863f002021-02-08 08:08:14 -0800211 snVendorPart := fmt.Sprintf("%s", snBytes[:4])
212 snNumberPart := hex.EncodeToString(snBytes[4:])
213 serialNumber := snVendorPart + snNumberPart
214 assert.Equal(t, serialNumber, "BBSM00010101")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800215}