bgpvpn: simplify bgpvpn related code
[neutron.git] / transcriber / src / main / java / org / opendaylight / neutron / transcriber / NeutronRouterInterface.java
index e8102ff2c5d0c26a01022450a103076631f54c50..b4fa09bdf0257fcd2749c55091928122420aec41 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,36 +8,38 @@
 
 package org.opendaylight.neutron.transcriber;
 
-import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.HashSet;
 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.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.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;
+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.rev150712.routers.attributes.Routers;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.Router;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.RouterBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.ExternalGatewayInfo;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.ExternalGatewayInfoBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.external_gateway_info.ExternalFixedIpsBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.Interfaces;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.l3.rev150712.routers.attributes.routers.router.external_gateway_info.ExternalFixedIps;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
 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 NeutronRouterInterface extends  AbstractNeutronInterface<Router, NeutronRouter> implements INeutronRouterCRUD {
+public class NeutronRouterInterface extends AbstractNeutronInterface<Router, Routers, 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
 
 
@@ -45,101 +47,50 @@ public class NeutronRouterInterface extends  AbstractNeutronInterface<Router, Ne
         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;
-    }
-
 
     // IfNBRouterCRUD Interface methods
 
     @Override
     public boolean routerExists(String uuid) {
-        return routerDB.containsKey(uuid);
+        return exists(uuid);
     }
 
     @Override
     public NeutronRouter getRouter(String uuid) {
-        if (!routerExists(uuid)) {
-            return null;
-        }
-        return routerDB.get(uuid);
+        return get(uuid);
+    }
+
+    @Override
+    protected List<Router> getDataObjectList(Routers routers) {
+        return routers.getRouter();
     }
 
     @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);
-        }
-        LOGGER.debug("Exiting getAllRouters, Found {} Routers", allRouters.size());
-        List<NeutronRouter> ans = new ArrayList<NeutronRouter>();
-        ans.addAll(allRouters);
-        return ans;
+        return getAll();
     }
 
     @Override
     public boolean addRouter(NeutronRouter input) {
-        if (routerExists(input.getID())) {
-            return false;
-        }
-        routerDB.putIfAbsent(input.getID(), input);
-        addMd(input);
-        return true;
+        return add(input);
     }
 
     @Override
     public boolean removeRouter(String uuid) {
-        if (!routerExists(uuid)) {
-            return false;
-        }
-        routerDB.remove(uuid);
-        removeMd(toMd(uuid));
-        return true;
+        return remove(uuid);
     }
 
     @Override
     public boolean updateRouter(String uuid, NeutronRouter delta) {
-        if (!routerExists(uuid)) {
-            return false;
-        }
-        NeutronRouter target = routerDB.get(uuid);
-        boolean rc = overwrite(target, delta);
-        if (rc) {
-            updateMd(routerDB.get(uuid));
-        }
-        return rc;
+        return update(uuid, delta);
     }
 
     @Override
     public boolean routerInUse(String routerUUID) {
-        if (!routerExists(routerUUID)) {
+        if (!exists(routerUUID)) {
             return true;
         }
-        NeutronRouter target = routerDB.get(routerUUID);
+        NeutronRouter target = getRouter(routerUUID);
         return (target.getInterfaces().size() > 0);
     }
 
@@ -148,8 +99,8 @@ public class NeutronRouterInterface extends  AbstractNeutronInterface<Router, Ne
 
         RouterBuilder routerBuilder = new RouterBuilder();
 
-        if (router.getRouterUUID() != null) {
-            routerBuilder.setUuid(toUuid(router.getRouterUUID()));
+        if (router.getID() != null) {
+            routerBuilder.setUuid(toUuid(router.getID()));
         }
         if (router.getName() != null) {
             routerBuilder.setName(router.getName());
@@ -164,7 +115,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()) {
@@ -180,17 +131,22 @@ 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);
         }
         if (router.getInterfaces() != null) {
-            HashMap<String, NeutronRouter_Interface> mapInterfaces = new HashMap<String, NeutronRouter_Interface>();
+            Map<String, NeutronRouter_Interface> mapInterfaces = new HashMap<String, NeutronRouter_Interface>();
             List<Interfaces> interfaces = new ArrayList<Interfaces>();
             for (Entry<String, NeutronRouter_Interface> entry : mapInterfaces.entrySet()) {
                 interfaces.add((Interfaces) entry.getValue());
@@ -207,7 +163,14 @@ 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());
+    }
+
+    @Override
+    protected InstanceIdentifier<Routers> createInstanceIdentifier() {
+        return InstanceIdentifier.create(Neutron.class).child(Routers.class);
     }
 
     @Override
@@ -216,4 +179,66 @@ public class NeutronRouterInterface extends  AbstractNeutronInterface<Router, Ne
         routerBuilder.setUuid(toUuid(uuid));
         return routerBuilder.build();
     }
+
+    public static void registerNewInterface(BundleContext context,
+                                            ProviderContext providerContext,
+                                            List<ServiceRegistration<?>> registrations) {
+        NeutronRouterInterface neutronRouterInterface = new NeutronRouterInterface(providerContext);
+        ServiceRegistration<INeutronRouterCRUD> neutronRouterInterfaceRegistration = context.registerService(INeutronRouterCRUD.class, neutronRouterInterface, null);
+        if(neutronRouterInterfaceRegistration != null) {
+            registrations.add(neutronRouterInterfaceRegistration);
+        }
+    }
+
+    public NeutronRouter fromMd(Router router) {
+        NeutronRouter result = new NeutronRouter();
+        result.setID(router.getUuid().getValue());
+        result.setName(router.getName());
+        result.setTenantID(router.getTenantId());
+        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 = mdInterface.getUuid().getValue();
+                pojoInterface.setID(id);
+                pojoInterface.setTenantID(mdInterface.getTenantId());
+                pojoInterface.setSubnetUUID(String.valueOf(mdInterface.getSubnetId().getValue()));
+                pojoInterface.setPortUUID(String.valueOf(mdInterface.getPortId().getValue()));
+                interfaces.put(id, pojoInterface);
+            }
+            result.setInterfaces(interfaces);
+        }
+        return result;
+    }
 }