Skip to content

Commit

Permalink
feat(Messages): Uses _ping and the EPS version check when building …
Browse files Browse the repository at this point in the history
…the recipient list to determine if a health department is able to receive messages. This avoids additional configuration and enables faster propagation of the feature.

- uses health-departments _ping and EPS version check to determine if a health-department is able to receive messages
- uses simple caching by a map with stored timestamps and results

Co-authored-by: Jens Kutzsche <[email protected]>
Co-authored-by: lucky-lusa <[email protected]>
PR #668
  • Loading branch information
mad-nuts authored Mar 15, 2022
1 parent 7186e28 commit 446da17
Showing 1 changed file with 64 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
package iris.client_bff.iris_messages.eps;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;
import iris.client_bff.config.RPCClientProperties;
import iris.client_bff.iris_messages.*;
import iris.client_bff.iris_messages.IrisMessage;
import iris.client_bff.iris_messages.IrisMessageException;
import iris.client_bff.iris_messages.IrisMessageHdContact;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.time.Duration;
import java.time.Instant;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.*;

import org.springframework.data.util.Version;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.googlecode.jsonrpc4j.JsonRpcHttpClient;

@Service
@RequiredArgsConstructor
@Slf4j
public class EPSIrisMessageClient {

private static final int READ_TIMEOUT = 12 * 1000;
private static final Duration CACHING_TIME = Duration.ofMinutes(30);

private static final Version MESSAGE_CLIENT_MIN_VERSION = new Version(0, 2, 4);

private static final Map<String, MapEntry> hdCache = new ConcurrentHashMap<>();

private final JsonRpcHttpClient epsRpcClient;
private final RPCClientProperties rpcClientProps;
Expand Down Expand Up @@ -46,15 +66,27 @@ public List<IrisMessageHdContact> getIrisMessageHdContacts() throws IrisMessageE
}
}

private boolean isHealthDepartmentWithInterGaCommunication(DirectoryEntry directoryEntry) {

return
directoryEntry.groups() != null &&
directoryEntry.groups().contains("health-departments") &&
directoryEntry.services() != null &&
directoryEntry.services().stream().anyMatch(service -> service.name().equals("inter-ga-communication"));
private boolean isHealthDepartment(DirectoryEntry directoryEntry) {
return
directoryEntry.groups() != null &&
directoryEntry.groups().contains("health-departments");
}

private boolean isHealthDepartmentWithInterGaCommunication(DirectoryEntry directoryEntry) {

if (!isHealthDepartment(directoryEntry))
return false;

return hdCache.compute(directoryEntry.name, (key, value) -> {

if (value == null || value.validatedAt().isBefore(Instant.now().minus(CACHING_TIME))) {
return new MapEntry(Instant.now(), checkIfEpsVersionGreatEnough(directoryEntry.name));
}

return value;
}).valid();
}

public void createIrisMessage(IrisMessage message) throws IrisMessageException {
String methodName = message.getHdRecipient().getId() + ".createIrisMessage";
Map<String, IrisMessageTransferDto> payload = Map.of("irisMessage", IrisMessageTransferDto.fromEntity(message));
Expand All @@ -69,13 +101,31 @@ public void createIrisMessage(IrisMessage message) throws IrisMessageException {
}
}

private boolean checkIfEpsVersionGreatEnough(String name) {

var methodName = name + "._ping";

try {
Ping ping = epsRpcClient.invoke(methodName, null, Ping.class);
String semver = ping.version.replaceAll("^v", "");
Version version = Version.parse(semver);
return version.isGreaterThanOrEqualTo(MESSAGE_CLIENT_MIN_VERSION);
} catch (Throwable t) {

log.warn("Can't ping hd client " + name);

return false;
}
}

@JsonIgnoreProperties(ignoreUnknown = true)
record Directory(@NotNull List<@Valid DirectoryEntry> entries) {}

@JsonIgnoreProperties(ignoreUnknown = true)
record DirectoryEntry(@NotNull String name, Set<String> groups, List<@Valid DirectoryEntryService> services) {}
record DirectoryEntry(@NotNull String name, Set<String> groups) {}

@JsonIgnoreProperties(ignoreUnknown = true)
record DirectoryEntryService(@NotNull String name) {}
record Ping(String version) {};

private record MapEntry(Instant validatedAt, boolean valid) {};
}

0 comments on commit 446da17

Please sign in to comment.