Reversion yang model
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronSubnetInterface.java
index 5e8f585ad0046bffc235ba934cce011349973193..6c6d27d56c3cf4cc84cd3c8edb591fb696477424 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright IBM Corporation, 2013.  All rights reserved.
+ * Copyright (c) 2013, 2015 IBM Corporation 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,
@@ -8,83 +8,86 @@
 
 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.INeutronNetworkCRUD;
+import org.opendaylight.neutron.spi.INeutronPortCRUD;
 import org.opendaylight.neutron.spi.INeutronSubnetCRUD;
+import org.opendaylight.neutron.spi.Neutron_IPs;
 import org.opendaylight.neutron.spi.NeutronCRUDInterfaces;
 import org.opendaylight.neutron.spi.NeutronNetwork;
+import org.opendaylight.neutron.spi.NeutronPort;
 import org.opendaylight.neutron.spi.NeutronSubnet;
+import org.opendaylight.neutron.spi.NeutronSubnetIPAllocationPool;
+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.constants.rev150712.Dhcpv6Base;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Off;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Slaac;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateful;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.Dhcpv6Stateless;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionBase;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV4;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.constants.rev150712.IpVersionV6;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.AllocationPools;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnet.attributes.AllocationPoolsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.Subnets;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.Subnet;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.subnets.rev150712.subnets.attributes.subnets.SubnetBuilder;
+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 NeutronSubnetInterface implements INeutronSubnetCRUD {
-    private static final Logger logger = LoggerFactory.getLogger(NeutronSubnetInterface.class);
-    private ConcurrentMap<String, NeutronSubnet> subnetDB  = new ConcurrentHashMap<String, NeutronSubnet>();
+import com.google.common.collect.ImmutableBiMap;
 
+public class NeutronSubnetInterface extends AbstractNeutronInterface<Subnet, Subnets, NeutronSubnet> implements INeutronSubnetCRUD {
+    private static final Logger LOGGER = LoggerFactory.getLogger(NeutronSubnetInterface.class);
 
+    private static final ImmutableBiMap<Class<? extends IpVersionBase>,Integer> IPV_MAP
+            = new ImmutableBiMap.Builder<Class<? extends IpVersionBase>,Integer>()
+            .put(IpVersionV4.class,Integer.valueOf(4))
+            .put(IpVersionV6.class,Integer.valueOf(6))
+            .build();
 
-    // this method uses reflection to update an object from it's delta.
+    private static final ImmutableBiMap<Class<? extends Dhcpv6Base>,String> DHCPV6_MAP
+            = new ImmutableBiMap.Builder<Class<? extends Dhcpv6Base>,String>()
+            .put(Dhcpv6Off.class,"off")
+            .put(Dhcpv6Stateful.class,"dhcpv6-stateful")
+            .put(Dhcpv6Slaac.class,"slaac")
+            .put(Dhcpv6Stateless.class,"dhcpv6-stateless")
+            .build();
 
-    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) {
-                    e.printStackTrace();
-                    return false;
-                }
-            }
-        }
-        return true;
+    NeutronSubnetInterface(ProviderContext providerContext) {
+        super(providerContext);
     }
 
-
     // IfNBSubnetCRUD methods
 
     @Override
     public boolean subnetExists(String uuid) {
-        return subnetDB.containsKey(uuid);
+        return exists(uuid);
     }
 
     @Override
     public NeutronSubnet getSubnet(String uuid) {
-        if (!subnetExists(uuid)) {
-            return null;
-        }
-        return subnetDB.get(uuid);
+        return get(uuid);
+    }
+
+    @Override
+    protected List<Subnet> getDataObjectList(Subnets subnets) {
+        return subnets.getSubnet();
     }
 
     @Override
     public List<NeutronSubnet> getAllSubnets() {
-        Set<NeutronSubnet> allSubnets = new HashSet<NeutronSubnet>();
-        for (Entry<String, NeutronSubnet> entry : subnetDB.entrySet()) {
-            NeutronSubnet subnet = entry.getValue();
-            allSubnets.add(subnet);
-        }
-        logger.debug("Exiting getAllSubnets, Found {} OpenStackSubnets", allSubnets.size());
-        List<NeutronSubnet> ans = new ArrayList<NeutronSubnet>();
-        ans.addAll(allSubnets);
-        return ans;
+        return getAll();
     }
 
     @Override
@@ -93,8 +96,10 @@ public class NeutronSubnetInterface implements INeutronSubnetCRUD {
         if (subnetExists(id)) {
             return false;
         }
-        subnetDB.putIfAbsent(id, input);
-        INeutronNetworkCRUD networkIf = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
+        addMd(input);
+        NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
+            .fetchINeutronNetworkCRUD(this);
+        INeutronNetworkCRUD networkIf = interfaces.getNetworkInterface();
 
         NeutronNetwork targetNet = networkIf.getNetwork(input.getNetworkUUID());
         targetNet.addSubnet(id);
@@ -103,33 +108,181 @@ public class NeutronSubnetInterface implements INeutronSubnetCRUD {
 
     @Override
     public boolean removeSubnet(String uuid) {
-        if (!subnetExists(uuid)) {
+        NeutronSubnet target = getSubnet(uuid);
+        if (target == null) {
             return false;
         }
-        NeutronSubnet target = subnetDB.get(uuid);
-        INeutronNetworkCRUD networkIf = NeutronCRUDInterfaces.getINeutronNetworkCRUD(this);
+        removeMd(toMd(uuid));
+        NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
+            .fetchINeutronNetworkCRUD(this);
+        INeutronNetworkCRUD networkIf = interfaces.getNetworkInterface();
 
         NeutronNetwork targetNet = networkIf.getNetwork(target.getNetworkUUID());
         targetNet.removeSubnet(uuid);
-        subnetDB.remove(uuid);
         return true;
     }
 
     @Override
     public boolean updateSubnet(String uuid, NeutronSubnet delta) {
-        if (!subnetExists(uuid)) {
-            return false;
-        }
-        NeutronSubnet target = subnetDB.get(uuid);
-        return overwrite(target, delta);
+/* note: because what we get is *not* a delta but (at this point) the updated
+ * object, this is much simpler - just replace the value and update the mdsal
+ * with it */
+        return update(uuid, delta);
     }
 
+// note: this is being set to false in preparation for deprecation and removal
     @Override
     public boolean subnetInUse(String subnetUUID) {
-        if (!subnetExists(subnetUUID)) {
-            return true;
+        return false;
+    }
+
+    protected NeutronSubnet fromMd(Subnet subnet) {
+        NeutronSubnet result = new NeutronSubnet();
+        result.setName(subnet.getName());
+        result.setTenantID(subnet.getTenantId());
+        result.setNetworkUUID(subnet.getNetworkId().getValue());
+        result.setIpVersion(IPV_MAP.get(subnet.getIpVersion()));
+        result.setCidr(subnet.getCidr());
+        if (subnet.getGatewayIp() != null) {
+            result.setGatewayIP(String.valueOf(subnet.getGatewayIp().getValue()));
+        }
+        if (subnet.getIpv6RaMode() != null) {
+            result.setIpV6RaMode(DHCPV6_MAP.get(subnet.getIpv6RaMode()));
+        }
+        if (subnet.getIpv6AddressMode() != null) {
+            result.setIpV6AddressMode(DHCPV6_MAP.get(subnet.getIpv6AddressMode()));
+        }
+        result.setEnableDHCP(subnet.isEnableDhcp());
+        if (subnet.getAllocationPools() != null) {
+            List<NeutronSubnetIPAllocationPool> allocationPools = new ArrayList<NeutronSubnetIPAllocationPool>();
+            for (AllocationPools allocationPool : subnet.getAllocationPools()) {
+                NeutronSubnetIPAllocationPool pool = new NeutronSubnetIPAllocationPool();
+                pool.setPoolStart(allocationPool.getStart());
+                pool.setPoolEnd(allocationPool.getEnd());
+                allocationPools.add(pool);
+            }
+            result.setAllocationPools(allocationPools);
+        }
+        if (subnet.getDnsNameservers() != null) {
+            List<String> dnsNameServers = new ArrayList<String>();
+            for (IpAddress dnsNameServer : subnet.getDnsNameservers()) {
+                dnsNameServers.add(String.valueOf(dnsNameServer.getValue()));
+            }
+            result.setDnsNameservers(dnsNameServers);
+        }
+        result.setID(subnet.getUuid().getValue());
+// read through the ports and put the ones in this subnet into the internal
+// myPorts object.
+// @deprecated and will be removed in Boron
+        Set<NeutronPort> allPorts = new HashSet<NeutronPort>();
+        NeutronCRUDInterfaces interfaces = new NeutronCRUDInterfaces()
+            .fetchINeutronPortCRUD(this);
+        INeutronPortCRUD portIf = interfaces.getPortInterface();
+        for (NeutronPort port : portIf.getAllPorts()) {
+            if (port.getFixedIPs() != null) {
+                for (Neutron_IPs ip : port.getFixedIPs()) {
+                    if (ip.getSubnetUUID().equals(result.getID())) {
+                        allPorts.add(port);
+                    }
+                }
+            }
+        }
+        List<NeutronPort> ports = new ArrayList<NeutronPort>();
+        ports.addAll(allPorts);
+        result.setPorts(ports);
+        return result;
+    }
+
+    protected Subnet toMd(NeutronSubnet subnet) {
+        SubnetBuilder subnetBuilder = new SubnetBuilder();
+        if (subnet.getName() != null) {
+            subnetBuilder.setName(subnet.getName());
+        }
+        if (subnet.getTenantID() != null) {
+            subnetBuilder.setTenantId(toUuid(subnet.getTenantID()));
+        }
+        if (subnet.getNetworkUUID() != null) {
+            subnetBuilder.setNetworkId(toUuid(subnet.getNetworkUUID()));
+        }
+        if (subnet.getIpVersion() != null) {
+            ImmutableBiMap<Integer, Class<? extends IpVersionBase>> mapper =
+                    IPV_MAP.inverse();
+            subnetBuilder.setIpVersion((Class<? extends IpVersionBase>) mapper.get(subnet
+                    .getIpVersion()));
+        }
+        if (subnet.getCidr() != null) {
+            subnetBuilder.setCidr(subnet.getCidr());
+        }
+        if (subnet.getGatewayIP() != null) {
+            IpAddress ipAddress = new IpAddress(subnet.getGatewayIP()
+                    .toCharArray());
+            subnetBuilder.setGatewayIp(ipAddress);
+        }
+        if (subnet.getIpV6RaMode() != null) {
+            ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper =
+                    DHCPV6_MAP.inverse();
+            subnetBuilder.setIpv6RaMode((Class<? extends Dhcpv6Base>) mapper.get(subnet.getIpV6RaMode()));
+        }
+        if (subnet.getIpV6AddressMode() != null) {
+            ImmutableBiMap<String, Class<? extends Dhcpv6Base>> mapper =
+                    DHCPV6_MAP.inverse();
+            subnetBuilder.setIpv6AddressMode((Class<? extends Dhcpv6Base>) mapper.get(subnet.getIpV6AddressMode()));
+        }
+        subnetBuilder.setEnableDhcp(subnet.getEnableDHCP());
+        if (subnet.getAllocationPools() != null) {
+            List<AllocationPools> allocationPools = new ArrayList<AllocationPools>();
+            for (NeutronSubnetIPAllocationPool allocationPool : subnet
+                    .getAllocationPools()) {
+                AllocationPoolsBuilder builder = new AllocationPoolsBuilder();
+                builder.setStart(allocationPool.getPoolStart());
+                builder.setEnd(allocationPool.getPoolEnd());
+                AllocationPools temp = builder.build();
+                allocationPools.add(temp);
+            }
+            subnetBuilder.setAllocationPools(allocationPools);
+        }
+        if (subnet.getDnsNameservers() != null) {
+            List<IpAddress> dnsNameServers = new ArrayList<IpAddress>();
+            for (String dnsNameServer : subnet.getDnsNameservers()) {
+                IpAddress ipAddress = new IpAddress(dnsNameServer.toCharArray());
+                dnsNameServers.add(ipAddress);
+            }
+            subnetBuilder.setDnsNameservers(dnsNameServers);
+        }
+        if (subnet.getID() != null) {
+            subnetBuilder.setUuid(toUuid(subnet.getID()));
+        } else {
+            LOGGER.warn("Attempting to write neutron subnet without UUID");
+        }
+        return subnetBuilder.build();
+    }
+
+    @Override
+    protected InstanceIdentifier<Subnet> createInstanceIdentifier(Subnet subnet) {
+        return InstanceIdentifier.create(Neutron.class).child(Subnets.class)
+                .child(Subnet.class, subnet.getKey());
+    }
+
+    @Override
+    protected InstanceIdentifier<Subnets> createInstanceIdentifier() {
+        return InstanceIdentifier.create(Neutron.class)
+                .child(Subnets.class);
+    }
+
+    @Override
+    protected Subnet toMd(String uuid) {
+        SubnetBuilder subnetBuilder = new SubnetBuilder();
+        subnetBuilder.setUuid(toUuid(uuid));
+        return subnetBuilder.build();
+    }
+
+    public static void registerNewInterface(BundleContext context,
+                                            ProviderContext providerContext,
+                                            List<ServiceRegistration<?>> registrations) {
+        NeutronSubnetInterface neutronSubnetInterface = new NeutronSubnetInterface(providerContext);
+        ServiceRegistration<INeutronSubnetCRUD> neutronSubnetInterfaceRegistration = context.registerService(INeutronSubnetCRUD.class, neutronSubnetInterface, null);
+        if(neutronSubnetInterfaceRegistration != null) {
+            registrations.add(neutronSubnetInterfaceRegistration);
         }
-        NeutronSubnet target = subnetDB.get(subnetUUID);
-        return (target.getPortsInSubnet().size() > 0);
     }
 }