-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding possibility to blacklist codespaces for each subscription
- Loading branch information
Showing
7 changed files
with
236 additions
and
12 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
src/main/java/no/rutebanken/anshar/routes/siri/processor/CodespaceBlackListProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Licensed under the EUPL, Version 1.2 or – as soon they will be approved by | ||
* the European Commission - subsequent versions of the EUPL (the "Licence"); | ||
* You may not use this work except in compliance with the Licence. | ||
* You may obtain a copy of the Licence at: | ||
* | ||
* https://joinup.ec.europa.eu/software/page/eupl | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the Licence is distributed on an "AS IS" basis, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the Licence for the specific language governing permissions and | ||
* limitations under the Licence. | ||
*/ | ||
|
||
package no.rutebanken.anshar.routes.siri.processor; | ||
|
||
import no.rutebanken.anshar.metrics.PrometheusMetricsService; | ||
import no.rutebanken.anshar.routes.siri.transformer.ApplicationContextHolder; | ||
import no.rutebanken.anshar.routes.siri.transformer.ValueAdapter; | ||
import no.rutebanken.anshar.subscription.SiriDataType; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import uk.org.siri.siri21.DatedVehicleJourneyRef; | ||
import uk.org.siri.siri21.EstimatedTimetableDeliveryStructure; | ||
import uk.org.siri.siri21.EstimatedVersionFrameStructure; | ||
import uk.org.siri.siri21.FramedVehicleJourneyRefStructure; | ||
import uk.org.siri.siri21.LineRef; | ||
import uk.org.siri.siri21.Siri; | ||
|
||
import java.util.List; | ||
|
||
import static no.rutebanken.anshar.routes.siri.transformer.MappingNames.REMOVE_INVALID_CODESPACE; | ||
|
||
public class CodespaceBlackListProcessor extends ValueAdapter implements PostProcessor { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(CodespaceBlackListProcessor.class); | ||
private final String codespace; | ||
private final List<String> blacklist; | ||
|
||
private PrometheusMetricsService metrics; | ||
|
||
public CodespaceBlackListProcessor(String codespace, List<String> blacklist) { | ||
this.codespace = codespace; | ||
this.blacklist = blacklist; | ||
} | ||
|
||
@Override | ||
protected String apply(String text) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public void process(Siri siri) { | ||
if (this.blacklist == null || this.blacklist.isEmpty()) { | ||
//Nothing to do - return immediately | ||
return; | ||
} | ||
if (siri != null && siri.getServiceDelivery() != null) { | ||
List<EstimatedTimetableDeliveryStructure> etDeliveries = siri.getServiceDelivery().getEstimatedTimetableDeliveries(); | ||
if (etDeliveries != null) { | ||
for (EstimatedTimetableDeliveryStructure etDelivery : etDeliveries) { | ||
List<EstimatedVersionFrameStructure> estimatedJourneyVersionFrames = etDelivery.getEstimatedJourneyVersionFrames(); | ||
for (EstimatedVersionFrameStructure estimatedJourneyVersionFrame : estimatedJourneyVersionFrames) { | ||
int size = estimatedJourneyVersionFrame.getEstimatedVehicleJourneies().size(); | ||
|
||
estimatedJourneyVersionFrame | ||
.getEstimatedVehicleJourneies() | ||
.removeIf(et -> isInvalidCodespace(et.getFramedVehicleJourneyRef())); | ||
|
||
estimatedJourneyVersionFrame | ||
.getEstimatedVehicleJourneies() | ||
.removeIf(et -> isInvalidCodespace(et.getDatedVehicleJourneyRef())); | ||
|
||
estimatedJourneyVersionFrame | ||
.getEstimatedVehicleJourneies() | ||
.removeIf(et -> isInvalidCodespace(et.getEstimatedVehicleJourneyCode())); | ||
|
||
estimatedJourneyVersionFrame | ||
.getEstimatedVehicleJourneies() | ||
.removeIf(et -> isInvalidCodespace(et.getLineRef())); | ||
|
||
if (estimatedJourneyVersionFrame.getEstimatedVehicleJourneies().size() != size) { | ||
final int removedDataCount = size - estimatedJourneyVersionFrame.getEstimatedVehicleJourneies().size(); | ||
logger.info("Removed {} ET-messages on blacklisted codespaces.", removedDataCount); | ||
getMetricsService() | ||
.registerDataMapping( | ||
SiriDataType.ESTIMATED_TIMETABLE, | ||
codespace, | ||
REMOVE_INVALID_CODESPACE, | ||
removedDataCount | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
private boolean isInvalidCodespace(LineRef lineRef) { | ||
if (lineRef != null) { | ||
return toBeRemoved(lineRef.getValue()); | ||
} | ||
return false; | ||
} | ||
private boolean isInvalidCodespace(String s) { | ||
if (s != null) { | ||
return toBeRemoved(s); | ||
} | ||
return false; | ||
} | ||
private boolean isInvalidCodespace(DatedVehicleJourneyRef datedVehicleJourneyRef) { | ||
if (datedVehicleJourneyRef != null) { | ||
return toBeRemoved(datedVehicleJourneyRef.getValue()); | ||
} | ||
return false; | ||
} | ||
|
||
private boolean isInvalidCodespace(FramedVehicleJourneyRefStructure framedVehicleJourneyRef) { | ||
if (framedVehicleJourneyRef != null) { | ||
String datedVehicleJourneyRef = framedVehicleJourneyRef.getDatedVehicleJourneyRef(); | ||
return toBeRemoved(datedVehicleJourneyRef); | ||
} | ||
return false; | ||
} | ||
|
||
private boolean toBeRemoved(String reference) { | ||
if (reference != null && reference.length() > 4) { | ||
return blacklist.contains( | ||
reference.substring(0,3) | ||
); | ||
} | ||
return false; | ||
} | ||
|
||
void prepareMetrics() { | ||
if (metrics == null) { | ||
metrics = ApplicationContextHolder.getContext().getBean(PrometheusMetricsService.class); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/test/java/no/rutebanken/anshar/siri/processor/CodespaceBlackListProcessorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package no.rutebanken.anshar.siri.processor; | ||
|
||
import no.rutebanken.anshar.integration.SpringBootBaseTest; | ||
import no.rutebanken.anshar.routes.siri.helpers.SiriObjectFactory; | ||
import no.rutebanken.anshar.routes.siri.processor.CodespaceBlackListProcessor; | ||
import org.junit.jupiter.api.Test; | ||
import uk.org.siri.siri21.EstimatedVehicleJourney; | ||
import uk.org.siri.siri21.FramedVehicleJourneyRefStructure; | ||
import uk.org.siri.siri21.Siri; | ||
|
||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public class CodespaceBlackListProcessorTest extends SpringBootBaseTest { | ||
|
||
CodespaceBlackListProcessor processor = new CodespaceBlackListProcessor("testing-testing", List.of("TST", "ABC")); | ||
|
||
@Test | ||
public void testCodespaceNotOnBlacklistIsNotRemoved() { | ||
Siri siri = createEt("RUT"); | ||
assertEquals(1, getEstimatedVehicleJourneies(siri).size()); | ||
processor.process(siri); | ||
assertFalse(getEstimatedVehicleJourneies(siri).isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testCodespaceOnBlacklistIsRemoved() { | ||
|
||
Siri siri = createEt("TST"); | ||
assertEquals(1, getEstimatedVehicleJourneies(siri).size()); | ||
processor.process(siri); | ||
assertTrue(getEstimatedVehicleJourneies(siri).isEmpty()); | ||
|
||
} | ||
@Test | ||
public void testInvalidCodespaceIsRemovedAndValidIsKept() { | ||
|
||
Siri siri = createEt("RUT", "TST"); | ||
assertEquals(2, getEstimatedVehicleJourneies(siri).size()); | ||
processor.process(siri); | ||
assertFalse(getEstimatedVehicleJourneies(siri).isEmpty()); | ||
assertEquals(1, getEstimatedVehicleJourneies(siri).size()); | ||
} | ||
|
||
private static List<EstimatedVehicleJourney> getEstimatedVehicleJourneies(Siri tst) { | ||
return tst.getServiceDelivery().getEstimatedTimetableDeliveries().get(0).getEstimatedJourneyVersionFrames().get(0).getEstimatedVehicleJourneies(); | ||
} | ||
|
||
private Siri createEt(String... codespacePrefix) { | ||
|
||
List<EstimatedVehicleJourney> etList = new ArrayList<>(); | ||
for (String prefix : codespacePrefix) { | ||
EstimatedVehicleJourney et = new EstimatedVehicleJourney(); | ||
FramedVehicleJourneyRefStructure framedVehicleJourneyRef = new FramedVehicleJourneyRefStructure(); | ||
framedVehicleJourneyRef.setDatedVehicleJourneyRef(prefix + ":ServiceJourney:1234"); | ||
et.setFramedVehicleJourneyRef(framedVehicleJourneyRef); | ||
etList.add(et); | ||
} | ||
|
||
return new SiriObjectFactory(Instant.now()).createETServiceDelivery(etList); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters