Skip to content

Commit

Permalink
Added config-property to disable all usage of MappingAdapters (#165)
Browse files Browse the repository at this point in the history
  • Loading branch information
lassetyr committed Nov 25, 2024
1 parent c18de18 commit 3984d0c
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 57 deletions.
11 changes: 11 additions & 0 deletions src/main/java/no/rutebanken/anshar/config/AnsharConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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),
Expand Down
19 changes: 11 additions & 8 deletions src/main/java/no/rutebanken/anshar/routes/siri/SiriLiteRoute.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -126,7 +129,7 @@ public void configure() throws Exception {

Siri response = situations.createServiceDelivery(requestorId, datasetId, etClientName, maxSize);

List<ValueAdapter> outboundAdapters = MappingAdapterPresets.getOutboundAdapters(
List<ValueAdapter> outboundAdapters = mappingAdapterPresets.getOutboundAdapters(
SiriDataType.SITUATION_EXCHANGE,
SiriHandler.getIdMappingPolicy(originalId)
);
Expand Down Expand Up @@ -179,7 +182,7 @@ public void configure() throws Exception {
response = vehicleActivities.createServiceDelivery(requestorId, datasetId, etClientName, excludedIdList, maxSize);
}

List<ValueAdapter> outboundAdapters = MappingAdapterPresets.getOutboundAdapters(
List<ValueAdapter> outboundAdapters = mappingAdapterPresets.getOutboundAdapters(
SiriDataType.VEHICLE_MONITORING,
SiriHandler.getIdMappingPolicy(originalId)
);
Expand Down Expand Up @@ -240,7 +243,7 @@ public void configure() throws Exception {
response = estimatedTimetables.createServiceDelivery(requestorId, datasetId, etClientName, excludedIdList, maxSize, previewIntervalMillis);
}

List<ValueAdapter> outboundAdapters = MappingAdapterPresets.getOutboundAdapters(
List<ValueAdapter> outboundAdapters = mappingAdapterPresets.getOutboundAdapters(
SiriDataType.ESTIMATED_TIMETABLE,
SiriHandler.getIdMappingPolicy(originalId)
);
Expand Down Expand Up @@ -272,7 +275,7 @@ public void configure() throws Exception {
logger.info("Fetching monitored ET-data");
Siri response = siriObjectFactory.createETServiceDelivery(estimatedTimetables.getAllMonitored());

List<ValueAdapter> outboundAdapters = MappingAdapterPresets.getOutboundAdapters(
List<ValueAdapter> outboundAdapters = mappingAdapterPresets.getOutboundAdapters(
SiriDataType.ESTIMATED_TIMETABLE,
OutboundIdMappingPolicy.DEFAULT
);
Expand Down Expand Up @@ -308,7 +311,7 @@ public void configure() throws Exception {
datasetId, clientTrackingName
));

List<ValueAdapter> outboundAdapters = MappingAdapterPresets.getOutboundAdapters(
List<ValueAdapter> outboundAdapters = mappingAdapterPresets.getOutboundAdapters(
SiriDataType.SITUATION_EXCHANGE,
OutboundIdMappingPolicy.DEFAULT
);
Expand Down Expand Up @@ -360,7 +363,7 @@ public void configure() throws Exception {

Siri response = siriObjectFactory.createVMServiceDelivery(cachedUpdates);

List<ValueAdapter> outboundAdapters = MappingAdapterPresets.getOutboundAdapters(
List<ValueAdapter> outboundAdapters = mappingAdapterPresets.getOutboundAdapters(
SiriDataType.VEHICLE_MONITORING,
OutboundIdMappingPolicy.DEFAULT
);
Expand Down Expand Up @@ -399,7 +402,7 @@ public void configure() throws Exception {
datasetId, lineRef, clientTrackingName, maxSize
));

List<ValueAdapter> outboundAdapters = MappingAdapterPresets.getOutboundAdapters(
List<ValueAdapter> outboundAdapters = mappingAdapterPresets.getOutboundAdapters(
SiriDataType.ESTIMATED_TIMETABLE,
OutboundIdMappingPolicy.DEFAULT
);
Expand Down Expand Up @@ -435,7 +438,7 @@ public void configure() throws Exception {

Siri response = siriObjectFactory.createETServiceDelivery(estimatedTimetables.getAllCachedUpdates(null, null, clientTrackingName));

List<ValueAdapter> outboundAdapters = MappingAdapterPresets.getOutboundAdapters(
List<ValueAdapter> outboundAdapters = mappingAdapterPresets.getOutboundAdapters(
SiriDataType.ESTIMATED_TIMETABLE,
OutboundIdMappingPolicy.DEFAULT
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
);
Expand Down Expand Up @@ -300,7 +303,7 @@ private Siri processSiriServerRequest(Siri incoming, String datasetId, List<Stri
metrics.countOutgoingData(serviceResponse, SubscriptionSetup.SubscriptionMode.REQUEST_RESPONSE);
return SiriValueTransformer.transform(
serviceResponse,
MappingAdapterPresets.getOutboundAdapters(dataType, outboundIdMappingPolicy),
mappingAdapterPresets.getOutboundAdapters(dataType, outboundIdMappingPolicy),
false,
false
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import no.rutebanken.anshar.subscription.helpers.MappingAdapterPresets;
import org.apache.camel.LoggingLevel;
import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import uk.org.siri.siri21.Siri;

Expand All @@ -29,9 +30,12 @@ public class SiriOutputTransformerRoute extends RouteBuilder {

public static final String OUTPUT_ADAPTERS_HEADER_NAME = "adapters";

@Autowired
private MappingAdapterPresets mappingAdapterPresets;

@Override
public void configure() {
final List<ValueAdapter> outboundAdapters = MappingAdapterPresets.getOutboundAdapters(OutboundIdMappingPolicy.DEFAULT);
final List<ValueAdapter> outboundAdapters = mappingAdapterPresets.getOutboundAdapters(OutboundIdMappingPolicy.DEFAULT);

from("direct:siri.transform.data")
.process(p -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,16 @@ void createSubscriptions() {
logger.info("App started with mode(s): {}", configuration.getAppModes());
}

final Map<String, Object> mappingBeans = ApplicationContextHolder.getContext().getBeansWithAnnotation(Mapping.class);
final Map<String, Class> 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<String, Object> 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());
Expand Down Expand Up @@ -161,44 +165,47 @@ void createSubscriptions() {
reduceLogging(subscriptionSetup.getSubscriptionId());
}

List<ValueAdapter> valueAdapters = new ArrayList<>();
if (!configuration.isDisableAllMappingAdapters()) {

if (mappingAdaptersById.containsKey(subscriptionSetup.getMappingAdapterId())) {
Class adapterClass = mappingAdaptersById.get(subscriptionSetup.getMappingAdapterId());
try {
valueAdapters.addAll((List<ValueAdapter>) adapterClass.getMethod("getValueAdapters", SubscriptionSetup.class).invoke(adapterClass.newInstance(), subscriptionSetup));
List<ValueAdapter> 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<ValueAdapter>) 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) {
Expand Down
Loading

0 comments on commit 3984d0c

Please sign in to comment.