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 | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 20 | "github.com/google/gopacket" |
| 21 | "github.com/opencord/omci-lib-go" |
| 22 | me "github.com/opencord/omci-lib-go/generated" |
| 23 | "gotest.tools/assert" |
| 24 | "testing" |
| 25 | ) |
| 26 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 27 | // used to verify that the first message in the MIB Sync (OnuData) |
| 28 | // reports the correct MDS |
| 29 | const MDS = uint8(128) |
| 30 | |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 31 | func 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 |
| 48 | type mibArgs struct { |
| 49 | omciPkt gopacket.Packet |
| 50 | omciMsg *omci.OMCI |
| 51 | } |
| 52 | |
| 53 | type mibExpected struct { |
| 54 | messageType omci.MessageType |
| 55 | transactionId uint16 |
| 56 | entityClass me.ClassID |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 57 | entityID uint16 |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 58 | attributes map[string]interface{} |
| 59 | } |
| 60 | |
| 61 | func createTestMibUploadNextArgs(t *testing.T, tid uint16, seqNumber uint16) mibArgs { |
| 62 | mibUploadNext, _ := CreateMibUploadNextRequest(tid, seqNumber) |
Matteo Scandolo | cedde46 | 2021-03-09 17:37:16 -0800 | [diff] [blame] | 63 | mibUploadNext = HexDecode(mibUploadNext) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 64 | mibUploadNextMsg, mibUploadNextPkt := omciBytesToMsg(t, mibUploadNext) |
| 65 | |
| 66 | return mibArgs{ |
| 67 | omciPkt: *mibUploadNextPkt, |
| 68 | omciMsg: mibUploadNextMsg, |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | func TestCreateMibUploadNextResponse(t *testing.T) { |
| 73 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 74 | 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 146 | tests := []struct { |
| 147 | name string |
| 148 | args mibArgs |
| 149 | want mibExpected |
| 150 | }{ |
| 151 | {"mibUploadNext-0", createTestMibUploadNextArgs(t, 1, 0), |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 152 | mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 1, entityID: onuDataEntityId.ToUint16(), entityClass: me.OnuDataClassID, attributes: map[string]interface{}{"MibDataSync": MDS}}}, |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 153 | {"mibUploadNext-1", createTestMibUploadNextArgs(t, 2, 1), |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 154 | 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 159 | } |
| 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 Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 165 | data, err := CreateMibUploadNextResponse(tt.args.omciPkt, tt.args.omciMsg, MDS, &mibDb) |
| 166 | assert.NilError(t, err) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 167 | 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 178 | |
Matteo Scandolo | 992a23e | 2021-02-04 15:35:04 -0800 | [diff] [blame] | 179 | assert.Equal(t, msgObj.ReportedME.GetEntityID(), tt.want.entityID) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 180 | |
| 181 | for k, v := range tt.want.attributes { |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 182 | attr, err := msgObj.ReportedME.GetAttribute(k) |
| 183 | assert.NilError(t, err) |
Matteo Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 184 | assert.Equal(t, attr, v) |
| 185 | } |
| 186 | }) |
| 187 | } |
| 188 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 189 | // 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 Scandolo | f9d4341 | 2021-01-12 11:11:34 -0800 | [diff] [blame] | 193 | } |