preparing QueueKeeper and message translation
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / MDController.java
index b13eada421257205eb91fc58d0dcb6b0a858dc12..0a9fcfe12b39ab8114a55e3d2ca34022801c6e74 100644 (file)
@@ -8,42 +8,77 @@
 
 package org.opendaylight.openflowplugin.openflow.md.core;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.TimeoutException;
 
 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionConfiguration;
 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
+import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
+import org.opendaylight.openflowplugin.openflow.md.core.translator.ErrorTranslator;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
+import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.google.common.collect.Collections2;
 import com.google.common.collect.Lists;
 
 /**
  * @author mirehak
  *
  */
-public class MDController {
+public class MDController implements IMDController {
 
-    private static final Logger LOG = LoggerFactory
-            .getLogger(MDController.class);
+    private static final Logger LOG = LoggerFactory.getLogger(MDController.class);
 
     private SwitchConnectionProvider switchConnectionProvider;
 
+    private ConcurrentMap<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> messageTranslators;
+
+    /**
+     * @return translator mapping
+     */
+    public Map<TranslatorKey, Collection<IMDMessageTranslator<OfHeader, DataObject>>> getMessageTranslators() {
+        return messageTranslators;
+    }
+
+    /**
+     * provisioning of translator mapping
+     */
+    public void init() {
+        LOG.debug("Initializing!");
+        messageTranslators = new ConcurrentHashMap<>();
+        addMessageTranslator(ErrorMessage.class, 4, new ErrorTranslator());
+        addMessageTranslator(ErrorMessage.class, 1, new ErrorTranslator());
+        
+        // Push the updated Listeners to Session Manager which will be then picked up by ConnectionConductor eventually
+        OFSessionUtil.getSessionManager().setTranslatorMapping(messageTranslators);
+    }
+
     /**
-     * @param switchConnectionProvider the switchConnectionProvider to set
+     * @param switchConnectionProvider
+     *            the switchConnectionProvider to set
      */
-    public void setSwitchConnectionProvider(
-            SwitchConnectionProvider switchConnectionProvider) {
+    public void setSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProvider) {
         this.switchConnectionProvider = switchConnectionProvider;
     }
 
     /**
-     * @param switchConnectionProviderToUnset the switchConnectionProvider to unset
+     * @param switchConnectionProviderToUnset
+     *            the switchConnectionProvider to unset
      */
-    public void unsetSwitchConnectionProvider(
-            SwitchConnectionProvider switchConnectionProviderToUnset) {
+    public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderToUnset) {
         if (this.switchConnectionProvider == switchConnectionProviderToUnset) {
             this.switchConnectionProvider = null;
         }
@@ -56,7 +91,7 @@ public class MDController {
      */
     public void start() {
         LOG.debug("starting ..");
-        LOG.debug("switchConnectionProvider: "+switchConnectionProvider);
+        LOG.debug("switchConnectionProvider: " + switchConnectionProvider);
         // setup handler
         SwitchConnectionHandler switchConnectionHandler = new SwitchConnectionHandlerImpl();
         switchConnectionProvider.setSwitchConnectionHandler(switchConnectionHandler);
@@ -69,7 +104,7 @@ public class MDController {
      * @return wished connections configurations
      */
     private static Collection<ConnectionConfiguration> getConnectionConfiguration() {
-        //TODO:: get config from state manager
+        // TODO:: get config from state manager
         ConnectionConfiguration configuration = ConnectionConfigurationFactory.getDefault();
         return Lists.newArrayList(configuration);
     }
@@ -82,6 +117,12 @@ public class MDController {
      */
     public void stop() {
         LOG.debug("stopping");
+        Future<List<Boolean>> srvStopped = switchConnectionProvider.shutdown();
+        try {
+            srvStopped.get(5000, TimeUnit.MILLISECONDS);
+        } catch (InterruptedException | ExecutionException | TimeoutException e) {
+            LOG.error(e.getMessage(), e);
+        }
     }
 
     /**
@@ -94,4 +135,31 @@ public class MDController {
         // do nothing
     }
 
+    @Override
+    public void addMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, DataObject> translator) {
+        TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
+        
+        Collection<IMDMessageTranslator<OfHeader, DataObject>> existingValues = messageTranslators.get(tKey);
+        if (existingValues == null) {
+            existingValues = new ArrayList<>();
+            messageTranslators.put(tKey, existingValues);
+        }
+        existingValues.add(translator);
+        LOG.debug("{} is now listened by {}", messageType, translator);
+    }
+
+    @Override
+    public void removeMessageTranslator(Class<? extends DataObject> messageType, int version, IMDMessageTranslator<OfHeader, DataObject> translator) {
+        TranslatorKey tKey = new TranslatorKey(version, messageType.getName());
+        Collection<IMDMessageTranslator<OfHeader, DataObject>> values = messageTranslators.get(tKey);
+        if (values != null) {
+            values.remove(translator);
+            if (values.isEmpty()) {
+                messageTranslators.remove(tKey);
+            }
+            LOG.debug("{} is now removed", translator);
+         }
+    }
+
+
 }