aboutsummaryrefslogtreecommitdiffstats
path: root/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/BbsActionsTaskImpl.java
blob: c949b774d28e40ff8307da0cd853e0f669a09dd7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * ============LICENSE_START=======================================================
 * PNF-REGISTRATION-HANDLER
 * ================================================================================
 * Copyright (C) 2019 NOKIA Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

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 static org.springframework.web.util.UriUtils.encodeFragment;

import com.google.gson.JsonObject;
import io.vavr.collection.HashMap;
import io.vavr.collection.Map;
import org.onap.dcaegen2.services.prh.adapter.aai.api.AaiPnfResultModel;
import org.onap.dcaegen2.services.prh.adapter.aai.api.ConsumerDmaapModel;
import org.onap.dcaegen2.services.prh.configuration.Config;
import org.onap.dcaegen2.services.prh.exceptions.AaiFailureException;
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.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.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;

@Component
public class BbsActionsTaskImpl implements BbsActionsTask {

    private static final Logger LOGGER = LoggerFactory.getLogger(BbsActionsTaskImpl.class);
    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;

    @Autowired
    BbsActionsTaskImpl(Config config) {
        this(config, RxHttpClientFactory.createInsecure());
    }

    BbsActionsTaskImpl(Config config, RxHttpClient httpClient) {
        this.config = config;
        this.httpClient = httpClient;
    }

    public Mono<ConsumerDmaapModel> execute(ConsumerDmaapModel consumerDmaapModel) {
        JsonObject additionalFields = consumerDmaapModel.getAdditionalFields();
        if (additionalFields == null || !additionalFields.has(ATTACHMENT_POINT)) {
            return Mono.just(consumerDmaapModel);
        }
        String linkName = additionalFields.get(ATTACHMENT_POINT).getAsString();
        if (linkName.isEmpty()) {
            LOGGER.warn("Attachment point is empty! Ignore related actions.");
            return Mono.just(consumerDmaapModel);
        }
        String pnfName = consumerDmaapModel.getCorrelationId();

        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 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 = buildLogicalLinkPutRequest(linkName, pnfName);
        LOGGER.debug("Creating logical link in AAI {} ", request);
        return httpClient.call(request)
            .flatMap(response -> handleResponse(response, "PUT LogicalLink request. Link name: " + linkName));
    }

    private Mono<HttpResponse> deleteLogicalLinkInAai(String linkName, String resourceVersion) {
        ImmutableHttpRequest request = buildLogicalLinkDeleteRequest(linkName, resourceVersion);
        LOGGER.debug("Deleting logical link in AAI {} ", request);
        return httpClient.call(request)
            .flatMap(response -> handleResponse(response, "DELETE LogicalLink request. Link name:  " + linkName));
    }

    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);

        return ImmutableHttpRequest
            .builder()
            .method(PUT)
            .url(uri)
            .body(requestBody)
            .customHeaders(aaiHeaders)
            .build();
    }

    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(relationship);
    }

    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 buildUri(String path) {
        return config.getAaiClientConfiguration().pnfUrl().replace(PNF_URI, encodeFragment(path, UTF_8));
    }
}