Skip to content

Commit

Permalink
Implement CIDR allocation/deallocation logic
Browse files Browse the repository at this point in the history
What is in this changelist:
1. CIDR allocation/deallocation logic
2. Deallocate CIDR upon compute network removal.
3. Unit tests.

What is not in this changelist:
1. Support for overlapping subnet CIDRs (will come in the next changelist).

Change-Id: I489204d87693e4434f26868412c3e9ea2ab65a31
Reviewed-on: http://bellevue-ci.eng.vmware.com:8080/8631
Compute-Verified: jenkins <[email protected]>
Upgrade-Verified: jenkins <[email protected]>
Closures-Verified: jenkins <[email protected]>
Bellevue-Verified: jenkins <[email protected]>
Reviewed-by: Rostislav Georgiev <[email protected]>
CS-Verified: jenkins <[email protected]>
  • Loading branch information
Sapata82 committed Apr 10, 2017
1 parent 68e4ddd commit 5ecf549
Show file tree
Hide file tree
Showing 9 changed files with 837 additions and 209 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,32 @@ protected <T extends ServiceDocument> T doPatch(T inState, String serviceDocumen
return outState;
}

protected <T, R extends ServiceDocument> R doPatch(
T request,
Class<R> resultClazz,
String serviceDocumentSelfLink) {

TestContext ctx = testCreate(1);

host.sendWithDeferredResult(Operation.createPatch(host, serviceDocumentSelfLink)
.setBody(request), resultClazz)
.whenComplete((result, ex) -> {
if (ex != null) {
ctx.failIteration(ex);
return;
}
ctx.completeIteration();
});
ctx.await();

URI uri = UriUtils.buildUri(host, serviceDocumentSelfLink);

R outState = host.getServiceState(null,
resultClazz,
uri);
return outState;
}

protected <T extends ServiceDocument> T doPut(T inState)
throws Throwable {
URI uri = UriUtils.buildUri(host, inState.documentSelfLink);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public static class NetworkProfile extends MultiTenantDocument {
@PropertyOptions(usage = { OPTIONAL })
public String isolationNetworkCIDRAllocationLink;

@Documentation(description = "The CIDR prefix length to be used for the isolated subnets "
+ "to be created (example: Subnet with CIDR 192.168.0.0/20 has CIDR prefix "
+ "length: 20.")
public Integer isolatedSubnetCIDRPrefix;

/**
* Defines the isolation network support this network profile provides.
*/
Expand All @@ -101,6 +106,7 @@ public void copyTo(MultiTenantDocument target) {
targetState.isolationNetworkLink = this.isolationNetworkLink;
targetState.isolationNetworkCIDRAllocationLink =
this.isolationNetworkCIDRAllocationLink;
targetState.isolatedSubnetCIDRPrefix = this.isolatedSubnetCIDRPrefix;
}
}
}
Expand Down Expand Up @@ -231,6 +237,14 @@ public void handlePatch(Operation patch) {
} else {
completion = DeferredResult.completed(currentState);
}

if (newState.isolatedSubnetCIDRPrefix != null
&& !newState.isolatedSubnetCIDRPrefix.equals(
currentState.isolatedSubnetCIDRPrefix)) {

currentState.isolatedSubnetCIDRPrefix = newState.isolatedSubnetCIDRPrefix;
completion = completion.thenCompose(this::updateIsolationSubnetCIDRPrefix);
}
} catch (NoSuchFieldException | IllegalAccessException e) {
patch.fail(e);
return;
Expand Down Expand Up @@ -266,7 +280,7 @@ private NetworkProfile processInput(Operation op) {
return state;
}

public DeferredResult<NetworkProfile> updateIsolationNetworkCIDRAllocation(
private DeferredResult<NetworkProfile> updateIsolationNetworkCIDRAllocation(
NetworkProfile networkProfile) {

logInfo(() -> "Update Isolation Network CIDR Allocation for network profile: " +
Expand Down Expand Up @@ -303,12 +317,14 @@ private DeferredResult<NetworkProfile> getExistingCIDRAllocationLink(
networkProfile.tenantLinks)
.setMaxResultsLimit(1);

return queryCIDRAllocation.collectLinks(Collectors.toList())
.thenApply(cidrAllocationLinks -> {
if (cidrAllocationLinks != null && cidrAllocationLinks.size() == 1) {
return queryCIDRAllocation.collectDocuments(Collectors.toList())
.thenApply(cidrAllocations -> {
if (cidrAllocations != null && cidrAllocations.size() == 1) {
// Found existing CIDRAllocationService
networkProfile.isolationNetworkCIDRAllocationLink =
cidrAllocationLinks.get(0);
ComputeNetworkCIDRAllocationState cidrAllocation = cidrAllocations.get(0);

networkProfile.isolationNetworkCIDRAllocationLink = cidrAllocation
.documentSelfLink;
} else {
// Clean up any previous CIDRAllocation link
networkProfile.isolationNetworkCIDRAllocationLink = null;
Expand All @@ -332,6 +348,12 @@ private DeferredResult<NetworkProfile> createCIDRAllocationIfNeeded(
ComputeNetworkCIDRAllocationState cidrAllocationState = new
ComputeNetworkCIDRAllocationState();
cidrAllocationState.networkLink = networkProfile.isolationNetworkLink;
if (networkProfile.isolatedSubnetCIDRPrefix == null) {
// TODO: Set a default for now. Remove when UI provides a value.
cidrAllocationState.subnetCIDRPrefixLength = 31;
} else {
cidrAllocationState.subnetCIDRPrefixLength = networkProfile.isolatedSubnetCIDRPrefix;
}
Operation createOp = Operation.createPost(getHost(),
ComputeNetworkCIDRAllocationService.FACTORY_LINK)
.setBody(cidrAllocationState);
Expand All @@ -344,12 +366,28 @@ private DeferredResult<NetworkProfile> createCIDRAllocationIfNeeded(
});
}

private DeferredResult<NetworkProfile> updateIsolationSubnetCIDRPrefix(
NetworkProfile networkProfile) {

ComputeNetworkCIDRAllocationState allocationState = new ComputeNetworkCIDRAllocationState();
allocationState.subnetCIDRPrefixLength = networkProfile.isolatedSubnetCIDRPrefix;

Operation patchOp = Operation.createPatch(getHost(),
networkProfile.isolationNetworkCIDRAllocationLink)
.setBody(allocationState);


return sendWithDeferredResult(patchOp, ComputeNetworkCIDRAllocationState.class)
.thenApply(resultCIDRAllocationState -> networkProfile);
}

/**
* Nullifies link fields if the patch body contains NULL_LINK_VALUE links.
* TODO: This is the same as ResourceUtils.nullifyLinkFields(). Unify in the next changelist.
*/
private static <T extends ResourceState> boolean nullifyLinkFields(
ServiceDocumentDescription desc, NetworkProfile currentState, NetworkProfile patchBody) {
ServiceDocumentDescription desc, NetworkProfile currentState,
NetworkProfile patchBody) {
boolean modified = false;
for (PropertyDescription prop : desc.propertyDescriptions.values()) {
if (prop.usageOptions != null &&
Expand Down
Loading

0 comments on commit 5ecf549

Please sign in to comment.