2 * Copyright (c) 2015 Tata Consultancy services 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.statistics.manager.impl;
11 import static org.junit.Assert.fail;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
15 import java.lang.reflect.InvocationTargetException;
16 import java.lang.reflect.Method;
17 import java.util.ArrayList;
18 import java.util.List;
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;
57 import com.google.common.base.Optional;
58 import com.google.common.util.concurrent.Futures;
61 * Unit tests for StatListenCommitFlow.
63 * @author Monika Verma
65 public class StatListenCommitFlowTest {
68 private NotificationProviderService mockNotificationProviderService;
71 private StatisticsManager mockStatisticsManager;
74 private DataBroker mockDataBroker;
77 private StatNodeRegistration statsNodeRegistration;
79 private StatListenCommitFlow statCommitFlow;
80 private TableKey tableKey = new TableKey((short) 12);
84 MockitoAnnotations.initMocks(this);
85 statCommitFlow = new StatListenCommitFlow(mockStatisticsManager, mockDataBroker,
86 mockNotificationProviderService, statsNodeRegistration);
90 public void testStatsFlowCommitAllWithAlienSystemFlowId() throws InvocationTargetException {
91 Class[] argClasses = { List.class, InstanceIdentifier.class, ReadWriteTransaction.class };
93 List<FlowAndStatisticsMapList> flowStats = new ArrayList<FlowAndStatisticsMapList>();
94 flowStats.add(createFlowAndStatisticsMapList());
96 FlowCapableNode flowCapableNode = createFlowCapableNode("#UF$TABLE*F1");
98 FlowsStatisticsUpdate flowsStatisticsUpdate = createFlowsStatisticsUpdate();
100 InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class,
101 new NodeKey(flowsStatisticsUpdate.getId()));
103 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
105 InstanceIdentifier<Table> path = InstanceIdentifier.create(Nodes.class)
106 .child(Node.class, new NodeKey(flowsStatisticsUpdate.getId())).augmentation(FlowCapableNode.class)
107 .child(Table.class, tableKey);
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,
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,
121 Object[] argObjects = { flowStats, nodeIdent, mockReadWriteTx };
125 method = StatListenCommitFlow.class.getDeclaredMethod("statsFlowCommitAll", argClasses);
126 method.setAccessible(true);
127 method.invoke(statCommitFlow, argObjects);
128 } catch (Exception e) {
130 fail(e.getCause().toString());
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();
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();
146 private FlowCapableNode createFlowCapableNode(String flowId) {
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()
160 .addAugmentation(FlowHashIdMapping.class,
161 new FlowHashIdMappingBuilder().setFlowHashIdMap(flowHashIdMapList).build()).build());
163 return new FlowCapableNodeBuilder().setDescription("test").setTable(tableList).build();
167 public void testStatsFlowCommitAll() throws InvocationTargetException {
168 Class[] argClasses = { List.class, InstanceIdentifier.class, ReadWriteTransaction.class };
170 List<FlowAndStatisticsMapList> flowStats = new ArrayList<FlowAndStatisticsMapList>();
171 flowStats.add(createFlowAndStatisticsMapList());
173 FlowCapableNode flowCapableNode = createFlowCapableNode("F1");
175 FlowsStatisticsUpdate flowsStatisticsUpdate = createFlowsStatisticsUpdate();
177 InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class,
178 new NodeKey(flowsStatisticsUpdate.getId()));
180 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
182 InstanceIdentifier<Table> path = InstanceIdentifier.create(Nodes.class)
183 .child(Node.class, new NodeKey(flowsStatisticsUpdate.getId())).augmentation(FlowCapableNode.class)
184 .child(Table.class, tableKey);
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,
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,
198 Object[] argObjects = { flowStats, nodeIdent, mockReadWriteTx };
202 method = StatListenCommitFlow.class.getDeclaredMethod("statsFlowCommitAll", argClasses);
203 method.setAccessible(true);
204 method.invoke(statCommitFlow, argObjects);
205 } catch (Exception e) {
207 fail(e.getCause().toString());
212 public void testStatsFlowCommitAllWithDefaultAlienSystemFlowId() throws InvocationTargetException {
213 Class[] argClasses = { List.class, InstanceIdentifier.class, ReadWriteTransaction.class };
215 List<FlowAndStatisticsMapList> flowStats = new ArrayList<FlowAndStatisticsMapList>();
216 flowStats.add(createFlowAndStatisticsMapList());
218 FlowCapableNode flowCapableNode = createFlowCapableNode("#UF$TABLE*12-1");
220 FlowsStatisticsUpdate flowsStatisticsUpdate = createFlowsStatisticsUpdate();
222 InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class,
223 new NodeKey(flowsStatisticsUpdate.getId()));
225 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
227 InstanceIdentifier<Table> path = InstanceIdentifier.create(Nodes.class)
228 .child(Node.class, new NodeKey(flowsStatisticsUpdate.getId())).augmentation(FlowCapableNode.class)
229 .child(Table.class, tableKey);
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,
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,
243 Object[] argObjects = { flowStats, nodeIdent, mockReadWriteTx };
247 method = StatListenCommitFlow.class.getDeclaredMethod("statsFlowCommitAll", argClasses);
248 method.setAccessible(true);
249 method.invoke(statCommitFlow, argObjects);
250 } catch (Exception e) {
252 fail(e.getCause().toString());