diff --git a/src/main/java/org/etsi/osl/metrico/MetricoRouteBuilder.java b/src/main/java/org/etsi/osl/metrico/MetricoRouteBuilder.java index 96e060db4e9827f1926b5c79b19a2e826ba8fd2e..879eefff884b5098224c90fdc7e6f5f047916e46 100644 --- a/src/main/java/org/etsi/osl/metrico/MetricoRouteBuilder.java +++ b/src/main/java/org/etsi/osl/metrico/MetricoRouteBuilder.java @@ -37,7 +37,6 @@ public class MetricoRouteBuilder extends RouteBuilder { from(MEASUREMENT_COLLECTION_JOB_CREATED) .log(LoggingLevel.INFO, log, MEASUREMENT_COLLECTION_JOB_CREATED + "message received!") .to("log:DEBUG?showBody=true&showHeaders=true") - .process(exchange -> prometheusQueries.startPeriodicQuery()) .setBody(simple("Message received and processed")) .to(MEASUREMENT_COLLECTION_JOB_RESPONSE); diff --git a/src/main/java/org/etsi/osl/metrico/mapper/JobMapper.java b/src/main/java/org/etsi/osl/metrico/mapper/JobMapper.java index d677cbc133d7a36dc44a1c2769c4b638f5860d3d..63230d843d30b9dc01cc4bc403fbbf9607331fc1 100644 --- a/src/main/java/org/etsi/osl/metrico/mapper/JobMapper.java +++ b/src/main/java/org/etsi/osl/metrico/mapper/JobMapper.java @@ -1,12 +1,15 @@ package org.etsi.osl.metrico.mapper; +import jakarta.validation.Valid; import org.etsi.osl.metrico.model.Job; -import org.etsi.osl.tmf.pm628.model.DataAccessEndpoint; -import org.etsi.osl.tmf.pm628.model.MeasurementCollectionJob; +import org.etsi.osl.metrico.model.SupportedDataAccessEndpoints; +import org.etsi.osl.tmf.pm628.model.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.UUID; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class JobMapper { @@ -16,33 +19,71 @@ public class JobMapper { Job job = new Job(); job.setMeasurementCollectionJobRef(UUID.fromString(measurementCollectionJob.getUuid())); + if(measurementCollectionJob.getDataAccessEndpoint().size()!=1){ throw new IllegalArgumentException("DataAccessEndpoint should be exactly one"); }else { DataAccessEndpoint dataAccessEndpoint = measurementCollectionJob.getDataAccessEndpoint().get(0); job.setDataAccessEndPointRef(UUID.fromString(dataAccessEndpoint.getUuid())); - if( dataAccessEndpoint.getApiType().equalsIgnoreCase("PROMETHEUS")){ + if(SupportedDataAccessEndpoints.contains(dataAccessEndpoint.getApiType())){ job.setApiType(dataAccessEndpoint.getApiType()); } else if (dataAccessEndpoint.getApiType() == null ) { throw new IllegalArgumentException("API type needs to be defined"); }else{ - throw new IllegalArgumentException("API type not supported"); + throw new IllegalArgumentException("API type " + dataAccessEndpoint.getApiType() + " not supported"); + } + if(dataAccessEndpoint.getUri()!=null){ + job.setDataAccessEndPointUri(dataAccessEndpoint.getUri()); + } else { + throw new IllegalArgumentException("DataAccessEndpointUri cannot be null"); } - job.setDateAccessEndPointUri(dataAccessEndpoint.getUri()); - job.setQuery(dataAccessEndpoint.getUriQueryFilter()); - } + if(measurementCollectionJob.getJobCollectionFilter().getMappings().size() == 1){ + DataFilterMap query = measurementCollectionJob.getJobCollectionFilter(); + // String stringQuery = measurementCollectionJob.getJobCollectionFilter().getMappings().get(0).getFilterTemplate().getDescription(); + job.setQuery(query); + } else { + throw new IllegalArgumentException("There should be exactly one query"); + } + + } job.setStartDateTime(measurementCollectionJob.getScheduleDefinition().get(0).getScheduleDefinitionStartTime()); job.setEndDateTime(measurementCollectionJob.getScheduleDefinition().get(0).getScheduleDefinitionEndTime()); - /* - Extend the reporting period and granularitypossible values - job.setStartDateTime(measurementCollectionJob.getReportingPeriod()); - job.setExecutionInterval(measurementCollectionJob.getGranularity()); - */ + + // job.setStartDateTime(measurementCollectionJob.getReportingPeriod()); + + @Valid Granularity granularity = measurementCollectionJob.getGranularity(); + + job.setExecutionInterval(convertGranularityToSeconds(measurementCollectionJob.getGranularity().getValue())); + + logger.atDebug().setMessage("Received MeasurementCollectionJob:\n" + measurementCollectionJob + "\nConverted it to Job:\n" + job).log(); return job; } + public static int convertGranularityToSeconds(String value) { + Pattern PATTERN = Pattern.compile("G_(\\d+)(SEC|MN|H|D|M|Y)"); + if (Granularity.contains(value)) { + Matcher matcher = PATTERN.matcher(value); + if (matcher.matches()) { + int amount = Integer.parseInt(matcher.group(1)); + String unit = matcher.group(2); + if(value.equalsIgnoreCase(Granularity.NA.getValue())){ + return Integer.parseInt(null); + } + return switch (unit) { + case "SEC" -> amount; + case "MIN" -> amount * 60; + case "H" -> amount * 3600; + case "D" -> amount * 86400; + case "M" -> amount * 2592000; // Approximate value for a month + case "Y" -> amount * 31536000; // Approximate value for a year + default -> throw new IllegalArgumentException("Unknown Granularity unit: " + unit); + }; + } + } + throw new IllegalArgumentException("Invalid Granularity format: " + value); + } } diff --git a/src/main/java/org/etsi/osl/metrico/model/Job.java b/src/main/java/org/etsi/osl/metrico/model/Job.java index a456d796f8168e9bc2fb94320bb74fbc8a62a373..6f5799b0827cae6c5ea9432fc939e120738665b9 100644 --- a/src/main/java/org/etsi/osl/metrico/model/Job.java +++ b/src/main/java/org/etsi/osl/metrico/model/Job.java @@ -45,7 +45,7 @@ public class Job{ // Should I check the granularity or the scheduleDefinitionRef for the recurringFrequency? private UUID measurementCollectionJobRef; - private URI dateAccessEndPointUri; + private URI dataAccessEndPointUri; private DataFilterMap query; diff --git a/src/main/java/org/etsi/osl/metrico/model/SupportedDataAccessEndpoints.java b/src/main/java/org/etsi/osl/metrico/model/SupportedDataAccessEndpoints.java new file mode 100644 index 0000000000000000000000000000000000000000..75b1b3486360df49d00fb73a26a1e02e2e2128f1 --- /dev/null +++ b/src/main/java/org/etsi/osl/metrico/model/SupportedDataAccessEndpoints.java @@ -0,0 +1,16 @@ +package org.etsi.osl.metrico.model; + +public enum SupportedDataAccessEndpoints { + PROMETHEUS; + + public static boolean contains(String apiType) { + try { + SupportedDataAccessEndpoints.valueOf(apiType.toUpperCase()); + return true; + } catch (IllegalArgumentException e) { + return false; + } + } +} + + diff --git a/src/test/java/org/etsi/osl/metrico/mapper/JobMapperTest.java b/src/test/java/org/etsi/osl/metrico/mapper/JobMapperTest.java new file mode 100644 index 0000000000000000000000000000000000000000..0f384de268709c5485a0481f25bed69c69229a79 --- /dev/null +++ b/src/test/java/org/etsi/osl/metrico/mapper/JobMapperTest.java @@ -0,0 +1,51 @@ +package org.etsi.osl.metrico.mapper; + +import org.etsi.osl.tmf.pm628.model.DataAccessEndpoint; +import org.etsi.osl.tmf.pm628.model.DataFilterMap; +import org.etsi.osl.tmf.pm628.model.DataFilterMapItem; +import org.etsi.osl.tmf.pm628.model.MeasurementCollectionJob; +import org.jetbrains.annotations.NotNull; + +import java.net.URI; +import java.util.*; + +public class JobMapperTest { + + public MeasurementCollectionJob measurementCollectionJobCreate() { + MeasurementCollectionJob measurementCollectionJob = new MeasurementCollectionJob(); + + List<DataAccessEndpoint> dataAccessEndpointList = new ArrayList<>(); + dataAccessEndpointList.add(dataAccessEndpointCreate()); + + measurementCollectionJob.setUuid("9f22dc98-f439-4fdd-98e3-f6471cf8ca67"); + measurementCollectionJob.setDataAccessEndpoint(dataAccessEndpointList); + measurementCollectionJob.set + + return measurementCollectionJob; + } + + public DataAccessEndpoint dataAccessEndpointCreate(){ + DataAccessEndpoint dataAccessEndpoint = new DataAccessEndpoint(); + dataAccessEndpoint.setUuid("123e4567-e89b-12d3-a456-426614174000"); + dataAccessEndpoint.setUri(URI.create("example.com")); + dataAccessEndpoint.setApiType("Prometheus"); + return dataAccessEndpoint; + + } + + public DataFilterMap dataFilterMapCreate(){ + DataFilterMap dataFilterMap = new DataFilterMap(); + // dataFilterMap.setMappings(); + + return dataFilterMap; + } + + public DataFilterMapItem dataFilterMapItemCreate(){ + DataFilterMapItem dataFilterMapItem = new DataFilterMapItem(); + dataFilterMapItem.setFilterTemplate(); + + + + } + +}