Bug 6110: Fixed bugs in statistics manager due to race condition.
[openflowplugin.git] / applications / forwardingrules-manager / src / test / java / test / mock / TableFeaturesListenerTest.java
1 /**
2  * Copyright (c) 2014 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 package test.mock;
9
10 import static org.junit.Assert.assertEquals;
11
12 import java.util.List;
13 import org.junit.After;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.junit.runner.RunWith;
17 import org.mockito.Mock;
18 import org.mockito.Mockito;
19 import org.mockito.runners.MockitoJUnitRunner;
20 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
23 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
24 import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
25 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesKey;
36 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
37 import test.mock.util.FRMTest;
38 import test.mock.util.RpcProviderRegistryMock;
39 import test.mock.util.SalTableServiceMock;
40
41 @RunWith(MockitoJUnitRunner.class)
42 public class TableFeaturesListenerTest extends FRMTest {
43     private ForwardingRulesManagerImpl forwardingRulesManager;
44     private final static NodeId NODE_ID = new NodeId("testnode:1");
45     private final static NodeKey s1Key = new NodeKey(NODE_ID);
46     RpcProviderRegistry rpcProviderRegistryMock = new RpcProviderRegistryMock();
47     @Mock
48     ClusterSingletonServiceProvider clusterSingletonService;
49     @Mock
50     DeviceMastershipManager deviceMastershipManager;
51
52     @Before
53     public void setUp() {
54         forwardingRulesManager = new ForwardingRulesManagerImpl(
55                 getDataBroker(),
56                 rpcProviderRegistryMock,
57                 getConfig(),
58                 clusterSingletonService);
59         forwardingRulesManager.start();
60         // TODO consider tests rewrite (added because of complicated access)
61         forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
62         Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
63     }
64
65     @Test
66     public void updateFlowTest() {
67         TableKey tableKey = new TableKey((short) 2);
68         TableFeaturesKey tableFeaturesKey = new TableFeaturesKey(tableKey.getId());
69
70         addTable(tableKey, s1Key);
71
72         TableFeatures tableFeaturesData = new TableFeaturesBuilder().setKey(tableFeaturesKey).build();
73         InstanceIdentifier<TableFeatures> tableFeaturesII = InstanceIdentifier.create(Nodes.class).child(Node.class, s1Key)
74                 .augmentation(FlowCapableNode.class).child(TableFeatures.class, tableFeaturesKey);
75         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
76         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableFeaturesII, tableFeaturesData);
77         assertCommit(writeTx.submit());
78
79         tableFeaturesData = new TableFeaturesBuilder().setKey(tableFeaturesKey).setName("dummy name").build();
80         writeTx = getDataBroker().newWriteOnlyTransaction();
81         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableFeaturesII, tableFeaturesData);
82         assertCommit(writeTx.submit());
83
84         SalTableServiceMock salTableServiceMock = (SalTableServiceMock) forwardingRulesManager.getSalTableService();
85         List<UpdateTableInput> updateTableInputs = salTableServiceMock.getUpdateTableInput();
86         assertEquals(1, updateTableInputs.size());
87         assertEquals("DOM-0", updateTableInputs.get(0).getTransactionUri().getValue());
88     }
89
90     @After
91     public void tearDown() throws Exception {
92         forwardingRulesManager.close();
93     }
94
95 }