diff options
Diffstat (limited to 'prh-app-server/src/main/java')
-rw-r--r-- | prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/BbsActionsTaskImpl.java | 143 |
1 files changed, 105 insertions, 38 deletions
diff --git a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/BbsActionsTaskImpl.java b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/BbsActionsTaskImpl.java index 126d6f99..7f4f163f 100644 --- a/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/BbsActionsTaskImpl.java +++ b/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/BbsActionsTaskImpl.java @@ -20,31 +20,41 @@ package org.onap.dcaegen2.services.prh.tasks; +import static io.netty.handler.codec.http.HttpHeaders.Values.APPLICATION_JSON; +import static java.nio.charset.StandardCharsets.UTF_8; +import static org.apache.http.HttpHeaders.CONTENT_TYPE; +import static org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod.DELETE; +import static org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod.GET; +import static org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod.PUT; + import com.google.gson.JsonObject; import io.vavr.collection.HashMap; import io.vavr.collection.Map; +import java.util.function.Function; +import org.jetbrains.annotations.NotNull; import org.onap.dcaegen2.services.prh.configuration.Config; import org.onap.dcaegen2.services.prh.exceptions.AaiFailureException; +import org.onap.dcaegen2.services.prh.model.AaiPnfResultModel; import org.onap.dcaegen2.services.prh.model.ConsumerDmaapModel; +import org.onap.dcaegen2.services.prh.model.ImmutableRelationshipDict; +import org.onap.dcaegen2.services.prh.model.Relationship; +import org.onap.dcaegen2.services.prh.model.RelationshipDict; import org.onap.dcaegen2.services.prh.model.bbs.ImmutableLogicalLink; -import org.onap.dcaegen2.services.prh.model.bbs.ImmutableRelationship; -import org.onap.dcaegen2.services.prh.model.bbs.ImmutableRelationshipWrapper; -import org.onap.dcaegen2.services.prh.model.bbs.RelationshipWrapper; +import org.onap.dcaegen2.services.prh.model.bbs.LogicalLink; import org.onap.dcaegen2.services.prh.model.utils.HttpUtils; import org.onap.dcaegen2.services.prh.model.utils.PrhModelAwareGsonBuilder; -import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.*; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpResponse; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.ImmutableHttpRequest; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RequestBody; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClient; +import org.onap.dcaegen2.services.sdk.rest.services.adapters.http.RxHttpClientFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import java.util.Arrays; -import java.util.List; -import java.util.function.Function; - -import static org.onap.dcaegen2.services.sdk.rest.services.adapters.http.HttpMethod.PUT; - @Component public class BbsActionsTaskImpl implements BbsActionsTask { @@ -52,6 +62,9 @@ public class BbsActionsTaskImpl implements BbsActionsTask { private static final String ATTACHMENT_POINT = "attachment-point"; private static final String LOGICAL_LINK_URI = "/network/logical-links/logical-link"; private static final String PNF_URI = "/network/pnfs/pnf"; + private static final String LINK_KEY = "logical-link.link-name"; + private static final String ERROR_PREFIX = "Incorrect response when performing BBS-related actions: "; + private static final String LOGICAL_LINK = "logical-link"; private final Config config; private final RxHttpClient httpClient; @@ -67,7 +80,6 @@ public class BbsActionsTaskImpl implements BbsActionsTask { } public Mono<ConsumerDmaapModel> execute(ConsumerDmaapModel consumerDmaapModel) { - JsonObject additionalFields = consumerDmaapModel.getAdditionalFields(); if (additionalFields == null || !additionalFields.has(ATTACHMENT_POINT)) { return Mono.just(consumerDmaapModel); @@ -78,31 +90,69 @@ public class BbsActionsTaskImpl implements BbsActionsTask { return Mono.just(consumerDmaapModel); } String pnfName = consumerDmaapModel.getCorrelationId(); - return createLogicalLinkInAai(linkName, pnfName).flatMap(handleResponse(consumerDmaapModel)); + + return getLinksByPnf(pnfName) + .flatMap(x -> Flux.fromIterable(x.getRelationshipData())) + .filter(x -> LINK_KEY.equals(x.getRelationshipKey())) + .map(x -> x.getRelationshipValue()) + .flatMap(oldLinkName -> getLogicalLink(oldLinkName)) + .filter(oldLink -> oldLink.getLinkType().equals(ATTACHMENT_POINT)) + .flatMap(oldLink -> deleteLogicalLinkInAai(oldLink.getLinkName(), oldLink.getResourceVersion())) + .then(createLogicalLinkInAai(linkName, pnfName)) + .flatMap(response -> handleFinalResponse(response, consumerDmaapModel)); } - private Function<HttpResponse, Mono<ConsumerDmaapModel>> handleResponse(ConsumerDmaapModel consumerDmaapModel) { - return response -> HttpUtils.isSuccessfulResponseCode(response.statusCode()) - ? Mono.just(consumerDmaapModel) - : Mono.error(new AaiFailureException( - "Incorrect response when performing BBS-related actions: " + response.statusCode())); + private Flux<RelationshipDict> getLinksByPnf(String pnfName) { + return httpClient.call(buildGetRequest(PNF_URI + "/" + pnfName)) + .flatMap(response -> handleResponse(response, "GET PNF request. Pnf name: " + pnfName)) + .map(httpResponse -> httpResponse.bodyAsJson(UTF_8, + PrhModelAwareGsonBuilder.createGson(), AaiPnfResultModel.class)) + .flatMapMany(pnfModel -> Flux.fromStream(pnfModel.getRelationshipList().getRelationship().stream())) + .filter(x -> LOGICAL_LINK.equals(x.getRelatedTo())); + } + + private Mono<LogicalLink> getLogicalLink(String linkName) { + ImmutableHttpRequest request = buildGetRequest(LOGICAL_LINK_URI + "/" + linkName); + return httpClient.call(request) + .flatMap(response -> handleResponse(response, "GET LogicalLink request. Link name: " + linkName)) + .map(httpResponse -> httpResponse.bodyAsJson(UTF_8, + PrhModelAwareGsonBuilder.createGson(), LogicalLink.class)); } private Mono<HttpResponse> createLogicalLinkInAai(String linkName, String pnfName) { - ImmutableHttpRequest request = buildLogicalLinkRequest(linkName, pnfName); + ImmutableHttpRequest request = buildLogicalLinkPutRequest(linkName, pnfName); + return httpClient.call(request) + .flatMap(response -> handleResponse(response, "PUT LogicalLink request. Link name: " + linkName)); + } - return httpClient.call(request); + private Mono<HttpResponse> deleteLogicalLinkInAai(String linkName, String resourceVersion) { + ImmutableHttpRequest request = buildLogicalLinkDeleteRequest(linkName, resourceVersion); + return httpClient.call(request) + .flatMap(response -> handleResponse(response, "DELETE LogicalLink request. Link name: " + linkName)); } - private ImmutableHttpRequest buildLogicalLinkRequest(String linkName, String pnfName) { - String uri = buildLogicalLinkUri(linkName); - ImmutableLogicalLink logicalLink = buildModel(linkName, pnfName); + private ImmutableHttpRequest buildGetRequest(String path) { + String uri = buildUri(path); + Map<String, String> aaiHeaders = HashMap + .ofAll(config.getAaiClientConfiguration().aaiHeaders()); + + return ImmutableHttpRequest + .builder() + .method(GET) + .url(uri) + .customHeaders(aaiHeaders) + .build(); + } + + private ImmutableHttpRequest buildLogicalLinkPutRequest(String linkName, String pnfName) { + String uri = buildUri(LOGICAL_LINK_URI + "/" + linkName); + ImmutableLogicalLink logicalLink = prepareModelBuilder(linkName, pnfName).build(); RequestBody requestBody = RequestBody.fromString(PrhModelAwareGsonBuilder.createGson().toJson(logicalLink)); // FIXME: AAI headers for PUT are different than PATCH (taken from prh_endpoints.json) Map<String, String> aaiHeaders = HashMap - .ofAll(config.getAaiClientConfiguration().aaiHeaders()) - .put("Content-Type", "application/json"); + .ofAll(config.getAaiClientConfiguration().aaiHeaders()) + .put(CONTENT_TYPE, APPLICATION_JSON); return ImmutableHttpRequest .builder() @@ -113,29 +163,46 @@ public class BbsActionsTaskImpl implements BbsActionsTask { .build(); } - private ImmutableLogicalLink buildModel(String linkName, String pnfName) { - List<RelationshipWrapper> relationships = buildRelationLink(pnfName); + private ImmutableHttpRequest buildLogicalLinkDeleteRequest(String linkName, String resourceVersion) { + String uri = buildUri(LOGICAL_LINK_URI + "/" + linkName + "?resource-version=" + resourceVersion); + + Map<String, String> aaiHeaders = HashMap + .ofAll(config.getAaiClientConfiguration().aaiHeaders()) + .put(CONTENT_TYPE, APPLICATION_JSON); + + return ImmutableHttpRequest + .builder() + .method(DELETE) + .url(uri) + .customHeaders(aaiHeaders) + .build(); + } + + private ImmutableLogicalLink.Builder prepareModelBuilder(String linkName, String pnfName) { + Relationship relationship = org.onap.dcaegen2.services.prh.model.ImmutableRelationship.builder() + .addRelationship( + ImmutableRelationshipDict.builder().relatedLink(PNF_URI + pnfName).build()).build(); return ImmutableLogicalLink .builder() .linkName(linkName) .linkType(ATTACHMENT_POINT) - .relationshipList(relationships) - .build(); + .relationshipList(relationship); } - private List<RelationshipWrapper> buildRelationLink(String pnfName) { - return Arrays.asList(ImmutableRelationshipWrapper - .builder() - .relationship(ImmutableRelationship - .builder() - .relatedLink(PNF_URI + "/" + pnfName) - .build()) - .build()); + private Mono<HttpResponse> handleResponse(HttpResponse response, String msg) { + return HttpUtils.isSuccessfulResponseCode(response.statusCode()) ? Mono.just(response) : + Mono.error(new AaiFailureException(ERROR_PREFIX + response.statusCode() + ". Occurred in " + msg)); + } + + private Mono<? extends ConsumerDmaapModel> handleFinalResponse( + HttpResponse response, ConsumerDmaapModel consumerDmaapModel) { + return HttpUtils.isSuccessfulResponseCode(response.statusCode()) + ? Mono.just(consumerDmaapModel) : Mono.error(new AaiFailureException(ERROR_PREFIX + response.statusCode())); } - private String buildLogicalLinkUri(String linkName) { - return config.getAaiClientConfiguration().pnfUrl().replace(PNF_URI, LOGICAL_LINK_URI) + "/" + linkName; + private String buildUri(String path) { + return config.getAaiClientConfiguration().pnfUrl().replace(PNF_URI, path); } } |