blob: 4d706b91919f962cee3bbfa97a968b7602ffd563 [file] [log] [blame]
Matteo Scandolo992a23e2021-02-04 15:35:04 -08001/*
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 devices
18
19import (
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080020 "github.com/google/gopacket"
Matteo Scandolo992a23e2021-02-04 15:35:04 -080021 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
22 omcilib "github.com/opencord/bbsim/internal/common/omci"
23 "github.com/opencord/omci-lib-go"
24 me "github.com/opencord/omci-lib-go/generated"
25 "github.com/opencord/voltha-protos/v4/go/openolt"
26 "gotest.tools/assert"
27 "testing"
28)
29
30var mockAttr = me.AttributeValueMap{
Matteo Scandolo992a23e2021-02-04 15:35:04 -080031 "PortId": 0,
32 "TContPointer": 0,
33 "Direction": 0,
34 "TrafficManagementPointerForUpstream": 0,
35 "TrafficDescriptorProfilePointerForUpstream": 0,
36 "PriorityQueuePointerForDownStream": 0,
37 "TrafficDescriptorProfilePointerForDownstream": 0,
38 "EncryptionKeyRing": 0,
39}
40
41func makeOmciCreateRequest(t *testing.T) []byte {
42 omciReq := &omci.CreateRequest{
43 MeBasePacket: omci.MeBasePacket{
44 EntityClass: me.GemPortNetworkCtpClassID,
45 EntityInstance: 12,
46 },
47 Attributes: mockAttr,
48 }
Matteo Scandolo4b077aa2021-02-16 17:33:37 -080049
Matteo Scandolo992a23e2021-02-04 15:35:04 -080050 omciPkt, err := omcilib.Serialize(omci.CreateRequestType, omciReq, 66)
51 if err != nil {
52 t.Fatal(err.Error())
53 }
54
55 omciPkt, _ = omcilib.HexEncode(omciPkt)
56
57 return omciPkt
58}
59
60func makeOmciSetRequest(t *testing.T) []byte {
61 omciReq := &omci.SetRequest{
62 MeBasePacket: omci.MeBasePacket{
63 EntityClass: me.GemPortNetworkCtpClassID,
64 EntityInstance: 12,
65 },
66 Attributes: mockAttr,
67 }
68 omciPkt, err := omcilib.Serialize(omci.SetRequestType, omciReq, 66)
69 if err != nil {
70 t.Fatal(err.Error())
71 }
72
73 omciPkt, _ = omcilib.HexEncode(omciPkt)
74
75 return omciPkt
76}
77
78func makeOmciDeleteRequest(t *testing.T) []byte {
79 omciReq := &omci.DeleteRequest{
80 MeBasePacket: omci.MeBasePacket{
81 EntityClass: me.GemPortNetworkCtpClassID,
82 EntityInstance: 12,
83 },
84 }
85 omciPkt, err := omcilib.Serialize(omci.DeleteRequestType, omciReq, 66)
86 if err != nil {
87 t.Fatal(err.Error())
88 }
89
90 omciPkt, _ = omcilib.HexEncode(omciPkt)
91
92 return omciPkt
93}
94
95func makeOmciMibResetRequest(t *testing.T) []byte {
96 omciReq := &omci.MibResetRequest{
97 MeBasePacket: omci.MeBasePacket{
98 EntityClass: me.OnuDataClassID,
99 },
100 }
101 omciPkt, err := omcilib.Serialize(omci.MibResetRequestType, omciReq, 66)
102 if err != nil {
103 t.Fatal(err.Error())
104 }
105
106 omciPkt, _ = omcilib.HexEncode(omciPkt)
107
108 return omciPkt
109}
110
111func makeOmciMessage(t *testing.T, onu *Onu, pkt []byte) bbsim.OmciMessage {
112 return bbsim.OmciMessage{
113 OnuSN: onu.SerialNumber,
114 OnuID: onu.ID,
115 OmciMsg: &openolt.OmciMsg{
116 IntfId: onu.PonPortID,
117 OnuId: onu.ID,
118 Pkt: pkt,
119 },
120 }
121}
122
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800123func omciBytesToMsg(t *testing.T, data []byte) (*omci.OMCI, *gopacket.Packet) {
124 packet := gopacket.NewPacket(data, omci.LayerTypeOMCI, gopacket.NoCopy)
125 if packet == nil {
126 t.Fatal("could not decode rxMsg as OMCI")
127 }
128 omciLayer := packet.Layer(omci.LayerTypeOMCI)
129 if omciLayer == nil {
130 t.Fatal("could not decode omci layer")
131 }
132 omciMsg, ok := omciLayer.(*omci.OMCI)
133 if !ok {
134 t.Fatal("could not assign omci layer")
135 }
136 return omciMsg, &packet
137}
138
139func omciToCreateResponse(t *testing.T, omciPkt *gopacket.Packet) *omci.CreateResponse {
140 msgLayer := (*omciPkt).Layer(omci.LayerTypeCreateResponse)
141 if msgLayer == nil {
142 t.Fatal("omci Msg layer could not be detected for CreateResponse - handling of MibSyncChan stopped")
143 }
144 msgObj, msgOk := msgLayer.(*omci.CreateResponse)
145 if !msgOk {
146 t.Fatal("omci Msg layer could not be assigned for CreateResponse - handling of MibSyncChan stopped")
147 }
148 return msgObj
149}
150
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800151func Test_MibDataSyncIncrease(t *testing.T) {
152 onu := createMockOnu(1, 1)
153
154 assert.Equal(t, onu.MibDataSync, uint8(0))
155
156 stream := &mockStream{
157 Calls: make(map[int]*openolt.Indication),
158 }
159
160 // send a Create and check that MDS has been increased
161 onu.handleOmciRequest(makeOmciMessage(t, onu, makeOmciCreateRequest(t)), stream)
162 assert.Equal(t, onu.MibDataSync, uint8(1))
163
164 // send a Set and check that MDS has been increased
165 onu.handleOmciRequest(makeOmciMessage(t, onu, makeOmciSetRequest(t)), stream)
166 assert.Equal(t, onu.MibDataSync, uint8(2))
167
168 // send a Delete and check that MDS has been increased
169 onu.handleOmciRequest(makeOmciMessage(t, onu, makeOmciDeleteRequest(t)), stream)
170 assert.Equal(t, onu.MibDataSync, uint8(3))
171
172 // TODO once supported MDS should increase for:
173 // - Start software download
174 // - End software download
175 // - Activate software
176 // - Commit software
177}
178
179func Test_MibDataSyncReset(t *testing.T) {
180 onu := createMockOnu(1, 1)
181 onu.MibDataSync = 192
182 assert.Equal(t, onu.MibDataSync, uint8(192))
183
184 stream := &mockStream{
185 Calls: make(map[int]*openolt.Indication),
186 }
187
188 // send a MibReset and check that MDS has reset to 0
189 onu.handleOmciRequest(makeOmciMessage(t, onu, makeOmciMibResetRequest(t)), stream)
190 assert.Equal(t, onu.MibDataSync, uint8(0))
191}
192
193func Test_MibDataSyncRotation(t *testing.T) {
194 onu := createMockOnu(1, 1)
195 onu.MibDataSync = 255
196 assert.Equal(t, onu.MibDataSync, uint8(255))
197
198 stream := &mockStream{
199 Calls: make(map[int]*openolt.Indication),
200 }
201
202 // send a request that increases the MDS, but once we're at 255 we should go back to 0 (8bit)
203 onu.handleOmciRequest(makeOmciMessage(t, onu, makeOmciDeleteRequest(t)), stream)
204 assert.Equal(t, onu.MibDataSync, uint8(0))
205}
Matteo Scandolo4b077aa2021-02-16 17:33:37 -0800206
207func Test_GemPortValidation(t *testing.T) {
208
209 // setup
210 onu := createMockOnu(1, 1)
211
212 stream := &mockStream{
213 Calls: make(map[int]*openolt.Indication),
214 }
215
216 // create a gem port via OMCI (gemPortId 12)
217 onu.handleOmciRequest(makeOmciMessage(t, onu, makeOmciCreateRequest(t)), stream)
218
219 // the first time we created the gemPort
220 // the MDS should be incremented
221 assert.Equal(t, stream.CallCount, 1)
222 assert.Equal(t, onu.MibDataSync, uint8(1))
223
224 // and the OMCI response status should be me.Success
225 indication := stream.Calls[1].GetOmciInd()
226 _, omciPkt := omciBytesToMsg(t, indication.Pkt)
227 responseLayer := omciToCreateResponse(t, omciPkt)
228 assert.Equal(t, responseLayer.Result, me.Success)
229
230 // send a request to create the same gem port via OMCI (gemPortId 12)
231 onu.handleOmciRequest(makeOmciMessage(t, onu, makeOmciCreateRequest(t)), stream)
232
233 // this time the MDS should not be incremented
234 assert.Equal(t, stream.CallCount, 2)
235 assert.Equal(t, onu.MibDataSync, uint8(1))
236
237 // and the OMCI response status should be me.ProcessingError
238 _, omciPkt = omciBytesToMsg(t, stream.Calls[2].GetOmciInd().Pkt)
239 responseLayer = omciToCreateResponse(t, omciPkt)
240 assert.Equal(t, responseLayer.Result, me.ProcessingError)
241}