BUG8607 Fix checkstyle issues
[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
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.md.sal.common.api.data.ReadFailedException;
23 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
24 import org.opendaylight.controller.sal.binding.api.RpcConsumerRegistry;
25 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
26 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
27 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
28 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
29 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
30 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesProperty;
31 import org.opendaylight.openflowplugin.applications.reconciliation.NotificationRegistration;
32 import org.opendaylight.openflowplugin.applications.reconciliation.ReconciliationManager;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
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.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfig;
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 ForwardingRulesCommiter<Flow> flowListener;
77     private ForwardingRulesCommiter<Group> groupListener;
78     private ForwardingRulesCommiter<Meter> meterListener;
79     private ForwardingRulesCommiter<TableFeatures> tableListener;
80     private FlowNodeReconciliation nodeListener;
81     private NotificationRegistration reconciliationNotificationRegistration;
82     private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
83     private DeviceMastershipManager deviceMastershipManager;
84     private final ReconciliationManager reconciliationManager;
85
86     private boolean disableReconciliation;
87     private boolean staleMarkingEnabled;
88     private int reconciliationRetryCount;
89     private boolean isBundleBasedReconciliationEnabled;
90
91     public ForwardingRulesManagerImpl(final DataBroker dataBroker, final RpcConsumerRegistry rpcRegistry,
92             final ForwardingRulesManagerConfig config, final ClusterSingletonServiceProvider clusterSingletonService,
93             final NotificationProviderService notificationService, final ConfigurationService configurationService,
94             final ReconciliationManager reconciliationManager) {
95         disableReconciliation = config.isDisableReconciliation();
96         staleMarkingEnabled = config.isStaleMarkingEnabled();
97         reconciliationRetryCount = config.getReconciliationRetryCount();
98         isBundleBasedReconciliationEnabled = config.isBundleBasedReconciliationEnabled();
99         this.configurationServiceRegistration = configurationService.registerListener(this);
100         this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
101         this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
102                 "ClusterSingletonService provider can not be null");
103         this.notificationService = Preconditions.checkNotNull(notificationService,
104                 "Notification publisher configurationService is" + " not available");
105         this.reconciliationManager = reconciliationManager;
106
107         Preconditions.checkArgument(rpcRegistry != null, "RpcConsumerRegistry can not be null !");
108
109         this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
110                 "RPC SalFlowService not found.");
111         this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
112                 "RPC SalGroupService not found.");
113         this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
114                 "RPC SalMeterService not found.");
115         this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
116                 "RPC SalTableService not found.");
117         this.salBundleService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalBundleService.class),
118                 "RPC SalBundlService not found.");
119     }
120
121     @Override
122     public void start() {
123         this.nodeListener = new FlowNodeReconciliationImpl(this, dataService, SERVICE_NAME, FRM_RECONCILIATION_PRIORITY,
124                 ResultState.DONOTHING);
125         if (this.isReconciliationDisabled()) {
126             LOG.debug("Reconciliation is disabled by user");
127         } else {
128             this.reconciliationNotificationRegistration = reconciliationManager.registerService(this.nodeListener);
129             LOG.debug("Reconciliation is enabled by user and successfully registered to the reconciliation framework");
130         }
131         this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider, notificationService,
132                 this.nodeListener, dataService);
133         flowNodeConnectorInventoryTranslatorImpl = new FlowNodeConnectorInventoryTranslatorImpl(this, dataService);
134
135         this.flowListener = new FlowForwarder(this, dataService);
136         this.groupListener = new GroupForwarder(this, dataService);
137         this.meterListener = new MeterForwarder(this, dataService);
138         this.tableListener = new TableForwarder(this, dataService);
139         LOG.info("ForwardingRulesManager has started successfully.");
140     }
141
142     @Override
143     public void close() throws Exception {
144         configurationServiceRegistration.close();
145
146         if (this.flowListener != null) {
147             this.flowListener.close();
148             this.flowListener = null;
149         }
150         if (this.groupListener != null) {
151             this.groupListener.close();
152             this.groupListener = null;
153         }
154         if (this.meterListener != null) {
155             this.meterListener.close();
156             this.meterListener = null;
157         }
158         if (this.tableListener != null) {
159             this.tableListener.close();
160             this.tableListener = null;
161         }
162         if (this.nodeListener != null) {
163             this.nodeListener.close();
164             this.nodeListener = null;
165         }
166         if (deviceMastershipManager != null) {
167             deviceMastershipManager.close();
168         }
169         if (this.reconciliationNotificationRegistration != null) {
170             this.reconciliationNotificationRegistration.close();
171             this.reconciliationNotificationRegistration = null;
172         }
173     }
174
175     @Override
176     public ReadOnlyTransaction getReadTranaction() {
177         return dataService.newReadOnlyTransaction();
178     }
179
180     @Override
181     public String getNewTransactionId() {
182         return "DOM-" + txNum.getAndIncrement();
183     }
184
185     @Override
186     public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
187         return deviceMastershipManager.isNodeActive(ident.firstKeyOf(Node.class).getId());
188     }
189
190     @Override
191     public boolean checkNodeInOperationalDataStore(InstanceIdentifier<FlowCapableNode> ident) {
192         boolean result = false;
193         InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
194         final ReadOnlyTransaction transaction = dataService.newReadOnlyTransaction();
195         CheckedFuture<com.google.common.base.Optional<Node>, ReadFailedException> future = transaction
196                 .read(LogicalDatastoreType.OPERATIONAL, nodeIid);
197         try {
198             com.google.common.base.Optional<Node> optionalDataObject = future.checkedGet();
199             if (optionalDataObject.isPresent()) {
200                 result = true;
201             } else {
202                 LOG.debug("{}: Failed to read {}", Thread.currentThread().getStackTrace()[1], nodeIid);
203             }
204         } catch (ReadFailedException e) {
205             LOG.warn("Failed to read {} ", nodeIid, e);
206         }
207         transaction.close();
208
209         return result;
210     }
211
212     @Override
213     public SalFlowService getSalFlowService() {
214         return salFlowService;
215     }
216
217     @Override
218     public SalGroupService getSalGroupService() {
219         return salGroupService;
220     }
221
222     @Override
223     public SalMeterService getSalMeterService() {
224         return salMeterService;
225     }
226
227     @Override
228     public SalTableService getSalTableService() {
229         return salTableService;
230     }
231
232     @Override
233     public SalBundleService getSalBundleService() {
234         return salBundleService;
235     }
236
237     @Override
238     public ForwardingRulesCommiter<Flow> getFlowCommiter() {
239         return flowListener;
240     }
241
242     @Override
243     public ForwardingRulesCommiter<Group> getGroupCommiter() {
244         return groupListener;
245     }
246
247     @Override
248     public ForwardingRulesCommiter<Meter> getMeterCommiter() {
249         return meterListener;
250     }
251
252     @Override
253     public ForwardingRulesCommiter<TableFeatures> getTableFeaturesCommiter() {
254         return tableListener;
255     }
256
257     @Override
258     public boolean isReconciliationDisabled() {
259         return disableReconciliation;
260     }
261
262     @Override
263     public boolean isStaleMarkingEnabled() {
264         return staleMarkingEnabled;
265     }
266
267     @Override
268     public int getReconciliationRetryCount() {
269         return reconciliationRetryCount;
270     }
271
272     @Override
273     public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
274         return flowNodeConnectorInventoryTranslatorImpl;
275     }
276
277     @Override
278     public boolean isBundleBasedReconciliationEnabled() {
279         return isBundleBasedReconciliationEnabled;
280     }
281
282     @Override
283     public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
284         return Objects.nonNull(ident) && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
285     }
286
287     @VisibleForTesting
288     public void setDeviceMastershipManager(final DeviceMastershipManager deviceMastershipManager) {
289         this.deviceMastershipManager = deviceMastershipManager;
290     }
291
292     @Override
293     public void onPropertyChanged(@Nonnull final String propertyName, @Nonnull final String propertyValue) {
294         Optional.ofNullable(ForwardingRulesProperty.forValue(propertyName)).ifPresent(forwardingRulesProperty -> {
295             switch (forwardingRulesProperty) {
296                 case DISABLE_RECONCILIATION:
297                     disableReconciliation = Boolean.valueOf(propertyValue);
298                     break;
299                 case STALE_MARKING_ENABLED:
300                     staleMarkingEnabled = Boolean.valueOf(propertyValue);
301                     break;
302                 case RECONCILIATION_RETRY_COUNT:
303                     reconciliationRetryCount = Integer.valueOf(propertyValue);
304                     break;
305                 case BUNDLE_BASED_RECONCILIATION_ENABLED:
306                     isBundleBasedReconciliationEnabled = Boolean.valueOf(propertyValue);
307                     break;
308                 default:
309                     LOG.warn("Not forwarding rule property found.");
310                     break;
311             }
312         });
313     }
314 }