Bump upstreams
[openflowplugin.git] / applications / forwardingrules-manager / src / test / java / test / mock / util / AbstractFRMTest.java
1 /*
2  * Copyright (c) 2014, 2017 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.util;
9
10 import static org.mockito.ArgumentMatchers.any;
11 import static org.mockito.ArgumentMatchers.eq;
12 import static org.mockito.Mockito.lenient;
13 import static org.mockito.Mockito.mock;
14 import static org.mockito.Mockito.when;
15
16 import org.mockito.Mock;
17 import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
18 import org.opendaylight.mdsal.binding.api.RpcProviderService;
19 import org.opendaylight.mdsal.binding.api.WriteTransaction;
20 import org.opendaylight.mdsal.binding.dom.adapter.test.AbstractDataBrokerTest;
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.configuration.ConfigurationService;
25 import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
26 import org.opendaylight.openflowplugin.applications.frm.impl.DeviceMastershipManager;
27 import org.opendaylight.openflowplugin.applications.frm.impl.ForwardingRulesManagerImpl;
28 import org.opendaylight.openflowplugin.applications.frm.impl.ListenerRegistrationHelper;
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.FlowCapableNodeBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodesBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.SalBundleService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.arbitrator.reconcile.service.rev180227.ArbitratorReconcileService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfig;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfigBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
48 import org.opendaylight.yangtools.yang.common.Uint16;
49
50 public abstract class AbstractFRMTest extends AbstractDataBrokerTest {
51     private ForwardingRulesManagerImpl forwardingRulesManager;
52
53     @Mock
54     private RpcConsumerRegistry rpcConsumerRegistry;
55     @Mock
56     private RpcProviderService rpcProviderService;
57     @Mock
58     private ClusterSingletonServiceProvider clusterSingletonService;
59     @Mock
60     private DeviceMastershipManager deviceMastershipManager;
61     @Mock
62     private ReconciliationManager reconciliationManager;
63     @Mock
64     private OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler;
65     @Mock
66     private ServiceRecoveryRegistry serviceRecoveryRegistry;
67     @Mock
68     private MastershipChangeServiceManager mastershipChangeServiceManager;
69     @Mock
70     private FlowGroupCacheManager flowGroupCacheManager;
71
72     protected void setUpForwardingRulesManager() {
73         when(rpcConsumerRegistry.getRpcService(SalFlowService.class))
74                 .thenReturn(new SalFlowServiceMock());
75         when(rpcConsumerRegistry.getRpcService(SalGroupService.class))
76                 .thenReturn(new SalGroupServiceMock());
77         when(rpcConsumerRegistry.getRpcService(SalMeterService.class))
78                 .thenReturn(new SalMeterServiceMock());
79         when(rpcConsumerRegistry.getRpcService(SalTableService.class))
80                 .thenReturn(new SalTableServiceMock());
81         when(rpcConsumerRegistry.getRpcService(SalBundleService.class))
82                 .thenReturn(new SalBundleServiceMock());
83         when(rpcConsumerRegistry.getRpcService(ArbitratorReconcileService.class))
84                 .thenReturn(new ArbitratorReconcileServiceMock());
85
86         forwardingRulesManager = new ForwardingRulesManagerImpl(getDataBroker(), rpcConsumerRegistry,
87                 rpcProviderService, getConfig(), mastershipChangeServiceManager, clusterSingletonService,
88                 getConfigurationService(), reconciliationManager, openflowServiceRecoveryHandler,
89                 serviceRecoveryRegistry, flowGroupCacheManager, getRegistrationHelper());
90         forwardingRulesManager.start();
91     }
92
93     protected void setDeviceMastership(final NodeId nodeId) {
94         // TODO consider tests rewrite (added because of complicated access)
95         forwardingRulesManager.setDeviceMastershipManager(deviceMastershipManager);
96         when(deviceMastershipManager.isDeviceMastered(nodeId)).thenReturn(true);
97     }
98
99     protected ForwardingRulesManagerImpl getForwardingRulesManager() {
100         return forwardingRulesManager;
101     }
102
103     protected void addFlowCapableNode(final NodeKey nodeKey) {
104         Nodes nodes = new NodesBuilder()
105             .setNode(BindingMap.of(new NodeBuilder()
106                 .withKey(nodeKey)
107                 .addAugmentation(new FlowCapableNodeBuilder().setDescription("test node").build())
108                 .build()))
109             .build();
110
111         WriteTransaction writeTx = getDataBroker().newWriteOnlyTransaction();
112         writeTx.put(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.create(Nodes.class), nodes);
113         assertCommit(writeTx.commit());
114
115         writeTx = getDataBroker().newWriteOnlyTransaction();
116         writeTx.put(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier.create(Nodes.class), nodes);
117         assertCommit(writeTx.commit());
118     }
119
120     private static ForwardingRulesManagerConfig getConfig() {
121         return new ForwardingRulesManagerConfigBuilder()
122                 .setDisableReconciliation(false)
123                 .setStaleMarkingEnabled(false)
124                 .setReconciliationRetryCount(Uint16.ZERO)
125                 .setBundleBasedReconciliationEnabled(false)
126                 .build();
127     }
128
129     private static ConfigurationService getConfigurationService() {
130         final ConfigurationService configurationService = mock(ConfigurationService.class);
131         final ForwardingRulesManagerConfig config = getConfig();
132
133         when(configurationService.registerListener(any())).thenReturn(() -> {
134         });
135
136         lenient().when(configurationService.getProperty(eq("disable-reconciliation"), any()))
137                 .thenReturn(config.getDisableReconciliation());
138
139         lenient().when(configurationService.getProperty(eq("stale-marking-enabled"), any()))
140                 .thenReturn(config.getStaleMarkingEnabled());
141
142         lenient().when(configurationService.getProperty(eq("reconciliation-retry-count"),
143                 any())).thenReturn(config.getReconciliationRetryCount());
144
145         lenient().when(configurationService.getProperty(eq("bundle-based-reconciliation-enabled"),
146                 any())).thenReturn(config.getBundleBasedReconciliationEnabled());
147
148         return configurationService;
149     }
150
151     private ListenerRegistrationHelper getRegistrationHelper() {
152         return new ListenerRegistrationHelper(getDataBroker());
153     }
154 }