code submission - M5 release
[plugin2oc.git] / neutron / src / main / java / org / opendaylight / plugin2oc / neutron / SubnetHandler.java
index d43b28480579d4015aa55e1e51d5de35083973b8..706c7fb42affe0cb0aefb93dc7f6a91a430bdd27 100755 (executable)
@@ -12,6 +12,7 @@ import java.io.IOException;
 import java.net.HttpURLConnection;
 import java.util.Iterator;
 import java.util.List;
+import java.util.UUID;
 
 import net.juniper.contrail.api.ApiConnector;
 import net.juniper.contrail.api.ObjectReference;
@@ -20,7 +21,6 @@ import net.juniper.contrail.api.types.SubnetType;
 import net.juniper.contrail.api.types.VirtualNetwork;
 import net.juniper.contrail.api.types.VnSubnetsType;
 import net.juniper.contrail.api.types.VnSubnetsType.IpamSubnetType;
-//import net.juniper.contrail.api.types.VnSubnetsType.IpamSubnetType.AllocationPoolType;
 
 import org.apache.commons.net.util.SubnetUtils;
 import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
@@ -66,8 +66,29 @@ public class SubnetHandler implements INeutronSubnetAware {
             LOGGER.error("Incorrect gateway IP....");
             return HttpURLConnection.HTTP_BAD_REQUEST;
         }
+        String networkUUID = subnet.getNetworkUUID();
+        String subnetUUID = subnet.getSubnetUUID();
         try {
-            virtualnetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, subnet.getNetworkUUID());
+            if (!(networkUUID.contains("-"))) {
+                networkUUID = Utils.uuidFormater(networkUUID);
+            }
+            if (!(subnetUUID.contains("-"))) {
+                subnetUUID = Utils.uuidFormater(subnetUUID);
+            }
+            boolean isValidNetworkUUID = Utils.isValidHexNumber(networkUUID);
+            boolean isValidSubnetUUID = Utils.isValidHexNumber(subnetUUID);
+            if (!isValidNetworkUUID || !isValidSubnetUUID) {
+                LOGGER.info("Badly formed Hexadecimal UUID...");
+                return HttpURLConnection.HTTP_BAD_REQUEST;
+            }
+            networkUUID = UUID.fromString(networkUUID).toString();
+            subnetUUID = UUID.fromString(subnetUUID).toString();
+        } catch (Exception ex) {
+            LOGGER.error("UUID input incorrect", ex);
+            return HttpURLConnection.HTTP_BAD_REQUEST;
+        }
+        try {
+            virtualnetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, networkUUID);
         } catch (IOException e) {
             LOGGER.error("Exception : " + e);
             return HttpURLConnection.HTTP_INTERNAL_ERROR;
@@ -82,14 +103,11 @@ public class SubnetHandler implements INeutronSubnetAware {
                     LOGGER.error("The subnet already exists..");
                     return HttpURLConnection.HTTP_FORBIDDEN;
                 }
-                return createSubnet(subnet, virtualnetwork);
-            } catch (IOException ie) {
-                LOGGER.error("IOException:     " + ie);
-                return HttpURLConnection.HTTP_INTERNAL_ERROR;
             } catch (Exception e) {
                 LOGGER.error("Exception:  " + e);
                 return HttpURLConnection.HTTP_INTERNAL_ERROR;
             }
+            return HttpURLConnection.HTTP_OK;
         }
     }
 
@@ -122,15 +140,27 @@ public class SubnetHandler implements INeutronSubnetAware {
      */
     @Override
     public void neutronSubnetCreated(NeutronSubnet subnet) {
-        VirtualNetwork virtualNetwork = null;
+        String networkUUID = subnet.getNetworkUUID();
+        String subnetUUID = subnet.getSubnetUUID();
         try {
-            virtualNetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, subnet.getNetworkUUID());
+            if (!(networkUUID.contains("-"))) {
+                networkUUID = Utils.uuidFormater(networkUUID);
+            }
+            networkUUID = UUID.fromString(networkUUID).toString();
+            if (!(subnetUUID.contains("-"))) {
+                subnetUUID = Utils.uuidFormater(subnetUUID);
+            }
+            subnetUUID = UUID.fromString(subnetUUID).toString();
+            createSubnet(subnet);
+            VirtualNetwork virtualNetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, networkUUID);
             boolean ifSubnetExists = subnetExists(virtualNetwork.getNetworkIpam(), subnet);
             if (ifSubnetExists) {
                 LOGGER.info("Subnet creation verified...");
             }
-        } catch (Exception e) {
-            LOGGER.error("Exception :    " + e);
+        } catch (IOException ie) {
+            LOGGER.error("IOException :   " + ie);
+        } catch (Exception ex) {
+            LOGGER.error("Exception :   ", ex);
         }
     }
 
@@ -144,20 +174,33 @@ public class SubnetHandler implements INeutronSubnetAware {
      *
      * @return A HTTP status code to the creation request.
      */
-    private int createSubnet(NeutronSubnet subnet, VirtualNetwork virtualNetwork) throws IOException {
+    private void createSubnet(NeutronSubnet subnet) throws IOException {
         // add subnet properties to the virtual-network object
-        VirtualNetwork virtualnetwork = mapSubnetProperties(subnet, virtualNetwork);
-        boolean subnetCreate = apiConnector.update(virtualnetwork);
-        if (!subnetCreate) {
-            LOGGER.warn("Subnet creation failed..");
-            return HttpURLConnection.HTTP_INTERNAL_ERROR;
-        } else {
-            LOGGER.info("Subnet " + subnet.getCidr() + " sucessfully added to the network having UUID : " + virtualnetwork.getUuid());
-            return HttpURLConnection.HTTP_OK;
+        String networkUUID = subnet.getNetworkUUID();
+        try {
+            if (!(networkUUID.contains("-"))) {
+                networkUUID = Utils.uuidFormater(networkUUID);
+            }
+            networkUUID = UUID.fromString(networkUUID).toString();
+        } catch (Exception ex) {
+            LOGGER.error("UUID input incorrect", ex);
+        }
+        try {
+            VirtualNetwork virtualNetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, networkUUID);
+            virtualNetwork = mapSubnetProperties(subnet, virtualNetwork);
+            boolean subnetCreate = apiConnector.update(virtualNetwork);
+            if (!subnetCreate) {
+                LOGGER.warn("Subnet creation failed..");
+            } else {
+                LOGGER.info("Subnet " + subnet.getCidr() + " sucessfully added to the network having UUID : " + virtualNetwork.getUuid());
+            }
+        } catch (IOException ioEx) {
+            LOGGER.error("IOException   : ", ioEx);
+        } catch (Exception ex) {
+            LOGGER.error("IOException   : ", ex);
         }
     }
 
-
     /**
      * Invoked to add the NeutronSubnet properties to the virtualNetwork object.
      *
@@ -169,11 +212,20 @@ public class SubnetHandler implements INeutronSubnetAware {
      * @return {@link VirtualNetwork}
      */
     private VirtualNetwork mapSubnetProperties(NeutronSubnet subnet, VirtualNetwork vn) {
+        String subnetUUID = subnet.getSubnetUUID();
         String[] ipPrefix = null;
         NetworkIpam ipam = null;
         VnSubnetsType vnSubnetsType = new VnSubnetsType();
         SubnetType subnetType = new SubnetType();
         try {
+            try {
+                if (!(subnetUUID.contains("-"))) {
+                    subnetUUID = Utils.uuidFormater(subnetUUID);
+                }
+                subnetUUID = UUID.fromString(subnetUUID).toString();
+            } catch (Exception ex) {
+                LOGGER.error("UUID input incorrect", ex);
+            }
             if (subnet.getCidr().contains("/")) {
                 ipPrefix = subnet.getCidr().split("/");
             } else {
@@ -190,14 +242,12 @@ public class SubnetHandler implements INeutronSubnetAware {
         if (ipPrefix != null) {
             subnetType.setIpPrefix(ipPrefix[0]);
             subnetType.setIpPrefixLen(Integer.valueOf(ipPrefix[1]));
-//            AllocationPoolType allocationPoolType = (AllocationPoolType) subnet.getAllocationPools();
             IpamSubnetType ipamSubnetType = new IpamSubnetType();
             ipamSubnetType.setSubnet(subnetType);
             ipamSubnetType.setDefaultGateway(subnet.getGatewayIP());
-            ipamSubnetType.setSubnetUuid(subnet.getSubnetUUID());
+            ipamSubnetType.setSubnetUuid(subnetUUID);
             ipamSubnetType.setSubnetName(subnet.getName());
             ipamSubnetType.setEnableDhcp(subnet.isEnableDHCP());
-//            ipamSubnetType.addAllocationPools(allocationPoolType);
             if (vn.getNetworkIpam() != null) {
                 for (ObjectReference<VnSubnetsType> ref : vn.getNetworkIpam()) {
                     vnSubnetsType = ref.getAttr();
@@ -247,20 +297,35 @@ public class SubnetHandler implements INeutronSubnetAware {
             LOGGER.error("Neutron Subnets can't be null..");
             return HttpURLConnection.HTTP_BAD_REQUEST;
         }
-//        if (deltaSubnet.getGatewayIP() == null || ("").equals(deltaSubnet.getGatewayIP().toString())) {
-//            LOGGER.error("Gateway IP can't be empty/null`..");
-//            return HttpURLConnection.HTTP_BAD_REQUEST;
-//        }
-        if (deltaSubnet.getGatewayIP() != null){
-        boolean isvalidGateway = validGatewayIP(originalSubnet, deltaSubnet.getGatewayIP());
-        if (!isvalidGateway) {
-            LOGGER.error("Incorrect gateway IP....");
-            return HttpURLConnection.HTTP_BAD_REQUEST;
-        }}
+        // if (deltaSubnet.getGatewayIP() == null ||
+        // ("").equals(deltaSubnet.getGatewayIP().toString())) {
+        // LOGGER.error("Gateway IP can't be empty/null`..");
+        // return HttpURLConnection.HTTP_BAD_REQUEST;
+        // }
+        if (deltaSubnet.getGatewayIP() != null) { // cant update gateway IP in
+                                                  // OpenContrail
+            if (!originalSubnet.getGatewayIP().matches(deltaSubnet.getGatewayIP())) {
+                // boolean isvalidGateway = validGatewayIP(originalSubnet,
+                // deltaSubnet.getGatewayIP());
+                // if (!isvalidGateway) {
+                LOGGER.error(" Cannot update gateway IP..");
+                return HttpURLConnection.HTTP_BAD_REQUEST;
+            }
+        }
         apiConnector = Activator.apiConnector;
+        VirtualNetwork virtualnetwork;
         try {
-            boolean ifSubnetExist = false;
-            VirtualNetwork virtualnetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, originalSubnet.getNetworkUUID());
+            String networkUUID = originalSubnet.getNetworkUUID();
+            String subnetUUID = originalSubnet.getSubnetUUID();
+            if (!(networkUUID.contains("-"))) {
+                networkUUID = Utils.uuidFormater(networkUUID);
+            }
+            networkUUID = UUID.fromString(networkUUID).toString();
+            if (!(subnetUUID.contains("-"))) {
+                subnetUUID = Utils.uuidFormater(subnetUUID);
+            }
+            subnetUUID = UUID.fromString(subnetUUID).toString();
+            virtualnetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, networkUUID);
             List<ObjectReference<VnSubnetsType>> ipamRefs = virtualnetwork.getNetworkIpam();
             if (ipamRefs != null) {
                 for (ObjectReference<VnSubnetsType> ref : ipamRefs) {
@@ -268,40 +333,23 @@ public class SubnetHandler implements INeutronSubnetAware {
                     if (vnSubnetsType != null) {
                         List<VnSubnetsType.IpamSubnetType> subnets = vnSubnetsType.getIpamSubnets();
                         for (VnSubnetsType.IpamSubnetType subnetValue : subnets) {
-                            boolean doesSubnetExist = subnetValue.getSubnetUuid().matches(originalSubnet.getSubnetUUID());
+                            boolean doesSubnetExist = subnetValue.getSubnetUuid().matches(subnetUUID);
                             if (doesSubnetExist) {
-                                if(deltaSubnet.getGatewayIP() != null){
-                                subnetValue.setDefaultGateway(deltaSubnet.getGatewayIP());
-                                }
-                                if(deltaSubnet.getEnableDHCP() != null){
-                                    subnetValue.setEnableDhcp(deltaSubnet.isEnableDHCP());
-                                }
-                                if(deltaSubnet.getName() != null){
-                                    subnetValue.setSubnetName(deltaSubnet.getName());
-                                }
-                                ifSubnetExist = true;
+                                return HttpURLConnection.HTTP_OK;
+                            } else {
+                                LOGGER.warn(" No subnet exists for specified UUID..");
+                                return HttpURLConnection.HTTP_BAD_REQUEST;
                             }
                         }
                     }
                 }
             }
-            if (ifSubnetExist) {
-                boolean subnetUpdate = apiConnector.update(virtualnetwork);
-                if (!subnetUpdate) {
-                    LOGGER.warn("Subnet upadtion failed..");
-                    return HttpURLConnection.HTTP_INTERNAL_ERROR;
-                } else {
-                    LOGGER.info(" Subnet " + originalSubnet.getCidr() + " sucessfully updated. ");
-                    return HttpURLConnection.HTTP_OK;
-                }
-            } else {
-                LOGGER.warn("Subnet upadtion failed..");
-                return HttpURLConnection.HTTP_INTERNAL_ERROR;
-            }
         } catch (IOException e) {
             LOGGER.error("Exception :     " + e);
             return HttpURLConnection.HTTP_INTERNAL_ERROR;
         }
+        LOGGER.warn("Subnet updation failed..");
+        return HttpURLConnection.HTTP_BAD_REQUEST;
     }
 
     /**
@@ -314,7 +362,17 @@ public class SubnetHandler implements INeutronSubnetAware {
     public void neutronSubnetUpdated(NeutronSubnet subnet) {
         try {
             boolean ifSubnetExist = false;
-            VirtualNetwork virtualnetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, subnet.getNetworkUUID());
+            String networkUUID = subnet.getNetworkUUID();
+            String subnetUUID = subnet.getSubnetUUID();
+            if (!(networkUUID.contains("-"))) {
+                networkUUID = Utils.uuidFormater(networkUUID);
+            }
+            networkUUID = UUID.fromString(networkUUID).toString();
+            if (!(subnetUUID.contains("-"))) {
+                subnetUUID = Utils.uuidFormater(subnetUUID);
+            }
+            subnetUUID = UUID.fromString(subnetUUID).toString();
+            VirtualNetwork virtualnetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, networkUUID);
             List<ObjectReference<VnSubnetsType>> ipamRefs = virtualnetwork.getNetworkIpam();
             if (ipamRefs != null) {
                 for (ObjectReference<VnSubnetsType> ref : ipamRefs) {
@@ -322,8 +380,19 @@ public class SubnetHandler implements INeutronSubnetAware {
                     if (vnSubnetsType != null) {
                         List<VnSubnetsType.IpamSubnetType> subnets = vnSubnetsType.getIpamSubnets();
                         for (VnSubnetsType.IpamSubnetType subnetValue : subnets) {
-                            boolean doesSubnetExist = subnetValue.getDefaultGateway().matches(subnet.getGatewayIP());
+                            boolean doesSubnetExist = subnetValue.getSubnetUuid().matches(subnetUUID);
                             if (doesSubnetExist) {
+                                // if(deltaSubnet.getGatewayIP() != null){
+                                // //Cannot update default gateway, enableDHCP
+                                // and cidr
+                                // subnetValue.setDefaultGateway(deltaSubnet.getGatewayIP());
+                                // }
+                                // if(deltaSubnet.getEnableDHCP() != null){
+                                // subnetValue.setEnableDhcp(deltaSubnet.isEnableDHCP());
+                                // }
+                                if (subnet.getName() != null) {
+                                    subnetValue.setSubnetName(subnet.getName());
+                                }
                                 ifSubnetExist = true;
                             }
                         }
@@ -331,10 +400,33 @@ public class SubnetHandler implements INeutronSubnetAware {
                 }
             }
             if (ifSubnetExist) {
-                LOGGER.info("Subnet upadtion verified..");
+                boolean subnetUpdate = apiConnector.update(virtualnetwork);
+                if (!subnetUpdate) {
+                    LOGGER.warn("Subnet upadtion failed..");
+                } else {
+                    LOGGER.info(" Subnet " + subnet.getCidr() + " has been sucessfully updated. ");
+                }
             } else {
                 LOGGER.warn("Subnet upadtion failed..");
             }
+            virtualnetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, networkUUID);
+            List<ObjectReference<VnSubnetsType>> ipamRef = virtualnetwork.getNetworkIpam();
+            if (ipamRef != null) {
+                for (ObjectReference<VnSubnetsType> ref : ipamRef) {
+                    VnSubnetsType vnSubnetsType = ref.getAttr();
+                    if (vnSubnetsType != null) {
+                        List<VnSubnetsType.IpamSubnetType> subnets = vnSubnetsType.getIpamSubnets();
+                        for (VnSubnetsType.IpamSubnetType subnetValue : subnets) {
+                            boolean isSubnetUpdated = subnetValue.getSubnetName().matches(subnet.getName());
+                            if (isSubnetUpdated) {
+                                LOGGER.info("Subnet upadtion verified..");
+                            } else {
+                                LOGGER.warn("Subnet upadtion failed..");
+                            }
+                        }
+                    }
+                }
+            }
         } catch (Exception ex) {
             LOGGER.error("Exception :     " + ex);
         }
@@ -353,44 +445,34 @@ public class SubnetHandler implements INeutronSubnetAware {
     public int canDeleteSubnet(NeutronSubnet subnet) {
         apiConnector = Activator.apiConnector;
         VirtualNetwork virtualNetwork = null;
+        String networkUUID = subnet.getNetworkUUID();
+        String subnetUUID = subnet.getSubnetUUID();
         try {
-            virtualNetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, subnet.getNetworkUUID());
-            boolean subnetDelete = deleteSubnet(subnet, virtualNetwork);
-            if (!subnetDelete) {
-                LOGGER.error("Subnet deletion failed..");
-                return HttpURLConnection.HTTP_INTERNAL_ERROR;
-            } else {
-                LOGGER.info("Subnet " + subnet.getCidr() + " sucessfully deleted from network  : " + virtualNetwork.getUuid());
-                return HttpURLConnection.HTTP_NO_CONTENT;
+            if (!(networkUUID.contains("-"))) {
+                networkUUID = Utils.uuidFormater(networkUUID);
             }
-        } catch (Exception e) {
-            LOGGER.error("Exception :     ", e.getMessage());
-            return HttpURLConnection.HTTP_INTERNAL_ERROR;
-        }
-    }
-
-    boolean validGatewayIP(NeutronSubnet subnet, String ipAddress) {
-        try {
-
-            SubnetUtils util = new SubnetUtils(subnet.getCidr());
-            SubnetInfo info = util.getInfo();
-            boolean inRange = info.isInRange(ipAddress);
-            if (!inRange) {
-                return false;
-            } else {
-                // ip available in allocation pool
-                Iterator<NeutronSubnet_IPAllocationPool> i = subnet.getAllocationPools().iterator();
-                while (i.hasNext()) {
-                    NeutronSubnet_IPAllocationPool pool = i.next();
-                    if (pool.contains(ipAddress)) {
-                        return true;
-                    }
+            networkUUID = UUID.fromString(networkUUID).toString();
+            if (!(subnetUUID.contains("-"))) {
+                subnetUUID = Utils.uuidFormater(subnetUUID);
+            }
+            subnetUUID = UUID.fromString(subnetUUID).toString();
+            virtualNetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, networkUUID);
+            boolean doesSubnetExist = subnetExists(virtualNetwork.getNetworkIpam(), subnet);
+            if(virtualNetwork.getNetworkIpam()!=null){
+                if (virtualNetwork.getNetworkIpam().get(0).getAttr().getIpamSubnets().size() == 1 && virtualNetwork.getFloatingIpPools() != null) {
+                    LOGGER.error("Cannot Delete subnet / IP Block, Floating Pool(s) in use...");
+                    return HttpURLConnection.HTTP_BAD_REQUEST;
                 }
-                return true;
+                }
+            if (!doesSubnetExist) {
+                LOGGER.error("No subnet exists with specified UUID...");
+                return HttpURLConnection.HTTP_BAD_REQUEST;
+            } else {
+                return HttpURLConnection.HTTP_OK;
             }
-        } catch (Exception e) {
-            LOGGER.error("Exception  :  " + e);
-            return false;
+        } catch (Exception ex) {
+            LOGGER.error("Exception :     " + ex);
+            return HttpURLConnection.HTTP_INTERNAL_ERROR;
         }
     }
 
@@ -461,8 +543,27 @@ public class SubnetHandler implements INeutronSubnetAware {
      */
     @Override
     public void neutronSubnetDeleted(NeutronSubnet subnet) {
+        String networkUUID = subnet.getNetworkUUID();
+        String subnetUUID = subnet.getSubnetUUID();
+        VirtualNetwork virtualNetwork;
         try {
-            VirtualNetwork virtualNetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, subnet.getNetworkUUID());
+            if (!(networkUUID.contains("-"))) {
+                networkUUID = Utils.uuidFormater(networkUUID);
+            }
+            networkUUID = UUID.fromString(networkUUID).toString();
+            if (!(subnetUUID.contains("-"))) {
+                subnetUUID = Utils.uuidFormater(subnetUUID);
+            }
+            subnetUUID = UUID.fromString(subnetUUID).toString();
+            virtualNetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, networkUUID);
+            boolean subnetDelete = deleteSubnet(subnet, virtualNetwork);
+            if (!subnetDelete) {
+                LOGGER.error("Subnet deletion failed..");
+            } else {
+                LOGGER.info("Subnet " + subnet.getCidr() + " sucessfully deleted from network  : " + virtualNetwork.getUuid());
+            }
+
+            virtualNetwork = (VirtualNetwork) apiConnector.findById(VirtualNetwork.class, networkUUID);
             boolean ifSubnetExist = subnetExists(virtualNetwork.getNetworkIpam(), subnet);
             if (!ifSubnetExist) {
                 LOGGER.info("Subnet deletion verified..");
@@ -473,4 +574,29 @@ public class SubnetHandler implements INeutronSubnetAware {
             LOGGER.error("Exception :    " + ex);
         }
     }
-}
+
+    boolean validGatewayIP(NeutronSubnet subnet, String ipAddress) {
+        try {
+
+            SubnetUtils util = new SubnetUtils(subnet.getCidr());
+            SubnetInfo info = util.getInfo();
+            boolean inRange = info.isInRange(ipAddress);
+            if (!inRange) {
+                return false;
+            } else {
+                // ip available in allocation pool
+                Iterator<NeutronSubnet_IPAllocationPool> i = subnet.getAllocationPools().iterator();
+                while (i.hasNext()) {
+                    NeutronSubnet_IPAllocationPool pool = i.next();
+                    if (pool.contains(ipAddress)) {
+                        return true;
+                    }
+                }
+                return true;
+            }
+        } catch (Exception e) {
+            LOGGER.error("Exception  :  " + e);
+            return false;
+        }
+    }
+}
\ No newline at end of file