Bump MRI upstreams
[openflowplugin.git] / applications / forwardingrules-manager / src / main / java / org / opendaylight / openflowplugin / applications / frm / impl / ForwardingRulesManagerImpl.java
index 1b393195c687e64fc8666510b0311549e368841c..7b62d7025051613953224c27a6e6c65c769f7670 100644 (file)
@@ -1,34 +1,46 @@
-/**
+/*
  * Copyright (c) 2014, 2017 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.openflowplugin.applications.frm.impl;
 
+import static java.util.Objects.requireNonNull;
+
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.util.concurrent.ListenableFuture;
-import java.util.Objects;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.atomic.AtomicLong;
-import javax.annotation.Nonnull;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
-import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.ReadTransaction;
+import org.opendaylight.mdsal.binding.api.RpcConsumerRegistry;
+import org.opendaylight.mdsal.binding.api.RpcProviderService;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.singleton.common.api.ClusterSingletonServiceProvider;
+import org.opendaylight.openflowplugin.api.openflow.FlowGroupCacheManager;
 import org.opendaylight.openflowplugin.api.openflow.configuration.ConfigurationService;
+import org.opendaylight.openflowplugin.api.openflow.mastership.MastershipChangeServiceManager;
+import org.opendaylight.openflowplugin.applications.frm.BundleMessagesCommiter;
 import org.opendaylight.openflowplugin.applications.frm.FlowNodeReconciliation;
 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesCommiter;
 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesManager;
 import org.opendaylight.openflowplugin.applications.frm.ForwardingRulesProperty;
+import org.opendaylight.openflowplugin.applications.frm.NodeConfigurator;
+import org.opendaylight.openflowplugin.applications.frm.nodeconfigurator.NodeConfiguratorImpl;
+import org.opendaylight.openflowplugin.applications.frm.recovery.OpenflowServiceRecoveryHandler;
 import org.opendaylight.openflowplugin.applications.reconciliation.NotificationRegistration;
 import org.opendaylight.openflowplugin.applications.reconciliation.ReconciliationManager;
+import org.opendaylight.serviceutils.srm.RecoverableListener;
+import org.opendaylight.serviceutils.srm.ServiceRecoveryRegistry;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
@@ -38,8 +50,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.SalMeterService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.SalBundleService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.arbitrator.reconcile.service.rev180227.ArbitratorReconcileService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.forwardingrules.manager.config.rev160511.ForwardingRulesManagerConfig;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.app.reconciliation.service.rev180227.ReconciliationService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflowplugin.rf.state.rev170713.ResultState;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.SalTableService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
@@ -55,12 +67,13 @@ import org.slf4j.LoggerFactory;
  * provide all RPC services.
  *
  */
-public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
+@Singleton
+public final class ForwardingRulesManagerImpl implements ForwardingRulesManager {
     private static final Logger LOG = LoggerFactory.getLogger(ForwardingRulesManagerImpl.class);
 
-    static final int STARTUP_LOOP_TICK = 500;
-    static final int STARTUP_LOOP_MAX_RETRIES = 8;
-    private static final int FRM_RECONCILIATION_PRIORITY = Integer.getInteger("frm.reconciliation.priority", 0);
+    static final int STARTUP_LOOP_TICK = 1000;
+    static final int STARTUP_LOOP_MAX_RETRIES = 240;
+    private static final int FRM_RECONCILIATION_PRIORITY = Integer.getInteger("frm.reconciliation.priority", 1);
     private static final String SERVICE_NAME = "FRM";
 
     private final AtomicLong txNum = new AtomicLong();
@@ -70,114 +83,144 @@ public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
     private final SalMeterService salMeterService;
     private final SalTableService salTableService;
     private final ClusterSingletonServiceProvider clusterSingletonServiceProvider;
-    private final NotificationProviderService notificationService;
     private final SalBundleService salBundleService;
     private final AutoCloseable configurationServiceRegistration;
-    private final RpcProviderRegistry rpcRegistry;
+    private final MastershipChangeServiceManager mastershipChangeServiceManager;
+    private final RpcProviderService rpcProviderService;
     private ForwardingRulesCommiter<Flow> flowListener;
     private ForwardingRulesCommiter<Group> groupListener;
     private ForwardingRulesCommiter<Meter> meterListener;
     private ForwardingRulesCommiter<TableFeatures> tableListener;
+    private BundleMessagesCommiter<Flow> bundleFlowListener;
+    private BundleMessagesCommiter<Group> bundleGroupListener;
     private FlowNodeReconciliation nodeListener;
     private NotificationRegistration reconciliationNotificationRegistration;
     private FlowNodeConnectorInventoryTranslatorImpl flowNodeConnectorInventoryTranslatorImpl;
     private DeviceMastershipManager deviceMastershipManager;
     private final ReconciliationManager reconciliationManager;
-
+    private DevicesGroupRegistry devicesGroupRegistry;
+    private NodeConfigurator nodeConfigurator;
+    private final ArbitratorReconcileService arbitratorReconciliationManager;
     private boolean disableReconciliation;
     private boolean staleMarkingEnabled;
     private int reconciliationRetryCount;
     private boolean isBundleBasedReconciliationEnabled;
-
-    public ForwardingRulesManagerImpl(final DataBroker dataBroker, final RpcProviderRegistry rpcRegistry,
-            final ForwardingRulesManagerConfig config, final ClusterSingletonServiceProvider clusterSingletonService,
-            final NotificationProviderService notificationService, final ConfigurationService configurationService,
-            final ReconciliationManager reconciliationManager) {
-        disableReconciliation = config.isDisableReconciliation();
-        staleMarkingEnabled = config.isStaleMarkingEnabled();
-        reconciliationRetryCount = config.getReconciliationRetryCount();
-        isBundleBasedReconciliationEnabled = config.isBundleBasedReconciliationEnabled();
-        this.configurationServiceRegistration = configurationService.registerListener(this);
-        this.dataService = Preconditions.checkNotNull(dataBroker, "DataBroker can not be null!");
-        this.clusterSingletonServiceProvider = Preconditions.checkNotNull(clusterSingletonService,
+    private final OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler;
+    private final ServiceRecoveryRegistry serviceRecoveryRegistry;
+    private final FlowGroupCacheManager flowGroupCacheManager;
+    private final ListenerRegistrationHelper registrationHelper;
+
+    @Inject
+    public ForwardingRulesManagerImpl(final DataBroker dataBroker,
+                                      final RpcConsumerRegistry rpcRegistry,
+                                      final RpcProviderService rpcProviderService,
+                                      final ForwardingRulesManagerConfig config,
+                                      final MastershipChangeServiceManager mastershipChangeServiceManager,
+                                      final ClusterSingletonServiceProvider clusterSingletonService,
+                                      final ConfigurationService configurationService,
+                                      final ReconciliationManager reconciliationManager,
+                                      final OpenflowServiceRecoveryHandler openflowServiceRecoveryHandler,
+                                      final ServiceRecoveryRegistry serviceRecoveryRegistry,
+                                      final FlowGroupCacheManager flowGroupCacheManager,
+                                      final ListenerRegistrationHelper registrationHelper) {
+        disableReconciliation = config.getDisableReconciliation();
+        staleMarkingEnabled = config.getStaleMarkingEnabled();
+        reconciliationRetryCount = config.getReconciliationRetryCount().toJava();
+        isBundleBasedReconciliationEnabled = config.getBundleBasedReconciliationEnabled();
+        configurationServiceRegistration = configurationService.registerListener(this);
+        this.registrationHelper = requireNonNull(registrationHelper, "RegistrationHelper cannot be null");
+        dataService = requireNonNull(dataBroker, "DataBroker can not be null!");
+        clusterSingletonServiceProvider = requireNonNull(clusterSingletonService,
                 "ClusterSingletonService provider can not be null");
-        this.notificationService = Preconditions.checkNotNull(notificationService,
-                "Notification publisher configurationService is" + " not available");
         this.reconciliationManager = reconciliationManager;
-        this.rpcRegistry = rpcRegistry;
+        this.rpcProviderService = rpcProviderService;
+        this.mastershipChangeServiceManager = mastershipChangeServiceManager;
+        this.flowGroupCacheManager = flowGroupCacheManager;
 
         Preconditions.checkArgument(rpcRegistry != null, "RpcProviderRegistry can not be null !");
 
-        this.salFlowService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalFlowService.class),
+        salFlowService = requireNonNull(rpcRegistry.getRpcService(SalFlowService.class),
                 "RPC SalFlowService not found.");
-        this.salGroupService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalGroupService.class),
+        salGroupService = requireNonNull(rpcRegistry.getRpcService(SalGroupService.class),
                 "RPC SalGroupService not found.");
-        this.salMeterService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalMeterService.class),
+        salMeterService = requireNonNull(rpcRegistry.getRpcService(SalMeterService.class),
                 "RPC SalMeterService not found.");
-        this.salTableService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalTableService.class),
+        salTableService = requireNonNull(rpcRegistry.getRpcService(SalTableService.class),
                 "RPC SalTableService not found.");
-        this.salBundleService = Preconditions.checkNotNull(rpcRegistry.getRpcService(SalBundleService.class),
+        salBundleService = requireNonNull(rpcRegistry.getRpcService(SalBundleService.class),
                 "RPC SalBundlService not found.");
+        this.openflowServiceRecoveryHandler = requireNonNull(openflowServiceRecoveryHandler,
+                "Openflow service recovery handler cannot be null");
+        this.serviceRecoveryRegistry = requireNonNull(serviceRecoveryRegistry,
+                "Service recovery registry cannot be null");
+        arbitratorReconciliationManager =
+                requireNonNull(rpcRegistry.getRpcService(ArbitratorReconcileService.class),
+                        "ArbitratorReconciliationManager can not be null!");
     }
 
     @Override
+    @PostConstruct
     public void start() {
-        this.nodeListener = new FlowNodeReconciliationImpl(this, dataService, SERVICE_NAME, FRM_RECONCILIATION_PRIORITY,
-                ResultState.DONOTHING);
+        nodeConfigurator = new NodeConfiguratorImpl();
+        devicesGroupRegistry = new DevicesGroupRegistry();
+        nodeListener = new FlowNodeReconciliationImpl(this, dataService, SERVICE_NAME, FRM_RECONCILIATION_PRIORITY,
+                ResultState.DONOTHING, flowGroupCacheManager);
         if (this.isReconciliationDisabled()) {
             LOG.debug("Reconciliation is disabled by user");
         } else {
-            this.reconciliationNotificationRegistration = reconciliationManager.registerService(this.nodeListener);
+            reconciliationNotificationRegistration = reconciliationManager.registerService(nodeListener);
             LOG.debug("Reconciliation is enabled by user and successfully registered to the reconciliation framework");
         }
-        this.deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider, notificationService,
-                this.nodeListener, dataService);
-        this.deviceMastershipManager.setRoutedRpcReg(rpcRegistry.addRoutedRpcImplementation(ReconciliationService.class,
-                new ReconciliationServiceImpl(this)));
+        deviceMastershipManager = new DeviceMastershipManager(clusterSingletonServiceProvider, nodeListener,
+                dataService, mastershipChangeServiceManager, rpcProviderService,
+                new FrmReconciliationServiceImpl(this));
         flowNodeConnectorInventoryTranslatorImpl = new FlowNodeConnectorInventoryTranslatorImpl(dataService);
 
-        this.flowListener = new FlowForwarder(this, dataService);
-        this.groupListener = new GroupForwarder(this, dataService);
-        this.meterListener = new MeterForwarder(this, dataService);
-        this.tableListener = new TableForwarder(this, dataService);
+        bundleFlowListener = new BundleFlowForwarder(this);
+        bundleGroupListener = new BundleGroupForwarder(this);
+        flowListener = new FlowForwarder(this, dataService, registrationHelper);
+        groupListener = new GroupForwarder(this, dataService, registrationHelper);
+        meterListener = new MeterForwarder(this, dataService, registrationHelper);
+        tableListener = new TableForwarder(this, dataService, registrationHelper);
         LOG.info("ForwardingRulesManager has started successfully.");
     }
 
     @Override
+    @PreDestroy
     public void close() throws Exception {
         configurationServiceRegistration.close();
 
-        if (this.flowListener != null) {
-            this.flowListener.close();
-            this.flowListener = null;
+        if (flowListener != null) {
+            flowListener.close();
+            flowListener = null;
         }
-        if (this.groupListener != null) {
-            this.groupListener.close();
-            this.groupListener = null;
+        if (groupListener != null) {
+            groupListener.close();
+            groupListener = null;
         }
-        if (this.meterListener != null) {
-            this.meterListener.close();
-            this.meterListener = null;
+        if (meterListener != null) {
+            meterListener.close();
+            meterListener = null;
         }
-        if (this.tableListener != null) {
-            this.tableListener.close();
-            this.tableListener = null;
+        if (tableListener != null) {
+            tableListener.close();
+            tableListener = null;
         }
-        if (this.nodeListener != null) {
-            this.nodeListener.close();
-            this.nodeListener = null;
+        if (nodeListener != null) {
+            nodeListener.close();
+            nodeListener = null;
         }
         if (deviceMastershipManager != null) {
             deviceMastershipManager.close();
         }
-        if (this.reconciliationNotificationRegistration != null) {
-            this.reconciliationNotificationRegistration.close();
-            this.reconciliationNotificationRegistration = null;
+        if (reconciliationNotificationRegistration != null) {
+            reconciliationNotificationRegistration.close();
+            reconciliationNotificationRegistration = null;
         }
     }
 
     @Override
-    public ReadOnlyTransaction getReadTranaction() {
+    public ReadTransaction getReadTransaction() {
         return dataService.newReadOnlyTransaction();
     }
 
@@ -187,19 +230,18 @@ public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
     }
 
     @Override
-    public boolean isNodeActive(InstanceIdentifier<FlowCapableNode> ident) {
+    public boolean isNodeActive(final InstanceIdentifier<FlowCapableNode> ident) {
         return deviceMastershipManager.isNodeActive(ident.firstKeyOf(Node.class).getId());
     }
 
     @Override
-    public boolean checkNodeInOperationalDataStore(InstanceIdentifier<FlowCapableNode> ident) {
+    public boolean checkNodeInOperationalDataStore(final InstanceIdentifier<FlowCapableNode> ident) {
         boolean result = false;
         InstanceIdentifier<Node> nodeIid = ident.firstIdentifierOf(Node.class);
-        final ReadOnlyTransaction transaction = dataService.newReadOnlyTransaction();
-        ListenableFuture<com.google.common.base.Optional<Node>> future = transaction
+        try (ReadTransaction transaction = dataService.newReadOnlyTransaction()) {
+            ListenableFuture<Optional<Node>> future = transaction
                 .read(LogicalDatastoreType.OPERATIONAL, nodeIid);
-        try {
-            com.google.common.base.Optional<Node> optionalDataObject = future.get();
+            Optional<Node> optionalDataObject = future.get();
             if (optionalDataObject.isPresent()) {
                 result = true;
             } else {
@@ -208,7 +250,6 @@ public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
         } catch (ExecutionException | InterruptedException e) {
             LOG.warn("Failed to read {} ", nodeIid, e);
         }
-        transaction.close();
 
         return result;
     }
@@ -233,6 +274,11 @@ public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
         return salTableService;
     }
 
+    @Override
+    public DevicesGroupRegistry getDevicesGroupRegistry() {
+        return devicesGroupRegistry;
+    }
+
     @Override
     public SalBundleService getSalBundleService() {
         return salBundleService;
@@ -258,6 +304,21 @@ public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
         return tableListener;
     }
 
+    @Override
+    public BundleMessagesCommiter<Flow> getBundleFlowListener() {
+        return bundleFlowListener;
+    }
+
+    @Override
+    public BundleMessagesCommiter<Group> getBundleGroupListener() {
+        return bundleGroupListener;
+    }
+
+    @Override
+    public ArbitratorReconcileService getArbitratorReconciliationManager() {
+        return arbitratorReconciliationManager;
+    }
+
     @Override
     public boolean isReconciliationDisabled() {
         return disableReconciliation;
@@ -273,11 +334,22 @@ public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
         return reconciliationRetryCount;
     }
 
+    @Override
+    public void addRecoverableListener(final RecoverableListener recoverableListener) {
+        serviceRecoveryRegistry.addRecoverableListener(openflowServiceRecoveryHandler.buildServiceRegistryKey(),
+                recoverableListener);
+    }
+
     @Override
     public FlowNodeConnectorInventoryTranslatorImpl getFlowNodeConnectorInventoryTranslatorImpl() {
         return flowNodeConnectorInventoryTranslatorImpl;
     }
 
+    @Override
+    public NodeConfigurator getNodeConfigurator() {
+        return nodeConfigurator;
+    }
+
     public FlowNodeReconciliation getNodeListener() {
         return nodeListener;
     }
@@ -288,8 +360,8 @@ public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
     }
 
     @Override
-    public boolean isNodeOwner(InstanceIdentifier<FlowCapableNode> ident) {
-        return Objects.nonNull(ident) && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
+    public boolean isNodeOwner(final InstanceIdentifier<FlowCapableNode> ident) {
+        return ident != null && deviceMastershipManager.isDeviceMastered(ident.firstKeyOf(Node.class).getId());
     }
 
     @VisibleForTesting
@@ -298,25 +370,26 @@ public class ForwardingRulesManagerImpl implements ForwardingRulesManager {
     }
 
     @Override
-    public void onPropertyChanged(@Nonnull final String propertyName, @Nonnull final String propertyValue) {
-        Optional.ofNullable(ForwardingRulesProperty.forValue(propertyName)).ifPresent(forwardingRulesProperty -> {
+    public void onPropertyChanged(@NonNull final String propertyName, @NonNull final String propertyValue) {
+        final ForwardingRulesProperty forwardingRulesProperty = ForwardingRulesProperty.forValue(propertyName);
+        if (forwardingRulesProperty != null) {
             switch (forwardingRulesProperty) {
                 case DISABLE_RECONCILIATION:
-                    disableReconciliation = Boolean.valueOf(propertyValue);
+                    disableReconciliation = Boolean.parseBoolean(propertyValue);
                     break;
                 case STALE_MARKING_ENABLED:
-                    staleMarkingEnabled = Boolean.valueOf(propertyValue);
+                    staleMarkingEnabled = Boolean.parseBoolean(propertyValue);
                     break;
                 case RECONCILIATION_RETRY_COUNT:
                     reconciliationRetryCount = Integer.parseInt(propertyValue);
                     break;
                 case BUNDLE_BASED_RECONCILIATION_ENABLED:
-                    isBundleBasedReconciliationEnabled = Boolean.valueOf(propertyValue);
+                    isBundleBasedReconciliationEnabled = Boolean.parseBoolean(propertyValue);
                     break;
                 default:
                     LOG.warn("No forwarding rule property found.");
                     break;
             }
-        });
+        }
     }
 }