blob: 7a589adcb512c5950b183602764a2614df4c9262 [file] [log] [blame]
Matteo Scandoloef4e8f82021-05-17 11:20:49 -07001/*
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
17package omci
18
19import (
Matteo Scandolocfedba42022-02-14 16:08:54 -080020 "fmt"
21 "github.com/google/gopacket"
22 "github.com/opencord/bbsim/internal/common"
Elia Battistonac63b112022-01-12 18:40:49 +010023 "testing"
24
Andrea Campanella10426e22021-10-15 17:58:04 +020025 "github.com/opencord/omci-lib-go/v2"
26 me "github.com/opencord/omci-lib-go/v2/generated"
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070027 "github.com/stretchr/testify/assert"
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070028)
29
30func TestEntityID_ToUint16(t *testing.T) {
31 var id EntityID
32 var res uint16
33
34 id = EntityID{0x01, 0x01}
35 res = id.ToUint16()
36 assert.Equal(t, uint16(257), res)
37
38 id = EntityID{0x00, 0x00}
39 res = id.ToUint16()
40 assert.Equal(t, uint16(0), res)
41}
42
Matteo Scandolo8a574812021-05-20 15:18:53 -070043func TestEntityID_FromUint16(t *testing.T) {
44 id := uint16(257)
45 e := EntityID{}.FromUint16(id)
46
47 assert.Equal(t, e.ToString(), "0101")
48 assert.Equal(t, e.ToUint16(), id)
49}
50
51func TestEntityID_Equals(t *testing.T) {
52 a := EntityID{0x01, 0x01}
53 b := EntityID{0x01, 0x01}
54 c := EntityID{0x01, 0x02}
55
56 assert.True(t, a.Equals(b))
57 assert.False(t, a.Equals(c))
58}
59
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070060func Test_GenerateMibDatabase(t *testing.T) {
Matteo Scandolocfedba42022-02-14 16:08:54 -080061 common.Config = &common.GlobalConfig{
62 BBSim: common.BBSimConfig{
63 InjectOmciUnknownAttributes: false,
64 },
65 }
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070066 const uniPortCount = 4
Elia Battistonb7bea222022-02-18 16:25:00 +010067 mibDb, err := GenerateMibDatabase(uniPortCount, 0, common.XGSPON)
Matteo Scandoloef4e8f82021-05-17 11:20:49 -070068
69 expectedItems := 9 //ONU-G + 2 Circuit Packs (4 messages each)
70 expectedItems += 2 * uniPortCount // 1 PPTP and 1 UniG per UNI
71 expectedItems += 1 // ANI-G
72 expectedItems += 2 * tconts // T-CONT and traffic schedulers
73 expectedItems += 1 // ONU-2g
74 expectedItems += 2 * 8 * tconts // 8 upstream queues for each T-CONT, and we report each queue twice
75 expectedItems += 2 * 16 * uniPortCount // 16 downstream queues for each T-CONT, and we report each queue twice
76
77 assert.NoError(t, err)
78 assert.NotNil(t, mibDb)
79 assert.Equal(t, expectedItems, int(mibDb.NumberOfCommands))
80
81 // now try to serialize all messages to check on the attributes
82 for _, entry := range mibDb.items {
83 reportedMe, meErr := me.LoadManagedEntityDefinition(entry.classId, me.ParamData{
84 EntityID: entry.entityId.ToUint16(),
85 Attributes: entry.params,
86 })
87 assert.NoError(t, meErr.GetError())
88
89 response := &omci.MibUploadNextResponse{
90 MeBasePacket: omci.MeBasePacket{
91 EntityClass: me.OnuDataClassID,
92 },
93 ReportedME: *reportedMe,
94 }
95
96 _, err := Serialize(omci.MibUploadNextResponseType, response, uint16(10))
97 assert.NoError(t, err)
98 }
99
100}
Elia Battistonac63b112022-01-12 18:40:49 +0100101
102func Test_GenerateMibDatabase_withPots(t *testing.T) {
Matteo Scandolocfedba42022-02-14 16:08:54 -0800103 common.Config = &common.GlobalConfig{
104 BBSim: common.BBSimConfig{
105 InjectOmciUnknownAttributes: false,
106 },
107 }
Elia Battistonac63b112022-01-12 18:40:49 +0100108 const uniPortCount = 4
109 const potsPortCount = 1
Elia Battistonb7bea222022-02-18 16:25:00 +0100110 mibDb, err := GenerateMibDatabase(uniPortCount, potsPortCount, common.XGSPON)
Elia Battistonac63b112022-01-12 18:40:49 +0100111
112 expectedItems := 13 //ONU-G + 3 Circuit Packs (4 messages each)
113 expectedItems += 2 * (uniPortCount + potsPortCount) // 1 PPTP and 1 UniG per UNI
114 expectedItems += 1 // ANI-G
115 expectedItems += 2 * tconts // T-CONT and traffic schedulers
116 expectedItems += 1 // ONU-2g
117 expectedItems += 2 * 8 * tconts // 8 upstream queues for each T-CONT, and we report each queue twice
118 expectedItems += 2 * 16 * (uniPortCount + potsPortCount) // 16 downstream queues for each T-CONT, and we report each queue twice
119
120 assert.NoError(t, err)
121 assert.NotNil(t, mibDb)
122 assert.Equal(t, expectedItems, int(mibDb.NumberOfCommands))
123
124 // now try to serialize all messages to check on the attributes
125 for _, entry := range mibDb.items {
126 reportedMe, meErr := me.LoadManagedEntityDefinition(entry.classId, me.ParamData{
127 EntityID: entry.entityId.ToUint16(),
128 Attributes: entry.params,
129 })
130 assert.NoError(t, meErr.GetError())
131
132 response := &omci.MibUploadNextResponse{
133 MeBasePacket: omci.MeBasePacket{
134 EntityClass: me.OnuDataClassID,
135 },
136 ReportedME: *reportedMe,
137 }
138
139 _, err := Serialize(omci.MibUploadNextResponseType, response, uint16(10))
140 assert.NoError(t, err)
141 }
142
143}
Matteo Scandolocfedba42022-02-14 16:08:54 -0800144
145func Test_GenerateMibDatabase_withUnkownAttrs(t *testing.T) {
146
147 common.Config = &common.GlobalConfig{
148 BBSim: common.BBSimConfig{
149 InjectOmciUnknownAttributes: true,
150 },
151 }
152
153 const uniPortCount = 4
154 const baseMibEntries = 291 // see Test_GenerateMibDatabase for breakdown
155 const expectedMibEntries = baseMibEntries + 1 // expecting one hardcoded packet
156 mibDb, err := GenerateMibDatabase(uniPortCount, 0, common.XGSPON)
157
158 assert.NoError(t, err)
159 assert.NotNil(t, mibDb)
160 assert.Equal(t, expectedMibEntries, int(mibDb.NumberOfCommands))
161
162 entry := mibDb.items[expectedMibEntries-1] // select the last entry, it's the hardcoded packet
163 fmt.Println(entry.packet)
164 assert.NotNil(t, entry)
165 assert.Equal(t, me.ClassID(37), entry.classId)
166 assert.Nil(t, entry.entityId)
167 assert.Equal(t, 0, len(entry.params))
168 assert.NotNil(t, entry.packet)
169
170 // check that we're generating a valid OMCI payload
171 packet := gopacket.NewPacket(entry.packet, omci.LayerTypeOMCI, gopacket.Lazy)
172 omciLayer := packet.Layer(omci.LayerTypeOMCI)
173 assert.NotNil(t, omciLayer)
174 omciMsg, ok := omciLayer.(*omci.OMCI)
175 assert.True(t, ok)
176 assert.Equal(t, omci.MibUploadNextResponseType, omciMsg.MessageType)
177 assert.Equal(t, uint16(33066), omciMsg.TransactionID)
178}