Remove Router ConcurrentHashMap 02/25302/3
authorRyan Moats <rmoats@us.ibm.com>
Fri, 14 Aug 2015 16:40:19 +0000 (11:40 -0500)
committerRyan Moats <rmoats@us.ibm.com>
Tue, 18 Aug 2015 13:26:53 +0000 (08:26 -0500)
Replace the CHM backing implemenation for routers
with the MD-SAL.

Change-Id: I2a7b02442f3fedde0e3b7a69ca3f362ada535b1b
Signed-off-by: Ryan Moats <rmoats@us.ibm.com>
model/src/main/yang/neutron-L3.yang
neutron-spi/src/main/java/org/opendaylight/neutron/spi/NeutronRouter.java
transcriber/src/main/java/org/opendaylight/neutron/transcriber/NeutronRouterInterface.java

index e1d8d9b0fefd75917f12546888073a38f436fdf1..e4c2fdbefb59c3309abe3f166c351e15adc3b3e1 100644 (file)
@@ -32,7 +32,7 @@ module neutron-L3 {
     }
 
     grouping L3-attributes {
-        leaf distribted {
+        leaf distributed {
             description "whether this router is distributed or not.";
             type boolean;
             default "false";
index ac54e61e17c30b52a92eac58a947300d5b6429e0..a05d981517efda21a369ef797503c188b36a4362 100644 (file)
@@ -189,6 +189,10 @@ public class NeutronRouter implements Serializable, INeutronObject {
         return ans;
     }
 
+    public void setInterfaces(Map<String, NeutronRouter_Interface> input) {
+        interfaces = input;
+    }
+
     public Map<String, NeutronRouter_Interface> getInterfaces() {
         return interfaces;
     }
index 616be7baca3ee736717aeb7b80dc1a3f98223794..7448a238092d2138f9658adeef098ba2fdcf2f2b 100644 (file)
@@ -15,19 +15,20 @@ import java.util.List;
 import java.util.Map;
 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.INeutronRouterCRUD;
+import org.opendaylight.neutron.spi.Neutron_IPs;
 import org.opendaylight.neutron.spi.NeutronRouter;
 import org.opendaylight.neutron.spi.NeutronRouter_Interface;
 import org.opendaylight.neutron.spi.NeutronRouter_NetworkReference;
+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.l3.rev141002.routers.attributes.Routers;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.Router;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.RouterBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.router.ExternalGatewayInfo;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.router.ExternalGatewayInfoBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.router.external_gateway_info.ExternalFixedIpsBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.router.Interfaces;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev141002.routers.attributes.routers.router.external_gateway_info.ExternalFixedIps;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150325.Neutron;
@@ -39,7 +40,6 @@ import org.slf4j.LoggerFactory;
 
 public class NeutronRouterInterface extends  AbstractNeutronInterface<Router, NeutronRouter> implements INeutronRouterCRUD {
     private static final Logger LOGGER = LoggerFactory.getLogger(NeutronRouterInterface.class);
-    private ConcurrentMap<String, NeutronRouter> routerDB  = new ConcurrentHashMap<String, NeutronRouter>();
     // methods needed for creating caches
 
 
@@ -52,23 +52,27 @@ public class NeutronRouterInterface extends  AbstractNeutronInterface<Router, Ne
 
     @Override
     public boolean routerExists(String uuid) {
-        return routerDB.containsKey(uuid);
+        Router router = readMd(createInstanceIdentifier(toMd(uuid)));
+        return router != null;
     }
 
     @Override
     public NeutronRouter getRouter(String uuid) {
-        if (!routerExists(uuid)) {
+        Router router = readMd(createInstanceIdentifier(toMd(uuid)));
+        if (router == null) {
             return null;
         }
-        return routerDB.get(uuid);
+        return fromMd(router);
     }
 
     @Override
     public List<NeutronRouter> getAllRouters() {
         Set<NeutronRouter> allRouters = new HashSet<NeutronRouter>();
-        for (Entry<String, NeutronRouter> entry : routerDB.entrySet()) {
-            NeutronRouter router = entry.getValue();
-            allRouters.add(router);
+        Routers routers = readMd(createInstanceIdentifier());
+        if (routers != null) {
+            for (Router router: routers.getRouter()) {
+                allRouters.add(fromMd(router));
+            }
         }
         LOGGER.debug("Exiting getAllRouters, Found {} Routers", allRouters.size());
         List<NeutronRouter> ans = new ArrayList<NeutronRouter>();
@@ -81,7 +85,6 @@ public class NeutronRouterInterface extends  AbstractNeutronInterface<Router, Ne
         if (routerExists(input.getID())) {
             return false;
         }
-        routerDB.putIfAbsent(input.getID(), input);
         addMd(input);
         return true;
     }
@@ -91,9 +94,7 @@ public class NeutronRouterInterface extends  AbstractNeutronInterface<Router, Ne
         if (!routerExists(uuid)) {
             return false;
         }
-        routerDB.remove(uuid);
-        removeMd(toMd(uuid));
-        return true;
+        return removeMd(toMd(uuid));
     }
 
     @Override
@@ -101,7 +102,6 @@ public class NeutronRouterInterface extends  AbstractNeutronInterface<Router, Ne
         if (!routerExists(uuid)) {
             return false;
         }
-        routerDB.put(uuid,delta);
         updateMd(delta);
         return true;
     }
@@ -111,7 +111,7 @@ public class NeutronRouterInterface extends  AbstractNeutronInterface<Router, Ne
         if (!routerExists(routerUUID)) {
             return true;
         }
-        NeutronRouter target = routerDB.get(routerUUID);
+        NeutronRouter target = getRouter(routerUUID);
         return (target.getInterfaces().size() > 0);
     }
 
@@ -136,7 +136,7 @@ public class NeutronRouterInterface extends  AbstractNeutronInterface<Router, Ne
             routerBuilder.setGatewayPortId(toUuid(router.getGatewayPortId()));
         }
         routerBuilder.setAdminStateUp(router.getAdminStateUp());
-        routerBuilder.setDistribted(router.getDistributed());
+        routerBuilder.setDistributed(router.getDistributed());
         if (router.getRoutes() != null) {
             List<String> routes = new ArrayList<String>();
             for (String route : router.getRoutes()) {
@@ -152,11 +152,16 @@ public class NeutronRouterInterface extends  AbstractNeutronInterface<Router, Ne
                 ExternalGatewayInfoBuilder builder = new ExternalGatewayInfoBuilder();
                 builder.setEnableSnat(externalGatewayInfos.getEnableSNAT());
                 builder.setExternalNetworkId(toUuid(externalGatewayInfos.getNetworkID()));
-                List<ExternalFixedIps> externalFixedIps = new ArrayList<ExternalFixedIps>();
-                for (int i = 0; i < externalFixedIps.size(); i++) {
-                    externalFixedIps.add((ExternalFixedIps) externalGatewayInfos.getExternalFixedIPs().get(i));
+                if (externalGatewayInfos.getExternalFixedIPs() != null) {
+                    List<ExternalFixedIps> externalFixedIps = new ArrayList<ExternalFixedIps>();
+                    for (Neutron_IPs eIP : externalGatewayInfos.getExternalFixedIPs()) {
+                        ExternalFixedIpsBuilder eFixedIpBuilder = new ExternalFixedIpsBuilder();
+                        eFixedIpBuilder.setIpAddress(new IpAddress(eIP.getIpAddress().toCharArray()));
+                        eFixedIpBuilder.setSubnetId(toUuid(eIP.getSubnetUUID()));
+                        externalFixedIps.add(eFixedIpBuilder.build());
+                    }
+                    builder.setExternalFixedIps(externalFixedIps);
                 }
-                builder.setExternalFixedIps(externalFixedIps);
                 externalGatewayInfo = builder.build();
             }
             routerBuilder.setExternalGatewayInfo(externalGatewayInfo);
@@ -179,7 +184,13 @@ public class NeutronRouterInterface extends  AbstractNeutronInterface<Router, Ne
 
     @Override
     protected InstanceIdentifier<Router> createInstanceIdentifier(Router router) {
-        return InstanceIdentifier.create(Neutron.class).child(Routers.class).child(Router.class, router.getKey());
+        return InstanceIdentifier.create(Neutron.class)
+                 .child(Routers.class)
+                 .child(Router.class, router.getKey());
+    }
+
+    protected InstanceIdentifier<Routers> createInstanceIdentifier() {
+        return InstanceIdentifier.create(Neutron.class).child(Routers.class);
     }
 
     @Override
@@ -198,4 +209,56 @@ public class NeutronRouterInterface extends  AbstractNeutronInterface<Router, Ne
             registrations.add(neutronRouterInterfaceRegistration);
         }
     }
+
+    public NeutronRouter fromMd(Router router) {
+        NeutronRouter result = new NeutronRouter();
+        result.setID(String.valueOf(router.getUuid().getValue()));
+        result.setName(router.getName());
+        result.setTenantID(String.valueOf(router.getTenantId().getValue()));
+        result.setAdminStateUp(router.isAdminStateUp());
+        result.setStatus(router.getStatus());
+        result.setDistributed(router.isDistributed());
+        if (router.getGatewayPortId() != null) {
+            result.setGatewayPortId(String.valueOf(router.getGatewayPortId().getValue()));
+        }
+        if (router.getRoutes() != null) {
+            List<String> routes = new ArrayList<String>();
+            for (String route : router.getRoutes()) {
+                routes.add(route);
+            }
+            result.setRoutes(routes);
+        }
+
+        if (router.getExternalGatewayInfo() != null) {
+            NeutronRouter_NetworkReference extGwInfo = new NeutronRouter_NetworkReference();
+            extGwInfo.setNetworkID(String.valueOf(router.getExternalGatewayInfo().getExternalNetworkId().getValue()));
+            extGwInfo.setEnableSNAT(router.getExternalGatewayInfo().isEnableSnat());
+            if (router.getExternalGatewayInfo().getExternalFixedIps() != null) {
+                List<Neutron_IPs> fixedIPs = new ArrayList<Neutron_IPs>();
+                for (ExternalFixedIps mdFixedIP : router.getExternalGatewayInfo().getExternalFixedIps()) {
+                     Neutron_IPs fixedIP = new Neutron_IPs();
+                     fixedIP.setSubnetUUID(String.valueOf(mdFixedIP.getSubnetId().getValue()));
+                     fixedIP.setIpAddress(String.valueOf(mdFixedIP.getIpAddress().getValue()));
+                     fixedIPs.add(fixedIP);
+                }
+                extGwInfo.setExternalFixedIPs(fixedIPs);
+            }
+            result.setExternalGatewayInfo(extGwInfo);
+        }
+
+        if (router.getInterfaces() != null) {
+            Map<String, NeutronRouter_Interface> interfaces = new HashMap<String, NeutronRouter_Interface>();
+            for (Interfaces mdInterface : router.getInterfaces()) {
+                NeutronRouter_Interface pojoInterface = new NeutronRouter_Interface();
+                String id = String.valueOf(mdInterface.getUuid().getValue());
+                pojoInterface.setID(id);
+                pojoInterface.setTenantID(String.valueOf(mdInterface.getTenantId().getValue()));
+                pojoInterface.setSubnetUUID(String.valueOf(mdInterface.getSubnetId().getValue()));
+                pojoInterface.setPortUUID(String.valueOf(mdInterface.getPortId().getValue()));
+                interfaces.put(id, pojoInterface);
+            }
+            result.setInterfaces(interfaces);
+        }
+        return result;
+    }
 }