model of switch features in md-sal 82/3682/6
authorJuraj Sebin <jsebin@cisco.com>
Thu, 12 Dec 2013 13:59:40 +0000 (14:59 +0100)
committerJuraj Sebin <jsebin@cisco.com>
Sat, 14 Dec 2013 14:22:18 +0000 (15:22 +0100)
updated method nodeAdded in SalRegistrationManager to store switch features in NodeUpdated

do not merge before: http://git.opendaylight.org/gerrit/3681

Change-Id: I4ddf9097185914abe1575bf54e3c3d7d72ccdec6
Signed-off-by: Juraj Sebin <jsebin@cisco.com>
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/OFConstants.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/BuildSwitchCapabilitiesOF10.java [new file with mode: 0644]
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/BuildSwitchCapabilitiesOF13.java [new file with mode: 0644]
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/BuildSwitchFeatures.java [new file with mode: 0644]
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/SalRegistrationManager.java
openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/SwitchFeaturesUtil.java [new file with mode: 0644]
openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/SwitchFeaturesUtilTest.java [new file with mode: 0644]

index 020ccb84b923f0f92a559b74c3c898e5d6c9858c..1c990651c283835dc3039073b8a4cc1dabaad8a9 100644 (file)
@@ -6,4 +6,5 @@ public class OFConstants {
     public static final short OFPP_ALL  = ((short)0xfffc);
     public static final short OFPP_LOCAL = ((short)0xfffe);
 
+
 }
diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/BuildSwitchCapabilitiesOF10.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/BuildSwitchCapabilitiesOF10.java
new file mode 100644 (file)
index 0000000..353dbec
--- /dev/null
@@ -0,0 +1,88 @@
+/**
+ * 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.util.ArrayList;
+import java.util.List;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FeatureCapability;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityArpMatchIp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityFlowStats;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityIpReasm;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityPortStats;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityQueueStats;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityReserved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityStp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityTableStats;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeaturesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
+
+/**
+ * SwitchFeature builder for OF 1.0
+ * 
+ * @author jsebin
+ *
+ */
+public class BuildSwitchCapabilitiesOF10 implements BuildSwitchFeatures {
+
+    private static BuildSwitchCapabilitiesOF10 instance = new BuildSwitchCapabilitiesOF10();
+    
+    private BuildSwitchCapabilitiesOF10() {}
+    
+    /**
+     * Get singleton instance
+     * 
+     * @return instance
+     */
+    public static BuildSwitchCapabilitiesOF10 getInstance() {
+        return instance;
+    }
+
+    @Override
+    public SwitchFeatures build(GetFeaturesOutput features) {
+               
+       SwitchFeaturesBuilder builderSwFeatures = new SwitchFeaturesBuilder();
+       builderSwFeatures.setMaxBuffers(features.getBuffers());
+       builderSwFeatures.setMaxTables(features.getTables());       
+       
+       List<Class<? extends FeatureCapability>> capabilities = new ArrayList<>();
+       
+       if(features.getCapabilitiesV10().isOFPCARPMATCHIP()) {
+           capabilities.add(FlowFeatureCapabilityArpMatchIp.class);
+       }
+       if(features.getCapabilitiesV10().isOFPCFLOWSTATS()) {
+           capabilities.add(FlowFeatureCapabilityFlowStats.class);
+       }
+       if(features.getCapabilitiesV10().isOFPCIPREASM()) {
+           capabilities.add(FlowFeatureCapabilityIpReasm.class);
+       }
+       if(features.getCapabilitiesV10().isOFPCPORTSTATS()) {
+           capabilities.add(FlowFeatureCapabilityPortStats.class);
+       }
+       if(features.getCapabilitiesV10().isOFPCQUEUESTATS()) {
+           capabilities.add(FlowFeatureCapabilityQueueStats.class);
+       }
+       if(features.getCapabilitiesV10().isOFPCRESERVED()) {
+           capabilities.add(FlowFeatureCapabilityReserved.class);
+       }
+       if(features.getCapabilitiesV10().isOFPCSTP()) {
+           capabilities.add(FlowFeatureCapabilityStp.class);
+       }
+       if(features.getCapabilitiesV10().isOFPCTABLESTATS()) {
+           capabilities.add(FlowFeatureCapabilityTableStats.class);
+       }
+       
+       builderSwFeatures.setCapabilities(capabilities);
+       
+       return builderSwFeatures.build();
+    }
+    
+    
+}
diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/BuildSwitchCapabilitiesOF13.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/BuildSwitchCapabilitiesOF13.java
new file mode 100644 (file)
index 0000000..830e655
--- /dev/null
@@ -0,0 +1,82 @@
+/**
+ * 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.util.ArrayList;
+import java.util.List;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FeatureCapability;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityFlowStats;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityGroupStats;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityIpReasm;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityPortBlocked;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityPortStats;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityQueueStats;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowFeatureCapabilityTableStats;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeaturesBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
+
+/**
+ * SwitchFeature builder for OF 1.3
+ * 
+ * @author jsebin
+ *
+ */
+public class BuildSwitchCapabilitiesOF13 implements BuildSwitchFeatures {
+
+private static BuildSwitchCapabilitiesOF13 instance = new BuildSwitchCapabilitiesOF13();
+    
+    private BuildSwitchCapabilitiesOF13() {}
+    
+    /**
+     * Get singleton instance
+     * 
+     * @return instance
+     */
+    public static BuildSwitchCapabilitiesOF13 getInstance() {
+        return instance;
+    }
+
+    @Override
+    public SwitchFeatures build(GetFeaturesOutput features) {
+        
+        SwitchFeaturesBuilder builderSwFeatures = new SwitchFeaturesBuilder();
+        builderSwFeatures.setMaxBuffers(features.getBuffers());
+        builderSwFeatures.setMaxTables(features.getTables());
+        
+        List<Class<? extends FeatureCapability>> capabilities = new ArrayList<>();
+        
+        if(features.getCapabilities().isOFPCFLOWSTATS()) {
+            capabilities.add(FlowFeatureCapabilityFlowStats.class);
+        }
+        if(features.getCapabilities().isOFPCGROUPSTATS()) {
+            capabilities.add(FlowFeatureCapabilityGroupStats.class);
+        }
+        if(features.getCapabilities().isOFPCIPREASM()) {
+            capabilities.add(FlowFeatureCapabilityIpReasm.class);
+        }
+        if(features.getCapabilities().isOFPCPORTBLOCKED()) {
+            capabilities.add(FlowFeatureCapabilityPortBlocked.class);
+        }
+        if(features.getCapabilities().isOFPCPORTSTATS()) {
+            capabilities.add(FlowFeatureCapabilityPortStats.class);
+        }
+        if(features.getCapabilities().isOFPCQUEUESTATS()) {
+            capabilities.add(FlowFeatureCapabilityQueueStats.class);
+        }
+        if(features.getCapabilities().isOFPCTABLESTATS()) {
+            capabilities.add(FlowFeatureCapabilityTableStats.class);
+        }
+        
+        builderSwFeatures.setCapabilities(capabilities);
+        
+        return builderSwFeatures.build();
+    }
+
+}
diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/BuildSwitchFeatures.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/BuildSwitchFeatures.java
new file mode 100644 (file)
index 0000000..2393259
--- /dev/null
@@ -0,0 +1,29 @@
+/**
+ * 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 org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
+
+/**
+ * Common interface for SwitchFeatures builders for different OF versions
+ * 
+ * @author jsebin
+ *
+ */
+public interface BuildSwitchFeatures {
+       
+    /**
+     * 
+     * @param features {@link org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput} 
+     * @return {@link org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures}
+     */
+       public SwitchFeatures build(GetFeaturesOutput features);
+
+}
index c5e11dac289387ef27ecc63985d8e6d8b6f63118..17bd6dadda1ae2ce74d56afcf22c5d43722d1184 100644 (file)
@@ -21,6 +21,8 @@ import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
 import org.opendaylight.openflowplugin.openflow.md.core.session.SessionContext;
 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.flow.inventory.rev130819.FlowCapableNodeUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeUpdatedBuilder;
 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;
@@ -50,6 +52,12 @@ public class SalRegistrationManager implements SessionListener, SwitchInventory
     private NotificationProviderService publishService;
 
     private DataProviderService dataService;
+    
+    private SwitchFeaturesUtil swFeaturesUtil;
+    
+    public SalRegistrationManager() {
+        swFeaturesUtil = SwitchFeaturesUtil.getInstance();
+    }
 
     public NotificationProviderService getPublishService() {
         return publishService;
@@ -107,6 +115,11 @@ public class SalRegistrationManager implements SessionListener, SwitchInventory
         NodeUpdatedBuilder builder = new NodeUpdatedBuilder();
         builder.setId(sw.getNodeId());
         builder.setNodeRef(nodeRef);
+        
+        FlowCapableNodeUpdatedBuilder builder2 = new FlowCapableNodeUpdatedBuilder();
+        builder2.setSwitchFeatures(swFeaturesUtil.buildSwitchFeatures(features));
+        builder.addAugmentation(FlowCapableNodeUpdated.class, builder2.build());
+        
         return builder.build();
     }
 
diff --git a/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/SwitchFeaturesUtil.java b/openflowplugin/src/main/java/org/opendaylight/openflowplugin/openflow/md/core/sal/SwitchFeaturesUtil.java
new file mode 100644 (file)
index 0000000..45030be
--- /dev/null
@@ -0,0 +1,65 @@
+/**
+ * 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.util.HashMap;
+import java.util.Map;
+
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.flow.node.SwitchFeatures;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * @author jsebin
+ *
+ */
+public class SwitchFeaturesUtil {
+
+    protected static final Logger LOG = LoggerFactory.getLogger(SwitchFeaturesUtil.class);
+    
+    private static SwitchFeaturesUtil instance = new SwitchFeaturesUtil();
+    private Map<Short, BuildSwitchFeatures> swFeaturesBuilders;
+    
+    private SwitchFeaturesUtil() {
+        swFeaturesBuilders = new HashMap<>();
+        swFeaturesBuilders.put((short) 1, BuildSwitchCapabilitiesOF10.getInstance());
+        swFeaturesBuilders.put((short) 4, BuildSwitchCapabilitiesOF13.getInstance());
+    }
+    
+    /**
+     * Get singleton instance
+     * 
+     * @return instance
+     */
+    public static SwitchFeaturesUtil getInstance() {
+        return instance;
+    }
+    
+    /**
+     * @param features {@link org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput}
+     * @return switch features
+     */
+    public SwitchFeatures buildSwitchFeatures(GetFeaturesOutput features) {
+
+        if(swFeaturesBuilders.containsKey(features.getVersion()) == true) {
+            LOG.info("map contains version {}", features.getVersion());
+            try {
+                return swFeaturesBuilders.get(features.getVersion()).build(features);
+            } catch (NullPointerException e) {
+                LOG.error("error while building switch features {}", e);
+            }
+        }
+        else {
+            LOG.warn("unknown version: {}", features.getVersion());            
+        }                
+        
+        return null;
+    }
+    
+}
diff --git a/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/SwitchFeaturesUtilTest.java b/openflowplugin/src/test/java/org/opendaylight/openflowplugin/openflow/md/core/sal/SwitchFeaturesUtilTest.java
new file mode 100644 (file)
index 0000000..db537b5
--- /dev/null
@@ -0,0 +1,119 @@
+/**
+ * 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 junit.framework.Assert;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.Capabilities;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.CapabilitiesV10;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder;
+
+/**
+ * Tests for setting switch features for different version of OF plugin
+ * 
+ * @author jsebin
+ *
+ */
+public class SwitchFeaturesUtilTest {
+
+    private GetFeaturesOutputBuilder featuresOutputBuilder;    
+    private SwitchFeaturesUtil swUtil; 
+    
+    
+    /**
+     * initialization of {@link org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutputBuilder GetFeaturesOutputBuilder}
+     * and {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SwitchFeaturesUtil SwitchFeaturesUtil}
+     * @throws Exception
+     */
+    @Before
+    public void setUp() throws Exception {
+        featuresOutputBuilder = new GetFeaturesOutputBuilder();
+        swUtil = SwitchFeaturesUtil.getInstance();
+    }
+
+    /**
+     * @throws Exception
+     */
+    @After
+    public void tearDown() throws Exception {
+        featuresOutputBuilder = null;
+        swUtil = null;
+    }
+
+    /**
+     * Test method for
+     * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SwitchFeaturesUtil#buildSwitchFeatures} for OF 1.0 version 
+     * and switch feature capabilities
+     * .
+     */
+    @Test
+    public void testSwFeaturesCapabilitiesV10() {        
+        CapabilitiesV10 capabilities = new CapabilitiesV10( true, false, true, false, true, false, true, false);
+        featuresOutputBuilder.setCapabilitiesV10(capabilities).setVersion((short) 1);
+        
+        Assert.assertNotNull(swUtil.buildSwitchFeatures(featuresOutputBuilder.build()));
+    }
+    
+    /**
+     * Test method for
+     * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SwitchFeaturesUtil#buildSwitchFeatures} for OF 1.3 version 
+     * and switch feature capabilities
+     * .
+     */
+    @Test
+    public void testSwFeaturesCapabilitiesV13() {
+        Capabilities capabilities = new Capabilities(true, false, true, false, true, false, true);
+        featuresOutputBuilder.setCapabilities(capabilities).setCapabilitiesV10(null).setVersion((short) 4);
+        
+        Assert.assertNotNull(swUtil.buildSwitchFeatures(featuresOutputBuilder.build()));
+    }
+    
+    /**
+     * Test method for
+     * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SwitchFeaturesUtil#buildSwitchFeatures} for malformed switch feature capabilities
+     * - at least one feature is null
+     * .
+     */    
+    //@Test TODO:do we need to check if capability is null?
+    public void testSwFeaturesCapabilitiesMalformed() {
+        CapabilitiesV10 capabilities = new CapabilitiesV10( true, false, true, false, true, false, true, null);
+        featuresOutputBuilder.setCapabilitiesV10(capabilities).setCapabilities(null).setVersion((short) 1);
+        
+        Assert.assertNull(swUtil.buildSwitchFeatures(featuresOutputBuilder.build()));
+    }
+    
+    /**
+     * Test method for
+     * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SwitchFeaturesUtil#buildSwitchFeatures} for mismatch between
+     * version and switch feature capabilities
+     * .
+     */
+    @Test
+    public void testSwFeaturesCapabilitiesVersionMismatch() {
+        CapabilitiesV10 capabilities = new CapabilitiesV10( true, false, true, false, true, false, true, false);
+        featuresOutputBuilder.setCapabilitiesV10(capabilities).setCapabilities(null).setVersion((short) 4);
+        
+        Assert.assertNull(swUtil.buildSwitchFeatures(featuresOutputBuilder.build()));
+    }
+    
+    /**
+     * Test method for
+     * {@link org.opendaylight.openflowplugin.openflow.md.core.sal.SwitchFeaturesUtil#buildSwitchFeatures} for nonexisting version
+     * .
+     */
+    @Test
+    public void testSwFeaturesCapabilitiesNonexistingVersion() {
+        CapabilitiesV10 capabilities = new CapabilitiesV10( true, false, true, false, true, false, true, false);
+        featuresOutputBuilder.setCapabilitiesV10(capabilities).setCapabilities(null).setVersion((short) 0);
+        
+        Assert.assertNull(swUtil.buildSwitchFeatures(featuresOutputBuilder.build()));
+    }
+}