blob: f569bce19e0baa09accc1fb81b666d70a98e9ddb [file] [log] [blame]
Matteo Scandolof9d43412021-01-12 11:11:34 -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 omci
18
19import (
Matteo Scandolof9d43412021-01-12 11:11:34 -080020 "github.com/google/gopacket"
21 "github.com/opencord/omci-lib-go"
22 me "github.com/opencord/omci-lib-go/generated"
23 "gotest.tools/assert"
24 "testing"
25)
26
Matteo Scandolo992a23e2021-02-04 15:35:04 -080027// used to verify that the first message in the MIB Sync (OnuData)
28// reports the correct MDS
29const MDS = uint8(128)
30
Matteo Scandolof9d43412021-01-12 11:11:34 -080031func TestCreateMibResetResponse(t *testing.T) {
32 data, _ := CreateMibResetResponse(1)
33
34 omciMsg, omciPkt := omciBytesToMsg(t, data)
35
36 assert.Equal(t, omciMsg.MessageType, omci.MibResetResponseType)
37
38 msgLayer := (*omciPkt).Layer(omci.LayerTypeMibResetResponse)
39 msgObj, msgOk := msgLayer.(*omci.MibResetResponse)
40 if !msgOk {
41 t.Fail()
42 }
43
44 assert.Equal(t, msgObj.Result, me.Success)
45}
46
47// types for TestCreateMibUploadNextResponse test
48type mibArgs struct {
49 omciPkt gopacket.Packet
50 omciMsg *omci.OMCI
51}
52
53type mibExpected struct {
54 messageType omci.MessageType
55 transactionId uint16
56 entityClass me.ClassID
Matteo Scandolo992a23e2021-02-04 15:35:04 -080057 entityID uint16
Matteo Scandolof9d43412021-01-12 11:11:34 -080058 attributes map[string]interface{}
59}
60
61func createTestMibUploadNextArgs(t *testing.T, tid uint16, seqNumber uint16) mibArgs {
62 mibUploadNext, _ := CreateMibUploadNextRequest(tid, seqNumber)
63 mibUploadNext = hexDecode(mibUploadNext)
64 mibUploadNextMsg, mibUploadNextPkt := omciBytesToMsg(t, mibUploadNext)
65
66 return mibArgs{
67 omciPkt: *mibUploadNextPkt,
68 omciMsg: mibUploadNextMsg,
69 }
70}
71
72func TestCreateMibUploadNextResponse(t *testing.T) {
73
74 tests := []struct {
75 name string
76 args mibArgs
77 want mibExpected
78 }{
79 {"mibUploadNext-0", createTestMibUploadNextArgs(t, 1, 0),
Matteo Scandolo9a1842d2021-02-08 16:00:17 -080080
81 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 1, entityID: 0, entityClass: me.OnuDataClassID, attributes: map[string]interface{}{"MibDataSync": MDS}}},
Matteo Scandolof9d43412021-01-12 11:11:34 -080082 {"mibUploadNext-1", createTestMibUploadNextArgs(t, 2, 1),
Matteo Scandolo9a1842d2021-02-08 16:00:17 -080083 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 2, entityID: 257, entityClass: me.CircuitPackClassID, attributes: map[string]interface{}{"Type": uint8(47), "NumberOfPorts": uint8(4)}}},
Matteo Scandolof9d43412021-01-12 11:11:34 -080084 {"mibUploadNext-4", createTestMibUploadNextArgs(t, 3, 4),
Matteo Scandolo9a1842d2021-02-08 16:00:17 -080085 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 3, entityID: 257, entityClass: me.CircuitPackClassID, attributes: map[string]interface{}{"PowerShedOverride": uint32(0)}}},
Matteo Scandolof9d43412021-01-12 11:11:34 -080086 {"mibUploadNext-10", createTestMibUploadNextArgs(t, 4, 10),
Matteo Scandolo9a1842d2021-02-08 16:00:17 -080087 mibExpected{messageType: omci.MibUploadNextResponseType, transactionId: 4, entityID: 258, entityClass: me.PhysicalPathTerminationPointEthernetUniClassID, attributes: map[string]interface{}{"SensedType": uint8(47)}}},
Matteo Scandolof9d43412021-01-12 11:11:34 -080088 }
89
90 for _, tt := range tests {
91 t.Run(tt.name, func(t *testing.T) {
92
93 // create the packet starting from the mibUploadNextRequest
Matteo Scandolo992a23e2021-02-04 15:35:04 -080094 data, _ := CreateMibUploadNextResponse(tt.args.omciPkt, tt.args.omciMsg, MDS)
Matteo Scandolof9d43412021-01-12 11:11:34 -080095 omciMsg, omciPkt := omciBytesToMsg(t, data)
96
97 assert.Equal(t, omciMsg.MessageType, tt.want.messageType)
98
99 msgLayer := (*omciPkt).Layer(omci.LayerTypeMibUploadNextResponse)
100 msgObj, msgOk := msgLayer.(*omci.MibUploadNextResponse)
101 if !msgOk {
102 t.Fail()
103 }
104
105 assert.Equal(t, omciMsg.TransactionID, tt.want.transactionId) // tid
Matteo Scandolof9d43412021-01-12 11:11:34 -0800106
Matteo Scandolo992a23e2021-02-04 15:35:04 -0800107 assert.Equal(t, msgObj.ReportedME.GetEntityID(), tt.want.entityID)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800108
109 for k, v := range tt.want.attributes {
110 attr, _ := msgObj.ReportedME.GetAttribute(k)
111 assert.Equal(t, attr, v)
112 }
113 })
114 }
Matteo Scandolo9a1842d2021-02-08 16:00:17 -0800115}
Matteo Scandolof9d43412021-01-12 11:11:34 -0800116
Matteo Scandolo9a1842d2021-02-08 16:00:17 -0800117type pqueueExpected struct {
118 entityId uint16
119 relatedPort uint32
120}
121
122func TestGeneratePriorityQueueMe(t *testing.T) {
123
124 tests := []struct {
125 name string
126 sequence uint16
127 want pqueueExpected
128 }{
129 {"generate-pq-downstream-1", 26,
130 pqueueExpected{entityId: 1, relatedPort: 16842752}},
131 {"generate-pq-downstream-2", 30,
132 pqueueExpected{entityId: 2, relatedPort: 16842753}},
133 {"generate-pq-downstream-3", 58,
134 pqueueExpected{entityId: 9, relatedPort: 16842760}},
135 {"generate-pq-upstream-1", 28,
136 pqueueExpected{entityId: 32769, relatedPort: 2147549184}},
137 {"generate-pq-upstream-2", 32,
138 pqueueExpected{entityId: 32770, relatedPort: 2147549185}},
139 {"generate-pq-upstream-3", 60,
140 pqueueExpected{entityId: 32777, relatedPort: 2147614720}},
141 }
142
143 for _, tt := range tests {
144 t.Run(tt.name, func(t *testing.T) {
145 reportedMe, meErr := GeneratePriorityQueueMe(tt.sequence)
146 if meErr.GetError() != nil {
147 t.Fatal(meErr.Error())
148 }
149
150 assert.Equal(t, reportedMe.GetEntityID(), tt.want.entityId)
151
152 relatedPort, _ := reportedMe.GetAttribute("RelatedPort")
153 assert.Equal(t, relatedPort, tt.want.relatedPort)
154 })
155 }
156
157 // test that the related ports are unique
158 allRelatedPorts := make(map[uint32]struct{})
159 for v := 26; v <= 281; v++ {
160 reportedMe, meErr := GeneratePriorityQueueMe(uint16(v))
161 if meErr.GetError() != nil {
162 t.Fatal(meErr.Error())
163 }
164 relatedPort, _ := reportedMe.GetAttribute("RelatedPort")
165 allRelatedPorts[relatedPort.(uint32)] = struct{}{}
166 }
167
168 // we report 128 queues total, but each of them is comprised of 2 messages
169 // that's why the 256 iterations
170 assert.Equal(t, len(allRelatedPorts), 128)
Matteo Scandolof9d43412021-01-12 11:11:34 -0800171}