blob: 78c588e5c2955befe6b4946ad7c1294570bef4ff [file] [log] [blame]
Matteo Scandoloef4e8f82021-05-17 11:20:49 -07001/*
Joey Armstrong14628cd2023-01-10 08:38:31 -05002 * Copyright 2018-2023 Open Networking Foundation (ONF) and the ONF Contributors
Matteo Scandoloef4e8f82021-05-17 11:20:49 -07003
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 Scandolocfedba42022-02-14 16:08:54 -080020 "fmt"
Holger Hildebrandt236e3742022-05-04 14:07:27 +000021 "testing"
22
Matteo Scandolocfedba42022-02-14 16:08:54 -080023 "github.com/google/gopacket"
24 "github.com/opencord/bbsim/internal/common"
Elia Battistonac63b112022-01-12 18:40:49 +010025
Andrea Campanella10426e22021-10-15 17:58:04 +020026 "github.com/opencord/omci-lib-go/v2"
27 me "github.com/opencord/omci-lib-go/v2/generated"
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070028 "github.com/stretchr/testify/assert"
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070029)
30
31func 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 Scandolo8a574812021-05-20 15:18:53 -070044func 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
52func 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 Scandoloef4e8f82021-05-17 11:20:49 -070061func Test_GenerateMibDatabase(t *testing.T) {
Matteo Scandolocfedba42022-02-14 16:08:54 -080062 common.Config = &common.GlobalConfig{
63 BBSim: common.BBSimConfig{
64 InjectOmciUnknownAttributes: false,
65 },
66 }
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070067 const uniPortCount = 4
Elia Battistonb7bea222022-02-18 16:25:00 +010068 mibDb, err := GenerateMibDatabase(uniPortCount, 0, common.XGSPON)
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070069
Holger Hildebrandt8e23aaa2022-11-29 14:20:24 +000070 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 Scandoloef4e8f82021-05-17 11:20:49 -070077
78 assert.NoError(t, err)
79 assert.NotNil(t, mibDb)
Holger Hildebrandt236e3742022-05-04 14:07:27 +000080 assert.Equal(t, expectedItems, int(mibDb.NumberOfBaselineCommands))
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070081
82 // now try to serialize all messages to check on the attributes
Holger Hildebrandt236e3742022-05-04 14:07:27 +000083 for _, entry := range mibDb.baselineItems {
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070084 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 Battistonac63b112022-01-12 18:40:49 +0100102
103func Test_GenerateMibDatabase_withPots(t *testing.T) {
Matteo Scandolocfedba42022-02-14 16:08:54 -0800104 common.Config = &common.GlobalConfig{
105 BBSim: common.BBSimConfig{
106 InjectOmciUnknownAttributes: false,
107 },
108 }
Elia Battistonac63b112022-01-12 18:40:49 +0100109 const uniPortCount = 4
110 const potsPortCount = 1
Elia Battistonb7bea222022-02-18 16:25:00 +0100111 mibDb, err := GenerateMibDatabase(uniPortCount, potsPortCount, common.XGSPON)
Elia Battistonac63b112022-01-12 18:40:49 +0100112
Holger Hildebrandt8e23aaa2022-11-29 14:20:24 +0000113 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 Battistonac63b112022-01-12 18:40:49 +0100120
121 assert.NoError(t, err)
122 assert.NotNil(t, mibDb)
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000123 assert.Equal(t, expectedItems, int(mibDb.NumberOfBaselineCommands))
Elia Battistonac63b112022-01-12 18:40:49 +0100124
125 // now try to serialize all messages to check on the attributes
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000126 for _, entry := range mibDb.baselineItems {
Elia Battistonac63b112022-01-12 18:40:49 +0100127 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 Scandolocfedba42022-02-14 16:08:54 -0800145
146func Test_GenerateMibDatabase_withUnkownAttrs(t *testing.T) {
147
148 common.Config = &common.GlobalConfig{
149 BBSim: common.BBSimConfig{
Matteo Scandolo7011fc92022-03-16 15:50:15 -0700150 InjectOmciUnknownMe: true,
Matteo Scandolocfedba42022-02-14 16:08:54 -0800151 },
152 }
153
154 const uniPortCount = 4
Holger Hildebrandt01f92ce2022-12-20 09:33:57 +0000155 const baseMibEntries = 227 // see Test_GenerateMibDatabase for breakdown
Matteo Scandolocfedba42022-02-14 16:08:54 -0800156 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 Hildebrandt236e3742022-05-04 14:07:27 +0000161 assert.Equal(t, expectedMibEntries, int(mibDb.NumberOfBaselineCommands))
Matteo Scandolocfedba42022-02-14 16:08:54 -0800162
Holger Hildebrandt236e3742022-05-04 14:07:27 +0000163 entry := mibDb.baselineItems[expectedMibEntries-1] // select the last entry, it's the hardcoded packet
Matteo Scandolocfedba42022-02-14 16:08:54 -0800164 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}