Cleanup mappingservice.yangmodel
[lispflowmapping.git] / mappingservice / neutron / src / main / java / org / opendaylight / lispflowmapping / neutron / LispNeutronSubnetHandler.java
index 9112bd7839e22a1f89de10928a8b82c01c2b209d..03ab6f90d6d10b2a6e1c18b95288ae2daaffe07a 100644 (file)
 
 package org.opendaylight.lispflowmapping.neutron;
 
-
 import java.net.HttpURLConnection;
 
+import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.apache.commons.net.util.SubnetUtils;
 import org.apache.commons.net.util.SubnetUtils.SubnetInfo;
+import org.opendaylight.lispflowmapping.lisp.util.LispAFIConvertor;
 import org.opendaylight.neutron.spi.INeutronSubnetAware;
 import org.opendaylight.neutron.spi.NeutronSubnet;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.LispAFIAddress;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainer;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.LispAddressContainerBuilder;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.Address;
-import org.opendaylight.yang.gen.v1.lispflowmapping.rev131031.lispaddress.lispaddresscontainer.address.Ipv4Builder;
-import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.LispAFIAddress;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev150820.lispaddress.LispAddressContainer;
 
 /**
- * Lisp Service implementation of NeutronSubnetAware API
- * Creation of a new Subnet results in defining the subnet as an EID prefix
- * in the LISP Mapping System with subnet's network UUID as the key to use
- * for registering mappings for the subnet.
+ * Lisp Service implementation of NeutronSubnetAware API Creation of a new
+ * Subnet results in defining the subnet as an EID prefix in the LISP Mapping
+ * System with subnet's network UUID as the key to use for registering mappings
+ * for the subnet.
+ *
+ * @author Vina Ermagan
  *
  */
-public class LispNeutronSubnetHandler extends LispNeutronService implements INeutronSubnetAware {
+public class LispNeutronSubnetHandler extends LispNeutronService implements
+               INeutronSubnetAware {
+       private static final Integer SIX = Integer.valueOf(6);
 
-    // The implementation for each of these services is resolved by the OSGi Service Manager
-    private volatile ILispNeutronService lispNeutronService;
+       // The implementation for each of these services is resolved by the OSGi
+       // Service Manager
+       private volatile ILispNeutronService lispNeutronService;
 
        @Override
        public int canCreateSubnet(NeutronSubnet subnet) {
-               logger.info("Neutron canCreateSubnet : Subnet name: " + subnet.getName() + " Subnet Cidr: " +
-                       subnet.getCidr());
-        logger.debug("Lisp Neutron Subnet: " + subnet.toString());
-
-        return HttpURLConnection.HTTP_OK;
+               LOG.info("Neutron canCreateSubnet : Subnet name: " + subnet.getName()
+                               + " Subnet Cidr: " + subnet.getCidr());
+               LOG.debug("Lisp Neutron Subnet: " + subnet.toString());
+               if (SIX.equals(subnet.getIpVersion())) {
+                       return HttpURLConnection.HTTP_PARTIAL;
+               }
+               return HttpURLConnection.HTTP_OK;
        }
 
        /**
         * Method adds the newly created subnet as an EID prefix to the
-        * MappingService. The subnet's network UUID is used as the key
-        * for this EID prefix.
+        * MappingService. The subnet's network UUID is used as the key for this EID
+        * prefix.
         *
         */
        @Override
        public void neutronSubnetCreated(NeutronSubnet subnet) {
-               //TODO update for multi-tenancy
+               // TODO update for multi-tenancy
 
-               logger.info("Neutron Subnet Created request : Subnet name: " + subnet.getName() + " Subnet Cidr: " +
-                       subnet.getCidr());
-        logger.debug("Lisp Neutron Subnet: " + subnet.toString());
+               LOG.info("Neutron Subnet Created request : Subnet name: "
+                               + subnet.getName() + " Subnet Cidr: " + subnet.getCidr());
+               LOG.debug("Lisp Neutron Subnet: " + subnet.toString());
 
-           int masklen = Integer.parseInt(subnet.getCidr().split("/")[1]);
-           SubnetUtils util = new SubnetUtils(subnet.getCidr());
-        SubnetInfo info = util.getInfo();
+               int masklen = Integer.parseInt(subnet.getCidr().split("/")[1]);
+               SubnetUtils util = new SubnetUtils(subnet.getCidr());
+               SubnetInfo info = util.getInfo();
 
-        // Determine the IANA code for the subnet IP version
-        // Default is set to IPv4 for neutron subnets
+               // Determine the IANA code for the subnet IP version
+               // Default is set to IPv4 for neutron subnets
 
-        short ianaCode = 1;
-        if ( (new Integer(6)).equals(subnet.getIpVersion()) )
-               ianaCode = 2;
+               LispAFIAddress lispAddress = LispAFIConvertor.asIPAfiAddress(info.getNetworkAddress());
+               LispAddressContainer addressContainer = LispAFIConvertor.toContainer(lispAddress);
 
-        LispAFIAddress lispAddress = new Ipv4Builder().setIpv4Address(new Ipv4Address(info.getNetworkAddress())).setAfi(ianaCode).build();
-        LispAddressContainer addressContainer =        new LispAddressContainerBuilder().setAddress((Address) lispAddress).build();
+               try {
 
-        try{
-               lispNeutronService.getMappingService().addAuthenticationKey(addressContainer, masklen, subnet.getNetworkUUID());
+            lispNeutronService.getMappingDbService().addKey((LispUtil.buildAddKeyInput(addressContainer,subnet.getNetworkUUID(), masklen)));
 
-               logger.debug("Neutron Subnet Added to MapServer : Subnet name: " + subnet.getName() + " EID Prefix: " +
-                       addressContainer.toString() + " Key: " + subnet.getNetworkUUID());
-        }
-        catch (Exception e){
-               logger.error("Adding new subnet to lisp service mapping service failed. Subnet : " + subnet.toString());
-        }
+                       LOG.debug("Neutron Subnet Added to MapServer : Subnet name: "
+                                       + subnet.getName() + " EID Prefix: "
+                                       + addressContainer.toString() + " Key: "
+                                       + subnet.getNetworkUUID());
+               } catch (Exception e) {
+                       LOG.error("Adding new subnet to lisp service mapping service failed. Subnet : "
+                                       + subnet.toString() + "Error: " + ExceptionUtils.getStackTrace(e));
+               }
+        LOG.info("Neutron Subnet Created request : Subnet name: "
+                + subnet.getName() + " Subnet Cidr: " + subnet.getCidr());
 
        }
 
        /**
-        * Method to check whether new Subnet can be created by LISP
-        * implementation of Neutron service API.
-        * Since we store the Cidr part of the subnet as the main key to the Lisp
-        * mapping service, we do not support updates to subnets that change it's cidr.
+        * Method to check whether new Subnet can be created by LISP implementation
+        * of Neutron service API. Since we store the Cidr part of the subnet as the
+        * main key to the Lisp mapping service, we do not support updates to
+        * subnets that change it's cidr.
         */
        @Override
        public int canUpdateSubnet(NeutronSubnet delta, NeutronSubnet original) {
-               if (delta == null || original == null){
-               logger.error("Neutron canUpdateSubnet rejected: subnet objects were null");
-               return HttpURLConnection.HTTP_BAD_REQUEST;
+               if (delta == null || original == null) {
+                       LOG.error("Neutron canUpdateSubnet rejected: subnet objects were null");
+                       return HttpURLConnection.HTTP_BAD_REQUEST;
                }
-               logger.info("Neutron canUpdateSubnet : Subnet name: " + original.getName() + " Subnet Cidr: " +
-                       original.getCidr());
-        logger.debug("Lisp Neutron Subnet update: original : " + original.toString() + " delta : " + delta.toString());
-
-
-               // We do not accept a Subnet update that changes the cidr. If cider or network UUID is changed, return error.
-               if ( !(original.getCidr().equals(delta.getCidr())) ){
-                       logger.error("Neutron canUpdateSubnet rejected: Subnet name: " + original.getName() + " Subnet Cidr: " +
-                               original.getCidr());
+               LOG.info("Neutron canUpdateSubnet : Subnet name: " + original.getName()
+                               + " Subnet Cidr: " + original.getCidr());
+               LOG.debug("Lisp Neutron Subnet update: original : "
+                               + original.toString() + " delta : " + delta.toString());
+
+               // We do not accept a Subnet update that changes the cidr. If cider or
+               // network UUID is changed, return error.
+               if (!(original.getCidr().equals(delta.getCidr()))) {
+                       LOG.error("Neutron canUpdateSubnet rejected: Subnet name: "
+                                       + original.getName() + " Subnet Cidr: "
+                                       + original.getCidr());
                        return HttpURLConnection.HTTP_CONFLICT;
                }
-        return HttpURLConnection.HTTP_OK;
+               return HttpURLConnection.HTTP_OK;
        }
 
        @Override
@@ -115,90 +122,53 @@ public class LispNeutronSubnetHandler extends LispNeutronService implements INeu
        }
 
        /**
-        * Method to check if subnet can be deleted.
-        * Returns error only if subnet does not exist
-        * in the lisp mapping service.
+        * Method to check if subnet can be deleted. Returns error only if subnet
+        * does not exist in the lisp mapping service.
         */
        @Override
        public int canDeleteSubnet(NeutronSubnet subnet) {
-        logger.info("Neutron canDeleteSubnet : Subnet name: " + subnet.getName() + " Subnet Cidr: " +
-                       subnet.getCidr() + "Key: " + subnet.getNetworkUUID());
-        logger.debug("Lisp Neutron Subnet: " + subnet.toString());
-
-        int result;
-           int masklen = Integer.parseInt(subnet.getCidr().split("/")[1]);
-           SubnetUtils util = new SubnetUtils(subnet.getCidr());
-        SubnetInfo info = util.getInfo();
-
-        // Determine the IANA code for the subnet IP version
-        // Default is set to IPv4 for neutron subnets
-        short ianaCode = 1;
-        if ( (new Integer(6)).equals(subnet.getIpVersion()) )
-               ianaCode = 2;
-
-        LispAFIAddress lispAddress = new Ipv4Builder().setIpv4Address(new Ipv4Address(info.getNetworkAddress())).setAfi(ianaCode).build();
-        LispAddressContainer addressContainer = new LispAddressContainerBuilder().setAddress((Address) lispAddress).build();
-
-        // if subnet does not exist in MapServer, return error
-        try{
-               if (lispNeutronService.getMappingService().getAuthenticationKey(addressContainer, masklen) == null){
-
-                       logger.error("Neutron canDeleteSubnet rejected : Subnet does not exist: Subnet name: " +
-                                       subnet.getName() +
-                                       " Eid Prefix: " + addressContainer.toString() +
-                                       " Key: " + subnet.getNetworkUUID());
-                       return HttpURLConnection.HTTP_BAD_REQUEST;
-               }
-               result =  HttpURLConnection.HTTP_OK;
-        }
-        catch (Exception e){
-               logger.error("canDeleteSubnet request rejected. Subnet : " + subnet.toString());
-               result =  HttpURLConnection.HTTP_BAD_REQUEST;
-        }
-        return result;
+               LOG.info("Neutron canDeleteSubnet : Subnet name: " + subnet.getName()
+                               + " Subnet Cidr: " + subnet.getCidr() + "Key: "
+                               + subnet.getNetworkUUID());
+               LOG.debug("Lisp Neutron Subnet: " + subnet.toString());
+
+
+                       return HttpURLConnection.HTTP_OK;
        }
 
        /**
-        * Method removes the EID prefix and key associated
-        * with the deleted subnet from Lisp mapping service.
+        * Method removes the EID prefix and key associated with the deleted subnet
+        * from Lisp mapping service.
         */
        @Override
        public void neutronSubnetDeleted(NeutronSubnet subnet) {
-               logger.info("Neutron Subnet Deleted Request : Subnet name: " + subnet.getName() + " Subnet Cidr: " +
-                       subnet.getCidr() + "Key: " + subnet.getNetworkUUID());
-        logger.debug("Lisp Neutron Subnet: " + subnet.toString());
-
-        int masklen = Integer.parseInt(subnet.getCidr().split("/")[1]);
-           SubnetUtils util = new SubnetUtils(subnet.getCidr());
-        SubnetInfo info = util.getInfo();
-
-        // Determine the IANA code for the subnet IP version
-        // Default is set to IPv4 for neutron subnets
-
-        short ianaCode = 1;
-        if ( (new Integer(6)).equals(subnet.getIpVersion()) )
-               ianaCode = 2;
-
-        LispAFIAddress lispAddress = new Ipv4Builder().setIpv4Address(new Ipv4Address(info.getNetworkAddress())).setAfi(ianaCode).build();
-        LispAddressContainer addressContainer = new LispAddressContainerBuilder().setAddress((Address) lispAddress).build();
-
-        try{
-               // if subnet does not exist in MapServer, return error
-               if (lispNeutronService.getMappingService().getAuthenticationKey(addressContainer,masklen) == null){
-                   logger.error("Neutron Delete Subnet Failed: Subnet does not exist: Subnet name: " + subnet.getName() +
-                               " Eid Prefix: " + addressContainer.toString() +
-                               "Key: " + subnet.getNetworkUUID());
-                   return;
-               }
-               lispNeutronService.getMappingService().removeAuthenticationKey(addressContainer, masklen);
-
-               logger.debug("Neutron Subnet Deleted from MapServer : Subnet name: " + subnet.getName() +
-                               " Eid Prefix: " + addressContainer.toString() +
-                               " Key: " + subnet.getNetworkUUID());
-        }
-        catch (Exception e){
-               logger.error("Deleting subnet's EID prefix from mapping service failed + Subnet: " + subnet.toString());
-        }
+               LOG.info("Neutron Subnet Deleted Request : Subnet name: "
+                               + subnet.getName() + " Subnet Cidr: " + subnet.getCidr()
+                               + "Key: " + subnet.getNetworkUUID());
+               LOG.debug("Lisp Neutron Subnet: " + subnet.toString());
+
+               int masklen = Integer.parseInt(subnet.getCidr().split("/")[1]);
+               SubnetUtils util = new SubnetUtils(subnet.getCidr());
+               SubnetInfo info = util.getInfo();
+
+               // Determine the IANA code for the subnet IP version
+               // Default is set to IPv4 for neutron subnets
+
+        LispAFIAddress lispAddress = LispAFIConvertor.asIPAfiAddress(info.getNetworkAddress());
+        LispAddressContainer addressContainer = LispAFIConvertor.toContainer(lispAddress);
+
+        try {
+
+            lispNeutronService.getMappingDbService().removeKey(LispUtil.buildRemoveKeyInput(addressContainer, masklen));
+
+                       LOG.debug("Neutron Subnet Deleted from MapServer : Subnet name: "
+                                       + subnet.getName() + " Eid Prefix: "
+                                       + addressContainer.toString() + " Key: "
+                                       + subnet.getNetworkUUID());
+               } catch (Exception e) {
+                       LOG.error("Deleting subnet's EID prefix from mapping service failed + Subnet: "
+                                       + subnet.toString() + "Error: " + ExceptionUtils.getStackTrace(e));
+               }
        }
 
 }