Skip to content

Commit

Permalink
fix: revise gtw-config props
Browse files Browse the repository at this point in the history
  • Loading branch information
gb-cic committed Dec 7, 2023
1 parent 04cfea4 commit 08b76fd
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand All @@ -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;
Expand All @@ -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();

Expand All @@ -67,7 +60,7 @@ public String getGatewayName() {
return Constants.AppConstants.MOCKED_GATEWAY_NAME;
}

final ResponseEntity<WhoIsResponseDTO> response = restTemplate.getForEntity(endpoint, WhoIsResponseDTO.class);
final ResponseEntity<WhoIsResponseDTO> response = client.getForEntity(endpoint, WhoIsResponseDTO.class);

if (response.getStatusCode().is2xxSuccessful() && response.hasBody()) {
WhoIsResponseDTO body = response.getBody();
Expand All @@ -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> T convertResponse(Object response, Object previous) {
try {
Class<T> targetType = (Class<T>) 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> items;
private String traceId;
private String spanId;

private List<ConfigDataItemDTO> configurationItems;

@Getter
@Setter
public static class ConfigDataItemDTO {
private String key;
private Map<String, String> items;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Pair<Long, Object>> props;
private final Map<String, Pair<Long, String>> props;

public ConfigSRV() {
this.props = new HashMap<>();
Expand All @@ -37,34 +37,37 @@ public ConfigSRV() {

@PostConstruct
public void postConstruct() {
for(ConfigItemTypeEnum en : ConfigItemTypeEnum.values()) {
for(Entry<String, String> 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<ConfigDataItemDTO> 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));
}
}

0 comments on commit 08b76fd

Please sign in to comment.