blob: 5c0cfc82ca15c9b170d5a28698ec25b63f77fb1a [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 Scandolof9d43412021-01-12 11:11:34 -080020 "github.com/google/gopacket"
Andrea Campanella10426e22021-10-15 17:58:04 +020021 "github.com/opencord/omci-lib-go/v2"
22 me "github.com/opencord/omci-lib-go/v2/generated"
Matteo Scandolof9d43412021-01-12 11:11:34 -080023 "gotest.tools/assert"
24 "testing"
25)
26
Matteo Scandolo992a23e2021-02-04 15:35:04 -080027// used to verify that the first message in the MIB Sync (OnuData)
28// reports the correct MDS
29const MDS = uint8(128)
30
Matteo Scandolof9d43412021-01-12 11:11:34 -080031func TestCreateMibResetResponse(t *testing.T) {
32 data, _ := CreateMibResetResponse(1)
33
34 omciMsg, omciPkt := omciBytesToMsg(t, data)
35
36 assert.Equal(t, omciMsg.MessageType, omci.MibResetResponseType)
37
38 msgLayer := (*omciPkt).Layer(omci.LayerTypeMibResetResponse)
39 msgObj, msgOk := msgLayer.(*omci.MibResetResponse)
40 if !msgOk {
41 t.Fail()
42 }
43
44 assert.Equal(t, msgObj.Result, me.Success)
45}
46
47// types for TestCreateMibUploadNextResponse test
48type mibArgs struct {
49 omciPkt gopacket.Packet
50 omciMsg *omci.OMCI
51}
52
53type mibExpected struct {
54 messageType omci.MessageType
55 transactionId uint16
56 entityClass me.ClassID
Matteo Scandolo992a23e2021-02-04 15:35:04 -080057 entityID uint16
Matteo Scandolof9d43412021-01-12 11:11:34 -080058 attributes map[string]interface{}
59}
60
61func createTestMibUploadNextArgs(t *testing.T, tid uint16, seqNumber uint16) mibArgs {
62 mibUploadNext, _ := CreateMibUploadNextRequest(tid, seqNumber)
Matteo Scandolocedde462021-03-09 17:37:16 -080063 mibUploadNext = HexDecode(mibUploadNext)
Matteo Scandolof9d43412021-01-12 11:11:34 -080064 mibUploadNextMsg, mibUploadNextPkt := omciBytesToMsg(t, mibUploadNext)
65
66 return mibArgs{
67 omciPkt: *mibUploadNextPkt,
68 omciMsg: mibUploadNextMsg,
69 }
70}
71
72func TestCreateMibUploadNextResponse(t *testing.T) {
73
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070074 const uniPortCount = 4
75
76 var (
77 onuDataEntityId = EntityID{0x00, 0x00}
78 onu2gEntityId = EntityID{0x00, 0x00}
79 anigEntityId = EntityID{tcontSlotId, aniGId}
80 )
81
82 // create a fake mibDb, we only need to test that given a CommandSequenceNumber
83 // we return the corresponding entry
84 // the only exception is for OnuData in which we need to replace the MibDataSync attribute with the current value
85 mibDb := MibDb{
86 NumberOfCommands: 4,
87 items: []MibDbEntry{},
88 }
89
90 mibDb.items = append(mibDb.items, MibDbEntry{
91 me.OnuDataClassID,
92 onuDataEntityId,
93 me.AttributeValueMap{"MibDataSync": 0},
94 })
95
96 mibDb.items = append(mibDb.items, MibDbEntry{
97 me.CircuitPackClassID,
98 circuitPackEntityID,
99 me.AttributeValueMap{
100 "Type": ethernetUnitType,
101 "NumberOfPorts": uniPortCount,
102 "SerialNumber": ToOctets("BBSM-Circuit-Pack", 20),
103 "Version": ToOctets("v0.0.1", 20),
104 },
105 })
106
107 mibDb.items = append(mibDb.items, MibDbEntry{
108 me.AniGClassID,
109 anigEntityId,
110 me.AttributeValueMap{
111 "Arc": 0,
112 "ArcInterval": 0,
113 "Deprecated": 0,
114 "GemBlockLength": 48,
115 "LowerOpticalThreshold": 255,
116 "LowerTransmitPowerThreshold": 129,
117 "OnuResponseTime": 0,
118 "OpticalSignalLevel": 57428,
119 "PiggybackDbaReporting": 0,
120 "SignalDegradeThreshold": 9,
121 "SignalFailThreshold": 5,
122 "SrIndication": 1,
123 "TotalTcontNumber": 8,
124 "TransmitOpticalLevel": 3171,
125 "UpperOpticalThreshold": 255,
126 "UpperTransmitPowerThreshold": 129,
127 },
128 })
129
130 mibDb.items = append(mibDb.items, MibDbEntry{
131 me.Onu2GClassID,
132 onu2gEntityId,
133 me.AttributeValueMap{
134 "ConnectivityCapability": 127,
135 "CurrentConnectivityMode": 0,
136 "Deprecated": 1,
137 "PriorityQueueScaleFactor": 1,
138 "QualityOfServiceQosConfigurationFlexibility": 63,
139 "Sysuptime": 0,
140 "TotalGemPortIdNumber": 8,
141 "TotalPriorityQueueNumber": 64,
142 "TotalTrafficSchedulerNumber": 8,
143 },
144 })
145
Matteo Scandolof9d43412021-01-12 11:11:34 -0800146 tests := []struct {
147 name string
148 args mibArgs
149 want mibExpected
150 }{
151 {"mibUploadNext-0", createTestMibUploadNextArgs(t, 1, 0),
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700152 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 1, entityID: onuDataEntityId.ToUint16(), entityClass: me.OnuDataClassID, attributes: map[string]interface{}{"MibDataSync": MDS}}},
Matteo Scandolof9d43412021-01-12 11:11:34 -0800153 {"mibUploadNext-1", createTestMibUploadNextArgs(t, 2, 1),
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700154 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 2, entityID: circuitPackEntityID.ToUint16(), entityClass: me.CircuitPackClassID, attributes: map[string]interface{}{"Type": uint8(47), "NumberOfPorts": uint8(4)}}},
155 {"mibUploadNext-2", createTestMibUploadNextArgs(t, 3, 2),
156 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 3, entityID: anigEntityId.ToUint16(), entityClass: me.AniGClassID, attributes: map[string]interface{}{"GemBlockLength": uint16(48)}}},
157 {"mibUploadNext-3", createTestMibUploadNextArgs(t, 4, 3),
158 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 4, entityID: onuDataEntityId.ToUint16(), entityClass: me.Onu2GClassID, attributes: map[string]interface{}{"TotalPriorityQueueNumber": uint16(64)}}},
Matteo Scandolof9d43412021-01-12 11:11:34 -0800159 }
160
161 for _, tt := range tests {
162 t.Run(tt.name, func(t *testing.T) {
163
164 // create the packet starting from the mibUploadNextRequest
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700165 data, err := CreateMibUploadNextResponse(tt.args.omciPkt, tt.args.omciMsg, MDS, &mibDb)
166 assert.NilError(t, err)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800167 omciMsg, omciPkt := omciBytesToMsg(t, data)
168
169 assert.Equal(t, omciMsg.MessageType, tt.want.messageType)
170
171 msgLayer := (*omciPkt).Layer(omci.LayerTypeMibUploadNextResponse)
172 msgObj, msgOk := msgLayer.(*omci.MibUploadNextResponse)
173 if !msgOk {
174 t.Fail()
175 }
176
177 assert.Equal(t, omciMsg.TransactionID, tt.want.transactionId) // tid
Matteo Scandolof9d43412021-01-12 11:11:34 -0800178
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800179 assert.Equal(t, msgObj.ReportedME.GetEntityID(), tt.want.entityID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800180
181 for k, v := range tt.want.attributes {
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700182 attr, err := msgObj.ReportedME.GetAttribute(k)
183 assert.NilError(t, err)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800184 assert.Equal(t, attr, v)
185 }
186 })
187 }
188
Matteo Scandoloef4e8f82021-05-17 11:20:49 -0700189 // now try to get a non existing command from the DB anche expect an error
190 args := createTestMibUploadNextArgs(t, 1, 20)
191 _, err := CreateMibUploadNextResponse(args.omciPkt, args.omciMsg, MDS, &mibDb)
192 assert.Error(t, err, "mibdb-does-not-contain-item")
Matteo Scandolof9d43412021-01-12 11:11:34 -0800193}