blob: d629af0e8a63f7f27e314fa93e01b7e9bd663068 [file] [log] [blame]
khenaidoo2bc48282019-07-16 18:13:46 -04001/*
2 * Copyright 2019-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 */
16package core
17
18import (
19 ofp "github.com/opencord/voltha-protos/go/openflow_13"
20 "github.com/opencord/voltha-protos/go/voltha"
21 "github.com/stretchr/testify/assert"
22 "testing"
23)
24
25func TestLogicalDeviceAgent_diff_nochange_1(t *testing.T) {
26 currentLogicalPorts := []*voltha.LogicalPort{}
27 updatedLogicalPorts := []*voltha.LogicalPort{}
28 newPorts, changedPorts, deletedPorts := diff(currentLogicalPorts, updatedLogicalPorts)
29 assert.Equal(t, 0, len(newPorts))
30 assert.Equal(t, 0, len(changedPorts))
31 assert.Equal(t, 0, len(deletedPorts))
32}
33
34func TestLogicalDeviceAgent_diff_nochange_2(t *testing.T) {
35 currentLogicalPorts := []*voltha.LogicalPort{
36 {
37 Id: "1231",
38 DeviceId: "d1234",
39 DevicePortNo: 1,
40 RootPort: true,
41 OfpPort: &ofp.OfpPort{
42 PortNo: 1,
43 Name: "port1",
44 Config: 1,
45 State: 1,
46 },
47 },
48 {
49 Id: "1232",
50 DeviceId: "d1234",
51 DevicePortNo: 2,
52 RootPort: false,
53 OfpPort: &ofp.OfpPort{
54 PortNo: 2,
55 Name: "port2",
56 Config: 1,
57 State: 1,
58 },
59 },
60 {
61 Id: "1233",
62 DeviceId: "d1234",
63 DevicePortNo: 3,
64 RootPort: false,
65 OfpPort: &ofp.OfpPort{
66 PortNo: 3,
67 Name: "port3",
68 Config: 1,
69 State: 1,
70 },
71 },
72 }
73 updatedLogicalPorts := []*voltha.LogicalPort{
74 {
75 Id: "1231",
76 DeviceId: "d1234",
77 DevicePortNo: 1,
78 RootPort: true,
79 OfpPort: &ofp.OfpPort{
80 PortNo: 1,
81 Name: "port1",
82 Config: 1,
83 State: 1,
84 },
85 },
86 {
87 Id: "1232",
88 DeviceId: "d1234",
89 DevicePortNo: 2,
90 RootPort: false,
91 OfpPort: &ofp.OfpPort{
92 PortNo: 2,
93 Name: "port2",
94 Config: 1,
95 State: 1,
96 },
97 },
98 {
99 Id: "1233",
100 DeviceId: "d1234",
101 DevicePortNo: 3,
102 RootPort: false,
103 OfpPort: &ofp.OfpPort{
104 PortNo: 3,
105 Name: "port3",
106 Config: 1,
107 State: 1,
108 },
109 },
110 }
111 newPorts, changedPorts, deletedPorts := diff(currentLogicalPorts, updatedLogicalPorts)
112 assert.Equal(t, 0, len(newPorts))
113 assert.Equal(t, 0, len(changedPorts))
114 assert.Equal(t, 0, len(deletedPorts))
115}
116
117func TestLogicalDeviceAgent_diff_add(t *testing.T) {
118 currentLogicalPorts := []*voltha.LogicalPort{}
119 updatedLogicalPorts := []*voltha.LogicalPort{
120 {
121 Id: "1231",
122 DeviceId: "d1234",
123 DevicePortNo: 1,
124 RootPort: true,
125 OfpPort: &ofp.OfpPort{
126 PortNo: 1,
127 Name: "port1",
128 Config: 1,
129 State: 1,
130 },
131 },
132 {
133 Id: "1232",
134 DeviceId: "d1234",
135 DevicePortNo: 2,
136 RootPort: true,
137 OfpPort: &ofp.OfpPort{
138 PortNo: 2,
139 Name: "port2",
140 Config: 1,
141 State: 1,
142 },
143 },
144 }
145 newPorts, changedPorts, deletedPorts := diff(currentLogicalPorts, updatedLogicalPorts)
146 assert.Equal(t, 2, len(newPorts))
147 assert.Equal(t, 0, len(changedPorts))
148 assert.Equal(t, 0, len(deletedPorts))
149 assert.Equal(t, updatedLogicalPorts[0], newPorts[0])
150 assert.Equal(t, updatedLogicalPorts[1], newPorts[1])
151}
152
153func TestLogicalDeviceAgent_diff_delete(t *testing.T) {
154 currentLogicalPorts := []*voltha.LogicalPort{
155 {
156 Id: "1231",
157 DeviceId: "d1234",
158 DevicePortNo: 1,
159 RootPort: true,
160 OfpPort: &ofp.OfpPort{
161 PortNo: 1,
162 Name: "port1",
163 Config: 1,
164 State: 1,
165 },
166 },
167 }
168 updatedLogicalPorts := []*voltha.LogicalPort{}
169 newPorts, changedPorts, deletedPorts := diff(currentLogicalPorts, updatedLogicalPorts)
170 assert.Equal(t, 0, len(newPorts))
171 assert.Equal(t, 0, len(changedPorts))
172 assert.Equal(t, 1, len(deletedPorts))
173 assert.Equal(t, currentLogicalPorts[0], deletedPorts[0])
174}
175
176func TestLogicalDeviceAgent_diff_changed(t *testing.T) {
177 currentLogicalPorts := []*voltha.LogicalPort{
178 {
179 Id: "1231",
180 DeviceId: "d1234",
181 DevicePortNo: 1,
182 RootPort: true,
183 OfpPort: &ofp.OfpPort{
184 PortNo: 1,
185 Name: "port1",
186 Config: 1,
187 State: 1,
188 },
189 },
190 {
191 Id: "1232",
192 DeviceId: "d1234",
193 DevicePortNo: 2,
194 RootPort: false,
195 OfpPort: &ofp.OfpPort{
196 PortNo: 2,
197 Name: "port2",
198 Config: 1,
199 State: 1,
200 },
201 },
202 {
203 Id: "1233",
204 DeviceId: "d1234",
205 DevicePortNo: 3,
206 RootPort: false,
207 OfpPort: &ofp.OfpPort{
208 PortNo: 3,
209 Name: "port3",
210 Config: 1,
211 State: 1,
212 },
213 },
214 }
215 updatedLogicalPorts := []*voltha.LogicalPort{
216 {
217 Id: "1231",
218 DeviceId: "d1234",
219 DevicePortNo: 1,
220 RootPort: true,
221 OfpPort: &ofp.OfpPort{
222 PortNo: 1,
223 Name: "port1",
224 Config: 4,
225 State: 4,
226 },
227 },
228 {
229 Id: "1232",
230 DeviceId: "d1234",
231 DevicePortNo: 2,
232 RootPort: false,
233 OfpPort: &ofp.OfpPort{
234 PortNo: 2,
235 Name: "port2",
236 Config: 4,
237 State: 4,
238 },
239 },
240 {
241 Id: "1233",
242 DeviceId: "d1234",
243 DevicePortNo: 3,
244 RootPort: false,
245 OfpPort: &ofp.OfpPort{
246 PortNo: 3,
247 Name: "port3",
248 Config: 1,
249 State: 1,
250 },
251 },
252 }
253 newPorts, changedPorts, deletedPorts := diff(currentLogicalPorts, updatedLogicalPorts)
254 assert.Equal(t, 0, len(newPorts))
255 assert.Equal(t, 2, len(changedPorts))
256 assert.Equal(t, 0, len(deletedPorts))
257 assert.Equal(t, updatedLogicalPorts[0], changedPorts[0])
258 assert.Equal(t, updatedLogicalPorts[1], changedPorts[1])
259}
260
261func TestLogicalDeviceAgent_diff_mix(t *testing.T) {
262 currentLogicalPorts := []*voltha.LogicalPort{
263 {
264 Id: "1231",
265 DeviceId: "d1234",
266 DevicePortNo: 1,
267 RootPort: true,
268 OfpPort: &ofp.OfpPort{
269 PortNo: 1,
270 Name: "port1",
271 Config: 1,
272 State: 1,
273 },
274 },
275 {
276 Id: "1232",
277 DeviceId: "d1234",
278 DevicePortNo: 2,
279 RootPort: false,
280 OfpPort: &ofp.OfpPort{
281 PortNo: 2,
282 Name: "port2",
283 Config: 1,
284 State: 1,
285 },
286 },
287 {
288 Id: "1233",
289 DeviceId: "d1234",
290 DevicePortNo: 3,
291 RootPort: false,
292 OfpPort: &ofp.OfpPort{
293 PortNo: 3,
294 Name: "port3",
295 Config: 1,
296 State: 1,
297 },
298 },
299 }
300 updatedLogicalPorts := []*voltha.LogicalPort{
301 {
302 Id: "1231",
303 DeviceId: "d1234",
304 DevicePortNo: 1,
305 RootPort: true,
306 OfpPort: &ofp.OfpPort{
307 PortNo: 1,
308 Name: "port1",
309 Config: 4,
310 State: 4,
311 },
312 },
313 {
314 Id: "1232",
315 DeviceId: "d1234",
316 DevicePortNo: 2,
317 RootPort: false,
318 OfpPort: &ofp.OfpPort{
319 PortNo: 2,
320 Name: "port2",
321 Config: 4,
322 State: 4,
323 },
324 },
325 {
326 Id: "1234",
327 DeviceId: "d1234",
328 DevicePortNo: 4,
329 RootPort: false,
330 OfpPort: &ofp.OfpPort{
331 PortNo: 4,
332 Name: "port4",
333 Config: 4,
334 State: 4,
335 },
336 },
337 }
338 newPorts, changedPorts, deletedPorts := diff(currentLogicalPorts, updatedLogicalPorts)
339 assert.Equal(t, 1, len(newPorts))
340 assert.Equal(t, 2, len(changedPorts))
341 assert.Equal(t, 1, len(deletedPorts))
342 assert.Equal(t, updatedLogicalPorts[0], changedPorts[0])
343 assert.Equal(t, updatedLogicalPorts[1], changedPorts[1])
344 assert.Equal(t, currentLogicalPorts[2], deletedPorts[0])
345}