blob: 1e2393beed3c75f2b56d7b958defcca312f7c47f [file] [log] [blame]
Matteo Scandolof9d43412021-01-12 11:11:34 -08001/*
Joey Armstrong14628cd2023-01-10 08:38:31 -05002 * Copyright 2018-2023 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 Scandolocfedba42022-02-14 16:08:54 -080020 "encoding/binary"
21 "reflect"
Elia Battiston9bfe1102022-02-03 10:38:03 +010022 "testing"
23
Matteo Scandolof9d43412021-01-12 11:11:34 -080024 "github.com/google/gopacket"
Andrea Campanella10426e22021-10-15 17:58:04 +020025 "github.com/opencord/omci-lib-go/v2"
26 me "github.com/opencord/omci-lib-go/v2/generated"
Matteo Scandolof9d43412021-01-12 11:11:34 -080027 "gotest.tools/assert"
Matteo Scandolof9d43412021-01-12 11:11:34 -080028)
29
30func TestCreateMibResetResponse(t *testing.T) {
31 data, _ := CreateMibResetResponse(1)
32
33 omciMsg, omciPkt := omciBytesToMsg(t, data)
34
35 assert.Equal(t, omciMsg.MessageType, omci.MibResetResponseType)
36
37 msgLayer := (*omciPkt).Layer(omci.LayerTypeMibResetResponse)
38 msgObj, msgOk := msgLayer.(*omci.MibResetResponse)
39 if !msgOk {
40 t.Fail()
41 }
42
43 assert.Equal(t, msgObj.Result, me.Success)
44}
45
Matteo Scandolocfedba42022-02-14 16:08:54 -080046func TestSetTxIdInEncodedPacket(t *testing.T) {
47 basePkt := []byte{129, 42, 46, 10, 0, 2, 0, 0, 0, 37, 0, 1, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 0, 0, 0, 40, 40, 206, 0, 226}
48 res := SetTxIdInEncodedPacket(basePkt, uint16(292))
49 assert.Equal(t, len(basePkt), len(res))
50
51 b := res[0:2]
52 txId := binary.BigEndian.Uint16(b)
53 assert.Equal(t, uint16(292), txId)
54}
55
Matteo Scandolof9d43412021-01-12 11:11:34 -080056// types for TestCreateMibUploadNextResponse test
57type mibArgs struct {
58 omciPkt gopacket.Packet
59 omciMsg *omci.OMCI
60}
61
62type mibExpected struct {
63 messageType omci.MessageType
64 transactionId uint16
65 entityClass me.ClassID
Matteo Scandolo992a23e2021-02-04 15:35:04 -080066 entityID uint16
Matteo Scandolof9d43412021-01-12 11:11:34 -080067 attributes map[string]interface{}
68}
69
70func createTestMibUploadNextArgs(t *testing.T, tid uint16, seqNumber uint16) mibArgs {
71 mibUploadNext, _ := CreateMibUploadNextRequest(tid, seqNumber)
Matteo Scandolocedde462021-03-09 17:37:16 -080072 mibUploadNext = HexDecode(mibUploadNext)
Matteo Scandolof9d43412021-01-12 11:11:34 -080073 mibUploadNextMsg, mibUploadNextPkt := omciBytesToMsg(t, mibUploadNext)
74
75 return mibArgs{
76 omciPkt: *mibUploadNextPkt,
77 omciMsg: mibUploadNextMsg,
78 }
79}
80
81func TestCreateMibUploadNextResponse(t *testing.T) {
82
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070083 const uniPortCount = 4
84
85 var (
86 onuDataEntityId = EntityID{0x00, 0x00}
87 onu2gEntityId = EntityID{0x00, 0x00}
88 anigEntityId = EntityID{tcontSlotId, aniGId}
89 )
90
91 // create a fake mibDb, we only need to test that given a CommandSequenceNumber
92 // we return the corresponding entry
93 // the only exception is for OnuData in which we need to replace the MibDataSync attribute with the current value
94 mibDb := MibDb{
Holger Hildebrandt236e3742022-05-04 14:07:27 +000095 NumberOfBaselineCommands: 4,
96 baselineItems: []MibDbEntry{},
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070097 }
98
Holger Hildebrandt236e3742022-05-04 14:07:27 +000099 mibDb.baselineItems = append(mibDb.baselineItems, MibDbEntry{
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700100 me.OnuDataClassID,
101 onuDataEntityId,
Elia Battiston9bfe1102022-02-03 10:38:03 +0100102 me.AttributeValueMap{me.OnuData_MibDataSync: 0},
Matteo Scandolocfedba42022-02-14 16:08:54 -0800103 nil,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700104 })
105
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000106 mibDb.baselineItems = append(mibDb.baselineItems, MibDbEntry{
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700107 me.CircuitPackClassID,
108 circuitPackEntityID,
109 me.AttributeValueMap{
Elia Battiston9bfe1102022-02-03 10:38:03 +0100110 me.CircuitPack_Type: ethernetUnitType,
111 me.CircuitPack_NumberOfPorts: uniPortCount,
112 me.CircuitPack_SerialNumber: ToOctets("BBSM-Circuit-Pack", 20),
113 me.CircuitPack_Version: ToOctets("v0.0.1", 20),
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700114 },
Matteo Scandolocfedba42022-02-14 16:08:54 -0800115 nil,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700116 })
117
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000118 mibDb.baselineItems = append(mibDb.baselineItems, MibDbEntry{
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700119 me.AniGClassID,
120 anigEntityId,
121 me.AttributeValueMap{
Elia Battiston9bfe1102022-02-03 10:38:03 +0100122 me.AniG_Arc: 0,
123 me.AniG_ArcInterval: 0,
124 me.AniG_Deprecated: 0,
125 me.AniG_GemBlockLength: 48,
126 me.AniG_LowerOpticalThreshold: 255,
127 me.AniG_LowerTransmitPowerThreshold: 129,
128 me.AniG_OnuResponseTime: 0,
129 me.AniG_OpticalSignalLevel: 57428,
130 me.AniG_PiggybackDbaReporting: 0,
131 me.AniG_SignalDegradeThreshold: 9,
132 me.AniG_SignalFailThreshold: 5,
133 me.AniG_SrIndication: 1,
134 me.AniG_TotalTcontNumber: 8,
135 me.AniG_TransmitOpticalLevel: 3171,
136 me.AniG_UpperOpticalThreshold: 255,
137 me.AniG_UpperTransmitPowerThreshold: 129,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700138 },
Matteo Scandolocfedba42022-02-14 16:08:54 -0800139 nil,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700140 })
141
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000142 mibDb.baselineItems = append(mibDb.baselineItems, MibDbEntry{
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700143 me.Onu2GClassID,
144 onu2gEntityId,
145 me.AttributeValueMap{
Elia Battiston9bfe1102022-02-03 10:38:03 +0100146 me.Onu2G_ConnectivityCapability: 127,
147 me.Onu2G_CurrentConnectivityMode: 0,
148 me.Onu2G_Deprecated: 1,
149 me.Onu2G_PriorityQueueScaleFactor: 1,
150 me.Onu2G_QualityOfServiceQosConfigurationFlexibility: 63,
151 me.Onu2G_Sysuptime: 0,
152 me.Onu2G_TotalGemPortIdNumber: 8,
153 me.Onu2G_TotalPriorityQueueNumber: 64,
154 me.Onu2G_TotalTrafficSchedulerNumber: 8,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700155 },
Matteo Scandolocfedba42022-02-14 16:08:54 -0800156 nil,
157 })
158
159 // create an entry with UnkownAttributes set
160 var customPktTxId uint16 = 5
161 customPkt := []byte{0, byte(customPktTxId), 46, 10, 0, 2, 0, 0, 0, 37, 0, 1, 128, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 0, 0, 0, 40, 40, 206, 0, 226}
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000162 mibDb.baselineItems = append(mibDb.baselineItems, MibDbEntry{
Matteo Scandolocfedba42022-02-14 16:08:54 -0800163 me.ClassID(37),
164 nil,
165 me.AttributeValueMap{},
166 customPkt,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700167 })
168
Matteo Scandolof9d43412021-01-12 11:11:34 -0800169 tests := []struct {
170 name string
171 args mibArgs
172 want mibExpected
173 }{
174 {"mibUploadNext-0", createTestMibUploadNextArgs(t, 1, 0),
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000175 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 1, entityID: onuDataEntityId.ToUint16(), entityClass: me.OnuDataClassID, attributes: map[string]interface{}{me.OnuData_MibDataSync: uint8(0)}}},
Matteo Scandolof9d43412021-01-12 11:11:34 -0800176 {"mibUploadNext-1", createTestMibUploadNextArgs(t, 2, 1),
Elia Battiston9bfe1102022-02-03 10:38:03 +0100177 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 2, entityID: circuitPackEntityID.ToUint16(), entityClass: me.CircuitPackClassID, attributes: map[string]interface{}{me.CircuitPack_Type: uint8(47), me.CircuitPack_NumberOfPorts: uint8(4)}}},
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700178 {"mibUploadNext-2", createTestMibUploadNextArgs(t, 3, 2),
Elia Battiston9bfe1102022-02-03 10:38:03 +0100179 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 3, entityID: anigEntityId.ToUint16(), entityClass: me.AniGClassID, attributes: map[string]interface{}{me.AniG_GemBlockLength: uint16(48)}}},
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700180 {"mibUploadNext-3", createTestMibUploadNextArgs(t, 4, 3),
Elia Battiston9bfe1102022-02-03 10:38:03 +0100181 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 4, entityID: onuDataEntityId.ToUint16(), entityClass: me.Onu2GClassID, attributes: map[string]interface{}{me.Onu2G_TotalPriorityQueueNumber: uint16(64)}}},
Matteo Scandolocfedba42022-02-14 16:08:54 -0800182 {"mibUploadNext-unknown-attrs", createTestMibUploadNextArgs(t, 5, 4),
183 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: customPktTxId, entityID: 1, entityClass: me.ClassID(37), attributes: map[string]interface{}{"UnknownAttr_1": []uint8{1}}}},
Matteo Scandolof9d43412021-01-12 11:11:34 -0800184 }
185
186 for _, tt := range tests {
187 t.Run(tt.name, func(t *testing.T) {
188
189 // create the packet starting from the mibUploadNextRequest
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000190 data, err := CreateMibUploadNextResponse(tt.args.omciPkt, tt.args.omciMsg, &mibDb)
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700191 assert.NilError(t, err)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800192 omciMsg, omciPkt := omciBytesToMsg(t, data)
193
194 assert.Equal(t, omciMsg.MessageType, tt.want.messageType)
195
196 msgLayer := (*omciPkt).Layer(omci.LayerTypeMibUploadNextResponse)
197 msgObj, msgOk := msgLayer.(*omci.MibUploadNextResponse)
198 if !msgOk {
199 t.Fail()
200 }
201
202 assert.Equal(t, omciMsg.TransactionID, tt.want.transactionId) // tid
Matteo Scandolof9d43412021-01-12 11:11:34 -0800203
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800204 assert.Equal(t, msgObj.ReportedME.GetEntityID(), tt.want.entityID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800205
206 for k, v := range tt.want.attributes {
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700207 attr, err := msgObj.ReportedME.GetAttribute(k)
208 assert.NilError(t, err)
Matteo Scandolocfedba42022-02-14 16:08:54 -0800209 if isSlice(attr) {
210 expected := v.([]uint8)
211 data := attr.([]uint8)
212 for i, val := range data {
213 assert.Equal(t, expected[i], val)
214 }
215 } else {
216 assert.Equal(t, attr, v)
217 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800218 }
219 })
220 }
221
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700222 // now try to get a non existing command from the DB anche expect an error
223 args := createTestMibUploadNextArgs(t, 1, 20)
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000224 _, err := CreateMibUploadNextResponse(args.omciPkt, args.omciMsg, &mibDb)
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700225 assert.Error(t, err, "mibdb-does-not-contain-item")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800226}
Matteo Scandolocfedba42022-02-14 16:08:54 -0800227
228func isSlice(v interface{}) bool {
229 return reflect.TypeOf(v).Kind() == reflect.Slice
230}