Skip to content

Commit

Permalink
Feature/bytte endepukter til kodeverk service (#3454)
Browse files Browse the repository at this point in the history
Oppdatere klienter til bruk av kodeverk-service
  • Loading branch information
krharum authored Apr 4, 2024
1 parent 57073c9 commit 970de99
Show file tree
Hide file tree
Showing 54 changed files with 340 additions and 756 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ public static void main(String[] args) {

SpringApplication.run(DollyBackendApplicationStarter.class, args);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,14 @@ public class CachingConfig {
public static final String CACHE_BRUKER = "bruker";
public static final String CACHE_GRUPPE = "gruppe";
public static final String CACHE_HELSEPERSONELL = "helsepersonell";
public static final String CACHE_KODEVERK = "kodeverk";
public static final String CACHE_KODEVERK_2 = "kodeverk2";

@Bean
@Profile({ "dev", "prod" })
public CacheManager cacheManager(Caffeine caffeine) {
var caffeineCacheManager = new CaffeineCacheManager(CACHE_BESTILLING,
CACHE_BRUKER,
CACHE_GRUPPE,
CACHE_HELSEPERSONELL,
CACHE_KODEVERK,
CACHE_KODEVERK_2
CACHE_HELSEPERSONELL
);
caffeineCacheManager.setCaffeine(caffeine);
caffeineCacheManager.setAsyncCacheMode(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,6 @@

import static lombok.AccessLevel.PACKAGE;

/**
* Samler alle placeholders for ulike {@code consumers.*}-konfigurasjon her, dvs. subklasser av {@code ServerProperties}.
* <br/><br/>
* Husk at Spring Boot bruker <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.external-config.typesafe-configuration-properties.relaxed-binding">relaxed binding</a>
* mellom configuration properties og field names.
*
* @see ServerProperties
*/
@Configuration
@ConfigurationProperties(prefix = "consumers")
@NoArgsConstructor(access = PACKAGE)
Expand All @@ -34,10 +26,11 @@ public class Consumers {
private ServerProperties testnavInntektsmeldingService;
private ServerProperties testnavInntektstubProxy;
private ServerProperties testnavInstProxy;
private ServerProperties kodeverkApi;
private ServerProperties testnavKodeverkService;
private ServerProperties testnavKontoregisterPersonProxy;
private ServerProperties testnavKrrstubProxy;
private ServerProperties testnavMedlProxy;
private ServerProperties testnavMiljoerService;
private ServerProperties testnavNorg2Proxy;
private ServerProperties testnavOrganisasjonForvalter;
private ServerProperties testnavOrganisasjonService;
Expand All @@ -50,7 +43,5 @@ public class Consumers {
private ServerProperties testnavSykemeldingApi;
private ServerProperties testnavSyntSykemeldingApi;
private ServerProperties testnavTpsMessagingService;
private ServerProperties testnavMiljoerService;
private ServerProperties testnavUdistubProxy;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,111 +2,42 @@

import lombok.extern.slf4j.Slf4j;
import no.nav.dolly.config.Consumers;
import no.nav.dolly.consumer.kodeverk.domain.KodeverkBetydningerResponse;
import no.nav.dolly.metrics.Timed;
import no.nav.testnav.libs.reactivecore.utils.WebClientFilter;
import no.nav.testnav.libs.securitycore.config.UserConstant;
import no.nav.dolly.consumer.kodeverk.command.KodeverkGetCommand;
import no.nav.testnav.libs.dto.kodeverkservice.v1.KodeverkDTO;
import no.nav.testnav.libs.securitycore.domain.ServerProperties;
import no.nav.testnav.libs.standalone.servletsecurity.exchange.TokenExchange;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import java.time.Duration;
import java.time.LocalDate;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import static no.nav.dolly.config.CachingConfig.CACHE_KODEVERK_2;
import static no.nav.dolly.domain.CommonKeysAndUtils.CONSUMER;
import static no.nav.dolly.domain.CommonKeysAndUtils.HEADER_NAV_CALL_ID;
import static no.nav.dolly.domain.CommonKeysAndUtils.HEADER_NAV_CONSUMER_ID;
import static no.nav.dolly.util.CallIdUtil.generateCallId;
import static no.nav.dolly.util.TokenXUtil.getUserJwt;

@Component
@Service
@Slf4j
public class KodeverkConsumer {

private static final String KODEVERK_URL_BEGINNING = "/api/v1/kodeverk";
private static final String KODEVERK_URL_KODER = "koder";
private static final String KODEVERK_URL_BETYDNINGER = "betydninger";

private final TokenExchange tokenService;
private final WebClient webClient;
private final ServerProperties serverProperties;

public KodeverkConsumer(
TokenExchange tokenService,
Consumers consumers,
WebClient.Builder webClientBuilder
) {
WebClient.Builder webClientBuilder) {

this.tokenService = tokenService;
serverProperties = consumers.getKodeverkApi();
serverProperties = consumers.getTestnavKodeverkService();
this.webClient = webClientBuilder
.exchangeStrategies(
ExchangeStrategies
.builder()
.codecs(configurer -> configurer
.defaultCodecs()
.maxInMemorySize(32 * 1024 * 1024))
.build())
.baseUrl(serverProperties.getUrl())
.build();
}

private static String getNorskBokmaal(Entry<String, java.util.List<KodeverkBetydningerResponse.Betydning>> entry) {

return entry.getValue().get(0).getBeskrivelser().get("nb").getTekst();
}

@Timed(name = "providers", tags = {"operation", "hentKodeverk"})
public Flux<KodeverkBetydningerResponse> fetchKodeverkByName(String kodeverk) {

return getKodeverk(kodeverk);
}

@Cacheable(CACHE_KODEVERK_2)
@Timed(name = "providers", tags = {"operation", "hentKodeverk"})
public Mono<Map<String, String>> getKodeverkByName(String kodeverk) {

return getKodeverk(kodeverk)
.map(KodeverkBetydningerResponse::getBetydninger)
.map(Map::entrySet)
.flatMap(Flux::fromIterable)
.filter(entry -> !entry.getValue().isEmpty())
.filter(entry -> LocalDate.now().isAfter(entry.getValue().get(0).getGyldigFra()) &&
LocalDate.now().isBefore(entry.getValue().get(0).getGyldigTil()))
.collect(Collectors.toMap(Entry::getKey, KodeverkConsumer::getNorskBokmaal))
.cache(Duration.ofHours(9));
}

private Flux<KodeverkBetydningerResponse> getKodeverk(String kodeverk) {

return tokenService.exchange(serverProperties)
.flatMapMany(token -> webClient
.get()
.uri(uriBuilder -> uriBuilder
.path(KODEVERK_URL_BEGINNING)
.pathSegment(kodeverk)
.pathSegment(KODEVERK_URL_KODER)
.pathSegment(KODEVERK_URL_BETYDNINGER)
.queryParam("ekskluderUgyldige", true)
.queryParam("spraak", "nb")
.build())
.header(HttpHeaders.AUTHORIZATION, "Bearer " + token.getTokenValue())
.header(UserConstant.USER_HEADER_JWT, getUserJwt())
.header(HEADER_NAV_CONSUMER_ID, CONSUMER)
.header(HEADER_NAV_CALL_ID, generateCallId())
.retrieve()
.bodyToFlux(KodeverkBetydningerResponse.class)
.retryWhen(Retry.backoff(3, Duration.ofSeconds(5))
.filter(WebClientFilter::is5xxException)));
.flatMap(token -> new KodeverkGetCommand(webClient, kodeverk, token.getTokenValue()).call())
.doOnNext(response -> log.info("Hentet kodeverk {}, status {} melding {}",
response.getKodeverknavn(), response.getStatus(), response.getMessage()))
.map(KodeverkDTO::getKodeverk);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package no.nav.dolly.consumer.kodeverk.command;

import lombok.RequiredArgsConstructor;
import no.nav.testnav.libs.dto.kodeverkservice.v1.KodeverkDTO;
import no.nav.testnav.libs.reactivecore.utils.WebClientFilter;
import org.springframework.http.HttpHeaders;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.util.retry.Retry;

import java.time.Duration;
import java.util.concurrent.Callable;

@RequiredArgsConstructor
public class KodeverkGetCommand implements Callable<Mono<KodeverkDTO>> {

private static final String KODEVERK_URL = "/api/v1/kodeverk";
private static final String KODEVERK = "kodeverk";

private final WebClient webClient;
private final String kodeverk;
private final String token;

@Override
public Mono<KodeverkDTO> call() {

return webClient.get()
.uri(uriBuilder -> uriBuilder.path(KODEVERK_URL)
.queryParam(KODEVERK, kodeverk)
.build())
.header(HttpHeaders.AUTHORIZATION, "Bearer " + token)
.retrieve()
.bodyToMono(KodeverkDTO.class)
.doOnError(WebClientFilter::logErrorMessage)
.onErrorResume(error -> Mono.just(KodeverkDTO.builder()
.kodeverknavn(kodeverk)
.status(WebClientFilter.getStatus(error))
.message(WebClientFilter.getMessage(error))
.build()))
.retryWhen(Retry.backoff(3, Duration.ofSeconds(5))
.filter(WebClientFilter::is5xxException));
}
}

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 970de99

Please sign in to comment.