Correct handling of SGRs and SGs 89/23389/1
authorEd Warnicke <hagbard@gmail.com>
Thu, 25 Jun 2015 22:50:42 +0000 (16:50 -0600)
committerEd Warnicke <hagbard@gmail.com>
Thu, 25 Jun 2015 22:51:48 +0000 (22:51 +0000)
Change-Id: I562d37a242d824809ed0cfdb16bbba2c1e91fb95
Signed-off-by: Ed Warnicke <hagbard@gmail.com>
transcriber/src/main/java/org/opendaylight/neutron/transcriber/NeutronSecurityGroupInterface.java
transcriber/src/main/java/org/opendaylight/neutron/transcriber/NeutronSecurityRuleInterface.java

index 34521cf0fe15fc0c290880a5cd76095de029547e..05619a229242248f8bdacd0031cb2529faaf7517 100644 (file)
@@ -20,6 +20,8 @@ import java.util.concurrent.ConcurrentMap;
 
 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
 import org.opendaylight.neutron.spi.INeutronSecurityGroupCRUD;
+import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
+import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
 import org.opendaylight.neutron.spi.NeutronSecurityGroup;
 import org.opendaylight.neutron.spi.NeutronSecurityRule;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
@@ -101,6 +103,10 @@ public class NeutronSecurityGroupInterface extends AbstractNeutronInterface<Secu
         if (neutronSecurityGroupExists(input.getSecurityGroupUUID())) {
             return false;
         }
+        INeutronSecurityRuleCRUD sgrCrud = NeutronCRUDInterfaces.getINeutronSecurityRuleCRUD(this);
+        for(NeutronSecurityRule sgr : input.getSecurityRules()) {
+            sgrCrud.addNeutronSecurityRule(sgr);
+        }
         securityGroupDB.putIfAbsent(input.getSecurityGroupUUID(), input);
         addMd(input);
         return true;
index 0aba1688bef3c1e3367f065c56ec760db3ccdd0b..729f4c4fb492dfe09836ec90deaabac1e0f92929 100644 (file)
@@ -19,7 +19,10 @@ 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;
 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
+import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
+import org.opendaylight.neutron.spi.NeutronSecurityGroup;
 import org.opendaylight.neutron.spi.NeutronSecurityRule;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150325.Neutron;
@@ -67,6 +70,36 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
         return true;
     }
 
+    private void updateSecGroupRuleInSecurityGroup(NeutronSecurityRule input) {
+        INeutronSecurityGroupCRUD sgCrud = NeutronCRUDInterfaces.getINeutronSecurityGroupCRUD(this);
+        NeutronSecurityGroup sg = sgCrud.getNeutronSecurityGroup(input.getSecurityRuleGroupID());
+        if(sg != null && sg.getSecurityRules() != null) {
+            for(NeutronSecurityRule sgr :sg.getSecurityRules()) {
+                if(sgr.getSecurityRuleUUID() != null && sgr.getSecurityRuleUUID().equals(input.getSecurityRuleUUID())) {
+                    int index = sg.getSecurityRules().indexOf(sgr);
+                    sg.getSecurityRules().set(index, input);
+                }
+            }
+        }
+        if (sg != null) {
+            sg.getSecurityRules().add(input);
+        }
+    }
+
+    private void removeSecGroupRuleFromSecurityGroup(NeutronSecurityRule input) {
+        INeutronSecurityGroupCRUD sgCrud = NeutronCRUDInterfaces.getINeutronSecurityGroupCRUD(this);
+        NeutronSecurityGroup sg = sgCrud.getNeutronSecurityGroup(input.getSecurityRuleGroupID());
+        if(sg != null && sg.getSecurityRules() != null) {
+            List<NeutronSecurityRule> toRemove = new ArrayList<NeutronSecurityRule>();
+            for(NeutronSecurityRule sgr :sg.getSecurityRules()) {
+                if(sgr.getSecurityRuleUUID() != null && sgr.getSecurityRuleUUID().equals(input.getSecurityRuleUUID())) {
+                    toRemove.add(sgr);
+                }
+            }
+            sg.getSecurityRules().removeAll(toRemove);
+        }
+    }
+
     @Override
     public boolean neutronSecurityRuleExists(String uuid) {
         return securityRuleDB.containsKey(uuid);
@@ -100,6 +133,7 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
             return false;
         }
         securityRuleDB.putIfAbsent(input.getSecurityRuleUUID(), input);
+        updateSecGroupRuleInSecurityGroup(input);
         addMd(input);
         return true;
     }
@@ -109,6 +143,7 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
         if (!neutronSecurityRuleExists(uuid)) {
             return false;
         }
+        removeSecGroupRuleFromSecurityGroup(securityRuleDB.get(uuid));
         securityRuleDB.remove(uuid);
         removeMd(toMd(uuid));
         return true;
@@ -121,6 +156,7 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
         }
         NeutronSecurityRule target = securityRuleDB.get(uuid);
         boolean rc = overwrite(target, delta);
+        updateSecGroupRuleInSecurityGroup(securityRuleDB.get(uuid));
         if (rc) {
             updateMd(securityRuleDB.get(uuid));
         }