Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package omci |
| 18 | |
| 19 | import ( |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 20 | "encoding/binary" |
| 21 | "reflect" |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 22 | "testing" |
| 23 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 24 | "github.com/google/gopacket" |
Andrea Campanella | 10426e2 | 2021-10-15 17:58:04 +0200 | [diff] [blame] | 25 | "github.com/opencord/omci-lib-go/v2" |
| 26 | me "github.com/opencord/omci-lib-go/v2/generated" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 27 | "gotest.tools/assert" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 28 | ) |
| 29 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 30 | // used to verify that the first message in the MIB Sync (OnuData) |
| 31 | // reports the correct MDS |
| 32 | const MDS = uint8(128) |
| 33 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 34 | func 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 Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 50 | func 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 60 | // types for TestCreateMibUploadNextResponse test |
| 61 | type mibArgs struct { |
| 62 | omciPkt gopacket.Packet |
| 63 | omciMsg *omci.OMCI |
| 64 | } |
| 65 | |
| 66 | type mibExpected struct { |
| 67 | messageType omci.MessageType |
| 68 | transactionId uint16 |
| 69 | entityClass me.ClassID |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 70 | entityID uint16 |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 71 | attributes map[string]interface{} |
| 72 | } |
| 73 | |
| 74 | func createTestMibUploadNextArgs(t *testing.T, tid uint16, seqNumber uint16) mibArgs { |
| 75 | mibUploadNext, _ := CreateMibUploadNextRequest(tid, seqNumber) |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 76 | mibUploadNext = HexDecode(mibUploadNext) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 77 | mibUploadNextMsg, mibUploadNextPkt := omciBytesToMsg(t, mibUploadNext) |
| 78 | |
| 79 | return mibArgs{ |
| 80 | omciPkt: *mibUploadNextPkt, |
| 81 | omciMsg: mibUploadNextMsg, |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestCreateMibUploadNextResponse(t *testing.T) { |
| 86 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 87 | 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 Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 106 | me.AttributeValueMap{me.OnuData_MibDataSync: 0}, |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 107 | nil, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 108 | }) |
| 109 | |
| 110 | mibDb.items = append(mibDb.items, MibDbEntry{ |
| 111 | me.CircuitPackClassID, |
| 112 | circuitPackEntityID, |
| 113 | me.AttributeValueMap{ |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 114 | 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 Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 118 | }, |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 119 | nil, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 120 | }) |
| 121 | |
| 122 | mibDb.items = append(mibDb.items, MibDbEntry{ |
| 123 | me.AniGClassID, |
| 124 | anigEntityId, |
| 125 | me.AttributeValueMap{ |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 126 | 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 Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 142 | }, |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 143 | nil, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 144 | }) |
| 145 | |
| 146 | mibDb.items = append(mibDb.items, MibDbEntry{ |
| 147 | me.Onu2GClassID, |
| 148 | onu2gEntityId, |
| 149 | me.AttributeValueMap{ |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 150 | 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 Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 159 | }, |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 160 | 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 Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 171 | }) |
| 172 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 173 | tests := []struct { |
| 174 | name string |
| 175 | args mibArgs |
| 176 | want mibExpected |
| 177 | }{ |
| 178 | {"mibUploadNext-0", createTestMibUploadNextArgs(t, 1, 0), |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 179 | mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 1, entityID: onuDataEntityId.ToUint16(), entityClass: me.OnuDataClassID, attributes: map[string]interface{}{me.OnuData_MibDataSync: MDS}}}, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 180 | {"mibUploadNext-1", createTestMibUploadNextArgs(t, 2, 1), |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 181 | 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 Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 182 | {"mibUploadNext-2", createTestMibUploadNextArgs(t, 3, 2), |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 183 | mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 3, entityID: anigEntityId.ToUint16(), entityClass: me.AniGClassID, attributes: map[string]interface{}{me.AniG_GemBlockLength: uint16(48)}}}, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 184 | {"mibUploadNext-3", createTestMibUploadNextArgs(t, 4, 3), |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 185 | mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 4, entityID: onuDataEntityId.ToUint16(), entityClass: me.Onu2GClassID, attributes: map[string]interface{}{me.Onu2G_TotalPriorityQueueNumber: uint16(64)}}}, |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 186 | {"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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 188 | } |
| 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 Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 194 | data, err := CreateMibUploadNextResponse(tt.args.omciPkt, tt.args.omciMsg, MDS, &mibDb) |
| 195 | assert.NilError(t, err) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 196 | 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 207 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 208 | assert.Equal(t, msgObj.ReportedME.GetEntityID(), tt.want.entityID) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 209 | |
| 210 | for k, v := range tt.want.attributes { |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 211 | attr, err := msgObj.ReportedME.GetAttribute(k) |
| 212 | assert.NilError(t, err) |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 213 | 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 222 | } |
| 223 | }) |
| 224 | } |
| 225 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 226 | // 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 230 | } |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 231 | |
| 232 | func isSlice(v interface{}) bool { |
| 233 | return reflect.TypeOf(v).Kind() == reflect.Slice |
| 234 | } |