Added PortSecurity attribute support 68/34868/5
authorDileep <dileep.ranganathan@intel.com>
Wed, 17 Feb 2016 14:12:39 +0000 (06:12 -0800)
committerDileep Ranganathan <dileep.ranganathan@intel.com>
Tue, 8 Mar 2016 21:54:25 +0000 (21:54 +0000)
Added portsecurity extension as augmentation for backward compatibility
Refer - http://developer.openstack.org/api-ref-networking-v2-ext.html

Change-Id: I75523702f6b1e83f192587396e3d103e507872d3
Signed-off-by: Dileep <dileep.ranganathan@intel.com>
model/src/main/yang/neutron-portsecurity.yang [new file with mode: 0644]
neutron-spi/src/main/java/org/opendaylight/neutron/spi/NeutronPort.java
neutron-spi/src/test/java/org/opendaylight/neutron/spi/NeutronPortSecurityJAXBTest.java [new file with mode: 0644]
northbound-api/src/main/java/org/opendaylight/neutron/northbound/api/NeutronPortsNorthbound.java
transcriber/src/main/java/org/opendaylight/neutron/transcriber/NeutronPortInterface.java

diff --git a/model/src/main/yang/neutron-portsecurity.yang b/model/src/main/yang/neutron-portsecurity.yang
new file mode 100644 (file)
index 0000000..1b35a64
--- /dev/null
@@ -0,0 +1,43 @@
+/*
+ * Copyright (c) 2016 Intel Corporation.  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
+ */
+
+module neutron-portsecurity {
+
+    yang-version 1;
+
+    namespace "urn:opendaylight:neutron-portsecurity";
+
+    prefix neutron-portsecurity;
+
+    import ietf-yang-types { prefix "yang"; }
+    import yang-ext { prefix "ext"; }
+    import neutron { prefix "neutron"; }
+
+
+    organization "OpenDaylight Neutron Group";
+
+    contact "Dileep Ranganathan <dileep.ranganathan@intel.com>";
+
+    description "This YANG module defines port-security attributes that are used
+        by OpenDaylight Neutron YANG modules.";
+
+    revision "2015-07-12" {
+        description
+                "OpenDaylight Beryllium release";
+    }
+
+    augment "/neutron:neutron/neutron:ports/neutron:port" {
+        description "This module augments the ports container in
+                the neutron-ports module with port-security information.";
+        ext:augment-identifier "port-security-extension";
+        leaf port-security-enabled {
+            type boolean;
+            description "The port security status. The status is enabled (true) or disabled (false).";
+        }
+    }
+}
index ad807be8b6682b2950170d37122d816e029d937c..435af783a9e07fdd99c9aeb71a573f74734915a7 100644 (file)
@@ -77,6 +77,10 @@ public class NeutronPort extends NeutronObject implements Serializable, INeutron
     @XmlElement (name = "extra_dhcp_opts")
     List<NeutronPort_ExtraDHCPOption> extraDHCPOptions;
 
+    //Port security is enabled by default for backward compatibility.
+    @XmlElement (defaultValue = "true", name = "port_security_enabled")
+    Boolean portSecurityEnabled;
+
     public NeutronPort() {
     }
 
@@ -116,7 +120,7 @@ public class NeutronPort extends NeutronObject implements Serializable, INeutron
     public Boolean getAdminStateUp() { return adminStateUp; }
 
     public void setAdminStateUp(Boolean newValue) {
-            adminStateUp = newValue;
+        adminStateUp = newValue;
     }
 
     public String getStatus() {
@@ -192,28 +196,39 @@ public class NeutronPort extends NeutronObject implements Serializable, INeutron
     }
 
     public String getBindinghostID() {
-      return bindinghostID;
+        return bindinghostID;
     }
 
     public void setBindinghostID(String bindinghostID) {
-      this.bindinghostID = bindinghostID;
+        this.bindinghostID = bindinghostID;
+    }
+
+    public String getBindingvnicType() {
+        return bindingvnicType;
     }
 
-  public String getBindingvnicType() {
-    return bindingvnicType;
-  }
+    public void setBindingvnicType(String bindingvnicType) {
+        this.bindingvnicType = bindingvnicType;
+    }
+
+    public String getBindingvifType() {
+        return bindingvifType;
+    }
 
-  public void setBindingvnicType(String bindingvnicType) {
-    this.bindingvnicType = bindingvnicType;
-  }
+    public void setBindingvifType(String bindingvifType) {
+        this.bindingvifType = bindingvifType;
+    }
 
-  public String getBindingvifType() {
-    return bindingvifType;
-  }
+    public Boolean getPortSecurityEnabled() {
+        if (portSecurityEnabled == null) {
+            return true;
+        }
+        return portSecurityEnabled;
+    }
 
-  public void setBindingvifType(String bindingvifType) {
-    this.bindingvifType = bindingvifType;
-  }
+    public void setPortSecurityEnabled(Boolean newValue) {
+        portSecurityEnabled = newValue;
+    }
 
     /**
      * This method copies selected fields from the object and returns them
@@ -261,6 +276,9 @@ public class NeutronPort extends NeutronObject implements Serializable, INeutron
             if ("security_groups".equals(field)) {
                 ans.setSecurityGroups(new ArrayList<NeutronSecurityGroup>(this.getSecurityGroups()));
             }
+            if ("port_security_enabled".equals(field)) {
+                ans.setPortSecurityEnabled(this.getPortSecurityEnabled());
+            }
         }
         return ans;
     }
@@ -268,6 +286,7 @@ public class NeutronPort extends NeutronObject implements Serializable, INeutron
     @Override
     public void initDefaults() {
         adminStateUp = true;
+        portSecurityEnabled = true;
         if (status == null) {
             status = "ACTIVE";
         }
@@ -283,6 +302,6 @@ public class NeutronPort extends NeutronObject implements Serializable, INeutron
                 + ", fixedIPs=" + fixedIPs + ", deviceID=" + deviceID + ", deviceOwner=" + deviceOwner + ", tenantID="
                 + tenantID + ", securityGroups=" + securityGroups
                 + ", bindinghostID=" + bindinghostID + ", bindingvnicType=" + bindingvnicType
-                + ", bindingvnicType=" + bindingvnicType + "]";
+                + ", bindingvnicType=" + bindingvnicType + ", portSecurityEnabled=" + portSecurityEnabled +"]";
     }
 }
diff --git a/neutron-spi/src/test/java/org/opendaylight/neutron/spi/NeutronPortSecurityJAXBTest.java b/neutron-spi/src/test/java/org/opendaylight/neutron/spi/NeutronPortSecurityJAXBTest.java
new file mode 100644 (file)
index 0000000..f345d8b
--- /dev/null
@@ -0,0 +1,163 @@
+/*
+ * Copyright (c) 2016 Intel Corporation  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.neutron.spi;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.List;
+
+public class NeutronPortSecurityJAXBTest {
+
+    // JSON with PortSecurityEnabled set true
+    private static final String portSecurityEnabled_sourceJson = "{ "
+            + "\"status\": \"ACTIVE\", "
+            + "\"name\": \"net1\", "
+            + "\"admin_state_up\": true, "
+            + "\"tenant_id\": \"9bacb3c5d39d41a79512987f338cf177\", "
+            + "\"fixed_ips\": [ { \"ip_address\":\"192.168.111.3\" , \"subnet_id\": \"22b44fc2-4ffb-4de4-b0f9-69d58b37ae27\" } ],"
+            + "\"binding:vif_details\": [{\"port_filter\": true , \"ovs_hybrid_plug\": true } ], "
+            + "\"extra_dhcp_opts\": [\"\"], " + "\"security_groups\": [\"\"], " + "\"allowed_address_pairs\": [\"\"], "
+            + "\"device_id\": \"257614cc-e178-4c92-9c61-3b28d40eca44\", " + "\"device_owner\": \"\", "
+            + "\"binding:host_id\": \"\", " + "\"binding:vif_type\": \"unbound\", "
+            + "\"binding:vnic_type\": \"normal\", " + "\"mac_address\": \"fa:16:3e:c9:cb:f0\", "
+            + "\"network_id\": \"e9330b1f-a2ef-4160-a991-169e56ab17f5\", "
+            + "\"id\": \"4e8e5957-649f-477b-9e5b-f1f75b21c03c\", "
+            + "\"port_security_enabled\": true }";
+
+
+    // JSON with PortSecurityEnabled set false
+    private static final String portSecurityDisabled_sourceJson = "{ "
+            + "\"status\": \"ACTIVE\", "
+            + "\"name\": \"net1\", "
+            + "\"admin_state_up\": true, "
+            + "\"tenant_id\": \"9bacb3c5d39d41a79512987f338cf177\", "
+            + "\"fixed_ips\": [ { \"ip_address\":\"192.168.111.3\" , \"subnet_id\": \"22b44fc2-4ffb-4de4-b0f9-69d58b37ae27\" } ],"
+            + "\"binding:vif_details\": [{\"port_filter\": true , \"ovs_hybrid_plug\": true } ], "
+            + "\"extra_dhcp_opts\": [\"\"], " + "\"security_groups\": [\"\"], " + "\"allowed_address_pairs\": [\"\"], "
+            + "\"device_id\": \"257614cc-e178-4c92-9c61-3b28d40eca44\", " + "\"device_owner\": \"\", "
+            + "\"binding:host_id\": \"\", " + "\"binding:vif_type\": \"unbound\", "
+            + "\"binding:vnic_type\": \"normal\", " + "\"mac_address\": \"fa:16:3e:c9:cb:f0\", "
+            + "\"network_id\": \"e9330b1f-a2ef-4160-a991-169e56ab17f5\", "
+            + "\"id\": \"4e8e5957-649f-477b-9e5b-f1f75b21c03c\", "
+            + "\"port_security_enabled\": false }";
+
+    // JSON with PortSecurityEnabled not set for compatibility test
+    private static final String portSecurityDefault_sourceJson = "{ "
+            + "\"status\": \"ACTIVE\", "
+            + "\"name\": \"net1\", "
+            + "\"admin_state_up\": true, "
+            + "\"tenant_id\": \"9bacb3c5d39d41a79512987f338cf177\", "
+            + "\"fixed_ips\": [ { \"ip_address\":\"192.168.111.3\" , \"subnet_id\": \"22b44fc2-4ffb-4de4-b0f9-69d58b37ae27\" } ],"
+            + "\"binding:vif_details\": [{\"port_filter\": true , \"ovs_hybrid_plug\": true } ], "
+            + "\"extra_dhcp_opts\": [\"\"], " + "\"security_groups\": [\"\"], " + "\"allowed_address_pairs\": [\"\"], "
+            + "\"device_id\": \"257614cc-e178-4c92-9c61-3b28d40eca44\", " + "\"device_owner\": \"\", "
+            + "\"binding:host_id\": \"\", " + "\"binding:vif_type\": \"unbound\", "
+            + "\"binding:vnic_type\": \"normal\", " + "\"mac_address\": \"fa:16:3e:c9:cb:f0\", "
+            + "\"network_id\": \"e9330b1f-a2ef-4160-a991-169e56ab17f5\", "
+            + "\"id\": \"4e8e5957-649f-477b-9e5b-f1f75b21c03c\" }";
+
+    @Test
+    public void test_NeutronPortSecurityEnabled_JAXB() {
+        test_PortSecurityEnabled_JAXB(portSecurityEnabled_sourceJson, true);
+    }
+
+    @Test
+    public void test_NeutronPortSecurityDisabled_JAXB() {
+        test_PortSecurityEnabled_JAXB(portSecurityDisabled_sourceJson, false);
+    }
+
+    /*
+        By Default PortSecurity is enabled for backward compatibility.
+        This test checks the case when the parameter is not available.
+     */
+    @Test
+    public void test_NeutronPortSecurityDefault_JAXB() {
+        test_PortSecurityEnabled_JAXB(portSecurityDefault_sourceJson, true);
+    }
+
+    private void test_PortSecurityEnabled_JAXB(String sourceJson, Boolean portSecurityEnabled) {
+        NeutronPort testObject = new NeutronPort();
+        try {
+            NeutronPort neutronObject = (NeutronPort) JaxbTestHelper.jaxbUnmarshall(testObject, sourceJson);
+            Assert.assertEquals("NeutronPort JAXB Test 1: Testing id failed", "4e8e5957-649f-477b-9e5b-f1f75b21c03c",
+                    neutronObject.getID());
+
+            Assert.assertEquals("NeutronPort JAXB Test 2: Testing tenant_id failed",
+                    "9bacb3c5d39d41a79512987f338cf177", neutronObject.getTenantID());
+
+            Assert.assertEquals("NeutronPort JAXB Test 3: Testing network_id failed",
+                    "e9330b1f-a2ef-4160-a991-169e56ab17f5", neutronObject.getNetworkUUID());
+
+            Assert.assertEquals("NeutronPort JAXB Test 4: Testing status failed", "ACTIVE", neutronObject.getStatus());
+
+            List<Neutron_IPs> fixedIPs = neutronObject.getFixedIPs();
+            Assert.assertEquals(" NeutronPort JAXB Test 5.1: Testing fixed_ips list length failed", 1, fixedIPs.size());
+
+            Assert.assertEquals(" NeutronPort JAXB Test 5.2: Testing ip_address value failed", "192.168.111.3",
+                    fixedIPs.get(0).ipAddress);
+
+            Assert.assertEquals(" NeutronPort JAXB Test 5.3: Testing subnet_id value failed",
+                    "22b44fc2-4ffb-4de4-b0f9-69d58b37ae27", fixedIPs.get(0).subnetUUID);
+
+            List<NeutronPort_ExtraDHCPOption> extraDHCPOptions = neutronObject.getExtraDHCPOptions();
+
+            Assert.assertEquals("NeutronPort JAXB Test 6: Testing extra_dhcp_opts list length failed", 1,
+                    extraDHCPOptions.size());
+
+            List<NeutronSecurityGroup> securityGroups = neutronObject.getSecurityGroups();
+            Assert.assertEquals("NeutronPort JAXB Test 7: Testing security_groups list length failed", 1,
+                    securityGroups.size());
+
+            List<NeutronPort_AllowedAddressPairs> allowedAddressPairs = neutronObject.getAllowedAddressPairs();
+            Assert.assertEquals("NeutronPort JAXB Test 8: Testing allowed_address_pairs list length failed", 1,
+                    allowedAddressPairs.size());
+
+            List<NeutronPort_VIFDetail> vifDetails = neutronObject.getVIFDetail();
+
+            Assert.assertEquals("NeutronPort JAXB Test 9.1: Testing vif_details list length failed", 1,
+                    vifDetails.size());
+
+            Assert.assertEquals("NeutronPort JAXB Test 9.2: Testing port_filter value failed", true,
+                    vifDetails.get(0).portFilter);
+
+            Assert.assertEquals("NeutronNetwork JAXB Test 9.3: Testing ovs_hybrid_plug value failed", true,
+                    vifDetails.get(0).ovsHybridPlug);
+
+            Assert.assertEquals("NeutronPort JAXB Test 10: Testing name failed", "net1", neutronObject.getName());
+
+            Assert.assertEquals("NeutronPort JAXB Test 11: Testing admin_state_up failed", true,
+                    neutronObject.getAdminStateUp());
+
+            Assert.assertEquals("NeutronPort JAXB Test 12: Testing binding:vif_type failed", "unbound",
+                    neutronObject.getBindingvifType());
+
+            Assert.assertEquals("NeutronPort JAXB Test 13: Testing binding:vnic_type failed", "normal",
+                    neutronObject.getBindingvnicType());
+
+            Assert.assertEquals("NeutronPort JAXB Test 14: Testing mac_address failed", "fa:16:3e:c9:cb:f0",
+                    neutronObject.getMacAddress());
+
+            Assert.assertEquals("NeutronPort JAXB Test 15: Testing device_id failed",
+                    "257614cc-e178-4c92-9c61-3b28d40eca44", neutronObject.getDeviceID());
+
+            Assert.assertEquals("NeutronPort JAXB Test 16: Testing device_owner failed", "",
+                    neutronObject.getDeviceOwner());
+
+            Assert.assertEquals("NeutronPort JAXB Test 17: Testing binding:host_id failed", "",
+                    neutronObject.getBindinghostID());
+
+            Assert.assertEquals("NeutronPort JAXB Test 18: Testing port_security_enabled failed", portSecurityEnabled,
+                    neutronObject.getPortSecurityEnabled());
+
+        } catch (Exception e) {
+            Assert.assertFalse("Tests Failed", true);
+        }
+    }
+}
index 57c311a53196f5862db86ab75c26b96bd64be0d8..99fd7843d2b0b7aa61defc51aab7f4159210d0e1 100644 (file)
@@ -172,6 +172,7 @@ public class NeutronPortsNorthbound
             @QueryParam("device_id") String queryDeviceID,
             @QueryParam("device_owner") String queryDeviceOwner,
             @QueryParam("tenant_id") String queryTenantID,
+            @QueryParam("port_security_enabled") Boolean queryPortSecurityEnabled,
             // linkTitle
             @QueryParam("limit") Integer limit,
             @QueryParam("marker") String marker,
@@ -192,7 +193,8 @@ public class NeutronPortsNorthbound
                     (queryMACAddress == null || queryMACAddress.equals(oSS.getMacAddress())) &&
                     (queryDeviceID == null || queryDeviceID.equals(oSS.getDeviceID())) &&
                     (queryDeviceOwner == null || queryDeviceOwner.equals(oSS.getDeviceOwner())) &&
-                    (queryTenantID == null || queryTenantID.equals(oSS.getTenantID()))) {
+                    (queryTenantID == null || queryTenantID.equals(oSS.getTenantID())) &&
+                    (queryPortSecurityEnabled == null || queryPortSecurityEnabled.equals(oSS.getPortSecurityEnabled()))) {
                 if (fields.size() > 0) {
                     ans.add(extractFields(oSS,fields));
                 } else {
index 2c65ded4aa561fa8d8b2bf9274f041bc7441dbdc..409ed76dffaa14fa7e00e952cf748cb1b56a397a 100644 (file)
@@ -45,6 +45,8 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.por
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.Ports;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.Port;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.ports.rev150712.ports.attributes.ports.PortBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.portsecurity.rev150712.PortSecurityExtension;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.portsecurity.rev150712.PortSecurityExtensionBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.osgi.framework.BundleContext;
@@ -147,6 +149,13 @@ public class NeutronPortInterface extends AbstractNeutronInterface<Port, Ports,
         result.setBindingvnicType(binding.getVnicType());
     }
 
+    private void portSecurityExtension(Port port, NeutronPort result) {
+        PortSecurityExtension portSecurity = port.getAugmentation(PortSecurityExtension.class);
+        if(portSecurity != null && portSecurity.isPortSecurityEnabled() != null) {
+            result.setPortSecurityEnabled(portSecurity.isPortSecurityEnabled());
+        }
+    }
+
     protected NeutronPort fromMd(Port port) {
         NeutronPort result = new NeutronPort();
         result.setAdminStateUp(port.isAdminStateUp());
@@ -203,6 +212,7 @@ public class NeutronPortInterface extends AbstractNeutronInterface<Port, Ports,
         }
         result.setID(port.getUuid().getValue());
         addExtensions(port, result);
+        portSecurityExtension(port, result);
         return result;
     }
 
@@ -233,9 +243,15 @@ public class NeutronPortInterface extends AbstractNeutronInterface<Port, Ports,
             bindingBuilder.setVnicType(neutronPort.getBindingvnicType());
         }
 
+        PortSecurityExtensionBuilder portSecurityBuilder = new PortSecurityExtensionBuilder();
+        if (neutronPort.getPortSecurityEnabled() != null) {
+            portSecurityBuilder.setPortSecurityEnabled(neutronPort.getPortSecurityEnabled());
+        }
+
         PortBuilder portBuilder = new PortBuilder();
         portBuilder.addAugmentation(PortBindingExtension.class,
                                     bindingBuilder.build());
+        portBuilder.addAugmentation(PortSecurityExtension.class, portSecurityBuilder.build());
         portBuilder.setAdminStateUp(neutronPort.isAdminStateUp());
         if(neutronPort.getAllowedAddressPairs() != null) {
             List<AllowedAddressPairs> listAllowedAddressPairs = new ArrayList<AllowedAddressPairs>();