From 77474e98e954a402c8f68a276bb659fe40c090f0 Mon Sep 17 00:00:00 2001 From: Giuseppe Baittiner Date: Thu, 7 Dec 2023 14:50:39 +0100 Subject: [PATCH] feat: attach gtw-config to metadata removal --- .../ms/edsclient/client/IConfigClient.java | 26 +++++++ .../edsclient/client/impl/ConfigClient.java | 65 +++++++++++++++++ .../client/routes/ConfigClientRoutes.java | 58 +++++++++++++++ .../client/routes/base/ClientRoutes.java | 40 ++++++++++ .../fse2/ms/edsclient/dto/ConfigItemDTO.java | 23 ++++++ .../edsclient/enums/ConfigItemTypeEnum.java | 6 ++ .../fse2/ms/edsclient/service/IConfigSRV.java | 6 ++ .../ms/edsclient/service/impl/ConfigSRV.java | 73 +++++++++++++++++++ .../service/impl/EdsInvocationSRV.java | 10 ++- src/main/resources/application-dev.properties | 1 + 10 files changed, 306 insertions(+), 2 deletions(-) create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/IConfigClient.java create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/impl/ConfigClient.java create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/routes/ConfigClientRoutes.java create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/routes/base/ClientRoutes.java create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/edsclient/dto/ConfigItemDTO.java create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/edsclient/enums/ConfigItemTypeEnum.java create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/IConfigSRV.java create mode 100644 src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/impl/ConfigSRV.java diff --git a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/IConfigClient.java b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/IConfigClient.java new file mode 100644 index 0000000..91c2879 --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/IConfigClient.java @@ -0,0 +1,26 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * Copyright (C) 2023 Ministero della Salute + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License along with this program. If not, see . + */ +package it.finanze.sanita.fse2.ms.edsclient.client; + + +import it.finanze.sanita.fse2.ms.edsclient.dto.ConfigItemDTO; +import it.finanze.sanita.fse2.ms.edsclient.enums.ConfigItemTypeEnum; + +/** + * Interface of gtw-config Client. + */ +public interface IConfigClient { + + ConfigItemDTO getConfigurationItems(ConfigItemTypeEnum type); + + String getProps(ConfigItemTypeEnum type, String props, String previous); +} diff --git a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/impl/ConfigClient.java b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/impl/ConfigClient.java new file mode 100644 index 0000000..334fdb4 --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/impl/ConfigClient.java @@ -0,0 +1,65 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * Copyright (C) 2023 Ministero della Salute + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License along with this program. If not, see . + */ +package it.finanze.sanita.fse2.ms.edsclient.client.impl; + +import it.finanze.sanita.fse2.ms.edsclient.client.IConfigClient; +import it.finanze.sanita.fse2.ms.edsclient.client.routes.ConfigClientRoutes; +import it.finanze.sanita.fse2.ms.edsclient.dto.ConfigItemDTO; +import it.finanze.sanita.fse2.ms.edsclient.enums.ConfigItemTypeEnum; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; +import org.springframework.web.client.ResourceAccessException; +import org.springframework.web.client.RestTemplate; + +/** + * Implementation of gtw-config Client. + */ +@Slf4j +@Component +public class ConfigClient implements IConfigClient { + + /** + * Config host. + */ + @Autowired + private ConfigClientRoutes routes; + + @Autowired + private RestTemplate client; + + @Override + public ConfigItemDTO getConfigurationItems(ConfigItemTypeEnum type) { + return client.getForObject(routes.getConfigItems(type), ConfigItemDTO.class); + } + + @Override + public String getProps(ConfigItemTypeEnum type, String props, String previous) { + String out = previous; + String endpoint = routes.getConfigItem(type, props); + if (isReachable()) out = client.getForObject(endpoint, String.class); + if(out == null || !out.equals(previous)) { + log.info("[GTW-CFG] Property {} is set as {} (previously: {})", props, out, previous); + } + return out; + } + + private boolean isReachable() { + try { + final String endpoint = routes.status(); + client.getForEntity(endpoint, String.class); + return true; + } catch (ResourceAccessException clientException) { + return false; + } + } +} diff --git a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/routes/ConfigClientRoutes.java b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/routes/ConfigClientRoutes.java new file mode 100644 index 0000000..9014c95 --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/routes/ConfigClientRoutes.java @@ -0,0 +1,58 @@ +package it.finanze.sanita.fse2.ms.edsclient.client.routes; + +import it.finanze.sanita.fse2.ms.edsclient.enums.ConfigItemTypeEnum; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.springframework.web.util.UriComponentsBuilder; + +import static it.finanze.sanita.fse2.ms.edsclient.client.routes.base.ClientRoutes.Config.*; + +@Component +public final class ConfigClientRoutes { + + @Value("${gtw-config.url.host}") + private String configHost; + + public UriComponentsBuilder base() { + return UriComponentsBuilder.fromHttpUrl(configHost); + } + + public String identifier() { + return IDENTIFIER; + } + + public String microservice() { + return IDENTIFIER_MS; + } + + public String status() { + return base() + .pathSegment(API_STATUS) + .build() + .toUriString(); + } + + public String whois() { + return base() + .pathSegment(API_WHOIS) + .build() + .toUriString(); + } + + public String getConfigItem(ConfigItemTypeEnum type, String props) { + return base() + .pathSegment(API_VERSION, API_CONFIG_ITEMS, API_PROPS) + .queryParam(QP_TYPE, type.name()) + .queryParam(QP_PROPS, props) + .build() + .toUriString(); + } + + public String getConfigItems(ConfigItemTypeEnum type) { + return base() + .pathSegment(API_VERSION, API_CONFIG_ITEMS) + .queryParam(QP_TYPE, type.name()) + .build() + .toUriString(); + } +} \ No newline at end of file diff --git a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/routes/base/ClientRoutes.java b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/routes/base/ClientRoutes.java new file mode 100644 index 0000000..1760ff0 --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/routes/base/ClientRoutes.java @@ -0,0 +1,40 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-or-later + * + * Copyright (C) 2023 Ministero della Salute + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License along with this program. If not, see . + */ +package it.finanze.sanita.fse2.ms.edsclient.client.routes.base; + +import lombok.NoArgsConstructor; + +import static lombok.AccessLevel.PRIVATE; + +@NoArgsConstructor(access = PRIVATE) +public final class ClientRoutes { + + @NoArgsConstructor(access = PRIVATE) + public static final class Config { + // COMMON + public static final String IDENTIFIER_MS = "cfg"; + public static final String IDENTIFIER = "[CFG]"; + // ENDPOINT + public static final String API_VERSION = "v1"; + public static final String API_CONFIG_ITEMS = "config-items"; + public static final String API_PROPS = "props"; + public static final String API_STATUS = "status"; + public static final String API_WHOIS = "whois"; + // QP + public static final String QP_TYPE = "type"; + public static final String QP_PROPS = "props"; + // VALUES + public static final String PROPS_NAME_REMOVE_METADATA_ENABLE = "delete-early-strategy"; + + } + +} diff --git a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/dto/ConfigItemDTO.java b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/dto/ConfigItemDTO.java new file mode 100644 index 0000000..c728e4c --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/dto/ConfigItemDTO.java @@ -0,0 +1,23 @@ +package it.finanze.sanita.fse2.ms.edsclient.dto; + +import lombok.Getter; +import lombok.Setter; + +import java.util.List; +import java.util.Map; + +@Getter +@Setter +public class ConfigItemDTO { + private String traceId; + private String spanId; + + private List configurationItems; + + @Getter + @Setter + public static class ConfigDataItemDTO { + private String key; + private Map items; + } +} \ No newline at end of file diff --git a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/enums/ConfigItemTypeEnum.java b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/enums/ConfigItemTypeEnum.java new file mode 100644 index 0000000..1735486 --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/enums/ConfigItemTypeEnum.java @@ -0,0 +1,6 @@ +package it.finanze.sanita.fse2.ms.edsclient.enums; + +public enum ConfigItemTypeEnum { + GENERIC, + EDS_CLIENT, +} \ No newline at end of file diff --git a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/IConfigSRV.java b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/IConfigSRV.java new file mode 100644 index 0000000..c2f253d --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/IConfigSRV.java @@ -0,0 +1,6 @@ +package it.finanze.sanita.fse2.ms.edsclient.service; + +public interface IConfigSRV { + + Boolean isRemoveMetadataEnable(); +} diff --git a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/impl/ConfigSRV.java b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/impl/ConfigSRV.java new file mode 100644 index 0000000..4c85373 --- /dev/null +++ b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/impl/ConfigSRV.java @@ -0,0 +1,73 @@ +package it.finanze.sanita.fse2.ms.edsclient.service.impl; + + +import it.finanze.sanita.fse2.ms.edsclient.client.IConfigClient; +import it.finanze.sanita.fse2.ms.edsclient.dto.ConfigItemDTO; +import it.finanze.sanita.fse2.ms.edsclient.enums.ConfigItemTypeEnum; +import it.finanze.sanita.fse2.ms.edsclient.service.IConfigSRV; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.tuple.Pair; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import javax.annotation.PostConstruct; +import java.util.Date; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static it.finanze.sanita.fse2.ms.edsclient.client.routes.base.ClientRoutes.Config.PROPS_NAME_REMOVE_METADATA_ENABLE; +import static it.finanze.sanita.fse2.ms.edsclient.enums.ConfigItemTypeEnum.EDS_CLIENT; +import static it.finanze.sanita.fse2.ms.edsclient.enums.ConfigItemTypeEnum.values; + +@Service +@Slf4j +public class ConfigSRV implements IConfigSRV { + + private static final long DELTA_MS = 300_000L; + + @Autowired + private IConfigClient client; + + private final Map> props; + + public ConfigSRV() { + this.props = new HashMap<>(); + } + + @PostConstruct + public void postConstruct() { + for(ConfigItemTypeEnum en : values()) { + log.info("[GTW-CFG] Retrieving {} properties ...", en.name()); + ConfigItemDTO items = client.getConfigurationItems(en); + List opts = items.getConfigurationItems(); + for(ConfigItemDTO.ConfigDataItemDTO opt : opts) { + opt.getItems().forEach((key, value) -> { + log.info("[GTW-CFG] Property {} is set as {}", key, value); + props.put(key, Pair.of(new Date().getTime(), value)); + }); + } + } + } + + @Override + public Boolean isRemoveMetadataEnable() { + long lastUpdate = props.get(PROPS_NAME_REMOVE_METADATA_ENABLE).getKey(); + if (new Date().getTime() - lastUpdate >= DELTA_MS) { + synchronized(ConfigSRV.class) { + if (new Date().getTime() - lastUpdate >= DELTA_MS) { + refresh(EDS_CLIENT, PROPS_NAME_REMOVE_METADATA_ENABLE); + } + } + } + return Boolean.parseBoolean( + props.get(PROPS_NAME_REMOVE_METADATA_ENABLE).getValue() + ); + } + + private void refresh(ConfigItemTypeEnum type, String name) { + String previous = props.getOrDefault(name, Pair.of(0L, null)).getValue(); + String prop = client.getProps(type, name, previous); + props.put(name, Pair.of(new Date().getTime(), prop)); + } +} diff --git a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/impl/EdsInvocationSRV.java b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/impl/EdsInvocationSRV.java index 16ce66f..ba94bf1 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/impl/EdsInvocationSRV.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/impl/EdsInvocationSRV.java @@ -11,6 +11,7 @@ */ package it.finanze.sanita.fse2.ms.edsclient.service.impl; +import it.finanze.sanita.fse2.ms.edsclient.service.IConfigSRV; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -35,6 +36,9 @@ public class EdsInvocationSRV implements IEdsInvocationSRV { @Autowired private IEdsInvocationRepo edsInvocationRepo; + @Autowired + private IConfigSRV configSRV; + @Autowired private IEdsClient edsClient; @@ -60,7 +64,7 @@ public EdsResponseDTO publishByWorkflowInstanceIdAndPriority(final PublicationRe } } - if(out != null && out.isEsito()) { + if(out != null && out.isEsito() && configSRV.isRemoveMetadataEnable()) { edsInvocationRepo.removeByWorkflowInstanceId(requestBodyDTO.getWorkflowInstanceId()); } @@ -105,7 +109,9 @@ public EdsResponseDTO replaceByWorkflowInstanceIdAndIdentifier(String identifier out = edsClient.dispatchAndSendData(req); } - if(out != null && out.isEsito()) edsInvocationRepo.removeByWorkflowInstanceId(workflowInstanceId); + if(out != null && out.isEsito() && configSRV.isRemoveMetadataEnable()){ + edsInvocationRepo.removeByWorkflowInstanceId(workflowInstanceId); + } return out; } diff --git a/src/main/resources/application-dev.properties b/src/main/resources/application-dev.properties index 4f2328f..bcc134d 100644 --- a/src/main/resources/application-dev.properties +++ b/src/main/resources/application-dev.properties @@ -11,6 +11,7 @@ data.mongodb.schema-name=FSE_GTW # Client Config ####################################### eds-ingestion.url.host=http://localhost:9087 +gtw-config.url.host=http://localhost:8018 ####### LOGGING OUTPUT FORMAT ############ # Must be one of console, json