Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add http2 routes support #312

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package com.sap.cloudfoundry.client.facade.adapters;

import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;

import org.cloudfoundry.client.v3.routes.Route;
import org.immutables.value.Value;

import com.sap.cloudfoundry.client.facade.Nullable;
import com.sap.cloudfoundry.client.facade.domain.CloudRoute;
import com.sap.cloudfoundry.client.facade.domain.ImmutableCloudDomain;
import com.sap.cloudfoundry.client.facade.domain.ImmutableCloudRoute;
import com.sap.cloudfoundry.client.facade.domain.ImmutableCloudMetadata;

import java.util.UUID;
import com.sap.cloudfoundry.client.facade.domain.ImmutableCloudRoute;
import com.sap.cloudfoundry.client.facade.domain.ImmutableRouteDestination;
import com.sap.cloudfoundry.client.facade.domain.RouteDestination;

@Value.Immutable
public abstract class RawCloudRoute extends RawCloudEntity<CloudRoute> {

@Value.Parameter
public abstract Route getRoute();

@Nullable
public abstract UUID getApplicationGuid();

@Override
public CloudRoute derive() {
Route route = getRoute();
List<RouteDestination> destinations = mapDestinations();
String domainGuid = route.getRelationships()
.getDomain()
.getData()
Expand All @@ -35,6 +45,8 @@ public CloudRoute derive() {
.build())
.path(route.getPath())
.url(route.getUrl())
.destinations(destinations)
.requestedProtocol(computeRequestedProtocol(destinations))
.build();
}

Expand All @@ -43,7 +55,8 @@ private static String computeDomain(Route route) {
if (!route.getHost()
.isEmpty()) {
domain = domain.substring(route.getHost()
.length() + 1);
.length()
+ 1);
}
if (!route.getPath()
.isEmpty()) {
Expand All @@ -55,4 +68,32 @@ private static String computeDomain(Route route) {
return domain;
}

private List<RouteDestination> mapDestinations() {
return getRoute().getDestinations()
.stream()
.map(destination -> ImmutableRouteDestination.builder()
.metadata(ImmutableCloudMetadata.builder()
.guid(UUID.fromString(destination.getDestinationId()))
.build())
.applicationGuid(UUID.fromString(destination.getApplication()
.getApplicationId()))
.port(destination.getPort())
.weight(destination.getWeight())
.protocol(destination.getProtocol())
.build())
.collect(Collectors.toList());
}

private String computeRequestedProtocol(List<RouteDestination> destinations) {
UUID applicationGuid = getApplicationGuid();
if (applicationGuid == null) {
return null;
}
return destinations.stream()
.filter(destination -> Objects.equals(destination.getApplicationGuid(), applicationGuid))
.findFirst()
.map(RouteDestination::getProtocol)
.orElse(null);
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.sap.cloudfoundry.client.facade.domain;

import java.util.List;
import java.util.Objects;

import org.immutables.value.Value;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.sap.cloudfoundry.client.facade.Nullable;

import java.util.Objects;

@Value.Immutable
@JsonSerialize(as = ImmutableCloudRoute.class)
@JsonDeserialize(as = ImmutableCloudRoute.class)
Expand All @@ -29,6 +30,12 @@ public int getAppsUsingRoute() {
@Nullable
public abstract Integer getPort();

@Nullable
public abstract String getRequestedProtocol();

@Nullable
public abstract List<RouteDestination> getDestinations();

public abstract String getUrl();

@Override
Expand Down Expand Up @@ -58,10 +65,12 @@ public boolean equals(Object object) {
var thisDomain = getDomain().getName();
var otherDomain = otherRoute.getDomain()
.getName();
// @formatter:off
return thisDomain.equals(otherDomain)
&& areEmptyOrEqual(getHost(), otherRoute.getHost())
&& areEmptyOrEqual(getPath(), otherRoute.getPath())
&& Objects.equals(getPort(), otherRoute.getPort());
// @formatter:on
}

private static boolean areEmptyOrEqual(String lhs, String rhs) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.sap.cloudfoundry.client.facade.domain;

import java.util.UUID;

import org.immutables.value.Value;

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.sap.cloudfoundry.client.facade.Nullable;

@Value.Immutable
@JsonSerialize(as = ImmutableRouteDestination.class)
@JsonDeserialize(as = ImmutableRouteDestination.class)
public abstract class RouteDestination extends CloudEntity implements Derivable<RouteDestination> {

public abstract UUID getApplicationGuid();

@Nullable
public abstract Integer getPort();

@Nullable
public abstract Integer getWeight();

public abstract String getProtocol();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be nullable as well? We can return null in the "raw" object when building RouteDestination

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Protocol to use for this destination. Valid protocols are http1 or http2 if route protocol is http, tcp if route protocol is tcp. A null value will set it either http1 or tcp based on the route protocol; this feature is only available if you turn it on in routing release
According to the doc, it should not return null. We use the object only for getting a route, for the creation of the destination requestedProtocol is used


@Override
public RouteDestination derive() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
Expand Down Expand Up @@ -204,6 +205,7 @@
import com.sap.cloudfoundry.client.facade.domain.ImmutableInstancesInfo;
import com.sap.cloudfoundry.client.facade.domain.ImmutableUpload;
import com.sap.cloudfoundry.client.facade.domain.InstancesInfo;
import com.sap.cloudfoundry.client.facade.domain.RouteDestination;
import com.sap.cloudfoundry.client.facade.domain.ServicePlanVisibility;
import com.sap.cloudfoundry.client.facade.domain.Staging;
import com.sap.cloudfoundry.client.facade.domain.Status;
Expand Down Expand Up @@ -412,7 +414,7 @@ private void addRoutes(Set<CloudRoute> routes, UUID applicationGuid) {
UUID domainGuid = domains.get(route.getDomain()
.getName());
UUID routeGuid = getOrAddRoute(domainGuid, route.getHost(), route.getPath());
bindRoute(routeGuid, applicationGuid);
bindRoute(routeGuid, applicationGuid, route.getRequestedProtocol());
}
}

Expand Down Expand Up @@ -703,7 +705,10 @@ public CloudProcess getApplicationProcess(UUID applicationGuid) {

@Override
public List<CloudRoute> getApplicationRoutes(UUID applicationGuid) {
return fetchList(() -> getRouteResourcesByAppGuid(applicationGuid), ImmutableRawCloudRoute::of);
return fetchList(() -> getRouteResourcesByAppGuid(applicationGuid), routeResource -> ImmutableRawCloudRoute.builder()
.route(routeResource)
.applicationGuid(applicationGuid)
.build());
}

@Override
Expand Down Expand Up @@ -1261,35 +1266,58 @@ public void updateApplicationStaging(String applicationName, Staging staging) {
@Override
public void updateApplicationRoutes(String applicationName, Set<CloudRoute> updatedRoutes) {
UUID applicationGuid = getApplicationGuid(applicationName);
List<RouteResource> appRoutes = getRouteResourcesByAppGuid(applicationGuid).collectList()
.defaultIfEmpty(Collections.emptyList())
.block();

List<RouteResource> outdatedRoutes = getOutdatedRoutes(appRoutes, updatedRoutes);
Set<CloudRoute> newRoutes = getNewRoutes(updatedRoutes, appRoutes);

List<CloudRoute> appRoutes = fetchList(() -> getRouteResourcesByAppGuid(applicationGuid), ImmutableRawCloudRoute::of);
Set<CloudRoute> outdatedRoutes = getOutdatedRoutes(applicationGuid, appRoutes, updatedRoutes);
Set<CloudRoute> newRoutes = getNewRoutes(applicationGuid, appRoutes, updatedRoutes);
removeRoutes(outdatedRoutes, applicationGuid);
addRoutes(newRoutes, applicationGuid);
}

private List<RouteResource> getOutdatedRoutes(List<RouteResource> currentRoutes, Set<CloudRoute> updatedRoutes) {
Set<String> urls = updatedRoutes.stream()
.map(CloudRoute::getUrl)
.collect(Collectors.toSet());
private Set<CloudRoute> getOutdatedRoutes(UUID applicationGuid, List<CloudRoute> currentRoutes, Set<CloudRoute> updatedRoutes) {
return currentRoutes.stream()
.filter(routeResource -> !urls.contains(routeResource.getUrl()))
.collect(Collectors.toList());
.filter(currentRoute -> isRouteOutdated(applicationGuid, currentRoute, updatedRoutes))
.collect(Collectors.toSet());
}

private Set<CloudRoute> getNewRoutes(Set<CloudRoute> updatedRoutes, List<RouteResource> currentRoutes) {
Set<String> urls = currentRoutes.stream()
.map(RouteResource::getUrl)
.collect(Collectors.toSet());
private boolean isRouteOutdated(UUID applicationGuid, CloudRoute currentRoute, Set<CloudRoute> updatedRoutes) {
Optional<CloudRoute> updatedRoute = findRoute(currentRoute.getUrl(), updatedRoutes);
if (updatedRoute.isEmpty()) {
return true;
}
return isProtocolChanged(applicationGuid, currentRoute, updatedRoute.get());
}

private Optional<CloudRoute> findRoute(String url, Collection<CloudRoute> routes) {
return routes.stream()
.filter(route -> Objects.equals(url, route.getUrl()))
.findFirst();
}

private boolean isProtocolChanged(UUID applicationGuid, CloudRoute currentRoute, CloudRoute updatedRoute) {
if (updatedRoute.getRequestedProtocol() == null) {
return false;
}
return currentRoute.getDestinations()
.stream()
.filter(routeDestination -> Objects.equals(routeDestination.getApplicationGuid(), applicationGuid))
.noneMatch(routeDestination -> Objects.equals(routeDestination.getProtocol(),
updatedRoute.getRequestedProtocol()));
}

private Set<CloudRoute> getNewRoutes(UUID applicationGuid, List<CloudRoute> currentRoutes, Set<CloudRoute> updatedRoutes) {
return updatedRoutes.stream()
.filter(route -> !urls.contains(route.getUrl()))
.filter(updatedRoute -> isRouteUpdated(applicationGuid, updatedRoute, currentRoutes))
.collect(Collectors.toSet());
}

private boolean isRouteUpdated(UUID applicationGuid, CloudRoute updatedRoute, List<CloudRoute> currentRoutes) {
Optional<CloudRoute> currentRoute = findRoute(updatedRoute.getUrl(), currentRoutes);
if (currentRoute.isEmpty()) {
return true;
}
return isProtocolChanged(applicationGuid, currentRoute.get(), updatedRoute);
}

@Override
public String updateServiceBroker(CloudServiceBroker serviceBroker) {
Assert.notNull(serviceBroker, "Service broker must not be null.");
Expand Down Expand Up @@ -1804,13 +1832,12 @@ private void assertSpaceProvided(String operation) {
Assert.notNull(target, "Unable to " + operation + " without specifying organization and space to use.");
}

private void removeRoutes(List<RouteResource> routes, UUID applicationGuid) {
for (RouteResource route : routes) {
for (Destination destination : route.getDestinations()) {
if (destination.getApplication()
.getApplicationId()
.equals(applicationGuid.toString())) {
unbindRoute(route.getId(), destination.getDestinationId());
private void removeRoutes(Set<CloudRoute> routes, UUID applicationGuid) {
for (CloudRoute route : routes) {
for (RouteDestination destination : route.getDestinations()) {
if (destination.getApplicationGuid()
.equals(applicationGuid)) {
unbindRoute(route.getGuid(), destination.getGuid());
}
}
}
Expand All @@ -1826,29 +1853,30 @@ private void validateDomainForRoute(CloudRoute route, Map<String, UUID> existing
}
}

private void unbindRoute(String routeGuid, String destinationGuid) {
private void unbindRoute(UUID routeGuid, UUID destinationGuid) {
delegate.routesV3()
.removeDestinations(RemoveRouteDestinationsRequest.builder()
.routeId(routeGuid)
.destinationId(destinationGuid)
.routeId(routeGuid.toString())
.destinationId(destinationGuid.toString())
.build())
.block();
}

private void bindRoute(UUID routeGuid, UUID applicationGuid) {
private void bindRoute(UUID routeGuid, UUID applicationGuid, String protocol) {
delegate.routesV3()
.insertDestinations(InsertRouteDestinationsRequest.builder()
.routeId(routeGuid.toString())
.destination(createDestination(applicationGuid))
.destination(createDestination(applicationGuid, protocol))
.build())
.block();
}

private Destination createDestination(UUID applicationGuid) {
private Destination createDestination(UUID applicationGuid, String protocol) {
return Destination.builder()
.application(org.cloudfoundry.client.v3.routes.Application.builder()
.applicationId(applicationGuid.toString())
.build())
.protocol(protocol)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sap.cloudfoundry.client.facade.adapters;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

Expand All @@ -24,7 +23,13 @@ class RawCloudRouteTest {
private static final String DOMAIN_NAME = "example.com";
private static final CloudDomain DOMAIN = buildTestDomain();
private static final Integer APPS_USING_ROUTE = 2;
private static final List<Destination> DESTINATIONS = buildTestDestinations(APPS_USING_ROUTE);
private static final UUID DESTINATION_1_GUID = UUID.randomUUID();
private static final UUID APPLICATION_1_GUID = UUID.randomUUID();
private static final String DESTINATION_1_PROTOCOL = "http2";
private static final UUID DESTINATION_2_GUID = UUID.randomUUID();
private static final UUID APPLICATION_2_GUID = UUID.randomUUID();
private static final String DESTINATION_2_PROTOCOL = "http1";
private static final List<Destination> DESTINATIONS = buildTestDestinations();

@Test
void testDerive() {
Expand Down Expand Up @@ -72,12 +77,19 @@ private static CloudDomain buildTestDomain() {
.build();
}

private static List<Destination> buildTestDestinations(int count) {
return Collections.nCopies(count, Destination.builder()
.application(Application.builder()
.applicationId("")
.build())
.build());
private static List<Destination> buildTestDestinations() {
return List.of(buildDestination(DESTINATION_1_GUID.toString(), APPLICATION_1_GUID.toString(), DESTINATION_1_PROTOCOL),
buildDestination(DESTINATION_2_GUID.toString(), APPLICATION_2_GUID.toString(), DESTINATION_2_PROTOCOL));
}

private static Destination buildDestination(String destinationGuid, String applicationGuid, String protocol) {
return Destination.builder()
.destinationId(destinationGuid)
.application(Application.builder()
.applicationId(applicationGuid)
.build())
.protocol(protocol)
.build();
}

private static ToOneRelationship buildToOneRelationship(UUID guid) {
Expand Down