diff --git a/application/src/main/java/org/opentripplanner/updater/alert/siri/mapping/AffectsMapper.java b/application/src/main/java/org/opentripplanner/updater/alert/siri/mapping/AffectsMapper.java index 6b5bb1b46cb..7ca3003b782 100644 --- a/application/src/main/java/org/opentripplanner/updater/alert/siri/mapping/AffectsMapper.java +++ b/application/src/main/java/org/opentripplanner/updater/alert/siri/mapping/AffectsMapper.java @@ -6,10 +6,13 @@ import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Locale; import java.util.Optional; import java.util.Set; import org.jspecify.annotations.Nullable; import org.opentripplanner.core.model.id.FeedScopedId; +import org.opentripplanner.graph_builder.issue.api.DataImportIssueStore; +import org.opentripplanner.gtfs.mapping.DirectionMapper; import org.opentripplanner.routing.alertpatch.EntitySelector; import org.opentripplanner.routing.alertpatch.StopCondition; import org.opentripplanner.transit.model.timetable.Direction; @@ -18,7 +21,10 @@ import org.opentripplanner.transit.repository.TimetableRepository; import org.opentripplanner.transit.service.TransitRepository; import org.opentripplanner.updater.trip.siri.EntityResolver; +import org.opentripplanner.utils.logging.Throttle; import org.opentripplanner.utils.time.ServiceDateUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import uk.org.ifopt.siri21.StopPlaceRef; import uk.org.siri.siri21.AffectedLineStructure; import uk.org.siri.siri21.AffectedOperatorStructure; @@ -47,6 +53,12 @@ */ public class AffectsMapper { + private static final Logger LOG = LoggerFactory.getLogger(AffectsMapper.class); + private static final DirectionMapper GTFS_DIRECTION_MAPPER = new DirectionMapper( + DataImportIssueStore.NOOP + ); + private static final Throttle UNKNOWN_DIRECTION_LOG_THROTTLE = Throttle.ofOneSecond(); + private final String feedId; private final TransitRepository transitRepository; @@ -292,13 +304,25 @@ private List mapDirections(List directionStructur return res.isEmpty() ? null : res; } + /// SIRI-SX producers using GTFS may send the trip's numeric direction ID. + /// Use the same mapping as the static feed so the alert selector matches those trips. private Optional mapDirection(String directionRef) { - return switch (directionRef.toUpperCase()) { + return switch (directionRef.toUpperCase(Locale.ROOT)) { + case "0", "1" -> Optional.of(GTFS_DIRECTION_MAPPER.map(Integer.parseInt(directionRef))); case "INBOUND" -> Optional.of(Direction.INBOUND); case "OUTBOUND" -> Optional.of(Direction.OUTBOUND); case "CLOCKWISE" -> Optional.of(Direction.CLOCKWISE); case "ANTICLOCKWISE" -> Optional.of(Direction.ANTICLOCKWISE); - default -> Optional.empty(); + default -> { + UNKNOWN_DIRECTION_LOG_THROTTLE.throttle(() -> + LOG.warn( + "Ignoring unrecognized SIRI-SX DirectionRef '{}' for feed {}", + directionRef, + feedId + ) + ); + yield Optional.empty(); + } }; } diff --git a/application/src/test/java/org/opentripplanner/updater/alert/siri/SiriAlertsUpdateHandlerTest.java b/application/src/test/java/org/opentripplanner/updater/alert/siri/SiriAlertsUpdateHandlerTest.java index 12719d6a335..249b64fba87 100644 --- a/application/src/test/java/org/opentripplanner/updater/alert/siri/SiriAlertsUpdateHandlerTest.java +++ b/application/src/test/java/org/opentripplanner/updater/alert/siri/SiriAlertsUpdateHandlerTest.java @@ -21,6 +21,8 @@ import org.jspecify.annotations.Nullable; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; import org.opentripplanner.GtfsTest; import org.opentripplanner.core.model.id.FeedScopedId; import org.opentripplanner.core.model.time.TimePeriod; @@ -647,6 +649,34 @@ public void testSiriSxUpdateForLine() { assertEquals(endTime, effectiveEndDate); } + @ParameterizedTest + @CsvSource({ "0, OUTBOUND, INBOUND", "1, INBOUND, OUTBOUND" }) + void testSiriSxUpdateForLineAndGtfsDirection( + String directionRef, + Direction affectedDirection, + Direction oppositeDirection + ) { + var lineRef = new FeedScopedId(FEED_ID, "route0"); + var situation = createPtSituationElement( + "TST:SituationNumber:1", + ZonedDateTime.parse("2014-01-01T00:00:00+01:00"), + ZonedDateTime.parse("2014-01-01T23:59:59+01:00"), + createAffectsLineAndDirection(lineRef.getId(), List.of(directionRef)) + ); + + alertsUpdateHandler.update(createServiceDelivery(situation), realTimeUpdateContext); + + var alerts = transitAlertService.getDirectionAndRouteAlerts(affectedDirection, lineRef); + assertEquals(1, alerts.size()); + assertThat(alerts.iterator().next().entities()).containsExactly( + new EntitySelector.DirectionAndRoute(lineRef, affectedDirection) + ); + assertThat( + transitAlertService.getDirectionAndRouteAlerts(oppositeDirection, lineRef) + ).isEmpty(); + assertThat(transitAlertService.getRouteAlerts(lineRef)).isEmpty(); + } + @Test public void testSiriSxUpdateForLineThenExpiry() { final FeedScopedId lineRef = new FeedScopedId(FEED_ID, "route0"); @@ -802,8 +832,9 @@ public void testSiriSxUpdateForLineAndExternallyDefinedStopPoint() { assertSeparateLineAndStopAlerts(situationNumber, routeId, stopId0, stopId1); } - @Test - public void testSiriSxUpdateForLineAndStopAndDirection() { + @ParameterizedTest + @CsvSource({ "OUTBOUND, INBOUND", "0, 1" }) + void testSiriSxUpdateForLineAndStopAndDirection(String outboundRef, String inboundRef) { var routeId = "route0"; var stopId0 = "stop0"; var stopId1 = "stop1"; @@ -815,7 +846,7 @@ public void testSiriSxUpdateForLineAndStopAndDirection() { situationNumber1, ZonedDateTime.parse("2014-01-01T00:00:00+01:00"), ZonedDateTime.parse("2014-01-01T23:59:59+01:00"), - createAffectsLineAndDirection(routeId, List.of("OUTBOUND"), stopId0, stopId1) + createAffectsLineAndDirection(routeId, List.of(outboundRef), stopId0, stopId1) ) ); var inbound = createServiceDelivery( @@ -823,7 +854,7 @@ public void testSiriSxUpdateForLineAndStopAndDirection() { situationNumber2, ZonedDateTime.parse("2014-01-01T00:00:00+01:00"), ZonedDateTime.parse("2014-01-01T23:59:59+01:00"), - createAffectsLineAndDirection(routeId, List.of("INBOUND"), stopId0, stopId1) + createAffectsLineAndDirection(routeId, List.of(inboundRef), stopId0, stopId1) ) ); diff --git a/doc/user/SIRI-Config.md b/doc/user/SIRI-Config.md index 4225d086832..ea39890b7a9 100644 --- a/doc/user/SIRI-Config.md +++ b/doc/user/SIRI-Config.md @@ -131,6 +131,12 @@ HTTP headers to add to the request. Any header key, value can be inserted. ## SIRI-SX Request/Response via HTTPS +For GTFS-based feeds, an affected line can use the GTFS `direction_id` in its +`DirectionRef`: `0` matches outbound trips and `1` matches inbound trips. Named +directions (`outbound`, `inbound`, `clockwise`, `anticlockwise`) are also supported. +Unrecognized values are ignored and logged as warnings. If no recognized +direction remains, the alert is not restricted by direction. This also applies to SIRI-SX Lite. +