Bug 5540 - FlowConvertor, FlowStatsResponseConvertor, FlowInstructionResponseConvertor
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / OpenflowPluginProvider.java
index 7ef15a4e883664b3c9045a5b7e92138288a5f3c5..58754980b056e55a3ea56f20250fa08864831720 100644 (file)
  */
 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.statistics.MessageCountCommandProvider;
-import org.opendaylight.openflowplugin.statistics.MessageCountDumper;
-import org.opendaylight.openflowplugin.statistics.MessageObservatory;
+import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl;
+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 {
-
-    private static Logger LOG = LoggerFactory.getLogger(OpenflowPluginProvider.class);
+public class OpenflowPluginProvider implements AutoCloseable, OpenFlowPluginExtensionRegistratorProvider {
 
-    private BindingAwareBroker broker;
+    private static final Logger LOG = LoggerFactory.getLogger(OpenflowPluginProvider.class);
 
-    private BundleContext context;
+    private static final boolean SKIP_TABLE_FEATURES = false;
 
     private Collection<SwitchConnectionProvider> switchConnectionProviders;
 
     private MDController mdController;
 
-    private MessageCountCommandProvider messageCountCommandProvider;
-
     private MessageObservatory<DataContainer> messageCountProvider;
 
     private SalRegistrationManager registrationManager;
 
-    /**
-     * @param switchConnectionProvider
-     */
-    public void setSwitchConnectionProviders(Collection<SwitchConnectionProvider> switchConnectionProvider) {
-        this.switchConnectionProviders = switchConnectionProvider;
-    }
+    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;
+
 
-    /**
-     * @return osgi context
-     */
-    public BundleContext getContext() {
-        return context;
-    }
 
     /**
-     * dependencymanager requirement
-     * @param context
-     *
-     * @deprecated we should stop relying on osgi to provide cli interface for messageCounter
+     * Initialization of services and msgSpy counter
      */
-    @Deprecated
-    public void setContext(BundleContext context) {
-        this.context = context;
-    }
-
-    @Override
-    public void onSessionInitiated(ProviderContext session) {
-        LOG.debug("onSessionInitiated");
+    public void initialization() {
+        messageCountProvider = new MessageSpyCounterImpl();
+        extensionConverterManager = new ExtensionConverterManagerImpl();
+        roleManager = new OFRoleManager(OFSessionUtil.getSessionManager());
+        openflowPluginConfig = readConfig(skipTableFeatures);
+        entManager = new OfEntityManager(entityOwnershipService,getOpenflowPluginConfig());
+        entManager.setDataBroker(dataBroker);
+        entManager.init();
+
+        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);
+        mdController.setExtensionConverterProvider(extensionConverterManager);
         mdController.init();
         mdController.start();
-        messageCountCommandProvider = new MessageCountCommandProvider(context, messageCountProvider);
-        messageCountCommandProvider.onSessionInitiated(session);
+    }
+
+    /**
+     * @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;
-        messageCountCommandProvider.close();
-        messageCountCommandProvider = null;
-    }
 
-    @Override
-    public void onSessionInitialized(ConsumerContext session) {
-        // NOOP
-    }
+        if(mdController != null) {
+            mdController.stop();
+            mdController = null;
+        }
 
-    @Override
-    public Collection<? extends ProviderFunctionality> getFunctionality() {
-        return Collections.emptySet();
+        if(registrationManager != null) {
+            registrationManager.close();
+            registrationManager = null;
+        }
     }
 
-    @Override
-    public java.util.Collection<? extends RpcService> getImplementations() {
-        return Collections.emptySet();
+    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
-     */
-    public void registerProvider() {
-        // TODO : create normal init
-        messageCountProvider = new MessageSpyCounterImpl();
-        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;
+    }
+
+    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;
     }
- }
+}