yang model revise: neutron-metering.yang
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronSecurityRuleInterface.java
index 514c7f92f638d430db8760a431c647995a8ae9a5..29a94e396bed2d9f0beca6a2457ada3af00ce978 100644 (file)
 /*
- * Copyright (C) 2014 Red Hat, Inc.
+ * Copyright (c) 2014, 2015 Red Hat, Inc. and others.  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.transcriber;
 
-import java.lang.reflect.Method;
-import java.util.ArrayList;
-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.INeutronSecurityRuleCRUD;
 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;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.secgroups.rev141002.SecurityRuleAttrs;
-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.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.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;
+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;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-
-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>();
-
+import com.google.common.collect.ImmutableBiMap;
+
+
+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 ProtocolBase>,String> PROTOCOL_MAP
+            = new ImmutableBiMap.Builder<Class<? extends ProtocolBase>,String>()
+            .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,"IPv4")
+            .put(EthertypeV6.class,"IPv6")
+            .build();
 
     NeutronSecurityRuleInterface(ProviderContext providerContext) {
         super(providerContext);
     }
 
-    // this method uses reflection to update an object from it's delta.
-    private boolean overwrite(Object target, Object delta) {
-        Method[] methods = target.getClass().getMethods();
-
-        for (Method toMethod : methods) {
-            if (toMethod.getDeclaringClass().equals(target.getClass())
-                && toMethod.getName().startsWith("set")) {
-
-                String toName = toMethod.getName();
-                String fromName = toName.replace("set", "get");
-
-                try {
-                    Method fromMethod = delta.getClass().getMethod(fromName);
-                    Object value = fromMethod.invoke(delta, (Object[]) null);
-                    if (value != null) {
-                        toMethod.invoke(target, value);
-                    }
-                } catch (Exception e) {
-                    logger.error(e.getMessage());
-                    return false;
-                }
-            }
-        }
-        return true;
-    }
-
     @Override
     public boolean neutronSecurityRuleExists(String uuid) {
-        return securityRuleDB.containsKey(uuid);
+        return exists(uuid);
     }
 
     @Override
     public NeutronSecurityRule getNeutronSecurityRule(String uuid) {
-        if (!neutronSecurityRuleExists(uuid)) {
-            logger.debug("No Security Rules Have Been Defined");
-            return null;
-        }
-        return securityRuleDB.get(uuid);
+        return get(uuid);
+    }
+
+    @Override
+    protected List<SecurityRule> getDataObjectList(SecurityRules rules) {
+        return rules.getSecurityRule();
     }
 
     @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);
-        }
-        logger.debug("Exiting getSecurityRule, Found {} OpenStackSecurityRule", allSecurityRules.size());
-        List<NeutronSecurityRule> ans = new ArrayList<NeutronSecurityRule>();
-        ans.addAll(allSecurityRules);
-        return ans;
+        return getAll();
     }
 
     @Override
     public boolean addNeutronSecurityRule(NeutronSecurityRule input) {
-        if (neutronSecurityRuleExists(input.getSecurityRuleUUID())) {
-            return false;
-        }
-        securityRuleDB.putIfAbsent(input.getSecurityRuleUUID(), input);
-        addMd(input);
-        return true;
+        return add(input);
     }
 
     @Override
     public boolean removeNeutronSecurityRule(String uuid) {
-        if (!neutronSecurityRuleExists(uuid)) {
-            return false;
-        }
-        securityRuleDB.remove(uuid);
-        removeMd(toMd(uuid));
-        return true;
+        return remove(uuid);
     }
 
     @Override
     public boolean updateNeutronSecurityRule(String uuid, NeutronSecurityRule delta) {
-        if (!neutronSecurityRuleExists(uuid)) {
-            return false;
-        }
-        NeutronSecurityRule target = securityRuleDB.get(uuid);
-        boolean rc = overwrite(target, delta);
-        if (rc) {
-            updateMd(securityRuleDB.get(uuid));
-        }
-        return rc;
+        return update(uuid, delta);
     }
 
     @Override
     public boolean neutronSecurityRuleInUse(String securityRuleUUID) {
-        return !neutronSecurityRuleExists(securityRuleUUID);
+        return !exists(securityRuleUUID);
+    }
+
+    protected NeutronSecurityRule fromMd(SecurityRule rule) {
+        final NeutronSecurityRule answer = new NeutronSecurityRule();
+        if (rule.getTenantId() != null) {
+            answer.setTenantID(rule.getTenantId());
+        }
+        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(new String(rule.getRemoteIpPrefix().getValue()));
+        }
+        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();
+        final SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
 
-        if (securityRule.getSecurityRuleTenantID() != null) {
-            securityRuleBuilder.setTenantId(toUuid(securityRule.getSecurityRuleTenantID()));
+        if (securityRule.getTenantID() != null) {
+            securityRuleBuilder.setTenantId(toUuid(securityRule.getTenantID()));
         }
         if (securityRule.getSecurityRuleDirection() != null) {
-            boolean foundMatch = false;
-            for (SecurityRuleAttrs.Direction direction : SecurityRuleAttrs.Direction.values()) {
-                if (direction.toString().equalsIgnoreCase(securityRule.getSecurityRuleDirection())) {
-                    securityRuleBuilder.setDirection(direction);
-                    foundMatch = true;
-                    break;
-                }
-            }
-            if (!foundMatch) {
-                logger.warn("Unable to find direction value for {}", securityRule.getSecurityRuleDirection());
-            }
+            final ImmutableBiMap<String, Class<? extends DirectionBase>> mapper =
+                    DIRECTION_MAP.inverse();
+            securityRuleBuilder.setDirection((Class<? extends DirectionBase>) mapper.get(securityRule.getSecurityRuleDirection()));
         }
         if (securityRule.getSecurityRuleGroupID() != null) {
             securityRuleBuilder.setSecurityGroupId(toUuid(securityRule.getSecurityRuleGroupID()));
@@ -159,58 +157,60 @@ 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);
+            final IpPrefix ipPrefix = new IpPrefix(securityRule.getSecurityRuleRemoteIpPrefix().toCharArray());
+            securityRuleBuilder.setRemoteIpPrefix(ipPrefix);
         }
         if (securityRule.getSecurityRuleProtocol() != null) {
-            boolean foundMatch = false;
-            for (SecurityRuleAttrs.Protocol protocol : SecurityRuleAttrs.Protocol.values()) {
-                if (protocol.toString().equalsIgnoreCase(securityRule.getSecurityRuleProtocol())) {
-                    securityRuleBuilder.setProtocol(protocol);
-                    foundMatch = true;
-                    break;
-                }
-            }
-            if (!foundMatch) {
-                logger.warn("Unable to find protocol value for {}", securityRule.getSecurityRuleProtocol());
-            }
+            final ImmutableBiMap<String, Class<? extends ProtocolBase>> mapper =
+                    PROTOCOL_MAP.inverse();
+            securityRuleBuilder.setProtocol((Class<? extends ProtocolBase>) mapper.get(securityRule.getSecurityRuleProtocol()));
         }
         if (securityRule.getSecurityRuleEthertype() != null) {
-            boolean foundMatch = false;
-            for (SecurityRuleAttrs.Ethertype etherType : SecurityRuleAttrs.Ethertype.values()) {
-                if (etherType.toString().equalsIgnoreCase(securityRule.getSecurityRuleEthertype())) {
-                    securityRuleBuilder.setEthertype(etherType);
-                    foundMatch = true;
-                    break;
-                }
-            }
-            if (!foundMatch) {
-                logger.warn("Unable to find ethertype value for {}", securityRule.getSecurityRuleEthertype());
-            }
+            final ImmutableBiMap<String, Class<? extends EthertypeBase>> mapper =
+                    ETHERTYPE_MAP.inverse();
+            securityRuleBuilder.setEthertype((Class<? extends EthertypeBase>) mapper.get(securityRule.getSecurityRuleEthertype()));
         }
         if (securityRule.getSecurityRulePortMin() != null) {
-            securityRuleBuilder.setPortRangeMin(new Long(securityRule.getSecurityRulePortMin()));
+            securityRuleBuilder.setPortRangeMin(Integer.valueOf(securityRule.getSecurityRulePortMin()));
         }
         if (securityRule.getSecurityRulePortMax() != null) {
-            securityRuleBuilder.setPortRangeMax(new Long(securityRule.getSecurityRulePortMax()));
+            securityRuleBuilder.setPortRangeMax(Integer.valueOf(securityRule.getSecurityRulePortMax()));
         }
-        if (securityRule.getSecurityRuleUUID() != null) {
-            securityRuleBuilder.setId(toUuid(securityRule.getSecurityRuleUUID()));
+        if (securityRule.getID() != null) {
+            securityRuleBuilder.setId(toUuid(securityRule.getID()));
         } else {
-            logger.warn("Attempting to write neutron securityRule without UUID");
+            LOGGER.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);
+        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) {
-        SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
+        final SecurityRuleBuilder securityRuleBuilder = new SecurityRuleBuilder();
         securityRuleBuilder.setId(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);
+        }
+    }
 }