Clustering support for existing design (FRM,IM,SM) - add retry mechanisms to avoid...
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / SalRegistrationManager.java
index ba478daaaed02f8e9b0aa73c59d022896d57e6cd..46463f1c7098076e337d2c8c5b20a4c60687a147 100644 (file)
@@ -42,6 +42,8 @@ import org.opendaylight.yangtools.concepts.CompositeObjectRegistration;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdentifierBuilder;
+import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
+import org.opendaylight.openflowplugin.openflow.md.core.role.OfEntityManager;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -62,6 +64,8 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
 
     private ListenerRegistration<SessionListener> sessionListenerRegistration;
 
+    private OfEntityManager entManager;
+
     public SalRegistrationManager() {
         swFeaturesUtil = SwitchFeaturesUtil.getInstance();
     }
@@ -82,6 +86,10 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
         this.rpcProviderRegistry = rpcProviderRegistry;
     }
 
+    public void setOfEntityManager(OfEntityManager entManager) {
+       this.entManager = entManager;
+    }
+
     public void init() {
         LOG.debug("init..");
         sessionListenerRegistration = getSessionManager().registerSessionListener(this);
@@ -97,17 +105,18 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
         NodeRef nodeRef = new NodeRef(identifier);
         NodeId nodeId = nodeIdFromDatapathId(datapathId);
-        ModelDrivenSwitchImpl ofSwitch = new ModelDrivenSwitchImpl(nodeId, identifier, context);
-        CompositeObjectRegistration<ModelDrivenSwitch> registration =
-                ofSwitch.register(rpcProviderRegistry);
-        context.setProviderRegistration(registration);
-
-        LOG.debug("ModelDrivenSwitch for {} registered to MD-SAL.", datapathId);
+        ModelDrivenSwitch ofSwitch = new ModelDrivenSwitchImpl(nodeId, identifier,context);
 
         NotificationQueueWrapper wrappedNotification = new NotificationQueueWrapper(
                 nodeAdded(ofSwitch, features, nodeRef),
                 context.getFeatures().getVersion());
-        context.getNotificationEnqueuer().enqueueNotification(wrappedNotification);
+
+        reqOpenflowEntityOwnership(ofSwitch, context, wrappedNotification, rpcProviderRegistry);
+    }
+
+    @Override
+    public void setRole (SessionContext context) {
+        entManager.setSlaveRole(context);
     }
 
     @Override
@@ -116,6 +125,8 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
         BigInteger datapathId = features.getDatapathId();
         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
         NodeRef nodeRef = new NodeRef(identifier);
+        NodeId nodeId = nodeIdFromDatapathId(datapathId);
+        unregOpenflowEntityOwnership(nodeId);
         NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
 
         CompositeObjectRegistration<ModelDrivenSwitch> registration = context.getProviderRegistration();
@@ -201,7 +212,6 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
 
     @Override
     public void close() {
-        LOG.debug("close");
         dataService = null;
         rpcProviderRegistry = null;
         publishService = null;
@@ -209,4 +219,17 @@ public class SalRegistrationManager implements SessionListener, AutoCloseable {
             sessionListenerRegistration.close();
         }
     }
+
+    private void reqOpenflowEntityOwnership(ModelDrivenSwitch ofSwitch,
+                                            SessionContext context,
+                                            NotificationQueueWrapper wrappedNotification,
+                                            RpcProviderRegistry rpcProviderRegistry) {
+        context.setValid(true);
+        entManager.requestOpenflowEntityOwnership(ofSwitch, context, wrappedNotification, rpcProviderRegistry);
+    }
+
+    private void unregOpenflowEntityOwnership(NodeId nodeId) {
+        entManager.unregisterEntityOwnershipRequest(nodeId);
+    }
+
 }