blob: 9578416e0cd3c5b7c09041d567f701dfc1743ff4 [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 (
Elia Battiston9bfe1102022-02-03 10:38:03 +010020 "testing"
21
Matteo Scandolof9d43412021-01-12 11:11:34 -080022 "github.com/google/gopacket"
Andrea Campanella10426e22021-10-15 17:58:04 +020023 "github.com/opencord/omci-lib-go/v2"
24 me "github.com/opencord/omci-lib-go/v2/generated"
Matteo Scandolof9d43412021-01-12 11:11:34 -080025 "gotest.tools/assert"
Matteo Scandolof9d43412021-01-12 11:11:34 -080026)
27
Matteo Scandolo992a23e2021-02-04 15:35:04 -080028// used to verify that the first message in the MIB Sync (OnuData)
29// reports the correct MDS
30const MDS = uint8(128)
31
Matteo Scandolof9d43412021-01-12 11:11:34 -080032func TestCreateMibResetResponse(t *testing.T) {
33 data, _ := CreateMibResetResponse(1)
34
35 omciMsg, omciPkt := omciBytesToMsg(t, data)
36
37 assert.Equal(t, omciMsg.MessageType, omci.MibResetResponseType)
38
39 msgLayer := (*omciPkt).Layer(omci.LayerTypeMibResetResponse)
40 msgObj, msgOk := msgLayer.(*omci.MibResetResponse)
41 if !msgOk {
42 t.Fail()
43 }
44
45 assert.Equal(t, msgObj.Result, me.Success)
46}
47
48// types for TestCreateMibUploadNextResponse test
49type mibArgs struct {
50 omciPkt gopacket.Packet
51 omciMsg *omci.OMCI
52}
53
54type mibExpected struct {
55 messageType omci.MessageType
56 transactionId uint16
57 entityClass me.ClassID
Matteo Scandolo992a23e2021-02-04 15:35:04 -080058 entityID uint16
Matteo Scandolof9d43412021-01-12 11:11:34 -080059 attributes map[string]interface{}
60}
61
62func createTestMibUploadNextArgs(t *testing.T, tid uint16, seqNumber uint16) mibArgs {
63 mibUploadNext, _ := CreateMibUploadNextRequest(tid, seqNumber)
Matteo Scandolocedde462021-03-09 17:37:16 -080064 mibUploadNext = HexDecode(mibUploadNext)
Matteo Scandolof9d43412021-01-12 11:11:34 -080065 mibUploadNextMsg, mibUploadNextPkt := omciBytesToMsg(t, mibUploadNext)
66
67 return mibArgs{
68 omciPkt: *mibUploadNextPkt,
69 omciMsg: mibUploadNextMsg,
70 }
71}
72
73func TestCreateMibUploadNextResponse(t *testing.T) {
74
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070075 const uniPortCount = 4
76
77 var (
78 onuDataEntityId = EntityID{0x00, 0x00}
79 onu2gEntityId = EntityID{0x00, 0x00}
80 anigEntityId = EntityID{tcontSlotId, aniGId}
81 )
82
83 // create a fake mibDb, we only need to test that given a CommandSequenceNumber
84 // we return the corresponding entry
85 // the only exception is for OnuData in which we need to replace the MibDataSync attribute with the current value
86 mibDb := MibDb{
87 NumberOfCommands: 4,
88 items: []MibDbEntry{},
89 }
90
91 mibDb.items = append(mibDb.items, MibDbEntry{
92 me.OnuDataClassID,
93 onuDataEntityId,
Elia Battiston9bfe1102022-02-03 10:38:03 +010094 me.AttributeValueMap{me.OnuData_MibDataSync: 0},
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070095 })
96
97 mibDb.items = append(mibDb.items, MibDbEntry{
98 me.CircuitPackClassID,
99 circuitPackEntityID,
100 me.AttributeValueMap{
Elia Battiston9bfe1102022-02-03 10:38:03 +0100101 me.CircuitPack_Type: ethernetUnitType,
102 me.CircuitPack_NumberOfPorts: uniPortCount,
103 me.CircuitPack_SerialNumber: ToOctets("BBSM-Circuit-Pack", 20),
104 me.CircuitPack_Version: ToOctets("v0.0.1", 20),
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700105 },
106 })
107
108 mibDb.items = append(mibDb.items, MibDbEntry{
109 me.AniGClassID,
110 anigEntityId,
111 me.AttributeValueMap{
Elia Battiston9bfe1102022-02-03 10:38:03 +0100112 me.AniG_Arc: 0,
113 me.AniG_ArcInterval: 0,
114 me.AniG_Deprecated: 0,
115 me.AniG_GemBlockLength: 48,
116 me.AniG_LowerOpticalThreshold: 255,
117 me.AniG_LowerTransmitPowerThreshold: 129,
118 me.AniG_OnuResponseTime: 0,
119 me.AniG_OpticalSignalLevel: 57428,
120 me.AniG_PiggybackDbaReporting: 0,
121 me.AniG_SignalDegradeThreshold: 9,
122 me.AniG_SignalFailThreshold: 5,
123 me.AniG_SrIndication: 1,
124 me.AniG_TotalTcontNumber: 8,
125 me.AniG_TransmitOpticalLevel: 3171,
126 me.AniG_UpperOpticalThreshold: 255,
127 me.AniG_UpperTransmitPowerThreshold: 129,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700128 },
129 })
130
131 mibDb.items = append(mibDb.items, MibDbEntry{
132 me.Onu2GClassID,
133 onu2gEntityId,
134 me.AttributeValueMap{
Elia Battiston9bfe1102022-02-03 10:38:03 +0100135 me.Onu2G_ConnectivityCapability: 127,
136 me.Onu2G_CurrentConnectivityMode: 0,
137 me.Onu2G_Deprecated: 1,
138 me.Onu2G_PriorityQueueScaleFactor: 1,
139 me.Onu2G_QualityOfServiceQosConfigurationFlexibility: 63,
140 me.Onu2G_Sysuptime: 0,
141 me.Onu2G_TotalGemPortIdNumber: 8,
142 me.Onu2G_TotalPriorityQueueNumber: 64,
143 me.Onu2G_TotalTrafficSchedulerNumber: 8,
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700144 },
145 })
146
Matteo Scandolof9d43412021-01-12 11:11:34 -0800147 tests := []struct {
148 name string
149 args mibArgs
150 want mibExpected
151 }{
152 {"mibUploadNext-0", createTestMibUploadNextArgs(t, 1, 0),
Elia Battiston9bfe1102022-02-03 10:38:03 +0100153 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 -0800154 {"mibUploadNext-1", createTestMibUploadNextArgs(t, 2, 1),
Elia Battiston9bfe1102022-02-03 10:38:03 +0100155 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 -0700156 {"mibUploadNext-2", createTestMibUploadNextArgs(t, 3, 2),
Elia Battiston9bfe1102022-02-03 10:38:03 +0100157 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 -0700158 {"mibUploadNext-3", createTestMibUploadNextArgs(t, 4, 3),
Elia Battiston9bfe1102022-02-03 10:38:03 +0100159 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 4, entityID: onuDataEntityId.ToUint16(), entityClass: me.Onu2GClassID, attributes: map[string]interface{}{me.Onu2G_TotalPriorityQueueNumber: uint16(64)}}},
Matteo Scandolof9d43412021-01-12 11:11:34 -0800160 }
161
162 for _, tt := range tests {
163 t.Run(tt.name, func(t *testing.T) {
164
165 // create the packet starting from the mibUploadNextRequest
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700166 data, err := CreateMibUploadNextResponse(tt.args.omciPkt, tt.args.omciMsg, MDS, &mibDb)
167 assert.NilError(t, err)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800168 omciMsg, omciPkt := omciBytesToMsg(t, data)
169
170 assert.Equal(t, omciMsg.MessageType, tt.want.messageType)
171
172 msgLayer := (*omciPkt).Layer(omci.LayerTypeMibUploadNextResponse)
173 msgObj, msgOk := msgLayer.(*omci.MibUploadNextResponse)
174 if !msgOk {
175 t.Fail()
176 }
177
178 assert.Equal(t, omciMsg.TransactionID, tt.want.transactionId) // tid
Matteo Scandolof9d43412021-01-12 11:11:34 -0800179
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800180 assert.Equal(t, msgObj.ReportedME.GetEntityID(), tt.want.entityID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800181
182 for k, v := range tt.want.attributes {
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700183 attr, err := msgObj.ReportedME.GetAttribute(k)
184 assert.NilError(t, err)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800185 assert.Equal(t, attr, v)
186 }
187 })
188 }
189
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700190 // now try to get a non existing command from the DB anche expect an error
191 args := createTestMibUploadNextArgs(t, 1, 20)
192 _, err := CreateMibUploadNextResponse(args.omciPkt, args.omciMsg, MDS, &mibDb)
193 assert.Error(t, err, "mibdb-does-not-contain-item")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800194}