-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: attach gtw-config to metadata removal
- Loading branch information
Showing
10 changed files
with
306 additions
and
2 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/IConfigClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/impl/ConfigClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/routes/ConfigClientRoutes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/main/java/it/finanze/sanita/fse2/ms/edsclient/client/routes/base/ClientRoutes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/it/finanze/sanita/fse2/ms/edsclient/dto/ConfigItemDTO.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/it/finanze/sanita/fse2/ms/edsclient/enums/ConfigItemTypeEnum.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/IConfigSRV.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
73 changes: 73 additions & 0 deletions
73
src/main/java/it/finanze/sanita/fse2/ms/edsclient/service/impl/ConfigSRV.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters