Switch to MD-SAL APIs
[openflowplugin.git] / applications / forwardingrules-sync / src / test / java / org / opendaylight / openflowplugin / applications / frsync / impl / SyncReactorImplTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.applications.frsync.impl;
9
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.SyncCrudCounters;
29 import org.opendaylight.openflowplugin.applications.frsync.util.SyncupEntry;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
41 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
42 import org.opendaylight.yangtools.yang.common.RpcResult;
43 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46
47 /**
48  * Test for {@link SyncReactorImpl}.
49  */
50 @RunWith(MockitoJUnitRunner.class)
51 public class SyncReactorImplTest {
52
53     private static final Logger LOG = LoggerFactory.getLogger(SyncReactorImplTest.class);
54
55     private static final NodeId NODE_ID = new NodeId("unit-nodeId");
56     private static final InstanceIdentifier<FlowCapableNode> NODE_IDENT = InstanceIdentifier.create(Nodes.class)
57             .child(Node.class, new NodeKey(NODE_ID)).augmentation(FlowCapableNode.class);
58     private SyncReactorImpl reactor;
59
60     @Mock
61     private DataBroker db;
62     @Mock
63     private SyncPlanPushStrategy syncPlanPushStrategy;
64     @Captor
65     private ArgumentCaptor<Group> groupCaptor;
66     @Captor
67     private ArgumentCaptor<Group> groupUpdateCaptor;
68     @Captor
69     private ArgumentCaptor<Flow> flowCaptor;
70     @Captor
71     private ArgumentCaptor<Flow> flowUpdateCaptor;
72     @Captor
73     private ArgumentCaptor<Meter> meterCaptor;
74     @Captor
75     private ArgumentCaptor<Meter> meterUpdateCaptor;
76     @Captor
77     private ArgumentCaptor<TableFeatures> tableFeaturesCaptor;
78     @Captor
79     private ArgumentCaptor<SynchronizationDiffInput> syncDiffInputCaptor;
80
81     @Before
82     public void setUp() throws Exception {
83         reactor = new SyncReactorImpl(syncPlanPushStrategy);
84     }
85
86     @Test
87     public void testSyncup() throws Exception {
88         final FlowCapableNode configFcn = new FlowCapableNodeBuilder()
89                 .setGroup(Collections.singletonList(DSInputFactory.createGroup(1L)))
90                 .setTable(Collections.singletonList(new TableBuilder()
91                         .setFlow(Collections.singletonList(DSInputFactory.createFlow("f1", 1)))
92                         .build()))
93                 .setMeter(Collections.singletonList(DSInputFactory.createMeter(1L)))
94                 .build();
95
96         final FlowCapableNode operationalFcn = new FlowCapableNodeBuilder()
97                 .setGroup(Collections.singletonList(DSInputFactory.createGroup(2L)))
98                 .setTable(Collections.singletonList(new TableBuilder()
99                         .setFlow(Collections.singletonList(DSInputFactory.createFlow("f2", 2)))
100                         .build()))
101                 .setMeter(Collections.singletonList(DSInputFactory.createMeter(2L)))
102                 .build();
103
104         final SyncupEntry syncupEntry = new SyncupEntry(configFcn, LogicalDatastoreType.CONFIGURATION,
105                                                         operationalFcn, LogicalDatastoreType.OPERATIONAL);
106
107         Mockito.when(syncPlanPushStrategy.executeSyncStrategy(
108                 ArgumentMatchers.<ListenableFuture<RpcResult<Void>>>any(),
109                 ArgumentMatchers.<SynchronizationDiffInput>any(),
110                 ArgumentMatchers.<SyncCrudCounters>any()))
111                 .thenReturn(RpcResultBuilder.<Void>success().buildFuture());
112
113         final ListenableFuture<Boolean> syncupResult = reactor.syncup(NODE_IDENT, syncupEntry);
114         Assert.assertTrue(syncupResult.isDone());
115         final Boolean voidRpcResult = syncupResult.get(2, TimeUnit.SECONDS);
116         Assert.assertTrue(voidRpcResult);
117
118         Mockito.verify(syncPlanPushStrategy).executeSyncStrategy(
119                 ArgumentMatchers.<ListenableFuture<RpcResult<Void>>>any(),
120                 syncDiffInputCaptor.capture(),
121                 ArgumentMatchers.<SyncCrudCounters>any());
122
123         final SynchronizationDiffInput diffInput = syncDiffInputCaptor.getValue();
124         Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getFlowsToAddOrUpdate().values()));
125         Assert.assertEquals(0, ReconcileUtil.countTotalUpdated(diffInput.getFlowsToAddOrUpdate().values()));
126         Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getFlowsToRemove().values()));
127
128         Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getGroupsToAddOrUpdate()));
129         Assert.assertEquals(0, ReconcileUtil.countTotalUpdated(diffInput.getGroupsToAddOrUpdate()));
130         Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getGroupsToRemove()));
131
132         Assert.assertEquals(1, diffInput.getMetersToAddOrUpdate().getItemsToPush().size());
133         Assert.assertEquals(0, diffInput.getMetersToAddOrUpdate().getItemsToUpdate().size());
134         Assert.assertEquals(1, diffInput.getMetersToRemove().getItemsToPush().size());
135     }
136 }