From 08b76fdf1f9fc2eb8e032588479cbb744caa87f5 Mon Sep 17 00:00:00 2001 From: Giuseppe Baittiner Date: Thu, 7 Dec 2023 17:14:50 +0100 Subject: [PATCH] fix: revise gtw-config props --- .../rulesmanager/client/IConfigClient.java | 2 +- .../client/impl/ConfigClient.java | 86 ++++++------------- .../client/routes/ConfigClientRoutes.java | 29 +++---- .../gtw/rulesmanager/dto/ConfigItemDTO.java | 14 ++- .../rulesmanager/service/impl/ConfigSRV.java | 73 ++++++++-------- 5 files changed, 93 insertions(+), 111 deletions(-) diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/client/IConfigClient.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/client/IConfigClient.java index 13ea026..791c9e1 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/client/IConfigClient.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/client/IConfigClient.java @@ -23,5 +23,5 @@ public interface IConfigClient { ConfigItemDTO getConfigurationItems(ConfigItemTypeEnum type); - Object getProps(ConfigItemTypeEnum type, String props, Object previous); + String getProps(ConfigItemTypeEnum type, String props, String previous); } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/client/impl/ConfigClient.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/client/impl/ConfigClient.java index 6accd11..53437e0 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/client/impl/ConfigClient.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/client/impl/ConfigClient.java @@ -11,14 +11,6 @@ */ package it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.impl; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Component; -import org.springframework.web.client.ResourceAccessException; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.UriComponentsBuilder; - import it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.IConfigClient; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes.ConfigClientRoutes; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.config.Constants; @@ -28,6 +20,11 @@ import it.finanze.sanita.fse2.ms.gtw.rulesmanager.exceptions.BusinessException; import it.finanze.sanita.fse2.ms.gtw.rulesmanager.utility.ProfileUtility; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Component; +import org.springframework.web.client.ResourceAccessException; +import org.springframework.web.client.RestTemplate; /** * Implementation of gtw-config Client. @@ -39,15 +36,11 @@ public class ConfigClient implements IConfigClient { /** * Config host. */ - @Value("${ms.url.gtw-config}") - private String configHost; - - - @Autowired - private ConfigClientRoutes routes; + @Autowired + private ConfigClientRoutes routes; @Autowired - private RestTemplate restTemplate; + private RestTemplate client; @Autowired private ProfileUtility profileUtility; @@ -57,7 +50,7 @@ public String getGatewayName() { String gatewayName; try { log.debug("Config Client - Calling Config Client to get Gateway Name"); - final String endpoint = configHost + "/v1/whois"; + final String endpoint = routes.whois(); final boolean isTestEnvironment = profileUtility.isDevOrDockerProfile() || profileUtility.isTestProfile(); @@ -67,7 +60,7 @@ public String getGatewayName() { return Constants.AppConstants.MOCKED_GATEWAY_NAME; } - final ResponseEntity response = restTemplate.getForEntity(endpoint, WhoIsResponseDTO.class); + final ResponseEntity response = client.getForEntity(endpoint, WhoIsResponseDTO.class); if (response.getStatusCode().is2xxSuccessful() && response.hasBody()) { WhoIsResponseDTO body = response.getBody(); @@ -82,52 +75,29 @@ public String getGatewayName() { return gatewayName; } + @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 = configHost + "/status"; - restTemplate.getForEntity(endpoint, String.class); + final String endpoint = routes.status(); + client.getForEntity(endpoint, String.class); return true; } catch (ResourceAccessException clientException) { return false; } } - - @Override - public Object getProps(ConfigItemTypeEnum type, String props, Object previous) { - Object out = previous; - - String endpoint = routes.getConfigItem(type, props); - - if (isReachable()) { - Object response = restTemplate.getForObject(endpoint, Object.class); - out = convertResponse(response, previous); - } - - return out; - } - - @SuppressWarnings("unchecked") - private T convertResponse(Object response, Object previous) { - try { - Class targetType = (Class) previous.getClass(); - - if (targetType == Integer.class) { - return (T) Integer.valueOf(response.toString()); - } else if (targetType == Boolean.class) { - return (T) Boolean.valueOf(response.toString()); - } else if (targetType == String.class) { - return (T) response.toString(); - } else { - return (T) response; - } - } catch (Exception e) { - return null; - } - } - - @Override - public ConfigItemDTO getConfigurationItems(ConfigItemTypeEnum type) { - UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(routes.base() + "/config-items").queryParam("type", type); - return restTemplate.getForObject(builder.toUriString(), ConfigItemDTO.class); - } } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/client/routes/ConfigClientRoutes.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/client/routes/ConfigClientRoutes.java index 2f79194..132e36c 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/client/routes/ConfigClientRoutes.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/client/routes/ConfigClientRoutes.java @@ -11,31 +11,22 @@ */ package it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes.base.ClientRoutes.Config.API_CONFIG_ITEMS; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes.base.ClientRoutes.Config.API_PROPS; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes.base.ClientRoutes.Config.API_STATUS; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes.base.ClientRoutes.Config.API_WHOIS; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes.base.ClientRoutes.Config.IDENTIFIER_MS; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes.base.ClientRoutes.Config.IDENTIFIER; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes.base.ClientRoutes.Config.API_VERSION; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes.base.ClientRoutes.Config.QP_TYPE; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes.base.ClientRoutes.Config.QP_PROPS; -import org.springframework.beans.factory.annotation.Autowired; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.ConfigItemTypeEnum; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.web.util.UriComponentsBuilder; -import it.finanze.sanita.fse2.ms.gtw.rulesmanager.config.MicroservicesURLCFG; -import it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.ConfigItemTypeEnum; +import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes.base.ClientRoutes.Config.*; @Component public final class ConfigClientRoutes { - @Autowired - private MicroservicesURLCFG msUrlCFG; + @Value("${ms.url.gtw-config}") + private String configHost; public UriComponentsBuilder base() { - return UriComponentsBuilder.fromHttpUrl(msUrlCFG.getConfigHost()); + return UriComponentsBuilder.fromHttpUrl(configHost); } public String identifier() { @@ -69,4 +60,12 @@ public String getConfigItem(ConfigItemTypeEnum type, String props) { .toUriString(); } + public String getConfigItems(ConfigItemTypeEnum type) { + return base() + .pathSegment(API_VERSION, API_CONFIG_ITEMS) + .queryParam(QP_TYPE, type.name()) + .build() + .toUriString(); + } + } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/dto/ConfigItemDTO.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/dto/ConfigItemDTO.java index 9c4411a..604c430 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/dto/ConfigItemDTO.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/dto/ConfigItemDTO.java @@ -3,11 +3,21 @@ import lombok.Getter; import lombok.Setter; +import java.util.List; import java.util.Map; @Getter @Setter public class ConfigItemDTO { - private String key; - private Map items; + private String traceId; + private String spanId; + + private List configurationItems; + + @Getter + @Setter + public static class ConfigDataItemDTO { + private String key; + private Map items; + } } diff --git a/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/service/impl/ConfigSRV.java b/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/service/impl/ConfigSRV.java index 1d49784..a1a2513 100644 --- a/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/service/impl/ConfigSRV.java +++ b/src/main/java/it/finanze/sanita/fse2/ms/gtw/rulesmanager/service/impl/ConfigSRV.java @@ -1,34 +1,34 @@ package it.finanze.sanita.fse2.ms.gtw.rulesmanager.service.impl; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes.base.ClientRoutes.Config.PROPS_NAME_CONTROL_LOG_ENABLED; -import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.ConfigItemTypeEnum.GENERIC; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.IConfigClient; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.dto.ConfigItemDTO; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.dto.ConfigItemDTO.ConfigDataItemDTO; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.ConfigItemTypeEnum; +import it.finanze.sanita.fse2.ms.gtw.rulesmanager.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 java.util.Map.Entry; - -import javax.annotation.PostConstruct; - -import org.apache.commons.lang3.tuple.Pair; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.IConfigClient; -import it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.ConfigItemTypeEnum; -import it.finanze.sanita.fse2.ms.gtw.rulesmanager.service.IConfigSRV; +import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.routes.base.ClientRoutes.Config.PROPS_NAME_CONTROL_LOG_ENABLED; +import static it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.ConfigItemTypeEnum.RULES_MANAGER; @Service +@Slf4j public class ConfigSRV implements IConfigSRV { private static final long DELTA_MS = 300_000L; @Autowired private IConfigClient client; - - private long lastUpdate; - private final Map> props; + private final Map> props; public ConfigSRV() { this.props = new HashMap<>(); @@ -37,34 +37,37 @@ public ConfigSRV() { @PostConstruct public void postConstruct() { - for(ConfigItemTypeEnum en : ConfigItemTypeEnum.values()) { - for(Entry el : client.getConfigurationItems(en).getItems().entrySet()) { - props.put(el.getKey(), Pair.of(new Date().getTime(), el.getValue())); - } - } - } - - - - private void refreshControlLogPersistenceEnable() { - Boolean previous = props.get(PROPS_NAME_CONTROL_LOG_ENABLED)!=null ? (Boolean)props.get(PROPS_NAME_CONTROL_LOG_ENABLED).getValue() : null; - Boolean controlLogEnabled = (Boolean)client.getProps(GENERIC, PROPS_NAME_CONTROL_LOG_ENABLED,previous); - props.put(PROPS_NAME_CONTROL_LOG_ENABLED, Pair.of(new Date().getTime(), controlLogEnabled)); - + for(ConfigItemTypeEnum en : ConfigItemTypeEnum.values()) { + log.info("[GTW-CFG] Retrieving {} properties ...", en.name()); + ConfigItemDTO items = client.getConfigurationItems(en); + List opts = items.getConfigurationItems(); + for(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 isControlLogPersistenceEnable() { + long lastUpdate = props.get(PROPS_NAME_CONTROL_LOG_ENABLED).getKey(); if (new Date().getTime() - lastUpdate >= DELTA_MS) { synchronized(ConfigSRV.class) { - if (new Date().getTime() - lastUpdate >= DELTA_MS) { - refreshControlLogPersistenceEnable(); - } + if (new Date().getTime() - lastUpdate >= DELTA_MS) { + refresh(RULES_MANAGER, PROPS_NAME_CONTROL_LOG_ENABLED); + } } } - return (Boolean)props.get(PROPS_NAME_CONTROL_LOG_ENABLED).getValue(); + return Boolean.parseBoolean( + props.get(PROPS_NAME_CONTROL_LOG_ENABLED).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)); + } }