diff --git a/src/main/java/no/rutebanken/anshar/config/AnsharConfiguration.java b/src/main/java/no/rutebanken/anshar/config/AnsharConfiguration.java index 9cc8f4428..0c574fea7 100644 --- a/src/main/java/no/rutebanken/anshar/config/AnsharConfiguration.java +++ b/src/main/java/no/rutebanken/anshar/config/AnsharConfiguration.java @@ -54,6 +54,9 @@ public class AnsharConfiguration { @Value("${anshar.healthcheck.interval.seconds}") private int healthCheckInterval = 30; + @Value("${anshar.mapping.adapters.disabled:false}") + private boolean disableAllMappingAdapters = false; + @Value("${anshar.environment}") private String environment; @@ -265,4 +268,12 @@ public Boolean splitDataForProcessing() { public Duration hardLimitForFutureEtUpdates() { return hardLimitForFutureUpdates; } + + public boolean isDisableAllMappingAdapters() { + return disableAllMappingAdapters; + } + + public void setDisableAllMappingAdapters(boolean disableAllMappingAdapters) { + this.disableAllMappingAdapters = disableAllMappingAdapters; + } } diff --git a/src/main/java/no/rutebanken/anshar/routes/export/file/ExportHelper.java b/src/main/java/no/rutebanken/anshar/routes/export/file/ExportHelper.java index 485a2b904..94f6baace 100644 --- a/src/main/java/no/rutebanken/anshar/routes/export/file/ExportHelper.java +++ b/src/main/java/no/rutebanken/anshar/routes/export/file/ExportHelper.java @@ -33,24 +33,27 @@ public class ExportHelper { @Autowired private SiriHelper siriHelper; + @Autowired + private MappingAdapterPresets mappingAdapterPresets; + public Siri exportET() { return transform(siriHelper.getAllET(), - MappingAdapterPresets.getOutboundAdapters( + mappingAdapterPresets.getOutboundAdapters( SiriDataType.ESTIMATED_TIMETABLE, OutboundIdMappingPolicy.DEFAULT) ); } public Siri exportSX() { return transform(siriHelper.getAllSX(), - MappingAdapterPresets.getOutboundAdapters( + mappingAdapterPresets.getOutboundAdapters( SiriDataType.SITUATION_EXCHANGE, OutboundIdMappingPolicy.DEFAULT) ); } public Siri exportVM() { return transform(siriHelper.getAllVM(), - MappingAdapterPresets.getOutboundAdapters(SiriDataType.VEHICLE_MONITORING, + mappingAdapterPresets.getOutboundAdapters(SiriDataType.VEHICLE_MONITORING, OutboundIdMappingPolicy.DEFAULT) ); } diff --git a/src/main/java/no/rutebanken/anshar/routes/outbound/ServerSubscriptionManager.java b/src/main/java/no/rutebanken/anshar/routes/outbound/ServerSubscriptionManager.java index f5df507e6..8b893e694 100644 --- a/src/main/java/no/rutebanken/anshar/routes/outbound/ServerSubscriptionManager.java +++ b/src/main/java/no/rutebanken/anshar/routes/outbound/ServerSubscriptionManager.java @@ -100,6 +100,9 @@ public class ServerSubscriptionManager { @Value("${anshar.outbound.pubsub.sx.topic.enabled}") private boolean pushToSxTopicEnabled; + @Autowired + private MappingAdapterPresets mappingAdapterPresets; + ExecutorService outboundSenderExecutorService = Executors.newFixedThreadPool(100); @Produce(value = "direct:send.to.pubsub.topic.estimated_timetable") @@ -217,7 +220,7 @@ private OutboundSubscriptionSetup createSubscription(SubscriptionRequest subscri getHeartbeatInterval(subscriptionRequest), getChangeBeforeUpdates(subscriptionRequest), siriHelper.getFilter(subscriptionRequest), - MappingAdapterPresets.getOutboundAdapters(outboundIdMappingPolicy), + mappingAdapterPresets.getOutboundAdapters(outboundIdMappingPolicy), findSubscriptionIdentifier(subscriptionRequest), subscriptionRequest.getRequestorRef().getValue(), findInitialTerminationTime(subscriptionRequest), diff --git a/src/main/java/no/rutebanken/anshar/routes/siri/SiriLiteRoute.java b/src/main/java/no/rutebanken/anshar/routes/siri/SiriLiteRoute.java index 79738ca59..658700638 100644 --- a/src/main/java/no/rutebanken/anshar/routes/siri/SiriLiteRoute.java +++ b/src/main/java/no/rutebanken/anshar/routes/siri/SiriLiteRoute.java @@ -72,6 +72,9 @@ public class SiriLiteRoute extends RestRouteBuilder { @Autowired private SiriObjectFactory siriObjectFactory; + @Autowired + private MappingAdapterPresets mappingAdapterPresets; + @Override public void configure() throws Exception { super.configure(); @@ -126,7 +129,7 @@ public void configure() throws Exception { Siri response = situations.createServiceDelivery(requestorId, datasetId, etClientName, maxSize); - List outboundAdapters = MappingAdapterPresets.getOutboundAdapters( + List outboundAdapters = mappingAdapterPresets.getOutboundAdapters( SiriDataType.SITUATION_EXCHANGE, SiriHandler.getIdMappingPolicy(originalId) ); @@ -179,7 +182,7 @@ public void configure() throws Exception { response = vehicleActivities.createServiceDelivery(requestorId, datasetId, etClientName, excludedIdList, maxSize); } - List outboundAdapters = MappingAdapterPresets.getOutboundAdapters( + List outboundAdapters = mappingAdapterPresets.getOutboundAdapters( SiriDataType.VEHICLE_MONITORING, SiriHandler.getIdMappingPolicy(originalId) ); @@ -240,7 +243,7 @@ public void configure() throws Exception { response = estimatedTimetables.createServiceDelivery(requestorId, datasetId, etClientName, excludedIdList, maxSize, previewIntervalMillis); } - List outboundAdapters = MappingAdapterPresets.getOutboundAdapters( + List outboundAdapters = mappingAdapterPresets.getOutboundAdapters( SiriDataType.ESTIMATED_TIMETABLE, SiriHandler.getIdMappingPolicy(originalId) ); @@ -272,7 +275,7 @@ public void configure() throws Exception { logger.info("Fetching monitored ET-data"); Siri response = siriObjectFactory.createETServiceDelivery(estimatedTimetables.getAllMonitored()); - List outboundAdapters = MappingAdapterPresets.getOutboundAdapters( + List outboundAdapters = mappingAdapterPresets.getOutboundAdapters( SiriDataType.ESTIMATED_TIMETABLE, OutboundIdMappingPolicy.DEFAULT ); @@ -308,7 +311,7 @@ public void configure() throws Exception { datasetId, clientTrackingName )); - List outboundAdapters = MappingAdapterPresets.getOutboundAdapters( + List outboundAdapters = mappingAdapterPresets.getOutboundAdapters( SiriDataType.SITUATION_EXCHANGE, OutboundIdMappingPolicy.DEFAULT ); @@ -360,7 +363,7 @@ public void configure() throws Exception { Siri response = siriObjectFactory.createVMServiceDelivery(cachedUpdates); - List outboundAdapters = MappingAdapterPresets.getOutboundAdapters( + List outboundAdapters = mappingAdapterPresets.getOutboundAdapters( SiriDataType.VEHICLE_MONITORING, OutboundIdMappingPolicy.DEFAULT ); @@ -399,7 +402,7 @@ public void configure() throws Exception { datasetId, lineRef, clientTrackingName, maxSize )); - List outboundAdapters = MappingAdapterPresets.getOutboundAdapters( + List outboundAdapters = mappingAdapterPresets.getOutboundAdapters( SiriDataType.ESTIMATED_TIMETABLE, OutboundIdMappingPolicy.DEFAULT ); @@ -435,7 +438,7 @@ public void configure() throws Exception { Siri response = siriObjectFactory.createETServiceDelivery(estimatedTimetables.getAllCachedUpdates(null, null, clientTrackingName)); - List outboundAdapters = MappingAdapterPresets.getOutboundAdapters( + List outboundAdapters = mappingAdapterPresets.getOutboundAdapters( SiriDataType.ESTIMATED_TIMETABLE, OutboundIdMappingPolicy.DEFAULT ); diff --git a/src/main/java/no/rutebanken/anshar/routes/siri/handlers/SiriHandler.java b/src/main/java/no/rutebanken/anshar/routes/siri/handlers/SiriHandler.java index 1a4267c43..03108876b 100644 --- a/src/main/java/no/rutebanken/anshar/routes/siri/handlers/SiriHandler.java +++ b/src/main/java/no/rutebanken/anshar/routes/siri/handlers/SiriHandler.java @@ -109,6 +109,9 @@ public class SiriHandler { @Autowired private PrometheusMetricsService metrics; + @Autowired + private MappingAdapterPresets mappingAdapterPresets; + public Siri handleIncomingSiri(String subscriptionId, InputStream xml) throws UnmarshalException { return handleIncomingSiri(subscriptionId, xml, null, -1); } @@ -199,7 +202,7 @@ public Siri handleSiriCacheRequest( metrics.countOutgoingData(serviceResponse, SubscriptionSetup.SubscriptionMode.REQUEST_RESPONSE); return SiriValueTransformer.transform( serviceResponse, - MappingAdapterPresets.getOutboundAdapters(dataType, OutboundIdMappingPolicy.DEFAULT), + mappingAdapterPresets.getOutboundAdapters(dataType, OutboundIdMappingPolicy.DEFAULT), false, false ); @@ -300,7 +303,7 @@ private Siri processSiriServerRequest(Siri incoming, String datasetId, List outboundAdapters = MappingAdapterPresets.getOutboundAdapters(OutboundIdMappingPolicy.DEFAULT); + final List outboundAdapters = mappingAdapterPresets.getOutboundAdapters(OutboundIdMappingPolicy.DEFAULT); from("direct:siri.transform.data") .process(p -> { diff --git a/src/main/java/no/rutebanken/anshar/subscription/SubscriptionInitializer.java b/src/main/java/no/rutebanken/anshar/subscription/SubscriptionInitializer.java index 6a0a7495f..ffba50fd1 100644 --- a/src/main/java/no/rutebanken/anshar/subscription/SubscriptionInitializer.java +++ b/src/main/java/no/rutebanken/anshar/subscription/SubscriptionInitializer.java @@ -112,12 +112,16 @@ void createSubscriptions() { logger.info("App started with mode(s): {}", configuration.getAppModes()); } - final Map mappingBeans = ApplicationContextHolder.getContext().getBeansWithAnnotation(Mapping.class); final Map mappingAdaptersById = new HashMap<>(); - for (final Object myFoo : mappingBeans.values()) { - final Class mappingAdapterClass = myFoo.getClass(); - final Mapping annotation = mappingAdapterClass.getAnnotation(Mapping.class); - mappingAdaptersById.put(annotation.id(), mappingAdapterClass); + if (configuration.isDisableAllMappingAdapters()) { + logger.info("All mapping adapters are disabled"); + } else { + final Map mappingBeans = ApplicationContextHolder.getContext().getBeansWithAnnotation(Mapping.class); + for (final Object myFoo : mappingBeans.values()) { + final Class mappingAdapterClass = myFoo.getClass(); + final Mapping annotation = mappingAdapterClass.getAnnotation(Mapping.class); + mappingAdaptersById.put(annotation.id(), mappingAdapterClass); + } } logger.info("Initializing subscriptions for environment: {}", configuration.getEnvironment()); @@ -161,44 +165,47 @@ void createSubscriptions() { reduceLogging(subscriptionSetup.getSubscriptionId()); } - List valueAdapters = new ArrayList<>(); + if (!configuration.isDisableAllMappingAdapters()) { - if (mappingAdaptersById.containsKey(subscriptionSetup.getMappingAdapterId())) { - Class adapterClass = mappingAdaptersById.get(subscriptionSetup.getMappingAdapterId()); - try { - valueAdapters.addAll((List) adapterClass.getMethod("getValueAdapters", SubscriptionSetup.class).invoke(adapterClass.newInstance(), subscriptionSetup)); + List valueAdapters = new ArrayList<>(); - } catch (Exception e) { - throw new ServiceConfigurationError("Invalid mappingAdapterId for subscription " + subscriptionSetup, e); + if (mappingAdaptersById.containsKey(subscriptionSetup.getMappingAdapterId())) { + Class adapterClass = mappingAdaptersById.get(subscriptionSetup.getMappingAdapterId()); + try { + valueAdapters.addAll((List) adapterClass.getMethod("getValueAdapters", SubscriptionSetup.class).invoke(adapterClass.newInstance(), subscriptionSetup)); + + } catch (Exception e) { + throw new ServiceConfigurationError("Invalid mappingAdapterId for subscription " + subscriptionSetup, e); + } + } + //Is added to ALL subscriptions AFTER subscription-specific adapters + valueAdapters.add(new CodespaceProcessor(subscriptionSetup.getDatasetId())); + + // SX + if (subscriptionSetup.getSubscriptionType() == SiriDataType.SITUATION_EXCHANGE) { + valueAdapters.add(new ReportTypeProcessor(subscriptionSetup.getDatasetId())); + valueAdapters.add(new RemovePersonalInformationProcessor()); + valueAdapters.add(new LimitClosedProgressValidityPostProcessor(subscriptionSetup.getDatasetId())); } - } - //Is added to ALL subscriptions AFTER subscription-specific adapters - valueAdapters.add(new CodespaceProcessor(subscriptionSetup.getDatasetId())); - - // SX - if (subscriptionSetup.getSubscriptionType() == SiriDataType.SITUATION_EXCHANGE) { - valueAdapters.add(new ReportTypeProcessor(subscriptionSetup.getDatasetId())); - valueAdapters.add(new RemovePersonalInformationProcessor()); - valueAdapters.add(new LimitClosedProgressValidityPostProcessor(subscriptionSetup.getDatasetId())); - } - // ET - if (subscriptionSetup.getSubscriptionType() == SiriDataType.ESTIMATED_TIMETABLE) { - valueAdapters.add(new EnsureIncreasingTimesForCancelledStopsProcessor(subscriptionSetup.getDatasetId())); - valueAdapters.add(new ExtraJourneyDestinationDisplayPostProcessor(subscriptionSetup.getDatasetId())); - valueAdapters.add(new AddOrderToAllCallsPostProcessor(subscriptionSetup.getDatasetId())); - valueAdapters.add(new EnsureNonNullVehicleModePostProcessor()); - valueAdapters.add(new ExtraJourneyPostProcessor(subscriptionSetup.getDatasetId())); - } + // ET + if (subscriptionSetup.getSubscriptionType() == SiriDataType.ESTIMATED_TIMETABLE) { + valueAdapters.add(new EnsureIncreasingTimesForCancelledStopsProcessor(subscriptionSetup.getDatasetId())); + valueAdapters.add(new ExtraJourneyDestinationDisplayPostProcessor(subscriptionSetup.getDatasetId())); + valueAdapters.add(new AddOrderToAllCallsPostProcessor(subscriptionSetup.getDatasetId())); + valueAdapters.add(new EnsureNonNullVehicleModePostProcessor()); + valueAdapters.add(new ExtraJourneyPostProcessor(subscriptionSetup.getDatasetId())); + } - if (!subscriptionSetup.getCodespaceWhiteList().isEmpty()) { - valueAdapters.add(new CodespaceWhiteListProcessor(subscriptionSetup.getDatasetId(), subscriptionSetup.getCodespaceWhiteList())); - } - if (!subscriptionSetup.getCodespaceBlackList().isEmpty()) { - valueAdapters.add(new CodespaceBlackListProcessor(subscriptionSetup.getDatasetId(), subscriptionSetup.getCodespaceBlackList())); - } + if (!subscriptionSetup.getCodespaceWhiteList().isEmpty()) { + valueAdapters.add(new CodespaceWhiteListProcessor(subscriptionSetup.getDatasetId(), subscriptionSetup.getCodespaceWhiteList())); + } + if (!subscriptionSetup.getCodespaceBlackList().isEmpty()) { + valueAdapters.add(new CodespaceBlackListProcessor(subscriptionSetup.getDatasetId(), subscriptionSetup.getCodespaceBlackList())); + } - subscriptionSetup.getMappingAdapters().addAll(valueAdapters); + subscriptionSetup.getMappingAdapters().addAll(valueAdapters); + } if (subscriptionSetup.getSubscriptionMode() == SubscriptionSetup.SubscriptionMode.FETCHED_DELIVERY | subscriptionSetup.getSubscriptionMode() == SubscriptionSetup.SubscriptionMode.POLLING_FETCHED_DELIVERY) { diff --git a/src/main/java/no/rutebanken/anshar/subscription/helpers/MappingAdapterPresets.java b/src/main/java/no/rutebanken/anshar/subscription/helpers/MappingAdapterPresets.java index 3dae29bff..0cf9cae45 100644 --- a/src/main/java/no/rutebanken/anshar/subscription/helpers/MappingAdapterPresets.java +++ b/src/main/java/no/rutebanken/anshar/subscription/helpers/MappingAdapterPresets.java @@ -16,12 +16,15 @@ package no.rutebanken.anshar.subscription.helpers; import com.google.common.base.Objects; +import no.rutebanken.anshar.config.AnsharConfiguration; import no.rutebanken.anshar.routes.siri.handlers.OutboundIdMappingPolicy; import no.rutebanken.anshar.routes.siri.processor.CodespaceOutboundProcessor; import no.rutebanken.anshar.routes.siri.processor.RemoveEmojiPostProcessor; import no.rutebanken.anshar.routes.siri.transformer.ValueAdapter; import no.rutebanken.anshar.routes.siri.transformer.impl.OutboundIdAdapter; import no.rutebanken.anshar.subscription.SiriDataType; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; import uk.org.ifopt.siri21.StopPlaceRef; import uk.org.siri.siri21.CourseOfJourneyRefStructure; import uk.org.siri.siri21.DestinationRef; @@ -31,15 +34,32 @@ import uk.org.siri.siri21.StopPointRefStructure; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; + +@Component public class MappingAdapterPresets { - private static Map> adapterCache = new HashMap<>(); + private Map> adapterCache = new HashMap<>(); + + private final boolean disableAllMappingAdapters; + + public MappingAdapterPresets(@Autowired AnsharConfiguration configuration) { + if (configuration == null) { + disableAllMappingAdapters = false; + } else { + disableAllMappingAdapters = configuration.isDisableAllMappingAdapters(); + } + } + + public List getOutboundAdapters(SiriDataType dataType, OutboundIdMappingPolicy outboundIdMappingPolicy) { + if (disableAllMappingAdapters) { + return Collections.emptyList(); + } - public static List getOutboundAdapters(SiriDataType dataType, OutboundIdMappingPolicy outboundIdMappingPolicy) { CacheKey key = new CacheKey(dataType, outboundIdMappingPolicy); if (!adapterCache.containsKey(key)) { List adapters = new ArrayList<>(); @@ -66,7 +86,10 @@ public static List getOutboundAdapters(SiriDataType dataType, Outb return adapterCache.get(key); } - public static List getOutboundAdapters(OutboundIdMappingPolicy outboundIdMappingPolicy) { + public List getOutboundAdapters(OutboundIdMappingPolicy outboundIdMappingPolicy) { + if (disableAllMappingAdapters) { + return Collections.emptyList(); + } CacheKey key = new CacheKey(null, outboundIdMappingPolicy); if (!adapterCache.containsKey(key)) { List adapters = new ArrayList<>(); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 98b6959fd..31b614ad7 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -215,6 +215,9 @@ anshar.reduced.logging.override.names=no.rutebanken.anshar.routes.siri.handlers. anshar.startup.wait.for.netex.initialization=false anshar.startup.load.mapping.data=true +# Set to true to globally disable all usage of mapping adapters +anshar.mapping.adapters.disabled=false + anshar.pubsub.avro.et.enabled=false anshar.pubsub.avro.vm.enabled=false anshar.pubsub.avro.sx.enabled=false diff --git a/src/test/java/no/rutebanken/anshar/siri/SiriValueTransformerTest.java b/src/test/java/no/rutebanken/anshar/siri/SiriValueTransformerTest.java index 9da29b5a6..31c3b9a74 100644 --- a/src/test/java/no/rutebanken/anshar/siri/SiriValueTransformerTest.java +++ b/src/test/java/no/rutebanken/anshar/siri/SiriValueTransformerTest.java @@ -16,6 +16,7 @@ package no.rutebanken.anshar.siri; import jakarta.xml.bind.JAXBException; +import no.rutebanken.anshar.config.AnsharConfiguration; import no.rutebanken.anshar.integration.SpringBootBaseTest; import no.rutebanken.anshar.routes.siri.handlers.OutboundIdMappingPolicy; import no.rutebanken.anshar.routes.siri.transformer.SiriValueTransformer; @@ -103,10 +104,12 @@ public void testOutboundMappingAdapters() throws JAXBException { assertEquals(paddedLineRefValue, getLineRefFromSiriObj(siri), "LineRef not set correctly"); - Siri mappedIdSiri = SiriValueTransformer.transform(siri, MappingAdapterPresets.getOutboundAdapters(OutboundIdMappingPolicy.DEFAULT)); + MappingAdapterPresets mappingAdapterPresets = new MappingAdapterPresets(new AnsharConfiguration()); + + Siri mappedIdSiri = SiriValueTransformer.transform(siri, mappingAdapterPresets.getOutboundAdapters(OutboundIdMappingPolicy.DEFAULT)); assertEquals(mappedLineRefValue, getLineRefFromSiriObj(mappedIdSiri), "Outbound adapters did not return mapped id"); - Siri originalIdSiri = SiriValueTransformer.transform(siri, MappingAdapterPresets.getOutboundAdapters(OutboundIdMappingPolicy.ORIGINAL_ID)); + Siri originalIdSiri = SiriValueTransformer.transform(siri, mappingAdapterPresets.getOutboundAdapters(OutboundIdMappingPolicy.ORIGINAL_ID)); assertEquals(lineRefValue, getLineRefFromSiriObj(originalIdSiri), "Outbound adapters did not return original id"); }