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