remove unnecessary cast
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronSecurityRuleInterface.java
index 2a12796162eaa2e83c8796e557ead04135fa07b3..9f74b8d159d41cfbfbec1a941a40097a2ef7f013 100644 (file)
@@ -8,12 +8,13 @@
 
 package org.opendaylight.neutron.transcriber;
 
+import com.google.common.collect.ImmutableBiMap;
 import java.util.List;
-
-import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.neutron.northbound.api.BadRequestException;
 import org.opendaylight.neutron.spi.INeutronSecurityRuleCRUD;
 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.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpPrefix;
 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;
@@ -21,36 +22,28 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712
 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.NeutronUtils.ProtocolMapper;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes.Protocol;
 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.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.SecurityRuleAttributes.Protocol;
-import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceRegistration;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev150712.security.rules.attributes.security.rules.SecurityRuleKey;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.ImmutableBiMap;
-
+public final class NeutronSecurityRuleInterface extends
+        AbstractNeutronInterface<SecurityRule, SecurityRules, SecurityRuleKey, NeutronSecurityRule>
+        implements INeutronSecurityRuleCRUD {
+    private static final Logger LOG = LoggerFactory.getLogger(NeutronSecurityRuleInterface.class);
 
-public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<SecurityRule, SecurityRules, NeutronSecurityRule> implements INeutronSecurityRuleCRUD {
-    private static final Logger LOGGER = LoggerFactory.getLogger(NeutronSecurityRuleInterface.class);
+    private static final ImmutableBiMap<Class<? extends DirectionBase>,
+            String> DIRECTION_MAP = new ImmutableBiMap.Builder<Class<? extends DirectionBase>, String>()
+                    .put(DirectionEgress.class, "egress").put(DirectionIngress.class, "ingress").build();
+    private static final ImmutableBiMap<Class<? extends EthertypeBase>,
+            String> ETHERTYPE_MAP = new ImmutableBiMap.Builder<Class<? extends EthertypeBase>, String>()
+                    .put(EthertypeV4.class, "IPv4").put(EthertypeV6.class, "IPv6").build();
 
-    private static final ImmutableBiMap<Class<? extends DirectionBase>,String> DIRECTION_MAP
-            = new ImmutableBiMap.Builder<Class<? extends DirectionBase>,String>()
-            .put(DirectionEgress.class,"egress")
-            .put(DirectionIngress.class,"ingress")
-            .build();
-    private static final ImmutableBiMap<Class<? extends EthertypeBase>,String> ETHERTYPE_MAP
-            = new ImmutableBiMap.Builder<Class<? extends EthertypeBase>,String>()
-            .put(EthertypeV4.class,"IPv4")
-            .put(EthertypeV6.class,"IPv6")
-            .build();
-
-    NeutronSecurityRuleInterface(ProviderContext providerContext) {
-        super(providerContext);
+    NeutronSecurityRuleInterface(DataBroker db) {
+        super(SecurityRuleBuilder.class, db);
     }
 
     @Override
@@ -58,6 +51,7 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
         return rules.getSecurityRule();
     }
 
+    @Override
     protected NeutronSecurityRule fromMd(SecurityRule rule) {
         final NeutronSecurityRule answer = new NeutronSecurityRule();
         if (rule.getTenantId() != null) {
@@ -108,9 +102,9 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
             securityRuleBuilder.setTenantId(toUuid(securityRule.getTenantID()));
         }
         if (securityRule.getSecurityRuleDirection() != null) {
-            final ImmutableBiMap<String, Class<? extends DirectionBase>> mapper =
-                    DIRECTION_MAP.inverse();
-            securityRuleBuilder.setDirection((Class<? extends DirectionBase>) mapper.get(securityRule.getSecurityRuleDirection()));
+            final ImmutableBiMap<String, Class<? extends DirectionBase>> mapper = DIRECTION_MAP.inverse();
+            securityRuleBuilder
+                    .setDirection(mapper.get(securityRule.getSecurityRuleDirection()));
         }
         if (securityRule.getSecurityRuleGroupID() != null) {
             securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
@@ -124,13 +118,18 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
         }
         if (securityRule.getSecurityRuleProtocol() != null) {
             final String protocolString = securityRule.getSecurityRuleProtocol();
-            final Protocol protocol = new Protocol(protocolString.toCharArray());
-            securityRuleBuilder.setProtocol(protocol);
+            try {
+                final Protocol protocol = new Protocol(protocolString.toCharArray());
+                securityRuleBuilder.setProtocol(protocol);
+            } catch (NumberFormatException e) {
+                throw new BadRequestException("Protocol {" + securityRule.getSecurityRuleProtocol()
+                        + "} is not supported");
+            }
         }
         if (securityRule.getSecurityRuleEthertype() != null) {
-            final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper =
-                    ETHERTYPE_MAP.inverse();
-            securityRuleBuilder.setEthertype((Class<? extends EthertypeBase>) mapper.get(securityRule.getSecurityRuleEthertype()));
+            final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper = ETHERTYPE_MAP.inverse();
+            securityRuleBuilder
+                    .setEthertype(mapper.get(securityRule.getSecurityRuleEthertype()));
         }
         if (securityRule.getSecurityRulePortMin() != null) {
             securityRuleBuilder.setPortRangeMin(Integer.valueOf(securityRule.getSecurityRulePortMin()));
@@ -141,38 +140,8 @@ public class NeutronSecurityRuleInterface extends AbstractNeutronInterface<Secur
         if (securityRule.getID() != null) {
             securityRuleBuilder.setUuid(toUuid(securityRule.getID()));
         } else {
-            LOGGER.warn("Attempting to write neutron securityRule without UUID");
+            LOG.warn("Attempting to write neutron securityRule without UUID");
         }
         return securityRuleBuilder.build();
     }
-
-    @Override
-    protected InstanceIdentifier<SecurityRule> createInstanceIdentifier(SecurityRule securityRule) {
-        return InstanceIdentifier.create(Neutron.class)
-            .child(SecurityRules.class).child(SecurityRule.class,
-                                              securityRule.getKey());
-    }
-
-    @Override
-    protected InstanceIdentifier<SecurityRules> createInstanceIdentifier() {
-        return InstanceIdentifier.create(Neutron.class)
-            .child(SecurityRules.class);
-    }
-
-    @Override
-    protected SecurityRule toMd(String uuid) {
-        final SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
-        securityRuleBuilder.setUuid(toUuid(uuid));
-        return securityRuleBuilder.build();
-    }
-
-    public static void registerNewInterface(BundleContext context,
-                                            ProviderContext providerContext,
-                                            List<ServiceRegistration<?>> registrations) {
-        final NeutronSecurityRuleInterface neutronSecurityRuleInterface = new NeutronSecurityRuleInterface(providerContext);
-        final ServiceRegistration<INeutronSecurityRuleCRUD> neutronSecurityRuleInterfaceRegistration = context.registerService(INeutronSecurityRuleCRUD.class, neutronSecurityRuleInterface, null);
-        if(neutronSecurityRuleInterfaceRegistration != null) {
-            registrations.add(neutronSecurityRuleInterfaceRegistration);
-        }
-    }
 }