blob: 4b9cc26fd4ea21ba0c9f1c8932b29116c23f6e5a [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 (
20 bbsim "github.com/opencord/bbsim/internal/bbsim/types"
21 omcilib "github.com/opencord/bbsim/internal/common/omci"
22 "github.com/opencord/omci-lib-go"
23 me "github.com/opencord/omci-lib-go/generated"
24 "github.com/opencord/voltha-protos/v4/go/openolt"
25 "gotest.tools/assert"
26 "testing"
27)
28
29var mockAttr = me.AttributeValueMap{
30 "ManagedEntityId": 12,
31 "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 }
49 omciPkt, err := omcilib.Serialize(omci.CreateRequestType, omciReq, 66)
50 if err != nil {
51 t.Fatal(err.Error())
52 }
53
54 omciPkt, _ = omcilib.HexEncode(omciPkt)
55
56 return omciPkt
57}
58
59func makeOmciSetRequest(t *testing.T) []byte {
60 omciReq := &omci.SetRequest{
61 MeBasePacket: omci.MeBasePacket{
62 EntityClass: me.GemPortNetworkCtpClassID,
63 EntityInstance: 12,
64 },
65 Attributes: mockAttr,
66 }
67 omciPkt, err := omcilib.Serialize(omci.SetRequestType, omciReq, 66)
68 if err != nil {
69 t.Fatal(err.Error())
70 }
71
72 omciPkt, _ = omcilib.HexEncode(omciPkt)
73
74 return omciPkt
75}
76
77func makeOmciDeleteRequest(t *testing.T) []byte {
78 omciReq := &omci.DeleteRequest{
79 MeBasePacket: omci.MeBasePacket{
80 EntityClass: me.GemPortNetworkCtpClassID,
81 EntityInstance: 12,
82 },
83 }
84 omciPkt, err := omcilib.Serialize(omci.DeleteRequestType, omciReq, 66)
85 if err != nil {
86 t.Fatal(err.Error())
87 }
88
89 omciPkt, _ = omcilib.HexEncode(omciPkt)
90
91 return omciPkt
92}
93
94func makeOmciMibResetRequest(t *testing.T) []byte {
95 omciReq := &omci.MibResetRequest{
96 MeBasePacket: omci.MeBasePacket{
97 EntityClass: me.OnuDataClassID,
98 },
99 }
100 omciPkt, err := omcilib.Serialize(omci.MibResetRequestType, omciReq, 66)
101 if err != nil {
102 t.Fatal(err.Error())
103 }
104
105 omciPkt, _ = omcilib.HexEncode(omciPkt)
106
107 return omciPkt
108}
109
110func makeOmciMessage(t *testing.T, onu *Onu, pkt []byte) bbsim.OmciMessage {
111 return bbsim.OmciMessage{
112 OnuSN: onu.SerialNumber,
113 OnuID: onu.ID,
114 OmciMsg: &openolt.OmciMsg{
115 IntfId: onu.PonPortID,
116 OnuId: onu.ID,
117 Pkt: pkt,
118 },
119 }
120}
121
122func Test_MibDataSyncIncrease(t *testing.T) {
123 onu := createMockOnu(1, 1)
124
125 assert.Equal(t, onu.MibDataSync, uint8(0))
126
127 stream := &mockStream{
128 Calls: make(map[int]*openolt.Indication),
129 }
130
131 // send a Create and check that MDS has been increased
132 onu.handleOmciRequest(makeOmciMessage(t, onu, makeOmciCreateRequest(t)), stream)
133 assert.Equal(t, onu.MibDataSync, uint8(1))
134
135 // send a Set and check that MDS has been increased
136 onu.handleOmciRequest(makeOmciMessage(t, onu, makeOmciSetRequest(t)), stream)
137 assert.Equal(t, onu.MibDataSync, uint8(2))
138
139 // send a Delete and check that MDS has been increased
140 onu.handleOmciRequest(makeOmciMessage(t, onu, makeOmciDeleteRequest(t)), stream)
141 assert.Equal(t, onu.MibDataSync, uint8(3))
142
143 // TODO once supported MDS should increase for:
144 // - Start software download
145 // - End software download
146 // - Activate software
147 // - Commit software
148}
149
150func Test_MibDataSyncReset(t *testing.T) {
151 onu := createMockOnu(1, 1)
152 onu.MibDataSync = 192
153 assert.Equal(t, onu.MibDataSync, uint8(192))
154
155 stream := &mockStream{
156 Calls: make(map[int]*openolt.Indication),
157 }
158
159 // send a MibReset and check that MDS has reset to 0
160 onu.handleOmciRequest(makeOmciMessage(t, onu, makeOmciMibResetRequest(t)), stream)
161 assert.Equal(t, onu.MibDataSync, uint8(0))
162}
163
164func Test_MibDataSyncRotation(t *testing.T) {
165 onu := createMockOnu(1, 1)
166 onu.MibDataSync = 255
167 assert.Equal(t, onu.MibDataSync, uint8(255))
168
169 stream := &mockStream{
170 Calls: make(map[int]*openolt.Indication),
171 }
172
173 // send a request that increases the MDS, but once we're at 255 we should go back to 0 (8bit)
174 onu.handleOmciRequest(makeOmciMessage(t, onu, makeOmciDeleteRequest(t)), stream)
175 assert.Equal(t, onu.MibDataSync, uint8(0))
176}