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 ( |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 20 | "testing" |
| 21 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 22 | "github.com/google/gopacket" |
Andrea Campanella | 10426e2 | 2021-10-15 17:58:04 +0200 | [diff] [blame] | 23 | "github.com/opencord/omci-lib-go/v2" |
| 24 | me "github.com/opencord/omci-lib-go/v2/generated" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 25 | "gotest.tools/assert" |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 26 | ) |
| 27 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 28 | // used to verify that the first message in the MIB Sync (OnuData) |
| 29 | // reports the correct MDS |
| 30 | const MDS = uint8(128) |
| 31 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 32 | func 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 |
| 49 | type mibArgs struct { |
| 50 | omciPkt gopacket.Packet |
| 51 | omciMsg *omci.OMCI |
| 52 | } |
| 53 | |
| 54 | type mibExpected struct { |
| 55 | messageType omci.MessageType |
| 56 | transactionId uint16 |
| 57 | entityClass me.ClassID |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 58 | entityID uint16 |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 59 | attributes map[string]interface{} |
| 60 | } |
| 61 | |
| 62 | func createTestMibUploadNextArgs(t *testing.T, tid uint16, seqNumber uint16) mibArgs { |
| 63 | mibUploadNext, _ := CreateMibUploadNextRequest(tid, seqNumber) |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 64 | mibUploadNext = HexDecode(mibUploadNext) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 65 | mibUploadNextMsg, mibUploadNextPkt := omciBytesToMsg(t, mibUploadNext) |
| 66 | |
| 67 | return mibArgs{ |
| 68 | omciPkt: *mibUploadNextPkt, |
| 69 | omciMsg: mibUploadNextMsg, |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | func TestCreateMibUploadNextResponse(t *testing.T) { |
| 74 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 75 | 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 Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 94 | me.AttributeValueMap{me.OnuData_MibDataSync: 0}, |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 95 | }) |
| 96 | |
| 97 | mibDb.items = append(mibDb.items, MibDbEntry{ |
| 98 | me.CircuitPackClassID, |
| 99 | circuitPackEntityID, |
| 100 | me.AttributeValueMap{ |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 101 | 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 Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 105 | }, |
| 106 | }) |
| 107 | |
| 108 | mibDb.items = append(mibDb.items, MibDbEntry{ |
| 109 | me.AniGClassID, |
| 110 | anigEntityId, |
| 111 | me.AttributeValueMap{ |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 112 | 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 Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 128 | }, |
| 129 | }) |
| 130 | |
| 131 | mibDb.items = append(mibDb.items, MibDbEntry{ |
| 132 | me.Onu2GClassID, |
| 133 | onu2gEntityId, |
| 134 | me.AttributeValueMap{ |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 135 | 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 Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 144 | }, |
| 145 | }) |
| 146 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 147 | tests := []struct { |
| 148 | name string |
| 149 | args mibArgs |
| 150 | want mibExpected |
| 151 | }{ |
| 152 | {"mibUploadNext-0", createTestMibUploadNextArgs(t, 1, 0), |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 153 | 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] | 154 | {"mibUploadNext-1", createTestMibUploadNextArgs(t, 2, 1), |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 155 | 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] | 156 | {"mibUploadNext-2", createTestMibUploadNextArgs(t, 3, 2), |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 157 | 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] | 158 | {"mibUploadNext-3", createTestMibUploadNextArgs(t, 4, 3), |
Elia Battiston | 9bfe110 | 2022-02-03 10:38:03 +0100 | [diff] [blame] | 159 | mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 4, entityID: onuDataEntityId.ToUint16(), entityClass: me.Onu2GClassID, attributes: map[string]interface{}{me.Onu2G_TotalPriorityQueueNumber: uint16(64)}}}, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 160 | } |
| 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 Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 166 | data, err := CreateMibUploadNextResponse(tt.args.omciPkt, tt.args.omciMsg, MDS, &mibDb) |
| 167 | assert.NilError(t, err) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 168 | 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 179 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 180 | assert.Equal(t, msgObj.ReportedME.GetEntityID(), tt.want.entityID) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 181 | |
| 182 | for k, v := range tt.want.attributes { |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 183 | attr, err := msgObj.ReportedME.GetAttribute(k) |
| 184 | assert.NilError(t, err) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 185 | assert.Equal(t, attr, v) |
| 186 | } |
| 187 | }) |
| 188 | } |
| 189 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 190 | // 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 194 | } |