blob: 2214f2e7404b8de4105eb9f8a5af9da1b070f36c [file] [log] [blame]
Matteo Scandolof9d43412021-01-12 11:11:34 -08001/*
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
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
Matteo Scandolo992a23e2021-02-04 15:35:04 -080030// used to verify that the first message in the MIB Sync (OnuData)
31// reports the correct MDS
32const MDS = uint8(128)
33
Matteo Scandolof9d43412021-01-12 11:11:34 -080034func TestCreateMibResetResponse(t *testing.T) {
35 data, _ := CreateMibResetResponse(1)
36
37 omciMsg, omciPkt := omciBytesToMsg(t, data)
38
39 assert.Equal(t, omciMsg.MessageType, omci.MibResetResponseType)
40
41 msgLayer := (*omciPkt).Layer(omci.LayerTypeMibResetResponse)
42 msgObj, msgOk := msgLayer.(*omci.MibResetResponse)
43 if !msgOk {
44 t.Fail()
45 }
46
47 assert.Equal(t, msgObj.Result, me.Success)
48}
49
Matteo Scandolocfedba42022-02-14 16:08:54 -080050func TestSetTxIdInEncodedPacket(t *testing.T) {
51 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}
52 res := SetTxIdInEncodedPacket(basePkt, uint16(292))
53 assert.Equal(t, len(basePkt), len(res))
54
55 b := res[0:2]
56 txId := binary.BigEndian.Uint16(b)
57 assert.Equal(t, uint16(292), txId)
58}
59
Matteo Scandolof9d43412021-01-12 11:11:34 -080060// types for TestCreateMibUploadNextResponse test
61type mibArgs struct {
62 omciPkt gopacket.Packet
63 omciMsg *omci.OMCI
64}
65
66type mibExpected struct {
67 messageType omci.MessageType
68 transactionId uint16
69 entityClass me.ClassID
Matteo Scandolo992a23e2021-02-04 15:35:04 -080070 entityID uint16
Matteo Scandolof9d43412021-01-12 11:11:34 -080071 attributes map[string]interface{}
72}
73
74func createTestMibUploadNextArgs(t *testing.T, tid uint16, seqNumber uint16) mibArgs {
75 mibUploadNext, _ := CreateMibUploadNextRequest(tid, seqNumber)
Matteo Scandolocedde462021-03-09 17:37:16 -080076 mibUploadNext = HexDecode(mibUploadNext)
Matteo Scandolof9d43412021-01-12 11:11:34 -080077 mibUploadNextMsg, mibUploadNextPkt := omciBytesToMsg(t, mibUploadNext)
78
79 return mibArgs{
80 omciPkt: *mibUploadNextPkt,
81 omciMsg: mibUploadNextMsg,
82 }
83}
84
85func TestCreateMibUploadNextResponse(t *testing.T) {
86
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070087 const uniPortCount = 4
88
89 var (
90 onuDataEntityId = EntityID{0x00, 0x00}
91 onu2gEntityId = EntityID{0x00, 0x00}
92 anigEntityId = EntityID{tcontSlotId, aniGId}
93 )
94
95 // create a fake mibDb, we only need to test that given a CommandSequenceNumber
96 // we return the corresponding entry
97 // the only exception is for OnuData in which we need to replace the MibDataSync attribute with the current value
98 mibDb := MibDb{
99 NumberOfCommands: 4,
100 items: []MibDbEntry{},
101 }
102
103 mibDb.items = append(mibDb.items, MibDbEntry{
104 me.OnuDataClassID,
105 onuDataEntityId,
Elia Battiston9bfe1102022-02-03 10:38:03 +0100106 me.AttributeValueMap{me.OnuData_MibDataSync: 0},
Matteo Scandolocfedba42022-02-14 16:08:54 -0800107 nil,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700108 })
109
110 mibDb.items = append(mibDb.items, MibDbEntry{
111 me.CircuitPackClassID,
112 circuitPackEntityID,
113 me.AttributeValueMap{
Elia Battiston9bfe1102022-02-03 10:38:03 +0100114 me.CircuitPack_Type: ethernetUnitType,
115 me.CircuitPack_NumberOfPorts: uniPortCount,
116 me.CircuitPack_SerialNumber: ToOctets("BBSM-Circuit-Pack", 20),
117 me.CircuitPack_Version: ToOctets("v0.0.1", 20),
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700118 },
Matteo Scandolocfedba42022-02-14 16:08:54 -0800119 nil,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700120 })
121
122 mibDb.items = append(mibDb.items, MibDbEntry{
123 me.AniGClassID,
124 anigEntityId,
125 me.AttributeValueMap{
Elia Battiston9bfe1102022-02-03 10:38:03 +0100126 me.AniG_Arc: 0,
127 me.AniG_ArcInterval: 0,
128 me.AniG_Deprecated: 0,
129 me.AniG_GemBlockLength: 48,
130 me.AniG_LowerOpticalThreshold: 255,
131 me.AniG_LowerTransmitPowerThreshold: 129,
132 me.AniG_OnuResponseTime: 0,
133 me.AniG_OpticalSignalLevel: 57428,
134 me.AniG_PiggybackDbaReporting: 0,
135 me.AniG_SignalDegradeThreshold: 9,
136 me.AniG_SignalFailThreshold: 5,
137 me.AniG_SrIndication: 1,
138 me.AniG_TotalTcontNumber: 8,
139 me.AniG_TransmitOpticalLevel: 3171,
140 me.AniG_UpperOpticalThreshold: 255,
141 me.AniG_UpperTransmitPowerThreshold: 129,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700142 },
Matteo Scandolocfedba42022-02-14 16:08:54 -0800143 nil,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700144 })
145
146 mibDb.items = append(mibDb.items, MibDbEntry{
147 me.Onu2GClassID,
148 onu2gEntityId,
149 me.AttributeValueMap{
Elia Battiston9bfe1102022-02-03 10:38:03 +0100150 me.Onu2G_ConnectivityCapability: 127,
151 me.Onu2G_CurrentConnectivityMode: 0,
152 me.Onu2G_Deprecated: 1,
153 me.Onu2G_PriorityQueueScaleFactor: 1,
154 me.Onu2G_QualityOfServiceQosConfigurationFlexibility: 63,
155 me.Onu2G_Sysuptime: 0,
156 me.Onu2G_TotalGemPortIdNumber: 8,
157 me.Onu2G_TotalPriorityQueueNumber: 64,
158 me.Onu2G_TotalTrafficSchedulerNumber: 8,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700159 },
Matteo Scandolocfedba42022-02-14 16:08:54 -0800160 nil,
161 })
162
163 // create an entry with UnkownAttributes set
164 var customPktTxId uint16 = 5
165 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}
166 mibDb.items = append(mibDb.items, MibDbEntry{
167 me.ClassID(37),
168 nil,
169 me.AttributeValueMap{},
170 customPkt,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700171 })
172
Matteo Scandolof9d43412021-01-12 11:11:34 -0800173 tests := []struct {
174 name string
175 args mibArgs
176 want mibExpected
177 }{
178 {"mibUploadNext-0", createTestMibUploadNextArgs(t, 1, 0),
Elia Battiston9bfe1102022-02-03 10:38:03 +0100179 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 1, entityID: onuDataEntityId.ToUint16(), entityClass: me.OnuDataClassID, attributes: map[string]interface{}{me.OnuData_MibDataSync: MDS}}},
Matteo Scandolof9d43412021-01-12 11:11:34 -0800180 {"mibUploadNext-1", createTestMibUploadNextArgs(t, 2, 1),
Elia Battiston9bfe1102022-02-03 10:38:03 +0100181 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 -0700182 {"mibUploadNext-2", createTestMibUploadNextArgs(t, 3, 2),
Elia Battiston9bfe1102022-02-03 10:38:03 +0100183 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 -0700184 {"mibUploadNext-3", createTestMibUploadNextArgs(t, 4, 3),
Elia Battiston9bfe1102022-02-03 10:38:03 +0100185 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 -0800186 {"mibUploadNext-unknown-attrs", createTestMibUploadNextArgs(t, 5, 4),
187 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 -0800188 }
189
190 for _, tt := range tests {
191 t.Run(tt.name, func(t *testing.T) {
192
193 // create the packet starting from the mibUploadNextRequest
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700194 data, err := CreateMibUploadNextResponse(tt.args.omciPkt, tt.args.omciMsg, MDS, &mibDb)
195 assert.NilError(t, err)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800196 omciMsg, omciPkt := omciBytesToMsg(t, data)
197
198 assert.Equal(t, omciMsg.MessageType, tt.want.messageType)
199
200 msgLayer := (*omciPkt).Layer(omci.LayerTypeMibUploadNextResponse)
201 msgObj, msgOk := msgLayer.(*omci.MibUploadNextResponse)
202 if !msgOk {
203 t.Fail()
204 }
205
206 assert.Equal(t, omciMsg.TransactionID, tt.want.transactionId) // tid
Matteo Scandolof9d43412021-01-12 11:11:34 -0800207
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800208 assert.Equal(t, msgObj.ReportedME.GetEntityID(), tt.want.entityID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800209
210 for k, v := range tt.want.attributes {
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700211 attr, err := msgObj.ReportedME.GetAttribute(k)
212 assert.NilError(t, err)
Matteo Scandolocfedba42022-02-14 16:08:54 -0800213 if isSlice(attr) {
214 expected := v.([]uint8)
215 data := attr.([]uint8)
216 for i, val := range data {
217 assert.Equal(t, expected[i], val)
218 }
219 } else {
220 assert.Equal(t, attr, v)
221 }
Matteo Scandolof9d43412021-01-12 11:11:34 -0800222 }
223 })
224 }
225
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700226 // now try to get a non existing command from the DB anche expect an error
227 args := createTestMibUploadNextArgs(t, 1, 20)
228 _, err := CreateMibUploadNextResponse(args.omciPkt, args.omciMsg, MDS, &mibDb)
229 assert.Error(t, err, "mibdb-does-not-contain-item")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800230}
Matteo Scandolocfedba42022-02-14 16:08:54 -0800231
232func isSlice(v interface{}) bool {
233 return reflect.TypeOf(v).Kind() == reflect.Slice
234}