bb30caec52d521f40157bd88a850a83a6a7c3273
[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.junit.MockitoJUnitRunner;
20 import org.opendaylight.mdsal.binding.api.WriteTransaction;
21 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
22 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
23 import org.opendaylight.openflowplugin.api.openflow.FlowGroupCacheManager;
24 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
25 import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
26 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
27 import org.opendaylight.openflowplugin.applications.frm.recovery.OpenflowServiceRecoveryHandler;
28 import org.opendaylight.openflowplugin.applications.reconciliation.ReconciliationManager;
29 import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
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.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesKey;
40 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
41 import test.mock.util.FRMTest;
42 import test.mock.util.RpcProviderRegistryMock;
43 import test.mock.util.SalTableServiceMock;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public class TableFeaturesListenerTest extends FRMTest {
47     private ForwardingRulesManagerImpl forwardingRulesManager;
48     private static final NodeId NODE_ID = new NodeId("testnode:1");
49     private static final NodeKey NODE_KEY = new NodeKey(NODE_ID);
50     RpcProviderRegistryMock rpcProviderRegistryMock = new RpcProviderRegistryMock();
51     @Mock
52     ClusterSingletonServiceProvider clusterSingletonService;
53     @Mock
54     DeviceMastershipManager deviceMastershipManager;
55     @Mock
56     private ReconciliationManager reconciliationManager;
57     @Mock
58     private OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler;
59     @Mock
60     private ServiceRecoveryRegistry serviceRecoveryRegistry;
61     @Mock
62     private MastershipChangeServiceManager mastershipChangeServiceManager;
63     @Mock
64     private FlowGroupCacheManager flowGroupCacheManager;
65
66     @Before
67     public void setUp() {
68         forwardingRulesManager = new ForwardingRulesManagerImpl(getDataBroker(), rpcProviderRegistryMock,
69                 rpcProviderRegistryMock, getConfig(), mastershipChangeServiceManager, clusterSingletonService,
70                 getConfigurationService(), reconciliationManager, openflowServiceRecoveryHandler,
71                 serviceRecoveryRegistry, flowGroupCacheManager);
72         forwardingRulesManager.start();
73         // TODO consider tests rewrite (added because of complicated access)
74         forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
75         Mockito.when(deviceMastershipManager.isDeviceMastered(NODE_ID)).thenReturn(true);
76     }
77
78     @Test
79     public void updateFlowTest() {
80         TableKey tableKey = new TableKey((short) 2);
81         TableFeaturesKey tableFeaturesKey = new TableFeaturesKey(tableKey.getId());
82
83         addTable(tableKey, NODE_KEY);
84
85         TableFeatures tableFeaturesData = new TableFeaturesBuilder().withKey(tableFeaturesKey).build();
86         InstanceIdentifier<TableFeatures> tableFeaturesII = InstanceIdentifier.create(Nodes.class)
87                 .child(Node.class, NODE_KEY).augmentation(FlowCapableNode.class)
88                 .child(TableFeatures.class, tableFeaturesKey);
89         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
90         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableFeaturesII, tableFeaturesData);
91         assertCommit(writeTx.commit());
92
93         tableFeaturesData = new TableFeaturesBuilder().withKey(tableFeaturesKey).setName("dummy name").build();
94         writeTx = getDataBroker().newWriteOnlyTransaction();
95         writeTx.put(LogicalDatastoreType.CONFIGURATION, tableFeaturesII, tableFeaturesData);
96         assertCommit(writeTx.commit());
97
98         SalTableServiceMock salTableServiceMock = (SalTableServiceMock) forwardingRulesManager.getSalTableService();
99         List<UpdateTableInput> updateTableInputs = salTableServiceMock.getUpdateTableInput();
100         assertEquals(1, updateTableInputs.size());
101         assertEquals("DOM-0", updateTableInputs.get(0).getTransactionUri().getValue());
102     }
103
104     @After
105     public void tearDown() throws Exception {
106         forwardingRulesManager.close();
107     }
108 }