Remove security group and security rule ConcurrentHashMaps 54/26554/1
authorRyan Moats <rmoats@us.ibm.com>
Sat, 5 Sep 2015 18:10:16 +0000 (13:10 -0500)
committerRyan Moats <rmoats@us.ibm.com>
Sat, 5 Sep 2015 18:10:16 +0000 (13:10 -0500)
Side Effect: tweak to yang model to make sure the whole kit
and kaboodle hangs together.

Change-Id: I3e9cd68aec27fdd73bd690a9e173ddf44e46fe0b
Signed-off-by: Ryan Moats <rmoats@us.ibm.com>
model/src/main/yang/neutron-secgroups.yang
transcriber/src/main/java/org/opendaylight/neutron/transcriber/NeutronSecurityGroupInterface.java
transcriber/src/main/java/org/opendaylight/neutron/transcriber/NeutronSecurityRuleInterface.java

index edc79e3a67f3daaa90e3fca77d327f3ec6cc2d9c..76db34d0e8b7c915c4d6ec43d2006bcbaf5f0caa 100644 (file)
@@ -70,7 +70,7 @@ module neutron-secgroups {
         leaf remote-ip-prefix {
             description "The remote IP Prefix to be associated with this security group rule.
                 You can specify either remote-ip-prefix or remote-group-id in the request body.";
-            type inet:ip-address;
+            type string;
         }
         leaf protocol {
             type identityref {
index 5693d0945990b8a3568ba64c5d89a3fa1ce8eb96..6357bdd1cdd272a51d6183b7013990dfc22ba42e 100644 (file)
@@ -13,8 +13,6 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map.Entry;
 import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
 
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.neutron.spi.INeutronSecurityGroupCRUD;
@@ -36,7 +34,6 @@ import org.slf4j.LoggerFactory;
 
 public class NeutronSecurityGroupInterface extends AbstractNeutronInterface<SecurityGroup,NeutronSecurityGroup> implements INeutronSecurityGroupCRUD {
     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronSecurityGroupInterface.class);
-    private ConcurrentMap<String, NeutronSecurityGroup> securityGroupDB  = new ConcurrentHashMap<String, NeutronSecurityGroup>();
 
 
     NeutronSecurityGroupInterface(ProviderContext providerContext) {
@@ -45,44 +42,43 @@ public class NeutronSecurityGroupInterface extends AbstractNeutronInterface<Secu
 
     @Override
     public boolean neutronSecurityGroupExists(String uuid) {
-        return securityGroupDB.containsKey(uuid);
+        SecurityGroup group = readMd(createInstanceIdentifier(toMd(uuid)));
+        if (group == null) {
+            return false;
+        }
+        return true;
     }
 
     @Override
     public NeutronSecurityGroup getNeutronSecurityGroup(String uuid) {
-        if (!neutronSecurityGroupExists(uuid)) {
-            LOGGER.debug("No Security Groups Have Been Defined");
+        SecurityGroup group = readMd(createInstanceIdentifier(toMd(uuid)));
+        if (group == null) {
             return null;
         }
-        return securityGroupDB.get(uuid);
+        return fromMd(group);
     }
 
     @Override
     public List<NeutronSecurityGroup> getAllNeutronSecurityGroups() {
         Set<NeutronSecurityGroup> allSecurityGroups = new HashSet<NeutronSecurityGroup>();
-        for (Entry<String, NeutronSecurityGroup> entry : securityGroupDB.entrySet()) {
-            NeutronSecurityGroup securityGroup = entry.getValue();
-            allSecurityGroups.add(securityGroup);
+        SecurityGroups groups = readMd(createInstanceIdentifier());
+        if (groups != null) {
+            for (SecurityGroup group: groups.getSecurityGroup()) {
+                allSecurityGroups.add(fromMd(group));
+            }
         }
         LOGGER.debug("Exiting getSecurityGroups, Found {} OpenStackSecurityGroup", allSecurityGroups.size());
         List<NeutronSecurityGroup> ans = new ArrayList<NeutronSecurityGroup>();
         ans.addAll(allSecurityGroups);
         return ans;
     }
-
     @Override
     public boolean addNeutronSecurityGroup(NeutronSecurityGroup input) {
         if (neutronSecurityGroupExists(input.getID())) {
             return false;
         }
-        NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
-            .fetchINeutronSecurityRuleCRUD(this);
-        INeutronSecurityRuleCRUD sgrCrud = interfaces.getSecurityRuleInterface();
-        for(NeutronSecurityRule sgr : input.getSecurityRules()) {
-            sgrCrud.addNeutronSecurityRule(sgr);
-        }
-        securityGroupDB.putIfAbsent(input.getID(), input);
-        addMd(input);
+        addMd(input);       
         return true;
     }
 
@@ -91,7 +87,6 @@ public class NeutronSecurityGroupInterface extends AbstractNeutronInterface<Secu
         if (!neutronSecurityGroupExists(uuid)) {
             return false;
         }
-        securityGroupDB.remove(uuid);
         removeMd(toMd(uuid));
         return true;
     }
@@ -101,12 +96,8 @@ public class NeutronSecurityGroupInterface extends AbstractNeutronInterface<Secu
         if (!neutronSecurityGroupExists(uuid)) {
             return false;
         }
-        NeutronSecurityGroup target = securityGroupDB.get(uuid);
-        boolean rc = overwrite(target, delta);
-        if (rc) {
-            updateMd(securityGroupDB.get(uuid));
-        }
-        return rc;
+        updateMd(delta);
+        return true;
     }
 
     @Override
@@ -114,6 +105,34 @@ public class NeutronSecurityGroupInterface extends AbstractNeutronInterface<Secu
         return !neutronSecurityGroupExists(securityGroupUUID);
     }
 
+    protected NeutronSecurityGroup fromMd(SecurityGroup group) {
+        NeutronSecurityGroup answer = new NeutronSecurityGroup();
+        if (group.getName() != null) {
+            answer.setSecurityGroupName(group.getName());
+        }
+        if (group.getDescription() != null) {
+            answer.setSecurityGroupDescription(group.getDescription());
+        }
+        if (group.getTenantId() != null) {
+            answer.setSecurityGroupTenantID(group.getTenantId().getValue().replace("-",""));
+        }
+        if (group.getSecurityRules() != null) {
+            NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
+                .fetchINeutronSecurityRuleCRUD(this);
+            INeutronSecurityRuleCRUD srCrud = interfaces.getSecurityRuleInterface();
+
+            List<NeutronSecurityRule> rules = new ArrayList<NeutronSecurityRule>();
+            for (Uuid uuid: group.getSecurityRules()) {
+                 rules.add(srCrud.getNeutronSecurityRule(uuid.getValue()));
+            }
+            answer.setSecurityRules(rules);
+        }
+        if (group.getUuid() != null) {
+            answer.setID(group.getUuid().getValue());
+        } 
+        return answer;
+    }
+
     @Override
     protected SecurityGroup toMd(NeutronSecurityGroup securityGroup) {
         SecurityGroupBuilder securityGroupBuilder = new SecurityGroupBuilder();
@@ -127,7 +146,7 @@ public class NeutronSecurityGroupInterface extends AbstractNeutronInterface<Secu
             securityGroupBuilder.setTenantId(toUuid(securityGroup.getSecurityGroupTenantID()));
         }
         if (securityGroup.getSecurityRules() != null) {
-            List<Uuid> neutronSecurityRule = new ArrayList<>();
+            List<Uuid> neutronSecurityRule = new ArrayList<Uuid>();
             for (NeutronSecurityRule securityRule : securityGroup.getSecurityRules()) {
                 if (securityRule.getID() != null) {
                     neutronSecurityRule.add(toUuid(securityRule.getID()));
@@ -146,8 +165,14 @@ public class NeutronSecurityGroupInterface extends AbstractNeutronInterface<Secu
 
     @Override
     protected InstanceIdentifier<SecurityGroup> createInstanceIdentifier(SecurityGroup securityGroup) {
-        return InstanceIdentifier.create(Neutron.class).child(SecurityGroups.class).child(SecurityGroup.class,
-                securityGroup.getKey());
+        return InstanceIdentifier.create(Neutron.class)
+            .child(SecurityGroups.class).child(SecurityGroup.class,
+                                               securityGroup.getKey());
+    }
+
+    protected InstanceIdentifier<SecurityGroups> createInstanceIdentifier() {
+        return InstanceIdentifier.create(Neutron.class)
+            .child(SecurityGroups.class);
     }
 
     @Override
index 077dbbfa97a295304492e659a359fce138a7a6e7..b515e79eb9cebea0894c734cd2956eb169670ded 100644 (file)
@@ -13,8 +13,6 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Map.Entry;
 import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.ConcurrentMap;
 
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.neutron.spi.INeutronSecurityGroupCRUD;
@@ -49,7 +47,6 @@ import com.google.common.collect.ImmutableBiMap;
 
 public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<SecurityRule, NeutronSecurityRule> implements INeutronSecurityRuleCRUD {
     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronSecurityRuleInterface.class);
-    private ConcurrentMap<String, NeutronSecurityRule> securityRuleDB = new ConcurrentHashMap<String, NeutronSecurityRule>();
 
     private static final ImmutableBiMap<Class<? extends DirectionBase>,String> DIRECTION_MAP
             = new ImmutableBiMap.Builder<Class<? extends DirectionBase>,String>()
@@ -109,24 +106,30 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
 
     @Override
     public boolean neutronSecurityRuleExists(String uuid) {
-        return securityRuleDB.containsKey(uuid);
+        SecurityRule rule = readMd(createInstanceIdentifier(toMd(uuid)));
+        if (rule == null) {
+            return false;
+        }
+        return true;
     }
 
     @Override
     public NeutronSecurityRule getNeutronSecurityRule(String uuid) {
-        if (!neutronSecurityRuleExists(uuid)) {
-            LOGGER.debug("No Security Rules Have Been Defined");
+        SecurityRule rule = readMd(createInstanceIdentifier(toMd(uuid)));
+        if (rule == null) {
             return null;
         }
-        return securityRuleDB.get(uuid);
+        return fromMd(rule);
     }
 
     @Override
     public List<NeutronSecurityRule> getAllNeutronSecurityRules() {
         Set<NeutronSecurityRule> allSecurityRules = new HashSet<NeutronSecurityRule>();
-        for (Entry<String, NeutronSecurityRule> entry : securityRuleDB.entrySet()) {
-            NeutronSecurityRule securityRule = entry.getValue();
-            allSecurityRules.add(securityRule);
+        SecurityRules rules = readMd(createInstanceIdentifier());
+        if (rules != null) {
+            for (SecurityRule rule: rules.getSecurityRule()) {
+                allSecurityRules.add(fromMd(rule));
+            }
         }
         LOGGER.debug("Exiting getSecurityRule, Found {} OpenStackSecurityRule", allSecurityRules.size());
         List<NeutronSecurityRule> ans = new ArrayList<NeutronSecurityRule>();
@@ -139,7 +142,6 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
         if (neutronSecurityRuleExists(input.getID())) {
             return false;
         }
-        securityRuleDB.putIfAbsent(input.getID(), input);
         updateSecGroupRuleInSecurityGroup(input);
         addMd(input);
         return true;
@@ -150,8 +152,7 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
         if (!neutronSecurityRuleExists(uuid)) {
             return false;
         }
-        removeSecGroupRuleFromSecurityGroup(securityRuleDB.get(uuid));
-        securityRuleDB.remove(uuid);
+        removeSecGroupRuleFromSecurityGroup(getNeutronSecurityRule(uuid));
         removeMd(toMd(uuid));
         return true;
     }
@@ -161,13 +162,9 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
         if (!neutronSecurityRuleExists(uuid)) {
             return false;
         }
-        NeutronSecurityRule target = securityRuleDB.get(uuid);
-        boolean rc = overwrite(target, delta);
-        updateSecGroupRuleInSecurityGroup(securityRuleDB.get(uuid));
-        if (rc) {
-            updateMd(securityRuleDB.get(uuid));
-        }
-        return rc;
+        updateSecGroupRuleInSecurityGroup(delta);
+        updateMd(delta);
+        return true;
     }
 
     @Override
@@ -175,6 +172,41 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
         return !neutronSecurityRuleExists(securityRuleUUID);
     }
 
+    protected NeutronSecurityRule fromMd(SecurityRule rule) {
+        NeutronSecurityRule answer = new NeutronSecurityRule();
+        if (rule.getTenantId() != null) {
+            answer.setSecurityRuleTenantID(rule.getTenantId().getValue().replace("-",""));
+        }
+        if (rule.getDirection() != null) {
+            answer.setSecurityRuleDirection(DIRECTION_MAP.get(rule.getDirection()));
+        }
+        if (rule.getSecurityGroupId() != null) {
+            answer.setSecurityRuleGroupID(rule.getSecurityGroupId().getValue());
+        }
+        if (rule.getRemoteGroupId() != null) {
+            answer.setSecurityRemoteGroupID(rule.getRemoteGroupId().getValue());
+        }
+        if (rule.getRemoteIpPrefix() != null) {
+            answer.setSecurityRuleRemoteIpPrefix(rule.getRemoteIpPrefix());
+        }
+        if (rule.getProtocol() != null) {
+            answer.setSecurityRuleProtocol(PROTOCOL_MAP.get(rule.getProtocol()));
+        }
+        if (rule.getEthertype() != null) {
+            answer.setSecurityRuleEthertype(ETHERTYPE_MAP.get(rule.getEthertype()));
+        }
+        if (rule.getPortRangeMin() != null) {
+            answer.setSecurityRulePortMin(Integer.valueOf(rule.getPortRangeMin()));
+        }
+        if (rule.getPortRangeMax() != null) {
+            answer.setSecurityRulePortMax(Integer.valueOf(rule.getPortRangeMax()));
+        }
+        if (rule.getId() != null) {
+            answer.setID(rule.getId().getValue());
+        }
+        return answer;
+    }
+
     @Override
     protected SecurityRule toMd(NeutronSecurityRule securityRule) {
         SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
@@ -194,8 +226,7 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
             securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
         }
         if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
-            IpAddress ipAddress = new IpAddress(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
-            securityRuleBuilder.setRemoteIpPrefix(ipAddress);
+            securityRuleBuilder.setRemoteIpPrefix(securityRule.getSecurityRuleRemoteIpPrefix());
         }
         if (securityRule.getSecurityRuleProtocol() != null) {
             ImmutableBiMap<String, Class<? extends ProtocolBase>> mapper =
@@ -223,8 +254,14 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
 
     @Override
     protected InstanceIdentifier<SecurityRule> createInstanceIdentifier(SecurityRule securityRule) {
-        return InstanceIdentifier.create(Neutron.class).child(SecurityRules.class).child(SecurityRule.class,
-                securityRule.getKey());
+        return InstanceIdentifier.create(Neutron.class)
+            .child(SecurityRules.class).child(SecurityRule.class,
+                                              securityRule.getKey());
+    }
+
+    protected InstanceIdentifier<SecurityRules> createInstanceIdentifier() {
+        return InstanceIdentifier.create(Neutron.class)
+            .child(SecurityRules.class);
     }
 
     @Override