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
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.Collections;
13 import java.util.concurrent.TimeUnit;
14 import org.junit.Assert;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.mockito.ArgumentCaptor;
19 import org.mockito.ArgumentMatchers;
20 import org.mockito.Captor;
21 import org.mockito.Mock;
22 import org.mockito.Mockito;
23 import org.mockito.runners.MockitoJUnitRunner;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.openflowplugin.applications.frsync.SyncPlanPushStrategy;
27 import org.opendaylight.openflowplugin.applications.frsync.impl.strategy.SynchronizationDiffInput;
28 import org.opendaylight.openflowplugin.applications.frsync.util.ReconcileUtil;
29 import org.opendaylight.openflowplugin.applications.frsync.util.SyncCrudCounters;
30 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43 import org.opendaylight.yangtools.yang.common.RpcResult;
44 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * Test for {@link SyncReactorImpl}.
51 @RunWith(MockitoJUnitRunner.class)
52 public class SyncReactorImplTest {
54 private static final Logger LOG = LoggerFactory.getLogger(SyncReactorImplTest.class);
56 private static final NodeId NODE_ID = new NodeId("unit-nodeId");
57 private static final InstanceIdentifier<FlowCapableNode> NODE_IDENT = InstanceIdentifier.create(Nodes.class)
58 .child(Node.class, new NodeKey(NODE_ID)).augmentation(FlowCapableNode.class);
59 private SyncReactorImpl reactor;
62 private DataBroker db;
64 private SyncPlanPushStrategy syncPlanPushStrategy;
66 private ArgumentCaptor<Group> groupCaptor;
68 private ArgumentCaptor<Group> groupUpdateCaptor;
70 private ArgumentCaptor<Flow> flowCaptor;
72 private ArgumentCaptor<Flow> flowUpdateCaptor;
74 private ArgumentCaptor<Meter> meterCaptor;
76 private ArgumentCaptor<Meter> meterUpdateCaptor;
78 private ArgumentCaptor<TableFeatures> tableFeaturesCaptor;
80 private ArgumentCaptor<SynchronizationDiffInput> syncDiffInputCaptor;
83 public void setUp() throws Exception {
84 reactor = new SyncReactorImpl(syncPlanPushStrategy);
88 public void testSyncup() throws Exception {
89 final FlowCapableNode configFcn = new FlowCapableNodeBuilder()
90 .setGroup(Collections.singletonList(DSInputFactory.createGroup(1L)))
91 .setTable(Collections.singletonList(new TableBuilder()
92 .setFlow(Collections.singletonList(DSInputFactory.createFlow("f1", 1)))
94 .setMeter(Collections.singletonList(DSInputFactory.createMeter(1L)))
97 final FlowCapableNode operationalFcn = new FlowCapableNodeBuilder()
98 .setGroup(Collections.singletonList(DSInputFactory.createGroup(2L)))
99 .setTable(Collections.singletonList(new TableBuilder()
100 .setFlow(Collections.singletonList(DSInputFactory.createFlow("f2", 2)))
102 .setMeter(Collections.singletonList(DSInputFactory.createMeter(2L)))
105 final SyncupEntry syncupEntry = new SyncupEntry(configFcn, LogicalDatastoreType.CONFIGURATION,
106 operationalFcn, LogicalDatastoreType.OPERATIONAL);
108 Mockito.when(syncPlanPushStrategy.executeSyncStrategy(
109 ArgumentMatchers.<ListenableFuture<RpcResult<Void>>>any(),
110 ArgumentMatchers.<SynchronizationDiffInput>any(),
111 ArgumentMatchers.<SyncCrudCounters>any()))
112 .thenReturn(RpcResultBuilder.<Void>success().buildFuture());
114 final ListenableFuture<Boolean> syncupResult = reactor.syncup(NODE_IDENT, syncupEntry);
115 Assert.assertTrue(syncupResult.isDone());
116 final Boolean voidRpcResult = syncupResult.get(2, TimeUnit.SECONDS);
117 Assert.assertTrue(voidRpcResult);
119 Mockito.verify(syncPlanPushStrategy).executeSyncStrategy(
120 ArgumentMatchers.<ListenableFuture<RpcResult<Void>>>any(),
121 syncDiffInputCaptor.capture(),
122 ArgumentMatchers.<SyncCrudCounters>any());
124 final SynchronizationDiffInput diffInput = syncDiffInputCaptor.getValue();
125 Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getFlowsToAddOrUpdate().values()));
126 Assert.assertEquals(0, ReconcileUtil.countTotalUpdated(diffInput.getFlowsToAddOrUpdate().values()));
127 Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getFlowsToRemove().values()));
129 Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getGroupsToAddOrUpdate()));
130 Assert.assertEquals(0, ReconcileUtil.countTotalUpdated(diffInput.getGroupsToAddOrUpdate()));
131 Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getGroupsToRemove()));
133 Assert.assertEquals(1, diffInput.getMetersToAddOrUpdate().getItemsToPush().size());
134 Assert.assertEquals(0, diffInput.getMetersToAddOrUpdate().getItemsToUpdate().size());
135 Assert.assertEquals(1, diffInput.getMetersToRemove().getItemsToPush().size());