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