Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 1 | /* |
Joey Armstrong | 2c03936 | 2024-02-04 18:51:52 -0500 | [diff] [blame] | 2 | * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 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 | "fmt" |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 21 | "testing" |
| 22 | |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 23 | "github.com/google/gopacket" |
| 24 | "github.com/opencord/bbsim/internal/common" |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 25 | |
Andrea Campanella | 10426e2 | 2021-10-15 17:58:04 +0200 | [diff] [blame] | 26 | "github.com/opencord/omci-lib-go/v2" |
| 27 | me "github.com/opencord/omci-lib-go/v2/generated" |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 28 | "github.com/stretchr/testify/assert" |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | func TestEntityID_ToUint16(t *testing.T) { |
| 32 | var id EntityID |
| 33 | var res uint16 |
| 34 | |
| 35 | id = EntityID{0x01, 0x01} |
| 36 | res = id.ToUint16() |
| 37 | assert.Equal(t, uint16(257), res) |
| 38 | |
| 39 | id = EntityID{0x00, 0x00} |
| 40 | res = id.ToUint16() |
| 41 | assert.Equal(t, uint16(0), res) |
| 42 | } |
| 43 | |
Matteo Scandolo | 8a57481 | 2021-05-20 15:18:53 -0700 | [diff] [blame] | 44 | func TestEntityID_FromUint16(t *testing.T) { |
| 45 | id := uint16(257) |
| 46 | e := EntityID{}.FromUint16(id) |
| 47 | |
| 48 | assert.Equal(t, e.ToString(), "0101") |
| 49 | assert.Equal(t, e.ToUint16(), id) |
| 50 | } |
| 51 | |
| 52 | func TestEntityID_Equals(t *testing.T) { |
| 53 | a := EntityID{0x01, 0x01} |
| 54 | b := EntityID{0x01, 0x01} |
| 55 | c := EntityID{0x01, 0x02} |
| 56 | |
| 57 | assert.True(t, a.Equals(b)) |
| 58 | assert.False(t, a.Equals(c)) |
| 59 | } |
| 60 | |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 61 | func Test_GenerateMibDatabase(t *testing.T) { |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 62 | common.Config = &common.GlobalConfig{ |
| 63 | BBSim: common.BBSimConfig{ |
| 64 | InjectOmciUnknownAttributes: false, |
| 65 | }, |
| 66 | } |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 67 | const uniPortCount = 4 |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 68 | mibDb, err := GenerateMibDatabase(uniPortCount, 0, common.XGSPON) |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 69 | |
Holger Hildebrandt | 8e23aaa | 2022-11-29 14:20:24 +0000 | [diff] [blame] | 70 | expectedItems := 9 //ONU-G + 2 Circuit Packs (4 messages each) |
| 71 | expectedItems += 2 * uniPortCount // 1 PPTP and 1 UniG per UNI |
| 72 | expectedItems += 1 // ANI-G |
| 73 | expectedItems += 2 * tconts // T-CONT and traffic schedulers |
| 74 | expectedItems += 1 // ONU-2g |
| 75 | expectedItems += 2 * upstreamPriorityQueues * tconts // upstream queues for each T-CONT, and we report each queue twice |
| 76 | expectedItems += 2 * downstreamPriorityQueues * uniPortCount // downstream queues for each T-CONT, and we report each queue twice |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 77 | |
| 78 | assert.NoError(t, err) |
| 79 | assert.NotNil(t, mibDb) |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 80 | assert.Equal(t, expectedItems, int(mibDb.NumberOfBaselineCommands)) |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 81 | |
| 82 | // now try to serialize all messages to check on the attributes |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 83 | for _, entry := range mibDb.baselineItems { |
Matteo Scandolo | ef4e8f8 | 2021-05-17 11:20:49 -0700 | [diff] [blame] | 84 | reportedMe, meErr := me.LoadManagedEntityDefinition(entry.classId, me.ParamData{ |
| 85 | EntityID: entry.entityId.ToUint16(), |
| 86 | Attributes: entry.params, |
| 87 | }) |
| 88 | assert.NoError(t, meErr.GetError()) |
| 89 | |
| 90 | response := &omci.MibUploadNextResponse{ |
| 91 | MeBasePacket: omci.MeBasePacket{ |
| 92 | EntityClass: me.OnuDataClassID, |
| 93 | }, |
| 94 | ReportedME: *reportedMe, |
| 95 | } |
| 96 | |
| 97 | _, err := Serialize(omci.MibUploadNextResponseType, response, uint16(10)) |
| 98 | assert.NoError(t, err) |
| 99 | } |
| 100 | |
| 101 | } |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 102 | |
| 103 | func Test_GenerateMibDatabase_withPots(t *testing.T) { |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 104 | common.Config = &common.GlobalConfig{ |
| 105 | BBSim: common.BBSimConfig{ |
| 106 | InjectOmciUnknownAttributes: false, |
| 107 | }, |
| 108 | } |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 109 | const uniPortCount = 4 |
| 110 | const potsPortCount = 1 |
Elia Battiston | b7bea22 | 2022-02-18 16:25:00 +0100 | [diff] [blame] | 111 | mibDb, err := GenerateMibDatabase(uniPortCount, potsPortCount, common.XGSPON) |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 112 | |
Holger Hildebrandt | 8e23aaa | 2022-11-29 14:20:24 +0000 | [diff] [blame] | 113 | expectedItems := 13 //ONU-G + 3 Circuit Packs (4 messages each) |
| 114 | expectedItems += 2 * (uniPortCount + potsPortCount) // 1 PPTP and 1 UniG per UNI |
| 115 | expectedItems += 1 // ANI-G |
| 116 | expectedItems += 2 * tconts // T-CONT and traffic schedulers |
| 117 | expectedItems += 1 // ONU-2g |
| 118 | expectedItems += 2 * upstreamPriorityQueues * tconts // upstream queues for each T-CONT, and we report each queue twice |
| 119 | expectedItems += 2 * downstreamPriorityQueues * (uniPortCount + potsPortCount) // downstream queues for each T-CONT, and we report each queue twice |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 120 | |
| 121 | assert.NoError(t, err) |
| 122 | assert.NotNil(t, mibDb) |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 123 | assert.Equal(t, expectedItems, int(mibDb.NumberOfBaselineCommands)) |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 124 | |
| 125 | // now try to serialize all messages to check on the attributes |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 126 | for _, entry := range mibDb.baselineItems { |
Elia Battiston | ac63b11 | 2022-01-12 18:40:49 +0100 | [diff] [blame] | 127 | reportedMe, meErr := me.LoadManagedEntityDefinition(entry.classId, me.ParamData{ |
| 128 | EntityID: entry.entityId.ToUint16(), |
| 129 | Attributes: entry.params, |
| 130 | }) |
| 131 | assert.NoError(t, meErr.GetError()) |
| 132 | |
| 133 | response := &omci.MibUploadNextResponse{ |
| 134 | MeBasePacket: omci.MeBasePacket{ |
| 135 | EntityClass: me.OnuDataClassID, |
| 136 | }, |
| 137 | ReportedME: *reportedMe, |
| 138 | } |
| 139 | |
| 140 | _, err := Serialize(omci.MibUploadNextResponseType, response, uint16(10)) |
| 141 | assert.NoError(t, err) |
| 142 | } |
| 143 | |
| 144 | } |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 145 | |
| 146 | func Test_GenerateMibDatabase_withUnkownAttrs(t *testing.T) { |
| 147 | |
| 148 | common.Config = &common.GlobalConfig{ |
| 149 | BBSim: common.BBSimConfig{ |
Matteo Scandolo | 7011fc9 | 2022-03-16 15:50:15 -0700 | [diff] [blame] | 150 | InjectOmciUnknownMe: true, |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 151 | }, |
| 152 | } |
| 153 | |
| 154 | const uniPortCount = 4 |
Holger Hildebrandt | 01f92ce | 2022-12-20 09:33:57 +0000 | [diff] [blame] | 155 | const baseMibEntries = 227 // see Test_GenerateMibDatabase for breakdown |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 156 | const expectedMibEntries = baseMibEntries + 1 // expecting one hardcoded packet |
| 157 | mibDb, err := GenerateMibDatabase(uniPortCount, 0, common.XGSPON) |
| 158 | |
| 159 | assert.NoError(t, err) |
| 160 | assert.NotNil(t, mibDb) |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 161 | assert.Equal(t, expectedMibEntries, int(mibDb.NumberOfBaselineCommands)) |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 162 | |
Holger Hildebrandt | 236e374 | 2022-05-04 14:07:27 +0000 | [diff] [blame] | 163 | entry := mibDb.baselineItems[expectedMibEntries-1] // select the last entry, it's the hardcoded packet |
Matteo Scandolo | cfedba4 | 2022-02-14 16:08:54 -0800 | [diff] [blame] | 164 | fmt.Println(entry.packet) |
| 165 | assert.NotNil(t, entry) |
| 166 | assert.Equal(t, me.ClassID(37), entry.classId) |
| 167 | assert.Nil(t, entry.entityId) |
| 168 | assert.Equal(t, 0, len(entry.params)) |
| 169 | assert.NotNil(t, entry.packet) |
| 170 | |
| 171 | // check that we're generating a valid OMCI payload |
| 172 | packet := gopacket.NewPacket(entry.packet, omci.LayerTypeOMCI, gopacket.Lazy) |
| 173 | omciLayer := packet.Layer(omci.LayerTypeOMCI) |
| 174 | assert.NotNil(t, omciLayer) |
| 175 | omciMsg, ok := omciLayer.(*omci.OMCI) |
| 176 | assert.True(t, ok) |
| 177 | assert.Equal(t, omci.MibUploadNextResponseType, omciMsg.MessageType) |
| 178 | assert.Equal(t, uint16(33066), omciMsg.TransactionID) |
| 179 | } |