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