Merge "Technical debt MeterUtil, GroupUtil, FlowUtil"
[openflowplugin.git] / openflowplugin-impl / src / test / java / org / opendaylight / openflowplugin / impl / registry / flow / DeviceFlowRegistryImplTest.java
1 /*
2  * Copyright (c) 2015 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.impl.registry.flow;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.inOrder;
15 import static org.mockito.Mockito.times;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
18
19 import com.google.common.base.Optional;
20 import com.google.common.util.concurrent.Futures;
21 import java.math.BigInteger;
22 import java.util.Collections;
23 import java.util.Map;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.InOrder;
31 import org.mockito.Mock;
32 import org.mockito.runners.MockitoJUnitRunner;
33 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
34 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
35 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
36 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
37 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
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.types.rev131026.FlowCookie;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
53
54 /**
55  * Test for {@link DeviceFlowRegistryImpl}.
56  */
57 @RunWith(MockitoJUnitRunner.class)
58 public class DeviceFlowRegistryImplTest {
59     private static final String NODE_ID = "openflow:1";
60     private static final Pattern INDEX_PATTERN = Pattern.compile("^#UF\\$TABLE\\*1-([0-9]+)$");
61     private static final Short DUMMY_TABLE_ID = 1;
62
63     private DeviceFlowRegistryImpl deviceFlowRegistry;
64     private FlowRegistryKey key;
65     private FlowDescriptor descriptor;
66     private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
67     @Mock
68     private DataBroker dataBroker;
69     @Mock
70     private ReadOnlyTransaction readOnlyTransaction;
71
72     @Before
73     public void setUp() throws Exception {
74         nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(NODE_ID)));
75         when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
76         deviceFlowRegistry = new DeviceFlowRegistryImpl(dataBroker, nodeInstanceIdentifier);
77         final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(1).build();
78         key = FlowRegistryKeyFactory.create(flowStats);
79         descriptor = FlowDescriptorFactory.create(key.getTableId(), new FlowId("ut:1"));
80
81         Assert.assertEquals(0, deviceFlowRegistry.getAllFlowDescriptors().size());
82         deviceFlowRegistry.store(key, descriptor);
83         Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
84     }
85
86     @Test
87     public void testFill() throws Exception {
88         final InstanceIdentifier<FlowCapableNode> path = nodeInstanceIdentifier.augmentation(FlowCapableNode.class);
89
90         final Flow flow = new FlowBuilder()
91                 .setTableId((short)1)
92                 .setPriority(10)
93                 .setCookie(new FlowCookie(BigInteger.TEN))
94                 .setId(new FlowId("HELLO"))
95                 .build();
96
97         final Table table = new TableBuilder()
98                 .setFlow(Collections.singletonList(flow))
99                 .build();
100
101         final FlowCapableNode flowCapableNode = new FlowCapableNodeBuilder()
102                 .setTable(Collections.singletonList(table))
103                 .build();
104
105         final Map<FlowRegistryKey, FlowDescriptor> allFlowDescriptors = testFill(path, flowCapableNode);
106         final FlowRegistryKey key = FlowRegistryKeyFactory.create(flow);
107
108         InOrder order = inOrder(dataBroker, readOnlyTransaction);
109         order.verify(dataBroker).newReadOnlyTransaction();
110         order.verify(readOnlyTransaction).read(LogicalDatastoreType.CONFIGURATION, path);
111         order.verify(dataBroker).newReadOnlyTransaction();
112         order.verify(readOnlyTransaction).read(LogicalDatastoreType.OPERATIONAL, path);
113         assertTrue(allFlowDescriptors.containsKey(key));
114
115         deviceFlowRegistry.markToBeremoved(key);
116         deviceFlowRegistry.removeMarked();
117     }
118
119     @Test
120     public void testFailedFill() throws Exception {
121         final InstanceIdentifier<FlowCapableNode> path = nodeInstanceIdentifier.augmentation(FlowCapableNode.class);
122
123         testFill(path, null);
124
125         testFill(path, new FlowCapableNodeBuilder()
126                 .setTable(null)
127                 .build());
128
129         testFill(path, new FlowCapableNodeBuilder()
130                 .setTable(Collections.singletonList(null))
131                 .build());
132
133         testFill(path, new FlowCapableNodeBuilder()
134                 .setTable(Collections.singletonList(new TableBuilder()
135                         .setFlow(null)
136                         .build()))
137                 .build());
138
139         testFill(path, new FlowCapableNodeBuilder()
140                 .setTable(Collections.singletonList(new TableBuilder()
141                         .setFlow(Collections.singletonList(null))
142                         .build()))
143                 .build());
144
145         testFill(path, new FlowCapableNodeBuilder()
146                 .setTable(Collections.singletonList(new TableBuilder()
147                         .setFlow(Collections.singletonList(new FlowBuilder()
148                                 .setId(null)
149                                 .build()))
150                         .build()))
151                 .build());
152
153         verify(dataBroker, times(12)).newReadOnlyTransaction();
154         verify(readOnlyTransaction, times(6)).read(LogicalDatastoreType.CONFIGURATION, path);
155         verify(readOnlyTransaction, times(6)).read(LogicalDatastoreType.OPERATIONAL, path);
156
157         Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
158     }
159
160     private Map<FlowRegistryKey, FlowDescriptor> testFill(final InstanceIdentifier<FlowCapableNode> path,
161                                                           final FlowCapableNode flowCapableNode) throws Exception {
162         when(readOnlyTransaction.read(any(), any())).thenReturn(Futures.immediateCheckedFuture(Optional.fromNullable(flowCapableNode)));
163         deviceFlowRegistry.fill().get();
164         return deviceFlowRegistry.getAllFlowDescriptors();
165     }
166
167     @Test
168     public void testRetrieveIdForFlow() throws Exception {
169         Assert.assertEquals(descriptor, deviceFlowRegistry.retrieveIdForFlow(key));
170     }
171
172     @Test
173     public void testStore() throws Exception {
174         //store the same key with different value
175         final FlowDescriptor descriptor2 = FlowDescriptorFactory.create(key.getTableId(), new FlowId("ut:2"));
176         deviceFlowRegistry.store(key, descriptor2);
177         Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
178         Assert.assertEquals("ut:2", deviceFlowRegistry.retrieveIdForFlow(key).getFlowId().getValue());
179
180         // store new key with old value
181         final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build();
182         final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(flowStats);
183         deviceFlowRegistry.store(key2, descriptor);
184         Assert.assertEquals(2, deviceFlowRegistry.getAllFlowDescriptors().size());
185         Assert.assertEquals("ut:1", deviceFlowRegistry.retrieveIdForFlow(key2).getFlowId().getValue());
186     }
187
188     @Test
189     public void testStoreIfNecessary() throws Exception {
190         FlowId newFlowId;
191
192         //store existing key
193         newFlowId = deviceFlowRegistry.storeIfNecessary(key);
194
195         Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
196         Assert.assertEquals(descriptor, deviceFlowRegistry.retrieveIdForFlow(key));
197         Assert.assertEquals(descriptor.getFlowId(), newFlowId);
198
199         //store new key
200         final String alienPrefix = "#UF$TABLE*2-";
201         final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build());
202         newFlowId = deviceFlowRegistry.storeIfNecessary(key2);
203
204         Assert.assertTrue(newFlowId.getValue().startsWith(alienPrefix));
205         Assert.assertTrue(deviceFlowRegistry.retrieveIdForFlow(key2).getFlowId().getValue().startsWith(alienPrefix));
206         Assert.assertEquals(2, deviceFlowRegistry.getAllFlowDescriptors().size());
207     }
208
209     @Test
210     public void testRemoveMarked() throws Exception {
211         deviceFlowRegistry.markToBeremoved(key);
212         deviceFlowRegistry.removeMarked();
213         Assert.assertEquals(0, deviceFlowRegistry.getAllFlowDescriptors().size());
214     }
215
216     @Test
217     public void testRemoveMarkedNegative() throws Exception {
218         final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build();
219         FlowRegistryKey key2 = FlowRegistryKeyFactory.create(flowStats);
220         deviceFlowRegistry.markToBeremoved(key2);
221         deviceFlowRegistry.removeMarked();
222         Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
223     }
224
225     @Test
226     public void testClose() throws Exception {
227         deviceFlowRegistry.markToBeremoved(key);
228         deviceFlowRegistry.close();
229         Assert.assertEquals(0, deviceFlowRegistry.getAllFlowDescriptors().size());
230
231         deviceFlowRegistry.store(key, descriptor);
232         Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
233         deviceFlowRegistry.removeMarked();
234         Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
235     }
236
237     @Test
238     public void createAlienFlowIdTest() throws Exception {
239         final String alienFlowId1 = DeviceFlowRegistryImpl.createAlienFlowId(DUMMY_TABLE_ID).getValue();
240         final Integer index1 = parseIndex(alienFlowId1);
241         final String alienFlowId2 = DeviceFlowRegistryImpl.createAlienFlowId(DUMMY_TABLE_ID).getValue();
242         final Integer index2 = parseIndex(alienFlowId2);
243
244         assertNotNull("index1 parsing failed: " + alienFlowId1, index1);
245         assertNotNull("index2 parsing failed: " + alienFlowId2, index2);
246         assertTrue(index1 < index2);
247     }
248
249     private static Integer parseIndex(String alienFlowIdValue) {
250         final Matcher mach = INDEX_PATTERN.matcher(alienFlowIdValue);
251
252         if (mach.find()) {
253             return Integer.valueOf(mach.group(1));
254         }
255
256         return null;
257     }
258 }