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.mockito.Matchers.any;
12 import static org.mockito.Mockito.times;
13 import static org.mockito.Mockito.verify;
14 import static org.mockito.Mockito.when;
16 import com.google.common.base.Optional;
17 import com.google.common.util.concurrent.Futures;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.Mock;
23 import org.mockito.runners.MockitoJUnitRunner;
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.common.api.data.LogicalDatastoreType;
27 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
28 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.statistics.rev130819.flow.and.statistics.map.list.FlowAndStatisticsMapList;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
40 * Test for {@link DeviceFlowRegistryImpl}.
42 @RunWith(MockitoJUnitRunner.class)
43 public class DeviceFlowRegistryImplTest {
44 private static final String NODE_ID = "openflow:1";
45 private DeviceFlowRegistryImpl deviceFlowRegistry;
46 private FlowRegistryKey key;
47 private FlowDescriptor descriptor;
48 private KeyedInstanceIdentifier<Node, NodeKey> nodeInstanceIdentifier;
50 private DataBroker dataBroker;
52 private ReadOnlyTransaction readOnlyTransaction;
55 public void setUp() throws Exception {
56 nodeInstanceIdentifier = InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(NODE_ID)));
57 when(dataBroker.newReadOnlyTransaction()).thenReturn(readOnlyTransaction);
58 when(readOnlyTransaction.read(any(), any())).thenReturn(Futures.immediateCheckedFuture(Optional.absent()));
59 deviceFlowRegistry = new DeviceFlowRegistryImpl(dataBroker, nodeInstanceIdentifier);
60 final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(1).build();
61 key = FlowRegistryKeyFactory.create(flowStats);
62 descriptor = FlowDescriptorFactory.create(key.getTableId(), new FlowId("ut:1"));
64 Assert.assertEquals(0, deviceFlowRegistry.getAllFlowDescriptors().size());
65 deviceFlowRegistry.store(key, descriptor);
66 Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
70 public void testFill() throws Exception {
71 final InstanceIdentifier<FlowCapableNode> path = nodeInstanceIdentifier.augmentation(FlowCapableNode.class);
73 deviceFlowRegistry.fill().get();
75 verify(dataBroker, times(2)).newReadOnlyTransaction();
76 verify(readOnlyTransaction).read(LogicalDatastoreType.CONFIGURATION, path);
77 verify(readOnlyTransaction).read(LogicalDatastoreType.OPERATIONAL, path);
81 public void testRetrieveIdForFlow() throws Exception {
82 Assert.assertEquals(descriptor, deviceFlowRegistry.retrieveIdForFlow(key));
86 public void testStore() throws Exception {
87 //store the same key with different value
88 final FlowDescriptor descriptor2 = FlowDescriptorFactory.create(key.getTableId(), new FlowId("ut:2"));
89 deviceFlowRegistry.store(key, descriptor2);
90 Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
91 Assert.assertEquals("ut:2", deviceFlowRegistry.retrieveIdForFlow(key).getFlowId().getValue());
93 // store new key with old value
94 final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build();
95 final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(flowStats);
96 deviceFlowRegistry.store(key2, descriptor);
97 Assert.assertEquals(2, deviceFlowRegistry.getAllFlowDescriptors().size());
98 Assert.assertEquals("ut:1", deviceFlowRegistry.retrieveIdForFlow(key2).getFlowId().getValue());
102 public void testStoreIfNecessary() throws Exception {
106 newFlowId = deviceFlowRegistry.storeIfNecessary(key);
108 Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
109 Assert.assertEquals(descriptor, deviceFlowRegistry.retrieveIdForFlow(key));
110 Assert.assertEquals(descriptor.getFlowId(), newFlowId);
113 final String alienPrefix = "#UF$TABLE*2-";
114 final FlowRegistryKey key2 = FlowRegistryKeyFactory.create(TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build());
115 newFlowId = deviceFlowRegistry.storeIfNecessary(key2);
117 Assert.assertTrue(newFlowId.getValue().startsWith(alienPrefix));
118 Assert.assertTrue(deviceFlowRegistry.retrieveIdForFlow(key2).getFlowId().getValue().startsWith(alienPrefix));
119 Assert.assertEquals(2, deviceFlowRegistry.getAllFlowDescriptors().size());
123 public void testRemoveMarked() throws Exception {
124 deviceFlowRegistry.markToBeremoved(key);
125 deviceFlowRegistry.removeMarked();
126 Assert.assertEquals(0, deviceFlowRegistry.getAllFlowDescriptors().size());
130 public void testRemoveMarkedNegative() throws Exception {
131 final FlowAndStatisticsMapList flowStats = TestFlowHelper.createFlowAndStatisticsMapListBuilder(2).build();
132 FlowRegistryKey key2 = FlowRegistryKeyFactory.create(flowStats);
133 deviceFlowRegistry.markToBeremoved(key2);
134 deviceFlowRegistry.removeMarked();
135 Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
139 public void testClose() throws Exception {
140 deviceFlowRegistry.markToBeremoved(key);
141 deviceFlowRegistry.close();
142 Assert.assertEquals(0, deviceFlowRegistry.getAllFlowDescriptors().size());
144 deviceFlowRegistry.store(key, descriptor);
145 Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());
146 deviceFlowRegistry.removeMarked();
147 Assert.assertEquals(1, deviceFlowRegistry.getAllFlowDescriptors().size());