2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.applications.frsync.impl;
10 import com.google.common.util.concurrent.ListenableFuture;
11 import java.util.Collections;
12 import java.util.concurrent.TimeUnit;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.ArgumentCaptor;
18 import org.mockito.ArgumentMatchers;
19 import org.mockito.Captor;
20 import org.mockito.Mock;
21 import org.mockito.Mockito;
22 import org.mockito.junit.MockitoJUnitRunner;
23 import org.opendaylight.mdsal.binding.api.DataBroker;
24 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
25 import org.opendaylight.openflowplugin.applications.frsync.SyncPlanPushStrategy;
26 import org.opendaylight.openflowplugin.applications.frsync.impl.strategy.SynchronizationDiffInput;
27 import org.opendaylight.openflowplugin.applications.frsync.util.ReconcileUtil;
28 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * Test for {@link SyncReactorImpl}.
48 @RunWith(MockitoJUnitRunner.class)
49 public class SyncReactorImplTest {
51 private static final Logger LOG = LoggerFactory.getLogger(SyncReactorImplTest.class);
53 private static final NodeId NODE_ID = new NodeId("unit-nodeId");
54 private static final InstanceIdentifier<FlowCapableNode> NODE_IDENT = InstanceIdentifier.create(Nodes.class)
55 .child(Node.class, new NodeKey(NODE_ID)).augmentation(FlowCapableNode.class);
56 private SyncReactorImpl reactor;
59 private DataBroker db;
61 private SyncPlanPushStrategy syncPlanPushStrategy;
63 private ArgumentCaptor<Group> groupCaptor;
65 private ArgumentCaptor<Group> groupUpdateCaptor;
67 private ArgumentCaptor<Flow> flowCaptor;
69 private ArgumentCaptor<Flow> flowUpdateCaptor;
71 private ArgumentCaptor<Meter> meterCaptor;
73 private ArgumentCaptor<Meter> meterUpdateCaptor;
75 private ArgumentCaptor<TableFeatures> tableFeaturesCaptor;
77 private ArgumentCaptor<SynchronizationDiffInput> syncDiffInputCaptor;
80 public void setUp() throws Exception {
81 reactor = new SyncReactorImpl(syncPlanPushStrategy);
85 public void testSyncup() throws Exception {
86 final FlowCapableNode configFcn = new FlowCapableNodeBuilder()
87 .setGroup(Collections.singletonList(DSInputFactory.createGroup(1L)))
88 .setTable(Collections.singletonList(new TableBuilder()
89 .setFlow(Collections.singletonList(DSInputFactory.createFlow("f1", 1)))
91 .setMeter(Collections.singletonList(DSInputFactory.createMeter(1L)))
94 final FlowCapableNode operationalFcn = new FlowCapableNodeBuilder()
95 .setGroup(Collections.singletonList(DSInputFactory.createGroup(2L)))
96 .setTable(Collections.singletonList(new TableBuilder()
97 .setFlow(Collections.singletonList(DSInputFactory.createFlow("f2", 2)))
99 .setMeter(Collections.singletonList(DSInputFactory.createMeter(2L)))
102 final SyncupEntry syncupEntry = new SyncupEntry(configFcn, LogicalDatastoreType.CONFIGURATION,
103 operationalFcn, LogicalDatastoreType.OPERATIONAL);
105 Mockito.when(syncPlanPushStrategy.executeSyncStrategy(
106 ArgumentMatchers.any(),
107 ArgumentMatchers.any(),
108 ArgumentMatchers.any()))
109 .thenReturn(RpcResultBuilder.<Void>success().buildFuture());
111 final ListenableFuture<Boolean> syncupResult = reactor.syncup(NODE_IDENT, syncupEntry);
112 Assert.assertTrue(syncupResult.isDone());
113 final Boolean voidRpcResult = syncupResult.get(2, TimeUnit.SECONDS);
114 Assert.assertTrue(voidRpcResult);
116 Mockito.verify(syncPlanPushStrategy).executeSyncStrategy(
117 ArgumentMatchers.any(),
118 syncDiffInputCaptor.capture(),
119 ArgumentMatchers.any());
121 final SynchronizationDiffInput diffInput = syncDiffInputCaptor.getValue();
122 Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getFlowsToAddOrUpdate().values()));
123 Assert.assertEquals(0, ReconcileUtil.countTotalUpdated(diffInput.getFlowsToAddOrUpdate().values()));
124 Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getFlowsToRemove().values()));
126 Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getGroupsToAddOrUpdate()));
127 Assert.assertEquals(0, ReconcileUtil.countTotalUpdated(diffInput.getGroupsToAddOrUpdate()));
128 Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getGroupsToRemove()));
130 Assert.assertEquals(1, diffInput.getMetersToAddOrUpdate().getItemsToPush().size());
131 Assert.assertEquals(0, diffInput.getMetersToAddOrUpdate().getItemsToUpdate().size());
132 Assert.assertEquals(1, diffInput.getMetersToRemove().getItemsToPush().size());