popListener mapping
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / SalRegistrationManager.java
index 2368fab2f898cfef8cbc797dcd053c094c4a970c..681aa1b71d0622cc0ba575040536bf6ca8ad7ef6 100644 (file)
@@ -1,12 +1,19 @@
+/**
+ * Copyright (c) 2013 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.openflow.md.core.sal;
 
-import java.io.Console;
 import java.math.BigInteger;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
+import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
 import org.opendaylight.openflowplugin.openflow.md.ModelDrivenSwitch;
 import org.opendaylight.openflowplugin.openflow.md.SwitchInventory;
 import org.opendaylight.openflowplugin.openflow.md.core.SwitchConnectionDistinguisher;
@@ -16,6 +23,8 @@ import org.opendaylight.openflowplugin.openflow.md.core.session.SessionListener;
 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionManager;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemovedBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdatedBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
@@ -27,6 +36,9 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier.InstanceIdenti
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+/**
+ * session and inventory listener implementation
+ */
 public class SalRegistrationManager implements SessionListener, SwitchInventory {
 
     private final static Logger LOG = LoggerFactory.getLogger(SalRegistrationManager.class);
@@ -37,6 +49,8 @@ public class SalRegistrationManager implements SessionListener, SwitchInventory
 
     private NotificationProviderService publishService;
 
+    private DataProviderService dataService;
+
     public NotificationProviderService getPublishService() {
         return publishService;
     }
@@ -52,9 +66,12 @@ public class SalRegistrationManager implements SessionListener, SwitchInventory
     public void onSessionInitiated(ProviderContext session) {
         this.providerContext = session;
         this.publishService = session.getSALService(NotificationProviderService.class);
+        this.dataService = session.getSALService(DataProviderService.class);
 
         // We register as listener for Session Manager
         getSessionManager().registerSessionListener(this);
+        getSessionManager().setNotificationProviderService(publishService);
+        getSessionManager().setDataProviderService(dataService);
         LOG.info("SalRegistrationManager initialized");
 
     }
@@ -64,7 +81,7 @@ public class SalRegistrationManager implements SessionListener, SwitchInventory
         GetFeaturesOutput features = context.getFeatures();
         BigInteger datapathId = features.getDatapathId();
         InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
-
+        NodeRef nodeRef = new NodeRef(identifier);
         NodeId nodeId = nodeIdFromDatapathId(datapathId);
         ModelDrivenSwitchImpl ofSwitch = new ModelDrivenSwitchImpl(nodeId, identifier, context);
         salSwitches.put(identifier, ofSwitch);
@@ -72,12 +89,31 @@ public class SalRegistrationManager implements SessionListener, SwitchInventory
 
         LOG.info("ModelDrivenSwitch for {} registered to MD-SAL.", datapathId.toString());
 
-        publishService.publish(nodeAdded(ofSwitch, features));
+        publishService.publish(nodeAdded(ofSwitch, features,nodeRef));
+    }
+
+    @Override
+    public void onSessionRemoved(SessionContext context) {
+        GetFeaturesOutput features = context.getFeatures();
+        BigInteger datapathId = features.getDatapathId();
+        InstanceIdentifier<Node> identifier = identifierFromDatapathId(datapathId);
+        NodeRef nodeRef = new NodeRef(identifier);
+        NodeRemoved nodeRemoved = nodeRemoved(nodeRef);
+
+        LOG.info("ModelDrivenSwitch for {} unregistred from MD-SAL.", datapathId.toString());
+        publishService.publish(nodeRemoved);
     }
 
-    private NodeUpdated nodeAdded(ModelDrivenSwitch sw, GetFeaturesOutput features) {
+    private NodeUpdated nodeAdded(ModelDrivenSwitch sw, GetFeaturesOutput features, NodeRef nodeRef) {
         NodeUpdatedBuilder builder = new NodeUpdatedBuilder();
         builder.setId(sw.getNodeId());
+        builder.setNodeRef(nodeRef);
+        return builder.build();
+    }
+
+    private NodeRemoved nodeRemoved(NodeRef nodeRef) {
+        NodeRemovedBuilder builder = new NodeRemovedBuilder();
+        builder.setNodeRef(nodeRef);
         return builder.build();
     }
 
@@ -100,7 +136,7 @@ public class SalRegistrationManager implements SessionListener, SwitchInventory
     public static NodeId nodeIdFromDatapathId(BigInteger datapathId) {
         // FIXME: Convert to textual representation of datapathID
         String current = datapathId.toString();
-        return new NodeId("openflow://" + current);
+        return new NodeId("openflow:" + current);
     }
 
     public SessionManager getSessionManager() {