blob: be5fdb1890a6c27100e06445219907153553af77 [file] [log] [blame]
khenaidoo6e55d9e2019-12-12 18:26:26 -05001/*
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 "context"
20 "github.com/gogo/protobuf/proto"
21 "github.com/opencord/voltha-go/rw_core/config"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080022 com "github.com/opencord/voltha-lib-go/v3/pkg/adapters/common"
23 "github.com/opencord/voltha-lib-go/v3/pkg/kafka"
24 "github.com/opencord/voltha-lib-go/v3/pkg/log"
25 lm "github.com/opencord/voltha-lib-go/v3/pkg/mocks"
26 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
27 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoo6e55d9e2019-12-12 18:26:26 -050028 "github.com/phayes/freeport"
29 "github.com/stretchr/testify/assert"
30 "math/rand"
khenaidoob2121e52019-12-16 17:17:22 -050031 "sort"
khenaidoo6e55d9e2019-12-12 18:26:26 -050032 "strings"
33 "sync"
34 "testing"
35 "time"
36)
37
38type DATest struct {
39 etcdServer *lm.EtcdServer
40 core *Core
41 kClient kafka.Client
42 kvClientPort int
43 oltAdapterName string
44 onuAdapterName string
45 coreInstanceID string
46 defaultTimeout time.Duration
47 maxTimeout time.Duration
48 device *voltha.Device
49 done chan int
50}
51
52func newDATest() *DATest {
53 test := &DATest{}
54 // Start the embedded etcd server
55 var err error
56 test.etcdServer, test.kvClientPort, err = startEmbeddedEtcdServer("voltha.rwcore.da.test", "voltha.rwcore.da.etcd", "error")
57 if err != nil {
58 log.Fatal(err)
59 }
60 // Create the kafka client
61 test.kClient = lm.NewKafkaClient()
62 test.oltAdapterName = "olt_adapter_mock"
63 test.onuAdapterName = "onu_adapter_mock"
64 test.coreInstanceID = "rw-da-test"
65 test.defaultTimeout = 5 * time.Second
66 test.maxTimeout = 20 * time.Second
67 test.done = make(chan int)
68 parentID := com.GetRandomString(10)
69 test.device = &voltha.Device{
70 Type: "onu_adapter_mock",
71 ParentId: parentID,
72 ParentPortNo: 1,
73 VendorId: "onu_adapter_mock",
74 Adapter: "onu_adapter_mock",
75 Vlan: 100,
76 Address: nil,
77 ProxyAddress: &voltha.Device_ProxyAddress{
78 DeviceId: parentID,
79 DeviceType: "olt_adapter_mock",
80 ChannelId: 100,
81 ChannelGroupId: 0,
82 ChannelTermination: "",
83 OnuId: 2,
84 },
85 AdminState: voltha.AdminState_PREPROVISIONED,
86 OperStatus: voltha.OperStatus_UNKNOWN,
87 Reason: "All good",
88 ConnectStatus: voltha.ConnectStatus_UNKNOWN,
89 Custom: nil,
90 Ports: []*voltha.Port{
91 {PortNo: 1, Label: "pon-1", Type: voltha.Port_PON_ONU, AdminState: voltha.AdminState_ENABLED,
92 OperStatus: voltha.OperStatus_ACTIVE, Peers: []*voltha.Port_PeerPort{{DeviceId: parentID, PortNo: 1}}},
93 {PortNo: 100, Label: "uni-100", Type: voltha.Port_ETHERNET_UNI, AdminState: voltha.AdminState_ENABLED,
94 OperStatus: voltha.OperStatus_ACTIVE},
95 },
96 }
97
98 return test
99}
100
101func (dat *DATest) startCore(inCompeteMode bool) {
102 cfg := config.NewRWCoreFlags()
103 cfg.CorePairTopic = "rw_core"
104 cfg.DefaultRequestTimeout = dat.defaultTimeout.Nanoseconds() / 1000000 //TODO: change when Core changes to Duration
105 cfg.KVStorePort = dat.kvClientPort
106 cfg.InCompetingMode = inCompeteMode
107 grpcPort, err := freeport.GetFreePort()
108 if err != nil {
109 log.Fatal("Cannot get a freeport for grpc")
110 }
111 cfg.GrpcPort = grpcPort
112 cfg.GrpcHost = "127.0.0.1"
113 setCoreCompeteMode(inCompeteMode)
114 client := setupKVClient(cfg, dat.coreInstanceID)
Thomas Lee Se5a44012019-11-07 20:32:24 +0530115 dat.core = NewCore(context.Background(), dat.coreInstanceID, cfg, client, dat.kClient)
116 err = dat.core.Start(context.Background())
117 if err != nil {
118 log.Fatal("Cannot start core")
119 }
khenaidoo6e55d9e2019-12-12 18:26:26 -0500120}
121
122func (dat *DATest) stopAll() {
123 if dat.kClient != nil {
124 dat.kClient.Stop()
125 }
126 if dat.core != nil {
127 dat.core.Stop(context.Background())
128 }
129 if dat.etcdServer != nil {
130 stopEmbeddedEtcdServer(dat.etcdServer)
131 }
132}
133
134func (dat *DATest) createDeviceAgent(t *testing.T) *DeviceAgent {
135 deviceMgr := dat.core.deviceMgr
136 clonedDevice := proto.Clone(dat.device).(*voltha.Device)
137 deviceAgent := newDeviceAgent(deviceMgr.adapterProxy, clonedDevice, deviceMgr, deviceMgr.clusterDataProxy, deviceMgr.defaultTimeout)
138 d, err := deviceAgent.start(context.TODO(), clonedDevice)
139 assert.Nil(t, err)
140 assert.NotNil(t, d)
141 deviceMgr.addDeviceAgentToMap(deviceAgent)
142 return deviceAgent
143}
144
145func (dat *DATest) updateDeviceConcurrently(t *testing.T, da *DeviceAgent, globalWG *sync.WaitGroup) {
146 originalDevice := da.getDevice()
147 assert.NotNil(t, originalDevice)
148 var localWG sync.WaitGroup
149
150 // Update device routine
151 var (
152 root = false
153 vendor = "onu_adapter_mock"
154 model = "go-mock"
155 serialNumber = com.GetRandomSerialNumber()
156 macAddress = strings.ToUpper(com.GetRandomMacAddress())
157 vlan = rand.Uint32()
158 reason = "testing concurrent device update"
159 portToAdd = &voltha.Port{PortNo: 101, Label: "uni-101", Type: voltha.Port_ETHERNET_UNI, AdminState: voltha.AdminState_ENABLED,
160 OperStatus: voltha.OperStatus_ACTIVE}
161 )
162 localWG.Add(1)
163 go func() {
164 deviceToUpdate := proto.Clone(originalDevice).(*voltha.Device)
165 deviceToUpdate.Root = root
166 deviceToUpdate.Vendor = vendor
167 deviceToUpdate.Model = model
168 deviceToUpdate.SerialNumber = serialNumber
169 deviceToUpdate.MacAddress = macAddress
170 deviceToUpdate.Vlan = vlan
171 deviceToUpdate.Reason = reason
npujar467fe752020-01-16 20:17:45 +0530172 err := da.updateDeviceUsingAdapterData(context.Background(), deviceToUpdate)
khenaidoo6e55d9e2019-12-12 18:26:26 -0500173 assert.Nil(t, err)
174 localWG.Done()
175 }()
176
177 // Update the device status routine
178 localWG.Add(1)
179 go func() {
npujar467fe752020-01-16 20:17:45 +0530180 err := da.updateDeviceStatus(context.Background(), voltha.OperStatus_ACTIVE, voltha.ConnectStatus_REACHABLE)
khenaidoo6e55d9e2019-12-12 18:26:26 -0500181 assert.Nil(t, err)
182 localWG.Done()
183 }()
184
185 // Add a port routine
186 localWG.Add(1)
187 go func() {
npujar467fe752020-01-16 20:17:45 +0530188 err := da.addPort(context.Background(), portToAdd)
khenaidoo6e55d9e2019-12-12 18:26:26 -0500189 assert.Nil(t, err)
190 localWG.Done()
191 }()
192
193 // wait for go routines to be done
194 localWG.Wait()
195
196 expectedChange := proto.Clone(originalDevice).(*voltha.Device)
197 expectedChange.OperStatus = voltha.OperStatus_ACTIVE
198 expectedChange.ConnectStatus = voltha.ConnectStatus_REACHABLE
199 expectedChange.Ports = append(expectedChange.Ports, portToAdd)
200 expectedChange.Root = root
201 expectedChange.Vendor = vendor
202 expectedChange.Model = model
203 expectedChange.SerialNumber = serialNumber
204 expectedChange.MacAddress = macAddress
205 expectedChange.Vlan = vlan
206 expectedChange.Reason = reason
207
208 updatedDevice := da.getDevice()
209 assert.NotNil(t, updatedDevice)
210 assert.True(t, proto.Equal(expectedChange, updatedDevice))
211
212 globalWG.Done()
213}
214
215func TestConcurrentDevices(t *testing.T) {
216 da := newDATest()
217 assert.NotNil(t, da)
218 defer da.stopAll()
219
220 // Start the Core
221 da.startCore(false)
222
223 var wg sync.WaitGroup
224 numConCurrentDeviceAgents := 20
225 for i := 0; i < numConCurrentDeviceAgents; i++ {
226 wg.Add(1)
227 a := da.createDeviceAgent(t)
228 go da.updateDeviceConcurrently(t, a, &wg)
229 }
230
231 wg.Wait()
232}
khenaidoob2121e52019-12-16 17:17:22 -0500233
234func isFlowSliceEqual(a, b []*ofp.OfpFlowStats) bool {
235 if len(a) != len(b) {
236 return false
237 }
238 sort.Slice(a, func(i, j int) bool {
239 return a[i].Id < a[j].Id
240 })
241 sort.Slice(b, func(i, j int) bool {
242 return b[i].Id < b[j].Id
243 })
244 for idx := range a {
245 if !proto.Equal(a[idx], b[idx]) {
246 return false
247 }
248 }
249 return true
250}
251
252func isGroupSliceEqual(a, b []*ofp.OfpGroupEntry) bool {
253 if len(a) != len(b) {
254 return false
255 }
256 sort.Slice(a, func(i, j int) bool {
257 return a[i].Desc.GroupId < a[j].Desc.GroupId
258 })
259 sort.Slice(b, func(i, j int) bool {
260 return b[i].Desc.GroupId < b[j].Desc.GroupId
261 })
262 for idx := range a {
263 if !proto.Equal(a[idx], b[idx]) {
264 return false
265 }
266 }
267 return true
268}
269
270func TestFlowsToUpdateToDelete_EmptySlices(t *testing.T) {
271 newFlows := []*ofp.OfpFlowStats{}
272 existingFlows := []*ofp.OfpFlowStats{}
273 expectedNewFlows := []*ofp.OfpFlowStats{}
274 expectedFlowsToDelete := []*ofp.OfpFlowStats{}
275 expectedUpdatedAllFlows := []*ofp.OfpFlowStats{}
276 uNF, fD, uAF := flowsToUpdateToDelete(newFlows, existingFlows)
277 assert.True(t, isFlowSliceEqual(uNF, expectedNewFlows))
278 assert.True(t, isFlowSliceEqual(fD, expectedFlowsToDelete))
279 assert.True(t, isFlowSliceEqual(uAF, expectedUpdatedAllFlows))
280}
281
282func TestFlowsToUpdateToDelete_NoExistingFlows(t *testing.T) {
283 newFlows := []*ofp.OfpFlowStats{
284 {Id: 123, TableId: 1230, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1230000, PacketCount: 0},
285 {Id: 124, TableId: 1240, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1240000, PacketCount: 0},
286 {Id: 125, TableId: 1250, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1250000, PacketCount: 0},
287 }
288 existingFlows := []*ofp.OfpFlowStats{}
289 expectedNewFlows := []*ofp.OfpFlowStats{
290 {Id: 123, TableId: 1230, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1230000, PacketCount: 0},
291 {Id: 124, TableId: 1240, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1240000, PacketCount: 0},
292 {Id: 125, TableId: 1250, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1250000, PacketCount: 0},
293 }
294 expectedFlowsToDelete := []*ofp.OfpFlowStats{}
295 expectedUpdatedAllFlows := []*ofp.OfpFlowStats{
296 {Id: 123, TableId: 1230, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1230000, PacketCount: 0},
297 {Id: 124, TableId: 1240, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1240000, PacketCount: 0},
298 {Id: 125, TableId: 1250, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1250000, PacketCount: 0},
299 }
300 uNF, fD, uAF := flowsToUpdateToDelete(newFlows, existingFlows)
301 assert.True(t, isFlowSliceEqual(uNF, expectedNewFlows))
302 assert.True(t, isFlowSliceEqual(fD, expectedFlowsToDelete))
303 assert.True(t, isFlowSliceEqual(uAF, expectedUpdatedAllFlows))
304}
305
306func TestFlowsToUpdateToDelete_UpdateNoDelete(t *testing.T) {
307 newFlows := []*ofp.OfpFlowStats{
308 {Id: 123, TableId: 1230, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1230000, PacketCount: 0},
309 {Id: 124, TableId: 1240, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1240000, PacketCount: 0},
310 {Id: 125, TableId: 1250, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1250000, PacketCount: 0},
311 }
312 existingFlows := []*ofp.OfpFlowStats{
313 {Id: 121, TableId: 1210, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1210000, PacketCount: 0},
314 {Id: 124, TableId: 1240, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1240000, PacketCount: 0},
315 {Id: 122, TableId: 1220, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1220000, PacketCount: 0},
316 }
317 expectedNewFlows := []*ofp.OfpFlowStats{
318 {Id: 123, TableId: 1230, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1230000, PacketCount: 0},
319 {Id: 125, TableId: 1250, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1250000, PacketCount: 0},
320 }
321 expectedFlowsToDelete := []*ofp.OfpFlowStats{}
322 expectedUpdatedAllFlows := []*ofp.OfpFlowStats{
323 {Id: 123, TableId: 1230, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1230000, PacketCount: 0},
324 {Id: 124, TableId: 1240, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1240000, PacketCount: 0},
325 {Id: 125, TableId: 1250, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1250000, PacketCount: 0},
326 {Id: 121, TableId: 1210, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1210000, PacketCount: 0},
327 {Id: 122, TableId: 1220, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1220000, PacketCount: 0},
328 }
329 uNF, fD, uAF := flowsToUpdateToDelete(newFlows, existingFlows)
330 assert.True(t, isFlowSliceEqual(uNF, expectedNewFlows))
331 assert.True(t, isFlowSliceEqual(fD, expectedFlowsToDelete))
332 assert.True(t, isFlowSliceEqual(uAF, expectedUpdatedAllFlows))
333}
334
335func TestFlowsToUpdateToDelete_UpdateAndDelete(t *testing.T) {
336 newFlows := []*ofp.OfpFlowStats{
337 {Id: 123, TableId: 1230, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1230000, PacketCount: 20},
338 {Id: 124, TableId: 1240, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1240000, PacketCount: 0},
339 {Id: 125, TableId: 1250, Priority: 1000, IdleTimeout: 10, Flags: 0, Cookie: 1250000, PacketCount: 0},
340 {Id: 126, TableId: 1260, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1260000, PacketCount: 0},
341 {Id: 127, TableId: 1270, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1270000, PacketCount: 0},
342 }
343 existingFlows := []*ofp.OfpFlowStats{
344 {Id: 121, TableId: 1210, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1210000, PacketCount: 0},
345 {Id: 122, TableId: 1220, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1220000, PacketCount: 0},
346 {Id: 123, TableId: 1230, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1230000, PacketCount: 0},
347 {Id: 124, TableId: 1240, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1240000, PacketCount: 0},
348 {Id: 125, TableId: 1250, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1250000, PacketCount: 0},
349 }
350 expectedNewFlows := []*ofp.OfpFlowStats{
351 {Id: 123, TableId: 1230, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1230000, PacketCount: 20},
352 {Id: 125, TableId: 1250, Priority: 1000, IdleTimeout: 10, Flags: 0, Cookie: 1250000, PacketCount: 0},
353 {Id: 126, TableId: 1260, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1260000, PacketCount: 0},
354 {Id: 127, TableId: 1270, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1270000, PacketCount: 0},
355 }
356 expectedFlowsToDelete := []*ofp.OfpFlowStats{
357 {Id: 123, TableId: 1230, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1230000, PacketCount: 0},
358 {Id: 125, TableId: 1250, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1250000, PacketCount: 0},
359 }
360 expectedUpdatedAllFlows := []*ofp.OfpFlowStats{
361 {Id: 121, TableId: 1210, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1210000, PacketCount: 0},
362 {Id: 122, TableId: 1220, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1220000, PacketCount: 0},
363 {Id: 123, TableId: 1230, Priority: 100, IdleTimeout: 0, Flags: 0, Cookie: 1230000, PacketCount: 20},
364 {Id: 124, TableId: 1240, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1240000, PacketCount: 0},
365 {Id: 125, TableId: 1250, Priority: 1000, IdleTimeout: 10, Flags: 0, Cookie: 1250000, PacketCount: 0},
366 {Id: 126, TableId: 1260, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1260000, PacketCount: 0},
367 {Id: 127, TableId: 1270, Priority: 1000, IdleTimeout: 0, Flags: 0, Cookie: 1270000, PacketCount: 0},
368 }
369 uNF, fD, uAF := flowsToUpdateToDelete(newFlows, existingFlows)
370 assert.True(t, isFlowSliceEqual(uNF, expectedNewFlows))
371 assert.True(t, isFlowSliceEqual(fD, expectedFlowsToDelete))
372 assert.True(t, isFlowSliceEqual(uAF, expectedUpdatedAllFlows))
373}
374
375func TestGroupsToUpdateToDelete_EmptySlices(t *testing.T) {
376 newGroups := []*ofp.OfpGroupEntry{}
377 existingGroups := []*ofp.OfpGroupEntry{}
378 expectedNewGroups := []*ofp.OfpGroupEntry{}
379 expectedGroupsToDelete := []*ofp.OfpGroupEntry{}
380 expectedUpdatedAllGroups := []*ofp.OfpGroupEntry{}
381 uNG, gD, uAG := groupsToUpdateToDelete(newGroups, existingGroups)
382 assert.True(t, isGroupSliceEqual(uNG, expectedNewGroups))
383 assert.True(t, isGroupSliceEqual(gD, expectedGroupsToDelete))
384 assert.True(t, isGroupSliceEqual(uAG, expectedUpdatedAllGroups))
385}
386
387func TestGroupsToUpdateToDelete_NoExistingGroups(t *testing.T) {
388 newGroups := []*ofp.OfpGroupEntry{
389 {Desc: &ofp.OfpGroupDesc{Type: 1, GroupId: 10, Buckets: nil}},
390 {Desc: &ofp.OfpGroupDesc{Type: 2, GroupId: 20, Buckets: nil}},
391 }
392 existingGroups := []*ofp.OfpGroupEntry{}
393 expectedNewGroups := []*ofp.OfpGroupEntry{
394 {Desc: &ofp.OfpGroupDesc{Type: 1, GroupId: 10, Buckets: nil}},
395 {Desc: &ofp.OfpGroupDesc{Type: 2, GroupId: 20, Buckets: nil}},
396 }
397 expectedGroupsToDelete := []*ofp.OfpGroupEntry{}
398 expectedUpdatedAllGroups := []*ofp.OfpGroupEntry{
399 {Desc: &ofp.OfpGroupDesc{Type: 1, GroupId: 10, Buckets: nil}},
400 {Desc: &ofp.OfpGroupDesc{Type: 2, GroupId: 20, Buckets: nil}},
401 }
402 uNG, gD, uAG := groupsToUpdateToDelete(newGroups, existingGroups)
403 assert.True(t, isGroupSliceEqual(uNG, expectedNewGroups))
404 assert.True(t, isGroupSliceEqual(gD, expectedGroupsToDelete))
405 assert.True(t, isGroupSliceEqual(uAG, expectedUpdatedAllGroups))
406}
407
408func TestGroupsToUpdateToDelete_UpdateNoDelete(t *testing.T) {
409 newGroups := []*ofp.OfpGroupEntry{
410 {Desc: &ofp.OfpGroupDesc{Type: 1, GroupId: 10, Buckets: nil}},
411 {Desc: &ofp.OfpGroupDesc{Type: 2, GroupId: 20, Buckets: nil}},
412 }
413 existingGroups := []*ofp.OfpGroupEntry{
414 {Desc: &ofp.OfpGroupDesc{Type: 2, GroupId: 20, Buckets: nil}},
415 {Desc: &ofp.OfpGroupDesc{Type: 3, GroupId: 30, Buckets: nil}},
416 {Desc: &ofp.OfpGroupDesc{Type: 4, GroupId: 40, Buckets: nil}},
417 }
418 expectedNewGroups := []*ofp.OfpGroupEntry{
419 {Desc: &ofp.OfpGroupDesc{Type: 1, GroupId: 10, Buckets: nil}},
420 }
421 expectedGroupsToDelete := []*ofp.OfpGroupEntry{}
422 expectedUpdatedAllGroups := []*ofp.OfpGroupEntry{
423 {Desc: &ofp.OfpGroupDesc{Type: 1, GroupId: 10, Buckets: nil}},
424 {Desc: &ofp.OfpGroupDesc{Type: 2, GroupId: 20, Buckets: nil}},
425 {Desc: &ofp.OfpGroupDesc{Type: 3, GroupId: 30, Buckets: nil}},
426 {Desc: &ofp.OfpGroupDesc{Type: 4, GroupId: 40, Buckets: nil}},
427 }
428 uNG, gD, uAG := groupsToUpdateToDelete(newGroups, existingGroups)
429 assert.True(t, isGroupSliceEqual(uNG, expectedNewGroups))
430 assert.True(t, isGroupSliceEqual(gD, expectedGroupsToDelete))
431 assert.True(t, isGroupSliceEqual(uAG, expectedUpdatedAllGroups))
432}
433
434func TestGroupsToUpdateToDelete_UpdateWithDelete(t *testing.T) {
435 newGroups := []*ofp.OfpGroupEntry{
436 {Desc: &ofp.OfpGroupDesc{Type: 1, GroupId: 10, Buckets: nil}},
437 {Desc: &ofp.OfpGroupDesc{Type: 2, GroupId: 20, Buckets: []*ofp.OfpBucket{{WatchPort: 10}}}},
438 }
439 existingGroups := []*ofp.OfpGroupEntry{
440 {Desc: &ofp.OfpGroupDesc{Type: 2, GroupId: 20, Buckets: nil}},
441 {Desc: &ofp.OfpGroupDesc{Type: 3, GroupId: 30, Buckets: nil}},
442 {Desc: &ofp.OfpGroupDesc{Type: 4, GroupId: 40, Buckets: nil}},
443 }
444 expectedNewGroups := []*ofp.OfpGroupEntry{
445 {Desc: &ofp.OfpGroupDesc{Type: 1, GroupId: 10, Buckets: nil}},
446 {Desc: &ofp.OfpGroupDesc{Type: 2, GroupId: 20, Buckets: []*ofp.OfpBucket{{WatchPort: 10}}}},
447 }
448 expectedGroupsToDelete := []*ofp.OfpGroupEntry{
449 {Desc: &ofp.OfpGroupDesc{Type: 2, GroupId: 20, Buckets: nil}},
450 }
451 expectedUpdatedAllGroups := []*ofp.OfpGroupEntry{
452 {Desc: &ofp.OfpGroupDesc{Type: 1, GroupId: 10, Buckets: nil}},
453 {Desc: &ofp.OfpGroupDesc{Type: 2, GroupId: 20, Buckets: []*ofp.OfpBucket{{WatchPort: 10}}}},
454 {Desc: &ofp.OfpGroupDesc{Type: 3, GroupId: 30, Buckets: nil}},
455 {Desc: &ofp.OfpGroupDesc{Type: 4, GroupId: 40, Buckets: nil}},
456 }
457 uNG, gD, uAG := groupsToUpdateToDelete(newGroups, existingGroups)
458 assert.True(t, isGroupSliceEqual(uNG, expectedNewGroups))
459 assert.True(t, isGroupSliceEqual(gD, expectedGroupsToDelete))
460 assert.True(t, isGroupSliceEqual(uAG, expectedUpdatedAllGroups))
461}