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