Bug 4957 RoleContext updated with initialization
[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.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;
55
56 import com.google.common.base.Optional;
57 import com.google.common.util.concurrent.Futures;
58
59 /**
60  * Unit tests for StatListenCommitFlow.
61  * 
62  * @author Monika Verma
63  */
64 public class StatListenCommitFlowTest {
65
66     @Mock
67     private NotificationProviderService mockNotificationProviderService;
68
69     @Mock
70     private StatisticsManager mockStatisticsManager;
71
72     @Mock
73     private DataBroker mockDataBroker;
74
75     private StatListenCommitFlow statCommitFlow;
76     private TableKey tableKey = new TableKey((short) 12);
77
78     @Before
79     public void init() {
80         MockitoAnnotations.initMocks(this);
81         statCommitFlow = new StatListenCommitFlow(mockStatisticsManager, mockDataBroker,
82                 mockNotificationProviderService);
83     }
84
85     @Test
86     public void testStatsFlowCommitAllWithAlienSystemFlowId() throws InvocationTargetException {
87         Class[] argClasses = { List.class, InstanceIdentifier.class, ReadWriteTransaction.class };
88
89         List<FlowAndStatisticsMapList> flowStats = new ArrayList<FlowAndStatisticsMapList>();
90         flowStats.add(createFlowAndStatisticsMapList());
91
92         FlowCapableNode flowCapableNode = createFlowCapableNode("#UF$TABLE*F1");
93
94         FlowsStatisticsUpdate flowsStatisticsUpdate = createFlowsStatisticsUpdate();
95
96         InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class,
97                 new NodeKey(flowsStatisticsUpdate.getId()));
98
99         final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
100
101         InstanceIdentifier<Table> path = InstanceIdentifier.create(Nodes.class)
102                 .child(Node.class, new NodeKey(flowsStatisticsUpdate.getId())).augmentation(FlowCapableNode.class)
103                 .child(Table.class, tableKey);
104
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,
109                 fNodeIdent);
110
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,
115                 path);
116
117         Object[] argObjects = { flowStats, nodeIdent, mockReadWriteTx };
118
119         Method method;
120         try {
121             method = StatListenCommitFlow.class.getDeclaredMethod("statsFlowCommitAll", argClasses);
122             method.setAccessible(true);
123             method.invoke(statCommitFlow, argObjects);
124         } catch (Exception e) {
125             e.printStackTrace();
126             fail(e.getCause().toString());
127         }
128     }
129
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();
134     }
135
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();
140     }
141
142     private FlowCapableNode createFlowCapableNode(String flowId) {
143
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()
153                 .setFlow(flowList)
154                 .setId((short) 12)
155                 .setKey(tableKey)
156                 .addAugmentation(FlowHashIdMapping.class,
157                         new FlowHashIdMappingBuilder().setFlowHashIdMap(flowHashIdMapList).build()).build());
158
159         return new FlowCapableNodeBuilder().setDescription("test").setTable(tableList).build();
160     }
161
162     @Test
163     public void testStatsFlowCommitAll() throws InvocationTargetException {
164         Class[] argClasses = { List.class, InstanceIdentifier.class, ReadWriteTransaction.class };
165
166         List<FlowAndStatisticsMapList> flowStats = new ArrayList<FlowAndStatisticsMapList>();
167         flowStats.add(createFlowAndStatisticsMapList());
168
169         FlowCapableNode flowCapableNode = createFlowCapableNode("F1");
170
171         FlowsStatisticsUpdate flowsStatisticsUpdate = createFlowsStatisticsUpdate();
172
173         InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class,
174                 new NodeKey(flowsStatisticsUpdate.getId()));
175
176         final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
177
178         InstanceIdentifier<Table> path = InstanceIdentifier.create(Nodes.class)
179                 .child(Node.class, new NodeKey(flowsStatisticsUpdate.getId())).augmentation(FlowCapableNode.class)
180                 .child(Table.class, tableKey);
181
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,
186                 fNodeIdent);
187
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,
192                 path);
193
194         Object[] argObjects = { flowStats, nodeIdent, mockReadWriteTx };
195
196         Method method;
197         try {
198             method = StatListenCommitFlow.class.getDeclaredMethod("statsFlowCommitAll", argClasses);
199             method.setAccessible(true);
200             method.invoke(statCommitFlow, argObjects);
201         } catch (Exception e) {
202             e.printStackTrace();
203             fail(e.getCause().toString());
204         }
205     }
206
207     @Test
208     public void testStatsFlowCommitAllWithDefaultAlienSystemFlowId() throws InvocationTargetException {
209         Class[] argClasses = { List.class, InstanceIdentifier.class, ReadWriteTransaction.class };
210
211         List<FlowAndStatisticsMapList> flowStats = new ArrayList<FlowAndStatisticsMapList>();
212         flowStats.add(createFlowAndStatisticsMapList());
213
214         FlowCapableNode flowCapableNode = createFlowCapableNode("#UF$TABLE*12-1");
215
216         FlowsStatisticsUpdate flowsStatisticsUpdate = createFlowsStatisticsUpdate();
217
218         InstanceIdentifier<Node> nodeIdent = InstanceIdentifier.create(Nodes.class).child(Node.class,
219                 new NodeKey(flowsStatisticsUpdate.getId()));
220
221         final InstanceIdentifier<FlowCapableNode> fNodeIdent = nodeIdent.augmentation(FlowCapableNode.class);
222
223         InstanceIdentifier<Table> path = InstanceIdentifier.create(Nodes.class)
224                 .child(Node.class, new NodeKey(flowsStatisticsUpdate.getId())).augmentation(FlowCapableNode.class)
225                 .child(Table.class, tableKey);
226
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,
231                 fNodeIdent);
232
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,
237                 path);
238
239         Object[] argObjects = { flowStats, nodeIdent, mockReadWriteTx };
240
241         Method method;
242         try {
243             method = StatListenCommitFlow.class.getDeclaredMethod("statsFlowCommitAll", argClasses);
244             method.setAccessible(true);
245             method.invoke(statCommitFlow, argObjects);
246         } catch (Exception e) {
247             e.printStackTrace();
248             fail(e.getCause().toString());
249         }
250     }
251 }