Merge remote-tracking branch 'liblldp/master'
[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
9 package org.opendaylight.openflowplugin.applications.frsync.impl;
10
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.Captor;
20 import org.mockito.Matchers;
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;
47
48 /**
49  * Test for {@link SyncReactorImpl}.
50  */
51 @RunWith(MockitoJUnitRunner.class)
52 public class SyncReactorImplTest {
53
54     private static final Logger LOG = LoggerFactory.getLogger(SyncReactorImplTest.class);
55
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;
60
61     @Mock
62     private DataBroker db;
63     @Mock
64     private SyncPlanPushStrategy syncPlanPushStrategy;
65     @Captor
66     private ArgumentCaptor<Group> groupCaptor;
67     @Captor
68     private ArgumentCaptor<Group> groupUpdateCaptor;
69     @Captor
70     private ArgumentCaptor<Flow> flowCaptor;
71     @Captor
72     private ArgumentCaptor<Flow> flowUpdateCaptor;
73     @Captor
74     private ArgumentCaptor<Meter> meterCaptor;
75     @Captor
76     private ArgumentCaptor<Meter> meterUpdateCaptor;
77     @Captor
78     private ArgumentCaptor<TableFeatures> tableFeaturesCaptor;
79     @Captor
80     private ArgumentCaptor<SynchronizationDiffInput> syncDiffInputCaptor;
81
82     @Before
83     public void setUp() throws Exception {
84         reactor = new SyncReactorImpl(syncPlanPushStrategy);
85     }
86
87     @Test
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)))
93                         .build()))
94                 .setMeter(Collections.singletonList(DSInputFactory.createMeter(1L)))
95                 .build();
96
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)))
101                         .build()))
102                 .setMeter(Collections.singletonList(DSInputFactory.createMeter(2L)))
103                 .build();
104
105         final SyncupEntry syncupEntry = new SyncupEntry(configFcn, LogicalDatastoreType.CONFIGURATION,
106                                                         operationalFcn, LogicalDatastoreType.OPERATIONAL);
107
108         Mockito.when(syncPlanPushStrategy.executeSyncStrategy(
109                 Matchers.<ListenableFuture<RpcResult<Void>>>any(),
110                 Matchers.<SynchronizationDiffInput>any(),
111                 Matchers.<SyncCrudCounters>any()))
112                 .thenReturn(RpcResultBuilder.<Void>success().buildFuture());
113
114         final ListenableFuture<Boolean> syncupResult = reactor.syncup(NODE_IDENT, syncupEntry);
115         try {
116             Assert.assertTrue(syncupResult.isDone());
117             final Boolean voidRpcResult = syncupResult.get(2, TimeUnit.SECONDS);
118             Assert.assertTrue(voidRpcResult);
119
120             Mockito.verify(syncPlanPushStrategy).executeSyncStrategy(
121                     Matchers.<ListenableFuture<RpcResult<Void>>>any(),
122                     syncDiffInputCaptor.capture(),
123                     Matchers.<SyncCrudCounters>any()
124             );
125
126             final SynchronizationDiffInput diffInput = syncDiffInputCaptor.getValue();
127             Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getFlowsToAddOrUpdate().values()));
128             Assert.assertEquals(0, ReconcileUtil.countTotalUpdated(diffInput.getFlowsToAddOrUpdate().values()));
129             Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getFlowsToRemove().values()));
130
131             Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getGroupsToAddOrUpdate()));
132             Assert.assertEquals(0, ReconcileUtil.countTotalUpdated(diffInput.getGroupsToAddOrUpdate()));
133             Assert.assertEquals(1, ReconcileUtil.countTotalPushed(diffInput.getGroupsToRemove()));
134
135             Assert.assertEquals(1, diffInput.getMetersToAddOrUpdate().getItemsToPush().size());
136             Assert.assertEquals(0, diffInput.getMetersToAddOrUpdate().getItemsToUpdate().size());
137             Assert.assertEquals(1, diffInput.getMetersToRemove().getItemsToPush().size());
138         } catch (Exception e) {
139             LOG.warn("syncup failed", e);
140             Assert.fail("syncup failed: " + e.getMessage());
141         }
142     }
143 }