Skip to content

Commit

Permalink
feat: attach gtw-config to metadata removal
Browse files Browse the repository at this point in the history
  • Loading branch information
gb-cic committed Dec 7, 2023
1 parent faa1089 commit 77474e9
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
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);
}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/
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";

}

}
Original file line number Diff line number Diff line change
@@ -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<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
@@ -0,0 +1,6 @@
package it.finanze.sanita.fse2.ms.edsclient.enums;

public enum ConfigItemTypeEnum {
GENERIC,
EDS_CLIENT,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package it.finanze.sanita.fse2.ms.edsclient.service;

public interface IConfigSRV {

Boolean isRemoveMetadataEnable();
}
Original file line number Diff line number Diff line change
@@ -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<String, Pair<Long, String>> 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<ConfigItemDTO.ConfigDataItemDTO> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -35,6 +36,9 @@ public class EdsInvocationSRV implements IEdsInvocationSRV {
@Autowired
private IEdsInvocationRepo edsInvocationRepo;

@Autowired
private IConfigSRV configSRV;

@Autowired
private IEdsClient edsClient;

Expand All @@ -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());
}

Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application-dev.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 77474e9

Please sign in to comment.