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