Skip to content

Commit

Permalink
feat(strategy): add strategy support
Browse files Browse the repository at this point in the history
  • Loading branch information
gb-cic committed Sep 14, 2023
1 parent 0a8654f commit 829a28a
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
public interface IConfigClient {

String getGatewayName();

String getEDSStrategy();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.IConfigClient;
import it.finanze.sanita.fse2.ms.gtw.rulesmanager.config.Constants;
import it.finanze.sanita.fse2.ms.gtw.rulesmanager.dto.WhoIsResponseDTO;
import it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.EdsStrategyEnum;
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;
Expand All @@ -35,30 +36,30 @@ public class ConfigClient implements IConfigClient {
* Config host.
*/
@Value("${ms.url.gtw-config}")
private String configHost;
private String host;

@Autowired
private RestTemplate restTemplate;
private RestTemplate rest;

@Autowired
private ProfileUtility profileUtility;
private ProfileUtility profile;

@Override
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 = host + "/v1/whois";

final boolean isTestEnvironment = profileUtility.isDevOrDockerProfile() || profileUtility.isTestProfile();
final boolean isTestEnvironment = profile.isDevOrDockerProfile() || profile.isTestProfile();

// Check if the endpoint is reachable
if (isTestEnvironment && !isReachable()) {
log.warn("Config Client - Config Client is not reachable, mocking for testing purpose");
return Constants.AppConstants.MOCKED_GATEWAY_NAME;
}

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

if (response.getStatusCode().is2xxSuccessful() && response.hasBody()) {
WhoIsResponseDTO body = response.getBody();
Expand All @@ -73,10 +74,20 @@ public String getGatewayName() {
return gatewayName;
}

@Override
public String getEDSStrategy() {
String output = EdsStrategyEnum.NO_EDS.name();
if(isReachable()) {
String endpoint = host + "/v1/config-items/props?type=GENERIC&props=eds-strategy";
output = rest.getForObject(endpoint,String.class);
}
return output;
}

private boolean isReachable() {
try {
final String endpoint = configHost + "/status";
restTemplate.getForEntity(endpoint, String.class);
final String endpoint = host + "/status";
rest.getForEntity(endpoint, String.class);
return true;
} catch (ResourceAccessException clientException) {
return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums;

public enum EdsStrategyEnum {
NO_EDS,
NO_FHIR_EDS,
NO_EDS_WITH_LOG
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.IEDSClient;
import it.finanze.sanita.fse2.ms.gtw.rulesmanager.logging.LoggerHelper;
import it.finanze.sanita.fse2.ms.gtw.rulesmanager.repository.IExecutorRepo;
import it.finanze.sanita.fse2.ms.gtw.rulesmanager.service.IConfigSRV;
import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand All @@ -27,4 +28,6 @@ public final class BridgeEDS {
private IEDSClient client;
@Autowired
private LoggerHelper logger;
@Autowired
private IConfigSRV config;
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,14 +137,14 @@ protected ActionRes startup(String[] steps) {
// === LISTENERS ===
protected OnStepListener onStepFailure(Date timestamp) {
return (name, status) -> {
if (status == ActionRes.KO) {
if (bridge.getConfig().isNoFhirEds() && status == ActionRes.KO) {
bridge.getLogger().error(LOG_TYPE_CONTROL, "Error while updating GTW configuration items", config.getTitle() + " - " + name, ResultLogEnum.KO, timestamp);
}
};
}
protected OnPlanListener onPlanSuccess(Date timestamp) {
return (status) -> {
if (status == ActionRes.OK) {
if (bridge.getConfig().isNoFhirEds() && status == ActionRes.OK) {
bridge.getLogger().info(LOG_TYPE_CONTROL, "Successfully updated configuration items", "Update" + " - " + config.getTitle(), ResultLogEnum.OK, timestamp);
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.finanze.sanita.fse2.ms.gtw.rulesmanager.service;

public interface IConfigSRV {

String getEdsStrategy();

boolean isNoEds();

boolean isNoFhirEds();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package it.finanze.sanita.fse2.ms.gtw.rulesmanager.service.impl;


import it.finanze.sanita.fse2.ms.gtw.rulesmanager.client.IConfigClient;
import it.finanze.sanita.fse2.ms.gtw.rulesmanager.enums.EdsStrategyEnum;
import it.finanze.sanita.fse2.ms.gtw.rulesmanager.service.IConfigSRV;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ConfigSRV implements IConfigSRV {

private static final Long DELTA_MS = 300000L;

@Autowired
private IConfigClient client;

private String edsStrategy;

private long lastUpdate;

@Async
@EventListener(ApplicationStartedEvent.class)
void initialize() {
refreshEdsStrategy();
}

private void refreshEdsStrategy() {
synchronized (this) {
edsStrategy = client.getEDSStrategy();
lastUpdate = new Date().getTime();
}
}

@Override
public String getEdsStrategy() {
long passedTime = new Date().getTime() - lastUpdate;
if (passedTime >= DELTA_MS) {
synchronized (this) {
refreshEdsStrategy();
}
}
return edsStrategy;
}

@Override
public boolean isNoFhirEds() {
// Trigger refresh if necessary
String out = getEdsStrategy();
// Evaluate
return StringUtils.isNotBlank(out) && EdsStrategyEnum.NO_FHIR_EDS.name().equalsIgnoreCase(out);
}

//Se la strategy è null si setta come default no_eds (quindi non verranno emesse loggate)
@Override
public boolean isNoEds() {
String out = getEdsStrategy();
return StringUtils.isBlank(out) || EdsStrategyEnum.NO_EDS.name().equalsIgnoreCase(out);
}
}

0 comments on commit 829a28a

Please sign in to comment.