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;
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;
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;
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;
}
}
*/
@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);
}
}
*
* @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.
*
* @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 {
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();
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) {
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;
}
/**
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) {
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;
}
}
}
}
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);
}
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;
}
}
*/
@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..");
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