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

Support multiple profiles routing service #551

Merged
merged 11 commits into from
Feb 14, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import no.entur.uttu.model.Ref;
import no.entur.uttu.routing.RouteGeometry;
import no.entur.uttu.routing.RoutingService;
import no.entur.uttu.routing.RoutingServiceRequestParams;
import no.entur.uttu.stopplace.spi.StopPlaceRegistry;
import org.rutebanken.netex.model.LinkSequenceProjection;
import org.rutebanken.netex.model.Projections_RelStructure;
Expand Down Expand Up @@ -43,12 +44,15 @@ public List<ServiceLink> produce(NetexExportContext context) {
Quay quayFrom = getQuay(serviceLink.quayRefFrom());
Quay quayTo = getQuay(serviceLink.quayRefTo());

RouteGeometry routeGeometry = routingService.getRouteGeometry(
var params = new RoutingServiceRequestParams(
quayFrom.getCentroid().getLocation().getLongitude(),
quayFrom.getCentroid().getLocation().getLatitude(),
quayTo.getCentroid().getLocation().getLongitude(),
quayTo.getCentroid().getLocation().getLatitude()
quayTo.getCentroid().getLocation().getLatitude(),
serviceLink.transportMode()
);

RouteGeometry routeGeometry = routingService.getRouteGeometry(params);
List<Double> posListCoordinates = new ArrayList<>();
routeGeometry
.coordinates()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ public org.rutebanken.netex.model.JourneyPattern produce(
List<LinkInLinkSequence_VersionedChildStructure> linksInSequence;
if (
routingService != null &&
routingService.isEnabled() &&
local.getLine().getTransportMode() == VehicleModeEnumeration.BUS
routingService.isEnabled(local.getLine().getTransportMode())
) {
linksInSequence =
local
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/no/entur/uttu/graphql/LinesGraphQLSchema.java
Original file line number Diff line number Diff line change
Expand Up @@ -1187,6 +1187,14 @@ private GraphQLObjectType createQueryObject() {
.description("Second stop point's id")
.build()
)
.argument(
GraphQLArgument
.newArgument()
.name("mode")
.type(transportModeEnum)
.description("Transport mode")
.build()
)
.description("Fetch service link containing route geometry")
.dataFetcher(routingFetcher)
)
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/no/entur/uttu/graphql/fetchers/RoutingFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import no.entur.uttu.graphql.model.ServiceLink;
import no.entur.uttu.model.VehicleModeEnumeration;
import no.entur.uttu.routing.RouteGeometry;
import no.entur.uttu.routing.RoutingService;
import no.entur.uttu.routing.RoutingServiceRequestParams;
import no.entur.uttu.stopplace.spi.StopPlaceRegistry;
import org.rutebanken.netex.model.Quay;
import org.springframework.stereotype.Service;
Expand All @@ -27,20 +29,24 @@
public ServiceLink get(DataFetchingEnvironment environment) {
String quayRefFrom = environment.getArgument("quayRefFrom");
String quayRefTo = environment.getArgument("quayRefTo");
VehicleModeEnumeration mode = environment.getArgument("mode");

Check warning on line 32 in src/main/java/no/entur/uttu/graphql/fetchers/RoutingFetcher.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/no/entur/uttu/graphql/fetchers/RoutingFetcher.java#L32

Added line #L32 was not covered by tests
Quay quayFrom = getQuay(quayRefFrom);
Quay quayTo = getQuay(quayRefTo);

if (quayFrom == null || quayTo == null) {
return new ServiceLink(quayRefFrom + "_" + quayRefTo, null, null, null);
}

RouteGeometry routeGeometry = routingService.getRouteGeometry(
var params = new RoutingServiceRequestParams(

Check warning on line 40 in src/main/java/no/entur/uttu/graphql/fetchers/RoutingFetcher.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/no/entur/uttu/graphql/fetchers/RoutingFetcher.java#L40

Added line #L40 was not covered by tests
quayFrom.getCentroid().getLocation().getLongitude(),
quayFrom.getCentroid().getLocation().getLatitude(),
quayTo.getCentroid().getLocation().getLongitude(),
quayTo.getCentroid().getLocation().getLatitude()
quayTo.getCentroid().getLocation().getLatitude(),

Check warning on line 44 in src/main/java/no/entur/uttu/graphql/fetchers/RoutingFetcher.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/no/entur/uttu/graphql/fetchers/RoutingFetcher.java#L44

Added line #L44 was not covered by tests
mode != null ? mode : VehicleModeEnumeration.BUS
);

RouteGeometry routeGeometry = routingService.getRouteGeometry(params);

Check warning on line 48 in src/main/java/no/entur/uttu/graphql/fetchers/RoutingFetcher.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/no/entur/uttu/graphql/fetchers/RoutingFetcher.java#L48

Added line #L48 was not covered by tests

return new ServiceLink(
quayRefFrom + "_" + quayRefTo,
quayRefFrom,
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/no/entur/uttu/routing/DefaultRoutingService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package no.entur.uttu.routing;

import java.math.BigDecimal;
import java.util.ArrayList;
import no.entur.uttu.model.VehicleModeEnumeration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand All @@ -22,19 +21,16 @@
);

public DefaultRoutingService() {
logger.info("DefaultRoutingService got initialised");
logger.info("DefaultRoutingService initialised");
}

public RouteGeometry getRouteGeometry(
BigDecimal longitudeFrom,
BigDecimal latitudeFrom,
BigDecimal longitudeTo,
BigDecimal latitudeTo
) {
return new RouteGeometry(new ArrayList<>(), BigDecimal.ZERO);
@Override
public boolean isEnabled(VehicleModeEnumeration mode) {
return false;
}

public boolean isEnabled() {
return false;
@Override
public RouteGeometry getRouteGeometry(RoutingServiceRequestParams request) {
return null;

Check warning on line 34 in src/main/java/no/entur/uttu/routing/DefaultRoutingService.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/no/entur/uttu/routing/DefaultRoutingService.java#L34

Added line #L34 was not covered by tests
}
}
22 changes: 14 additions & 8 deletions src/main/java/no/entur/uttu/routing/RoutingService.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package no.entur.uttu.routing;

import java.math.BigDecimal;
import no.entur.uttu.model.VehicleModeEnumeration;

/**
* For getting the road geometry - resulting in service links and links in sequence in a journey pattern
*/
public interface RoutingService {
RouteGeometry getRouteGeometry(
BigDecimal longitudeFrom,
BigDecimal latitudeFrom,
BigDecimal longitudeTo,
BigDecimal latitudeTo
);
boolean isEnabled();
/**
* Check if the routing service is enabled for a given {@link VehicleModeEnumeration}
* @param mode
* @return
*/
boolean isEnabled(VehicleModeEnumeration mode);

/**
* Request route geometry
* @param request The parameters of the request
* @return An instance of {@link RouteGeometry}
*/
RouteGeometry getRouteGeometry(RoutingServiceRequestParams request);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package no.entur.uttu.routing;

import java.math.BigDecimal;
import no.entur.uttu.model.VehicleModeEnumeration;

public record RoutingServiceRequestParams(
BigDecimal longitudeFrom,
BigDecimal latitudeFrom,
BigDecimal longitudeTo,
BigDecimal latitudeTo,
VehicleModeEnumeration mode
) {}
42 changes: 37 additions & 5 deletions src/main/java/no/entur/uttu/routing/osrm/OsrmConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,51 @@
package no.entur.uttu.routing.osrm;

import java.util.HashMap;
import java.util.Map;
import no.entur.uttu.routing.RoutingService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
* Configuration class for the OSRM (Open Source Routing Machine) routing service.
*
* <p>This configuration is activated via the spring profile "osrm-routing-service".</p>
*
* <p>The routing profiles can be configured via properties to map a set of transport
* modes to specific OSRM endpoints. The configuration follows this pattern:</p>
*
* <pre>
* uttu.routing.osrm-api.profiles.[label].modes=[list of modes]
* uttu.routing.osrm-api.profiles.[label].endpoint=[url to service]
* </pre>
*
* <p>For example, to configure routing for "bus" mode to be handled by http://localhost:5000,
* use the following configuration:</p>
*
* <pre>
* uttu.routing.osrm-api.profiles.bus.modes=bus
* uttu.routing.osrm-api.profiles.bus.endpoint=http://localhost:5000
* </pre>
*
* <p>Note: The "label" in the configuration is arbitrary and used only for identification purposes.</p>
*
* @see OsrmService
*/
@Configuration
@ConfigurationProperties(prefix = "uttu.routing.osrm-api")
@Profile("osrm-routing-service")
public class OsrmConfiguration {

private Map<String, OsrmProfile> profiles = new HashMap<>();

Check warning on line 41 in src/main/java/no/entur/uttu/routing/osrm/OsrmConfiguration.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/no/entur/uttu/routing/osrm/OsrmConfiguration.java#L41

Added line #L41 was not covered by tests

public Map<String, OsrmProfile> getProfiles() {
return profiles;

Check warning on line 44 in src/main/java/no/entur/uttu/routing/osrm/OsrmConfiguration.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/no/entur/uttu/routing/osrm/OsrmConfiguration.java#L44

Added line #L44 was not covered by tests
}

@Bean
RoutingService routingService(
@Value("${uttu.routing.osrm-api}") String osrmApiEndpoint
) {
return new OsrmService(osrmApiEndpoint);
RoutingService routingService() {
return new OsrmService(profiles.values().stream().toList());

Check warning on line 49 in src/main/java/no/entur/uttu/routing/osrm/OsrmConfiguration.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/no/entur/uttu/routing/osrm/OsrmConfiguration.java#L49

Added line #L49 was not covered by tests
}
}
36 changes: 36 additions & 0 deletions src/main/java/no/entur/uttu/routing/osrm/OsrmProfile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package no.entur.uttu.routing.osrm;

import java.util.List;
import no.entur.uttu.model.VehicleModeEnumeration;

public class OsrmProfile {

private List<VehicleModeEnumeration> modes;
private String endpoint;

public OsrmProfile(List<VehicleModeEnumeration> modes, String endpoint) {
this.modes = modes;
this.endpoint = endpoint;
}

public List<VehicleModeEnumeration> getModes() {
return modes;
}

public void setModes(List<VehicleModeEnumeration> modes) {
this.modes = modes;
}

Check warning on line 22 in src/main/java/no/entur/uttu/routing/osrm/OsrmProfile.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/no/entur/uttu/routing/osrm/OsrmProfile.java#L21-L22

Added lines #L21 - L22 were not covered by tests

public String getEndpoint() {
return endpoint;
}

public void setEndpoint(String endpoint) {
this.endpoint = endpoint;
}

Check warning on line 30 in src/main/java/no/entur/uttu/routing/osrm/OsrmProfile.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/no/entur/uttu/routing/osrm/OsrmProfile.java#L29-L30

Added lines #L29 - L30 were not covered by tests

@Override
public String toString() {
return "OsrmProfile [modes=" + modes + ", endpoint=" + endpoint + "]";
}
}
Loading