Bug 5540 - FlowConvertor, FlowStatsResponseConvertor, FlowInstructionResponseConvertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / OpenflowPluginProvider.java
index 7cbbc262e16670dbca9b806ef5da244de3afd27f..58754980b056e55a3ea56f20250fa08864831720 100644 (file)
@@ -7,36 +7,37 @@
  */
 package org.opendaylight.openflowplugin.openflow.md.core.sal;
 
+import com.google.common.annotations.VisibleForTesting;
 import java.util.Collection;
-import java.util.Collections;
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
-import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
+import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
+import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
+import org.opendaylight.openflowplugin.api.openflow.statistics.MessageCountDumper;
+import org.opendaylight.openflowplugin.api.openflow.statistics.MessageObservatory;
 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterRegistrator;
+import org.opendaylight.openflowplugin.extension.api.OpenFlowPluginExtensionRegistratorProvider;
+import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterManager;
 import org.opendaylight.openflowplugin.openflow.md.core.MDController;
-import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManager;
 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl;
-import org.opendaylight.openflowplugin.api.statistics.MessageCountDumper;
-import org.opendaylight.openflowplugin.api.statistics.MessageObservatory;
+import org.opendaylight.openflowplugin.openflow.md.core.role.OfEntityManager;
+import org.opendaylight.openflowplugin.openflow.md.core.session.OFRoleManager;
+import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
 import org.opendaylight.openflowplugin.statistics.MessageSpyCounterImpl;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326.OfpRole;
 import org.opendaylight.yangtools.yang.binding.DataContainer;
-import org.opendaylight.yangtools.yang.binding.RpcService;
-import org.osgi.framework.BundleContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
  * OFPlugin provider implementation
  */
-public class OpenflowPluginProvider implements BindingAwareProvider, AutoCloseable {
+public class OpenflowPluginProvider implements AutoCloseable, OpenFlowPluginExtensionRegistratorProvider {
 
-    private static Logger LOG = LoggerFactory.getLogger(OpenflowPluginProvider.class);
+    private static final Logger LOG = LoggerFactory.getLogger(OpenflowPluginProvider.class);
 
-    private BindingAwareBroker broker;
-
-    private BundleContext context;
+    private static final boolean SKIP_TABLE_FEATURES = false;
 
     private Collection<SwitchConnectionProvider> switchConnectionProviders;
 
@@ -48,34 +49,40 @@ public class OpenflowPluginProvider implements BindingAwareProvider, AutoCloseab
 
     private ExtensionConverterManager extensionConverterManager;
 
+    private OfpRole role;
+    private Boolean skipTableFeatures;
+
+    private OFRoleManager roleManager;
+    private OfEntityManager entManager;
+    private DataBroker dataBroker;
+    private NotificationProviderService notificationService;
+    private RpcProviderRegistry rpcRegistry;
+    private EntityOwnershipService entityOwnershipService;
+
+    private OpenflowPluginConfig openflowPluginConfig;
+
+
+
     /**
      * Initialization of services and msgSpy counter
      */
     public void initialization() {
         messageCountProvider = new MessageSpyCounterImpl();
         extensionConverterManager = new ExtensionConverterManagerImpl();
-        this.registerProvider();
-    }
-
-    /**
-     * @param switchConnectionProvider
-     */
-    public void setSwitchConnectionProviders(Collection<SwitchConnectionProvider> switchConnectionProvider) {
-        this.switchConnectionProviders = switchConnectionProvider;
-    }
-
-    /**
-     * @return osgi context
-     */
-    public BundleContext getContext() {
-        return context;
-    }
+        roleManager = new OFRoleManager(OFSessionUtil.getSessionManager());
+        openflowPluginConfig = readConfig(skipTableFeatures);
+        entManager = new OfEntityManager(entityOwnershipService,getOpenflowPluginConfig());
+        entManager.setDataBroker(dataBroker);
+        entManager.init();
 
-    @Override
-    public void onSessionInitiated(ProviderContext session) {
-        LOG.debug("onSessionInitiated");
+        LOG.debug("dependencies gathered..");
         registrationManager = new SalRegistrationManager();
-        registrationManager.onSessionInitiated(session);
+        registrationManager.setDataService(dataBroker);
+        registrationManager.setPublishService(notificationService);
+        registrationManager.setRpcProviderRegistry(rpcRegistry);
+        registrationManager.setOfEntityManager(entManager);
+        registrationManager.init();
+
         mdController = new MDController();
         mdController.setSwitchConnectionProviders(switchConnectionProviders);
         mdController.setMessageSpyCounter(messageCountProvider);
@@ -84,64 +91,125 @@ public class OpenflowPluginProvider implements BindingAwareProvider, AutoCloseab
         mdController.start();
     }
 
+    /**
+     * @param switchConnectionProvider switch connection provider
+     */
+    public void setSwitchConnectionProviders(Collection<SwitchConnectionProvider> switchConnectionProvider) {
+        this.switchConnectionProviders = switchConnectionProvider;
+    }
+
     @Override
     public void close() {
         LOG.debug("close");
-        mdController.stop();
-        mdController = null;
-        registrationManager.close();
-        registrationManager = null;
+
+        if(mdController != null) {
+            mdController.stop();
+            mdController = null;
+        }
+
+        if(registrationManager != null) {
+            registrationManager.close();
+            registrationManager = null;
+        }
+    }
+
+    public MessageCountDumper getMessageCountDumper() {
+        return messageCountProvider;
     }
 
     /**
-     * @return BA default broker
+     * @return the extensionConverterRegistry
      */
-    public BindingAwareBroker getBroker() {
-        return broker;
+    @Override
+    public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
+        return extensionConverterManager;
     }
 
     /**
-     * dependencymanager requirement
-     *
-     * @param broker
+     * @param role of instance
      */
-    public void setBroker(BindingAwareBroker broker) {
-        this.broker = broker;
+    public void setRole(OfpRole role) {
+        this.role = role;
     }
 
     /**
-     * dependencymanager requirement
-     *
-     * @param brokerArg
+     * @param newRole new controller role
      */
-    public void unsetBroker(BindingAwareBroker brokerArg) {
-        this.broker = null;
+    public void fireRoleChange(OfpRole newRole) {
+        if (!role.equals(newRole)) {
+            LOG.debug("Controller role was changed from {} to {}", role, newRole);
+            role = newRole;
+            switch (role) {
+                case BECOMEMASTER:
+                    //TODO: implement appropriate action
+                    roleManager.manageRoleChange(role);
+                    break;
+                case BECOMESLAVE:
+                    //TODO: implement appropriate action
+                    roleManager.manageRoleChange(role);
+                    break;
+                case NOCHANGE:
+                    //TODO: implement appropriate action
+                    roleManager.manageRoleChange(role);
+                    break;
+                default:
+                    LOG.warn("role not supported: {}", role);
+                    break;
+            }
+        }
     }
 
-    private boolean hasAllDependencies() {
-        if (this.broker != null && this.switchConnectionProviders != null) {
-            return true;
+    private OpenflowPluginConfig readConfig(Boolean skipTableFeatures){
+
+        final OpenflowPluginConfig.OpenflowPluginConfigBuilder openflowCfgBuilder = OpenflowPluginConfig.builder();
+
+        if(skipTableFeatures !=null){
+            openflowCfgBuilder.setSkipTableFeatures(skipTableFeatures.booleanValue());
+        } else{
+            LOG.warn("Could not load XML configuration file via ConfigSubsystem! Fallback to default config value(s)");
+            openflowCfgBuilder.setSkipTableFeatures(SKIP_TABLE_FEATURES);
         }
-        return false;
+
+        return openflowCfgBuilder.build();
     }
 
-    /**
-     * register providers for md-sal
-     */
-    private void registerProvider() {
-        if (hasAllDependencies()) {
-            this.broker.registerProvider(this, context);
-        }
+    public void setDataBroker(DataBroker dataBroker) {
+        this.dataBroker = dataBroker;
     }
 
-    public MessageCountDumper getMessageCountDumper() {
-        return messageCountProvider;
+    public void setNotificationService(NotificationProviderService notificationService) {
+        this.notificationService = notificationService;
     }
 
-    /**
-     * @return the extensionConverterRegistry
-     */
-    public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
-        return extensionConverterManager;
+    public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
+        this.rpcRegistry = rpcRegistry;
+    }
+
+    public void setEntityOwnershipService(EntityOwnershipService entityOwnershipService) {
+        this.entityOwnershipService = entityOwnershipService;
+    }
+
+    public void setSkipTableFeatures(Boolean skipTableFeatures) {
+        this.skipTableFeatures = skipTableFeatures;
+    }
+
+    @VisibleForTesting
+    public OpenflowPluginConfig getOpenflowPluginConfig() {
+        return openflowPluginConfig;
+    }
+
+    @VisibleForTesting
+    protected RpcProviderRegistry getRpcRegistry() {
+        return rpcRegistry;
+    }
+
+    @VisibleForTesting
+    protected NotificationProviderService getNotificationService() {
+        return notificationService;
+    }
+
+    @VisibleForTesting
+    protected DataBroker getDataBroker() {
+        return dataBroker;
     }
 }