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.StatisticsManager;
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.FlowHashIdMapping;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowHashIdMappingBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.nodes.node.table.FlowHashIdMap;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.nodes.node.table.FlowHashIdMapBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.nodes.node.table.FlowHashIdMapKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdate;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.FlowsStatisticsUpdateBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapListBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
56 import com.google.common.base.Optional;
57 import com.google.common.util.concurrent.Futures;
60 * Unit tests for StatListenCommitFlow.
62 * @author Monika Verma
64 public class StatListenCommitFlowTest {
67 private NotificationProviderService mockNotificationProviderService;
70 private StatisticsManager mockStatisticsManager;
73 private DataBroker mockDataBroker;
75 private StatListenCommitFlow statCommitFlow;
76 private TableKey tableKey = new TableKey((short) 12);
80 MockitoAnnotations.initMocks(this);
81 statCommitFlow = new StatListenCommitFlow(mockStatisticsManager, mockDataBroker,
82 mockNotificationProviderService);
86 public void testStatsFlowCommitAllWithAlienSystemFlowId() throws InvocationTargetException {
87 Class[] argClasses = { List.class, InstanceIdentifier.class, ReadWriteTransaction.class };
89 List<FlowAndStatisticsMapList> flowStats = new ArrayList<FlowAndStatisticsMapList>();
90 flowStats.add(createFlowAndStatisticsMapList());
92 FlowCapableNode flowCapableNode = createFlowCapableNode("#UF$TABLE*F1");
94 FlowsStatisticsUpdate flowsStatisticsUpdate = createFlowsStatisticsUpdate();
96 InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class,
97 new NodeKey(flowsStatisticsUpdate.getId()));
99 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
101 InstanceIdentifier<Table> path = InstanceIdentifier.create(Nodes.class)
102 .child(Node.class, new NodeKey(flowsStatisticsUpdate.getId())).augmentation(FlowCapableNode.class)
103 .child(Table.class, tableKey);
105 ReadWriteTransaction mockReadWriteTx = mock(ReadWriteTransaction.class);
106 doReturn(mockReadWriteTx).when(mockDataBroker).newReadWriteTransaction();
107 Optional<FlowCapableNode> expected = Optional.of(flowCapableNode);
108 doReturn(Futures.immediateCheckedFuture(expected)).when(mockReadWriteTx).read(LogicalDatastoreType.OPERATIONAL,
111 ReadOnlyTransaction mockReadTx = mock(ReadOnlyTransaction.class);
112 doReturn(mockReadTx).when(mockDataBroker).newReadOnlyTransaction();
113 Optional<Table> expected1 = Optional.of(flowCapableNode.getTable().get(0));
114 doReturn(Futures.immediateCheckedFuture(expected1)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
117 Object[] argObjects = { flowStats, nodeIdent, mockReadWriteTx };
121 method = StatListenCommitFlow.class.getDeclaredMethod("statsFlowCommitAll", argClasses);
122 method.setAccessible(true);
123 method.invoke(statCommitFlow, argObjects);
124 } catch (Exception e) {
126 fail(e.getCause().toString());
130 private FlowsStatisticsUpdate createFlowsStatisticsUpdate() {
131 return new FlowsStatisticsUpdateBuilder().setId(new NodeId("S1"))
132 .setTransactionId(new TransactionId(new java.math.BigInteger("18446744073709551615")))
133 .setMoreReplies(false).build();
136 private FlowAndStatisticsMapList createFlowAndStatisticsMapList() {
137 return new FlowAndStatisticsMapListBuilder().setFlowName("testFlow").setFlowId(new FlowId("F1"))
138 .setTableId(tableKey.getId()).setMatch(new MatchBuilder().build()).setPriority(2)
139 .setCookie(new FlowCookie(new java.math.BigInteger("18446744073709551615"))).build();
142 private FlowCapableNode createFlowCapableNode(String flowId) {
144 List<Table> tableList = new ArrayList<Table>();
145 List<Flow> flowList = new ArrayList<Flow>();
146 flowList.add(new FlowBuilder().setFlowName("testFlow")
147 .setId(new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId("F1")).build());
148 List<FlowHashIdMap> flowHashIdMapList = new ArrayList<FlowHashIdMap>();
149 flowHashIdMapList.add(new FlowHashIdMapBuilder()
150 .setFlowId(new org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId(flowId))
151 .setHash("TestFlow").setKey(new FlowHashIdMapKey("FM1")).build());
152 tableList.add(new TableBuilder()
156 .addAugmentation(FlowHashIdMapping.class,
157 new FlowHashIdMappingBuilder().setFlowHashIdMap(flowHashIdMapList).build()).build());
159 return new FlowCapableNodeBuilder().setDescription("test").setTable(tableList).build();
163 public void testStatsFlowCommitAll() throws InvocationTargetException {
164 Class[] argClasses = { List.class, InstanceIdentifier.class, ReadWriteTransaction.class };
166 List<FlowAndStatisticsMapList> flowStats = new ArrayList<FlowAndStatisticsMapList>();
167 flowStats.add(createFlowAndStatisticsMapList());
169 FlowCapableNode flowCapableNode = createFlowCapableNode("F1");
171 FlowsStatisticsUpdate flowsStatisticsUpdate = createFlowsStatisticsUpdate();
173 InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class,
174 new NodeKey(flowsStatisticsUpdate.getId()));
176 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
178 InstanceIdentifier<Table> path = InstanceIdentifier.create(Nodes.class)
179 .child(Node.class, new NodeKey(flowsStatisticsUpdate.getId())).augmentation(FlowCapableNode.class)
180 .child(Table.class, tableKey);
182 ReadWriteTransaction mockReadWriteTx = mock(ReadWriteTransaction.class);
183 doReturn(mockReadWriteTx).when(mockDataBroker).newReadWriteTransaction();
184 Optional<FlowCapableNode> expected = Optional.of(flowCapableNode);
185 doReturn(Futures.immediateCheckedFuture(expected)).when(mockReadWriteTx).read(LogicalDatastoreType.OPERATIONAL,
188 ReadOnlyTransaction mockReadTx = mock(ReadOnlyTransaction.class);
189 doReturn(mockReadTx).when(mockDataBroker).newReadOnlyTransaction();
190 Optional<Table> expected1 = Optional.of(flowCapableNode.getTable().get(0));
191 doReturn(Futures.immediateCheckedFuture(expected1)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
194 Object[] argObjects = { flowStats, nodeIdent, mockReadWriteTx };
198 method = StatListenCommitFlow.class.getDeclaredMethod("statsFlowCommitAll", argClasses);
199 method.setAccessible(true);
200 method.invoke(statCommitFlow, argObjects);
201 } catch (Exception e) {
203 fail(e.getCause().toString());
208 public void testStatsFlowCommitAllWithDefaultAlienSystemFlowId() throws InvocationTargetException {
209 Class[] argClasses = { List.class, InstanceIdentifier.class, ReadWriteTransaction.class };
211 List<FlowAndStatisticsMapList> flowStats = new ArrayList<FlowAndStatisticsMapList>();
212 flowStats.add(createFlowAndStatisticsMapList());
214 FlowCapableNode flowCapableNode = createFlowCapableNode("#UF$TABLE*12-1");
216 FlowsStatisticsUpdate flowsStatisticsUpdate = createFlowsStatisticsUpdate();
218 InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class,
219 new NodeKey(flowsStatisticsUpdate.getId()));
221 final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
223 InstanceIdentifier<Table> path = InstanceIdentifier.create(Nodes.class)
224 .child(Node.class, new NodeKey(flowsStatisticsUpdate.getId())).augmentation(FlowCapableNode.class)
225 .child(Table.class, tableKey);
227 ReadWriteTransaction mockReadWriteTx = mock(ReadWriteTransaction.class);
228 doReturn(mockReadWriteTx).when(mockDataBroker).newReadWriteTransaction();
229 Optional<FlowCapableNode> expected = Optional.of(flowCapableNode);
230 doReturn(Futures.immediateCheckedFuture(expected)).when(mockReadWriteTx).read(LogicalDatastoreType.OPERATIONAL,
233 ReadOnlyTransaction mockReadTx = mock(ReadOnlyTransaction.class);
234 doReturn(mockReadTx).when(mockDataBroker).newReadOnlyTransaction();
235 Optional<Table> expected1 = Optional.of(flowCapableNode.getTable().get(0));
236 doReturn(Futures.immediateCheckedFuture(expected1)).when(mockReadTx).read(LogicalDatastoreType.CONFIGURATION,
239 Object[] argObjects = { flowStats, nodeIdent, mockReadWriteTx };
243 method = StatListenCommitFlow.class.getDeclaredMethod("statsFlowCommitAll", argClasses);
244 method.setAccessible(true);
245 method.invoke(statCommitFlow, argObjects);
246 } catch (Exception e) {
248 fail(e.getCause().toString());