Merge "Bug 4957 No empty transaction for every connection fix"
[openflowplugin.git] / applications / statistics-manager / src / test / java / org / opendaylight / openflowplugin / applications / statistics / manager / impl / StatListenCommitFlowTest.java
1 /*
2  * Copyright (c) 2015 Tata Consultancy services 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.statistics.manager.impl;
10
11 import static org.junit.Assert.fail;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
14
15 import java.lang.reflect.InvocationTargetException;
16 import java.lang.reflect.Method;
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.MockitoAnnotations;
24 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
25 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
26 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
27 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
28 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
29 import org.opendaylight.openflowplugin.applications.statistics.manager.StatNodeRegistration;
30 import org.opendaylight.openflowplugin.applications.statistics.manager.StatisticsManager;
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.FlowHashIdMapping;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowHashIdMappingBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.nodes.node.table.FlowHashIdMap;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.nodes.node.table.FlowHashIdMapBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.nodes.node.table.FlowHashIdMapKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowId;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdateBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
55 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56
57 import com.google.common.base.Optional;
58 import com.google.common.util.concurrent.Futures;
59
60 /**
61  * Unit tests for StatListenCommitFlow.
62  * 
63  * @author Monika Verma
64  */
65 public class StatListenCommitFlowTest {
66
67     @Mock
68     private NotificationProviderService mockNotificationProviderService;
69
70     @Mock
71     private StatisticsManager mockStatisticsManager;
72
73     @Mock
74     private DataBroker mockDataBroker;
75
76     @Mock
77     private StatNodeRegistration statsNodeRegistration;
78
79     private StatListenCommitFlow statCommitFlow;
80     private TableKey tableKey = new TableKey((short) 12);
81
82     @Before
83     public void init() {
84         MockitoAnnotations.initMocks(this);
85         statCommitFlow = new StatListenCommitFlow(mockStatisticsManager, mockDataBroker,
86                 mockNotificationProviderService, statsNodeRegistration);
87     }
88
89     @Test
90     public void testStatsFlowCommitAllWithAlienSystemFlowId() throws InvocationTargetException {
91         Class[] argClasses = { List.class, InstanceIdentifier.class, ReadWriteTransaction.class };
92
93         List<FlowAndStatisticsMapList> flowStats = new ArrayList<FlowAndStatisticsMapList>();
94         flowStats.add(createFlowAndStatisticsMapList());
95
96         FlowCapableNode flowCapableNode = createFlowCapableNode("#UF$TABLE*F1");
97
98         FlowsStatisticsUpdate flowsStatisticsUpdate = createFlowsStatisticsUpdate();
99
100         InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class,
101                 new NodeKey(flowsStatisticsUpdate.getId()));
102
103         final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
104
105         InstanceIdentifier<Table> path = InstanceIdentifier.create(Nodes.class)
106                 .child(Node.class, new NodeKey(flowsStatisticsUpdate.getId())).augmentation(FlowCapableNode.class)
107                 .child(Table.class, tableKey);
108
109         ReadWriteTransaction mockReadWriteTx = mock(ReadWriteTransaction.class);
110         doReturn(mockReadWriteTx).when(mockDataBroker).newReadWriteTransaction();
111         Optional<FlowCapableNode> expected = Optional.of(flowCapableNode);
112         doReturn(Futures.immediateCheckedFuture(expected)).when(mockReadWriteTx).read(LogicalDatastoreType.OPERATIONAL,
113                 fNodeIdent);
114
115         ReadOnlyTransaction mockReadTx = mock(ReadOnlyTransaction.class);
116         doReturn(mockReadTx).when(mockDataBroker).newReadOnlyTransaction();
117         Optional<Table> expected1 = Optional.of(flowCapableNode.getTable().get(0));
118         doReturn(Futures.immediateCheckedFuture(expected1)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
119                 path);
120
121         Object[] argObjects = { flowStats, nodeIdent, mockReadWriteTx };
122
123         Method method;
124         try {
125             method = StatListenCommitFlow.class.getDeclaredMethod("statsFlowCommitAll", argClasses);
126             method.setAccessible(true);
127             method.invoke(statCommitFlow, argObjects);
128         } catch (Exception e) {
129             e.printStackTrace();
130             fail(e.getCause().toString());
131         }
132     }
133
134     private FlowsStatisticsUpdate createFlowsStatisticsUpdate() {
135         return new FlowsStatisticsUpdateBuilder().setId(new NodeId("S1"))
136                 .setTransactionId(new TransactionId(new java.math.BigInteger("18446744073709551615")))
137                 .setMoreReplies(false).build();
138     }
139
140     private FlowAndStatisticsMapList createFlowAndStatisticsMapList() {
141         return new FlowAndStatisticsMapListBuilder().setFlowName("testFlow").setFlowId(new FlowId("F1"))
142                 .setTableId(tableKey.getId()).setMatch(new MatchBuilder().build()).setPriority(2)
143                 .setCookie(new FlowCookie(new java.math.BigInteger("18446744073709551615"))).build();
144     }
145
146     private FlowCapableNode createFlowCapableNode(String flowId) {
147
148         List<Table> tableList = new ArrayList<Table>();
149         List<Flow> flowList = new ArrayList<Flow>();
150         flowList.add(new FlowBuilder().setFlowName("testFlow")
151                 .setId(new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId("F1")).build());
152         List<FlowHashIdMap> flowHashIdMapList = new ArrayList<FlowHashIdMap>();
153         flowHashIdMapList.add(new FlowHashIdMapBuilder()
154                 .setFlowId(new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId(flowId))
155                 .setHash("TestFlow").setKey(new FlowHashIdMapKey("FM1")).build());
156         tableList.add(new TableBuilder()
157                 .setFlow(flowList)
158                 .setId((short) 12)
159                 .setKey(tableKey)
160                 .addAugmentation(FlowHashIdMapping.class,
161                         new FlowHashIdMappingBuilder().setFlowHashIdMap(flowHashIdMapList).build()).build());
162
163         return new FlowCapableNodeBuilder().setDescription("test").setTable(tableList).build();
164     }
165
166     @Test
167     public void testStatsFlowCommitAll() throws InvocationTargetException {
168         Class[] argClasses = { List.class, InstanceIdentifier.class, ReadWriteTransaction.class };
169
170         List<FlowAndStatisticsMapList> flowStats = new ArrayList<FlowAndStatisticsMapList>();
171         flowStats.add(createFlowAndStatisticsMapList());
172
173         FlowCapableNode flowCapableNode = createFlowCapableNode("F1");
174
175         FlowsStatisticsUpdate flowsStatisticsUpdate = createFlowsStatisticsUpdate();
176
177         InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class,
178                 new NodeKey(flowsStatisticsUpdate.getId()));
179
180         final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
181
182         InstanceIdentifier<Table> path = InstanceIdentifier.create(Nodes.class)
183                 .child(Node.class, new NodeKey(flowsStatisticsUpdate.getId())).augmentation(FlowCapableNode.class)
184                 .child(Table.class, tableKey);
185
186         ReadWriteTransaction mockReadWriteTx = mock(ReadWriteTransaction.class);
187         doReturn(mockReadWriteTx).when(mockDataBroker).newReadWriteTransaction();
188         Optional<FlowCapableNode> expected = Optional.of(flowCapableNode);
189         doReturn(Futures.immediateCheckedFuture(expected)).when(mockReadWriteTx).read(LogicalDatastoreType.OPERATIONAL,
190                 fNodeIdent);
191
192         ReadOnlyTransaction mockReadTx = mock(ReadOnlyTransaction.class);
193         doReturn(mockReadTx).when(mockDataBroker).newReadOnlyTransaction();
194         Optional<Table> expected1 = Optional.of(flowCapableNode.getTable().get(0));
195         doReturn(Futures.immediateCheckedFuture(expected1)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
196                 path);
197
198         Object[] argObjects = { flowStats, nodeIdent, mockReadWriteTx };
199
200         Method method;
201         try {
202             method = StatListenCommitFlow.class.getDeclaredMethod("statsFlowCommitAll", argClasses);
203             method.setAccessible(true);
204             method.invoke(statCommitFlow, argObjects);
205         } catch (Exception e) {
206             e.printStackTrace();
207             fail(e.getCause().toString());
208         }
209     }
210
211     @Test
212     public void testStatsFlowCommitAllWithDefaultAlienSystemFlowId() throws InvocationTargetException {
213         Class[] argClasses = { List.class, InstanceIdentifier.class, ReadWriteTransaction.class };
214
215         List<FlowAndStatisticsMapList> flowStats = new ArrayList<FlowAndStatisticsMapList>();
216         flowStats.add(createFlowAndStatisticsMapList());
217
218         FlowCapableNode flowCapableNode = createFlowCapableNode("#UF$TABLE*12-1");
219
220         FlowsStatisticsUpdate flowsStatisticsUpdate = createFlowsStatisticsUpdate();
221
222         InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class,
223                 new NodeKey(flowsStatisticsUpdate.getId()));
224
225         final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
226
227         InstanceIdentifier<Table> path = InstanceIdentifier.create(Nodes.class)
228                 .child(Node.class, new NodeKey(flowsStatisticsUpdate.getId())).augmentation(FlowCapableNode.class)
229                 .child(Table.class, tableKey);
230
231         ReadWriteTransaction mockReadWriteTx = mock(ReadWriteTransaction.class);
232         doReturn(mockReadWriteTx).when(mockDataBroker).newReadWriteTransaction();
233         Optional<FlowCapableNode> expected = Optional.of(flowCapableNode);
234         doReturn(Futures.immediateCheckedFuture(expected)).when(mockReadWriteTx).read(LogicalDatastoreType.OPERATIONAL,
235                 fNodeIdent);
236
237         ReadOnlyTransaction mockReadTx = mock(ReadOnlyTransaction.class);
238         doReturn(mockReadTx).when(mockDataBroker).newReadOnlyTransaction();
239         Optional<Table> expected1 = Optional.of(flowCapableNode.getTable().get(0));
240         doReturn(Futures.immediateCheckedFuture(expected1)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
241                 path);
242
243         Object[] argObjects = { flowStats, nodeIdent, mockReadWriteTx };
244
245         Method method;
246         try {
247             method = StatListenCommitFlow.class.getDeclaredMethod("statsFlowCommitAll", argClasses);
248             method.setAccessible(true);
249             method.invoke(statCommitFlow, argObjects);
250         } catch (Exception e) {
251             e.printStackTrace();
252             fail(e.getCause().toString());
253         }
254     }
255 }