Merge "OPNFLWPLUG-898 : remove deprecated checkedfuture"
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / ForwardingRulesManagerImpl.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
9 package org.opendaylight.openflowplugin.applications.frm.impl;
10
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Preconditions;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import java.util.Objects;
15 import java.util.Optional;
16 import java.util.concurrent.ExecutionException;
17 import java.util.concurrent.atomic.AtomicLong;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
21 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
22 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
23 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
24 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
25 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
26 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
27 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
28 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
29 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesProperty;
30 import org.opendaylight.openflowplugin.applications.reconciliation.NotificationRegistration;
31 import org.opendaylight.openflowplugin.applications.reconciliation.ReconciliationManager;
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.meters.Meter;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.SalBundleService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfig;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconciliationService;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * forwardingrules-manager org.opendaylight.openflowplugin.applications.frm.impl
52  *
53  * <p>
54  * Manager and middle point for whole module. It contains ActiveNodeHolder and
55  * provide all RPC services.
56  *
57  */
58 public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
59     private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
60
61     static final int STARTUP_LOOP_TICK = 500;
62     static final int STARTUP_LOOP_MAX_RETRIES = 8;
63     private static final int FRM_RECONCILIATION_PRIORITY = Integer.getInteger("frm.reconciliation.priority", 0);
64     private static final String SERVICE_NAME = "FRM";
65
66     private final AtomicLong txNum = new AtomicLong();
67     private final DataBroker dataService;
68     private final SalFlowService salFlowService;
69     private final SalGroupService salGroupService;
70     private final SalMeterService salMeterService;
71     private final SalTableService salTableService;
72     private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
73     private final NotificationProviderService notificationService;
74     private final SalBundleService salBundleService;
75     private final AutoCloseable configurationServiceRegistration;
76     private final RpcProviderRegistry rpcRegistry;
77     private ForwardingRulesCommiter<Flow> flowListener;
78     private ForwardingRulesCommiter<Group> groupListener;
79     private ForwardingRulesCommiter<Meter> meterListener;
80     private ForwardingRulesCommiter<TableFeatures> tableListener;
81     private FlowNodeReconciliation nodeListener;
82     private NotificationRegistration reconciliationNotificationRegistration;
83     private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
84     private DeviceMastershipManager deviceMastershipManager;
85     private final ReconciliationManager reconciliationManager;
86
87     private boolean disableReconciliation;
88     private boolean staleMarkingEnabled;
89     private int reconciliationRetryCount;
90     private boolean isBundleBasedReconciliationEnabled;
91
92     public ForwardingRulesManagerImpl(final DataBroker dataBroker, final RpcProviderRegistry rpcRegistry,
93             final ForwardingRulesManagerConfig config, final ClusterSingletonServiceProvider clusterSingletonService,
94             final NotificationProviderService notificationService, final ConfigurationService configurationService,
95             final ReconciliationManager reconciliationManager) {
96         disableReconciliation = config.isDisableReconciliation();
97         staleMarkingEnabled = config.isStaleMarkingEnabled();
98         reconciliationRetryCount = config.getReconciliationRetryCount();
99         isBundleBasedReconciliationEnabled = config.isBundleBasedReconciliationEnabled();
100         this.configurationServiceRegistration = configurationService.registerListener(this);
101         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
102         this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
103                 "ClusterSingletonService provider can not be null");
104         this.notificationService = Preconditions.checkNotNull(notificationService,
105                 "Notification publisher configurationService is" + " not available");
106         this.reconciliationManager = reconciliationManager;
107         this.rpcRegistry = rpcRegistry;
108
109         Preconditions.checkArgument(rpcRegistry != null, "RpcProviderRegistry can not be null !");
110
111         this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
112                 "RPC SalFlowService not found.");
113         this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
114                 "RPC SalGroupService not found.");
115         this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
116                 "RPC SalMeterService not found.");
117         this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
118                 "RPC SalTableService not found.");
119         this.salBundleService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalBundleService.class),
120                 "RPC SalBundlService not found.");
121     }
122
123     @Override
124     public void start() {
125         this.nodeListener = new FlowNodeReconciliationImpl(this, dataService, SERVICE_NAME, FRM_RECONCILIATION_PRIORITY,
126                 ResultState.DONOTHING);
127         if (this.isReconciliationDisabled()) {
128             LOG.debug("Reconciliation is disabled by user");
129         } else {
130             this.reconciliationNotificationRegistration = reconciliationManager.registerService(this.nodeListener);
131             LOG.debug("Reconciliation is enabled by user and successfully registered to the reconciliation framework");
132         }
133         this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider, notificationService,
134                 this.nodeListener, dataService);
135         this.deviceMastershipManager.setRoutedRpcReg(rpcRegistry.addRoutedRpcImplementation(ReconciliationService.class,
136                 new ReconciliationServiceImpl(this)));
137         flowNodeConnectorInventoryTranslatorImpl = new FlowNodeConnectorInventoryTranslatorImpl(dataService);
138
139         this.flowListener = new FlowForwarder(this, dataService);
140         this.groupListener = new GroupForwarder(this, dataService);
141         this.meterListener = new MeterForwarder(this, dataService);
142         this.tableListener = new TableForwarder(this, dataService);
143         LOG.info("ForwardingRulesManager has started successfully.");
144     }
145
146     @Override
147     public void close() throws Exception {
148         configurationServiceRegistration.close();
149
150         if (this.flowListener != null) {
151             this.flowListener.close();
152             this.flowListener = null;
153         }
154         if (this.groupListener != null) {
155             this.groupListener.close();
156             this.groupListener = null;
157         }
158         if (this.meterListener != null) {
159             this.meterListener.close();
160             this.meterListener = null;
161         }
162         if (this.tableListener != null) {
163             this.tableListener.close();
164             this.tableListener = null;
165         }
166         if (this.nodeListener != null) {
167             this.nodeListener.close();
168             this.nodeListener = null;
169         }
170         if (deviceMastershipManager != null) {
171             deviceMastershipManager.close();
172         }
173         if (this.reconciliationNotificationRegistration != null) {
174             this.reconciliationNotificationRegistration.close();
175             this.reconciliationNotificationRegistration = null;
176         }
177     }
178
179     @Override
180     public ReadOnlyTransaction getReadTranaction() {
181         return dataService.newReadOnlyTransaction();
182     }
183
184     @Override
185     public String getNewTransactionId() {
186         return "DOM-" + txNum.getAndIncrement();
187     }
188
189     @Override
190     public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
191         return deviceMastershipManager.isNodeActive(ident.firstKeyOf(Node.class).getId());
192     }
193
194     @Override
195     public boolean checkNodeInOperationalDataStore(InstanceIdentifier<FlowCapableNode> ident) {
196         boolean result = false;
197         InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
198         final ReadOnlyTransaction transaction = dataService.newReadOnlyTransaction();
199         ListenableFuture<com.google.common.base.Optional<Node>> future = transaction
200                 .read(LogicalDatastoreType.OPERATIONAL, nodeIid);
201         try {
202             com.google.common.base.Optional<Node> optionalDataObject = future.get();
203             if (optionalDataObject.isPresent()) {
204                 result = true;
205             } else {
206                 LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], nodeIid);
207             }
208         } catch (ExecutionException | InterruptedException e) {
209             LOG.warn("Failed to read {} ", nodeIid, e);
210         }
211         transaction.close();
212
213         return result;
214     }
215
216     @Override
217     public SalFlowService getSalFlowService() {
218         return salFlowService;
219     }
220
221     @Override
222     public SalGroupService getSalGroupService() {
223         return salGroupService;
224     }
225
226     @Override
227     public SalMeterService getSalMeterService() {
228         return salMeterService;
229     }
230
231     @Override
232     public SalTableService getSalTableService() {
233         return salTableService;
234     }
235
236     @Override
237     public SalBundleService getSalBundleService() {
238         return salBundleService;
239     }
240
241     @Override
242     public ForwardingRulesCommiter<Flow> getFlowCommiter() {
243         return flowListener;
244     }
245
246     @Override
247     public ForwardingRulesCommiter<Group> getGroupCommiter() {
248         return groupListener;
249     }
250
251     @Override
252     public ForwardingRulesCommiter<Meter> getMeterCommiter() {
253         return meterListener;
254     }
255
256     @Override
257     public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
258         return tableListener;
259     }
260
261     @Override
262     public boolean isReconciliationDisabled() {
263         return disableReconciliation;
264     }
265
266     @Override
267     public boolean isStaleMarkingEnabled() {
268         return staleMarkingEnabled;
269     }
270
271     @Override
272     public int getReconciliationRetryCount() {
273         return reconciliationRetryCount;
274     }
275
276     @Override
277     public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
278         return flowNodeConnectorInventoryTranslatorImpl;
279     }
280
281     public FlowNodeReconciliation getNodeListener() {
282         return nodeListener;
283     }
284
285     @Override
286     public boolean isBundleBasedReconciliationEnabled() {
287         return isBundleBasedReconciliationEnabled;
288     }
289
290     @Override
291     public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
292         return Objects.nonNull(ident) && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
293     }
294
295     @VisibleForTesting
296     public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
297         this.deviceMastershipManager = deviceMastershipManager;
298     }
299
300     @Override
301     public void onPropertyChanged(@Nonnull final String propertyName, @Nonnull final String propertyValue) {
302         Optional.ofNullable(ForwardingRulesProperty.forValue(propertyName)).ifPresent(forwardingRulesProperty -> {
303             switch (forwardingRulesProperty) {
304                 case DISABLE_RECONCILIATION:
305                     disableReconciliation = Boolean.valueOf(propertyValue);
306                     break;
307                 case STALE_MARKING_ENABLED:
308                     staleMarkingEnabled = Boolean.valueOf(propertyValue);
309                     break;
310                 case RECONCILIATION_RETRY_COUNT:
311                     reconciliationRetryCount = Integer.parseInt(propertyValue);
312                     break;
313                 case BUNDLE_BASED_RECONCILIATION_ENABLED:
314                     isBundleBasedReconciliationEnabled = Boolean.valueOf(propertyValue);
315                     break;
316                 default:
317                     LOG.warn("No forwarding rule property found.");
318                     break;
319             }
320         });
321     }
322 }