2 * Copyright (c) 2015 Cisco Systems, Inc. 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.impl.registry.flow;
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.times;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.when;
18 import com.google.common.base.Optional;
19 import com.google.common.util.concurrent.Futures;
20 import java.util.regex.Matcher;
21 import java.util.regex.Pattern;
22 import org.junit.Assert;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.runners.MockitoJUnitRunner;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
32 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
44 * Test for {@link DeviceFlowRegistryImpl}.
46 @RunWith(MockitoJUnitRunner.class)
47 public class DeviceFlowRegistryImplTest {
48 private static final String NODE_ID = "openflow:1";
49 private static final Pattern INDEX_PATTERN = Pattern.compile("^#UF\\$TABLE\\*1-([0-9]+)$");
50 private static final Short DUMMY_TABLE_ID = 1;
52 private DeviceFlowRegistryImpl deviceFlowRegistry;
53 private FlowRegistryKey key;
54 private FlowDescriptor descriptor;
55 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
57 private DataBroker dataBroker;
59 private ReadOnlyTransaction readOnlyTransaction;
62 public void setUp() throws Exception {
63 nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(NODE_ID)));
64 when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
65 when(readOnlyTransaction.read(any(), any())).thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
66 deviceFlowRegistry = new DeviceFlowRegistryImpl(dataBroker, nodeInstanceIdentifier);
67 final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(1).build();
68 key = FlowRegistryKeyFactory.create(flowStats);
69 descriptor = FlowDescriptorFactory.create(key.getTableId(), new FlowId("ut:1"));
71 Assert.assertEquals(0, deviceFlowRegistry.getAllFlowDescriptors().size());
72 deviceFlowRegistry.store(key, descriptor);
73 Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
77 public void testFill() throws Exception {
78 final InstanceIdentifier<FlowCapableNode> path = nodeInstanceIdentifier.augmentation(FlowCapableNode.class);
80 deviceFlowRegistry.fill().get();
82 verify(dataBroker, times(2)).newReadOnlyTransaction();
83 verify(readOnlyTransaction).read(LogicalDatastoreType.CONFIGURATION, path);
84 verify(readOnlyTransaction).read(LogicalDatastoreType.OPERATIONAL, path);
88 public void testRetrieveIdForFlow() throws Exception {
89 Assert.assertEquals(descriptor, deviceFlowRegistry.retrieveIdForFlow(key));
93 public void testStore() throws Exception {
94 //store the same key with different value
95 final FlowDescriptor descriptor2 = FlowDescriptorFactory.create(key.getTableId(), new FlowId("ut:2"));
96 deviceFlowRegistry.store(key, descriptor2);
97 Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
98 Assert.assertEquals("ut:2", deviceFlowRegistry.retrieveIdForFlow(key).getFlowId().getValue());
100 // store new key with old value
101 final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build();
102 final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(flowStats);
103 deviceFlowRegistry.store(key2, descriptor);
104 Assert.assertEquals(2, deviceFlowRegistry.getAllFlowDescriptors().size());
105 Assert.assertEquals("ut:1", deviceFlowRegistry.retrieveIdForFlow(key2).getFlowId().getValue());
109 public void testStoreIfNecessary() throws Exception {
113 newFlowId = deviceFlowRegistry.storeIfNecessary(key);
115 Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
116 Assert.assertEquals(descriptor, deviceFlowRegistry.retrieveIdForFlow(key));
117 Assert.assertEquals(descriptor.getFlowId(), newFlowId);
120 final String alienPrefix = "#UF$TABLE*2-";
121 final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build());
122 newFlowId = deviceFlowRegistry.storeIfNecessary(key2);
124 Assert.assertTrue(newFlowId.getValue().startsWith(alienPrefix));
125 Assert.assertTrue(deviceFlowRegistry.retrieveIdForFlow(key2).getFlowId().getValue().startsWith(alienPrefix));
126 Assert.assertEquals(2, deviceFlowRegistry.getAllFlowDescriptors().size());
130 public void testRemoveMarked() throws Exception {
131 deviceFlowRegistry.markToBeremoved(key);
132 deviceFlowRegistry.removeMarked();
133 Assert.assertEquals(0, deviceFlowRegistry.getAllFlowDescriptors().size());
137 public void testRemoveMarkedNegative() throws Exception {
138 final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build();
139 FlowRegistryKey key2 = FlowRegistryKeyFactory.create(flowStats);
140 deviceFlowRegistry.markToBeremoved(key2);
141 deviceFlowRegistry.removeMarked();
142 Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
146 public void testClose() throws Exception {
147 deviceFlowRegistry.markToBeremoved(key);
148 deviceFlowRegistry.close();
149 Assert.assertEquals(0, deviceFlowRegistry.getAllFlowDescriptors().size());
151 deviceFlowRegistry.store(key, descriptor);
152 Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
153 deviceFlowRegistry.removeMarked();
154 Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
158 public void createAlienFlowIdTest() throws Exception {
159 final String alienFlowId1 = DeviceFlowRegistryImpl.createAlienFlowId(DUMMY_TABLE_ID).getValue();
160 final Integer index1 = parseIndex(alienFlowId1);
161 final String alienFlowId2 = DeviceFlowRegistryImpl.createAlienFlowId(DUMMY_TABLE_ID).getValue();
162 final Integer index2 = parseIndex(alienFlowId2);
164 assertNotNull("index1 parsing failed: " + alienFlowId1, index1);
165 assertNotNull("index2 parsing failed: " + alienFlowId2, index2);
166 assertTrue(index1 < index2);
169 private static Integer parseIndex(String alienFlowIdValue) {
170 final Matcher mach = INDEX_PATTERN.matcher(alienFlowIdValue);
173 return Integer.valueOf(mach.group(1));