yang model revise: neutron-metering.yang
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronSecurityRuleInterface.java
index 26fcb6022fa138aa7c5bbb5bd969378cc5c3638d..29a94e396bed2d9f0beca6a2457ada3af00ce978 100644 (file)
@@ -8,34 +8,27 @@
 
 package org.opendaylight.neutron.transcriber;
 
-import java.util.ArrayList;
-import java.util.HashSet;
 import java.util.List;
-import java.util.Map.Entry;
-import java.util.Set;
 
 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.IpPrefix;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.DirectionBase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.DirectionEgress;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.DirectionIngress;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.EthertypeBase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.EthertypeV4;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.EthertypeV6;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.ProtocolBase;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.ProtocolHttp;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.ProtocolHttps;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.ProtocolIcmp;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev160807.ProtocolTcp;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150325.Neutron;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev141002.security.rules.attributes.SecurityRules;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev141002.security.rules.attributes.security.rules.SecurityRule;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev141002.security.rules.attributes.security.rules.SecurityRuleBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionBase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionEgress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.DirectionIngress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeBase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV4;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.EthertypeV6;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolBase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolIcmp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolIcmpV6;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolTcp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.ProtocolUdp;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.SecurityRules;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRule;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleBuilder;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceRegistration;
@@ -55,55 +48,21 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
             .build();
     private static final ImmutableBiMap<Class<? extends ProtocolBase>,String> PROTOCOL_MAP
             = new ImmutableBiMap.Builder<Class<? extends ProtocolBase>,String>()
-            .put(ProtocolHttp.class,"HTTP")
-            .put(ProtocolHttps.class,"HTTPS")
-            .put(ProtocolIcmp.class,"ICMP")
-            .put(ProtocolTcp.class,"TCP")
+            .put(ProtocolIcmp.class,"icmp")
+            .put(ProtocolTcp.class,"tcp")
+            .put(ProtocolUdp.class,"udp")
+            .put(ProtocolIcmpV6.class,"icmpv6")
             .build();
     private static final ImmutableBiMap<Class<? extends EthertypeBase>,String> ETHERTYPE_MAP
             = new ImmutableBiMap.Builder<Class<? extends EthertypeBase>,String>()
-            .put(EthertypeV4.class,"v4")
-            .put(EthertypeV6.class,"v6")
+            .put(EthertypeV4.class,"IPv4")
+            .put(EthertypeV6.class,"IPv6")
             .build();
 
     NeutronSecurityRuleInterface(ProviderContext providerContext) {
         super(providerContext);
     }
 
-    private void updateSecGroupRuleInSecurityGroup(NeutronSecurityRule input) {
-        NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
-            .fetchINeutronSecurityGroupCRUD(this);
-        INeutronSecurityGroupCRUD sgCrud = interfaces.getSecurityGroupInterface();
-        NeutronSecurityGroup sg = sgCrud.getNeutronSecurityGroup(input.getSecurityRuleGroupID());
-        if(sg != null && sg.getSecurityRules() != null) {
-            for(NeutronSecurityRule sgr :sg.getSecurityRules()) {
-                if(sgr != null && sgr.getID() != null && sgr.getID().equals(input.getID())) {
-                    int index = sg.getSecurityRules().indexOf(sgr);
-                    sg.getSecurityRules().set(index, input);
-                }
-            }
-        }
-        if (sg != null) {
-            sg.getSecurityRules().add(input);
-        }
-    }
-
-    private void removeSecGroupRuleFromSecurityGroup(NeutronSecurityRule input) {
-        NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
-            .fetchINeutronSecurityGroupCRUD(this);
-        INeutronSecurityGroupCRUD sgCrud = interfaces.getSecurityGroupInterface();
-        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.getID() != null && sgr.getID().equals(input.getID())) {
-                    toRemove.add(sgr);
-                }
-            }
-            sg.getSecurityRules().removeAll(toRemove);
-        }
-    }
-
     @Override
     public boolean neutronSecurityRuleExists(String uuid) {
         return exists(uuid);
@@ -126,32 +85,17 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
 
     @Override
     public boolean addNeutronSecurityRule(NeutronSecurityRule input) {
-        if (neutronSecurityRuleExists(input.getID())) {
-            return false;
-        }
-        updateSecGroupRuleInSecurityGroup(input);
-        addMd(input);
-        return true;
+        return add(input);
     }
 
     @Override
     public boolean removeNeutronSecurityRule(String uuid) {
-        if (!neutronSecurityRuleExists(uuid)) {
-            return false;
-        }
-        removeSecGroupRuleFromSecurityGroup(getNeutronSecurityRule(uuid));
-        removeMd(toMd(uuid));
-        return true;
+        return remove(uuid);
     }
 
     @Override
     public boolean updateNeutronSecurityRule(String uuid, NeutronSecurityRule delta) {
-        if (!neutronSecurityRuleExists(uuid)) {
-            return false;
-        }
-        updateSecGroupRuleInSecurityGroup(delta);
-        updateMd(delta);
-        return true;
+        return update(uuid, delta);
     }
 
     @Override
@@ -160,9 +104,9 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
     }
 
     protected NeutronSecurityRule fromMd(SecurityRule rule) {
-        NeutronSecurityRule answer = new NeutronSecurityRule();
+        final NeutronSecurityRule answer = new NeutronSecurityRule();
         if (rule.getTenantId() != null) {
-            answer.setTenantID(rule.getTenantId().getValue().replace("-",""));
+            answer.setTenantID(rule.getTenantId());
         }
         if (rule.getDirection() != null) {
             answer.setSecurityRuleDirection(DIRECTION_MAP.get(rule.getDirection()));
@@ -196,13 +140,13 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
 
     @Override
     protected SecurityRule toMd(NeutronSecurityRule securityRule) {
-        SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
+        final SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
 
         if (securityRule.getTenantID() != null) {
             securityRuleBuilder.setTenantId(toUuid(securityRule.getTenantID()));
         }
         if (securityRule.getSecurityRuleDirection() != null) {
-            ImmutableBiMap<String, Class<? extends DirectionBase>> mapper =
+            final ImmutableBiMap<String, Class<? extends DirectionBase>> mapper =
                     DIRECTION_MAP.inverse();
             securityRuleBuilder.setDirection((Class<? extends DirectionBase>) mapper.get(securityRule.getSecurityRuleDirection()));
         }
@@ -213,16 +157,16 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
             securityRuleBuilder.setRemoteGroupId(toUuid(securityRule.getSecurityRemoteGroupID()));
         }
         if (securityRule.getSecurityRuleRemoteIpPrefix() != null) {
-            IpPrefix ipPrefix = new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
+            final IpPrefix ipPrefix = new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
             securityRuleBuilder.setRemoteIpPrefix(ipPrefix);
         }
         if (securityRule.getSecurityRuleProtocol() != null) {
-            ImmutableBiMap<String, Class<? extends ProtocolBase>> mapper =
+            final ImmutableBiMap<String, Class<? extends ProtocolBase>> mapper =
                     PROTOCOL_MAP.inverse();
             securityRuleBuilder.setProtocol((Class<? extends ProtocolBase>) mapper.get(securityRule.getSecurityRuleProtocol()));
         }
         if (securityRule.getSecurityRuleEthertype() != null) {
-            ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper =
+            final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper =
                     ETHERTYPE_MAP.inverse();
             securityRuleBuilder.setEthertype((Class<? extends EthertypeBase>) mapper.get(securityRule.getSecurityRuleEthertype()));
         }
@@ -255,7 +199,7 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
 
     @Override
     protected SecurityRule toMd(String uuid) {
-        SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
+        final SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
         securityRuleBuilder.setId(toUuid(uuid));
         return securityRuleBuilder.build();
     }
@@ -263,8 +207,8 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
     public static void registerNewInterface(BundleContext context,
                                             ProviderContext providerContext,
                                             List<ServiceRegistration<?>> registrations) {
-        NeutronSecurityRuleInterface neutronSecurityRuleInterface = new NeutronSecurityRuleInterface(providerContext);
-        ServiceRegistration<INeutronSecurityRuleCRUD> neutronSecurityRuleInterfaceRegistration = context.registerService(INeutronSecurityRuleCRUD.class, neutronSecurityRuleInterface, null);
+        final NeutronSecurityRuleInterface neutronSecurityRuleInterface = new NeutronSecurityRuleInterface(providerContext);
+        final ServiceRegistration<INeutronSecurityRuleCRUD> neutronSecurityRuleInterfaceRegistration = context.registerService(INeutronSecurityRuleCRUD.class, neutronSecurityRuleInterface, null);
         if(neutronSecurityRuleInterfaceRegistration != null) {
             registrations.add(neutronSecurityRuleInterfaceRegistration);
         }