aboutsummaryrefslogtreecommitdiffstats
path: root/prh-app-server/src/main/java/org/onap/dcaegen2/services/prh/tasks/AaiQueryTaskImpl.java
blob: 7313192626516dea6c3fb25efcec504846d00311 (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
/*
 * ============LICENSE_START=======================================================
 * PNF-REGISTRATION-HANDLER
 * ================================================================================
 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
 * Copyright (C) 2023 Deutsche Telekom 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.
 *
 *      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 java.util.List;
import java.util.Optional;
import java.util.stream.Stream;
import org.onap.dcaegen2.services.prh.adapter.aai.api.AaiHttpClient;
import org.onap.dcaegen2.services.prh.adapter.aai.api.AaiPnfResultModel;
import org.onap.dcaegen2.services.prh.adapter.aai.api.AaiServiceInstanceQueryModel;
import org.onap.dcaegen2.services.prh.adapter.aai.api.AaiServiceInstanceResultModel;
import org.onap.dcaegen2.services.prh.adapter.aai.api.ConsumerDmaapModel;
import org.onap.dcaegen2.services.prh.adapter.aai.api.ImmutableAaiServiceInstanceQueryModel;
import org.onap.dcaegen2.services.prh.model.Relationship;
import org.onap.dcaegen2.services.prh.model.RelationshipData;
import org.onap.dcaegen2.services.prh.model.RelationshipDict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component
public class AaiQueryTaskImpl implements AaiQueryTask {
    static final String ACTIVE_STATUS = "Active";
    static final String RELATED_TO = "service-instance";
    static final String CUSTOMER = "customer.global-customer-id";
    static final String SERVICE_TYPE = "service-subscription.service-type";
    static final String SERVICE_INSTANCE_ID = "service-instance.service-instance-id";

    private static final Logger LOGGER = LoggerFactory.getLogger(AaiQueryTaskImpl.class);
    private final AaiHttpClient<ConsumerDmaapModel, AaiPnfResultModel> getPnfModelClient;
    private final AaiHttpClient<AaiServiceInstanceQueryModel, AaiServiceInstanceResultModel> getServiceClient;

    @Autowired
    public AaiQueryTaskImpl(
            final AaiHttpClient<ConsumerDmaapModel, AaiPnfResultModel> getPnfModelClient,
            final AaiHttpClient<AaiServiceInstanceQueryModel, AaiServiceInstanceResultModel> getServiceClient) {
        this.getPnfModelClient = getPnfModelClient;
        this.getServiceClient = getServiceClient;
    }

    

    @Override
    public Mono<Boolean> execute(ConsumerDmaapModel aaiModel) {

        return getPnfModelClient
                .getAaiResponse(aaiModel)
                .flatMap(this::checkIfPnfHasRelationToService)
                .flatMap(getServiceClient::getAaiResponse)
                .map(this::checkIfRelatedServiceInstanceIsActive)
                .defaultIfEmpty(false);
    }


   // Added by DTAG, March 2023
    @Override
    public Mono<ConsumerDmaapModel> findPnfinAAI(final ConsumerDmaapModel model) {

        return getPnfModelClient
                .getAaiResponse(model)
                .flatMap(aaiModel -> Mono.just(model));
    }
     


    private Mono<AaiServiceInstanceQueryModel> checkIfPnfHasRelationToService(final AaiPnfResultModel model) {

        return Mono
                .justOrEmpty(model.getRelationshipList())
                .map(this::findRelatedTo)
                .flatMap(Mono::justOrEmpty)
                .map(RelationshipDict::getRelationshipData)
                .flatMap(x -> {
                    final Optional<String> customer = findValue(x, CUSTOMER);
                    final Optional<String> serviceType = findValue(x, SERVICE_TYPE);
                    final Optional<String> serviceInstanceId= findValue(x, SERVICE_INSTANCE_ID);

                    return customer.isPresent() && serviceType.isPresent() && serviceInstanceId.isPresent()
                            ? Mono.just(ImmutableAaiServiceInstanceQueryModel
                            .builder()
                            .customerId(customer.get())
                            .serviceType(serviceType.get())
                            .serviceInstanceId(serviceInstanceId.get())
                            .build())
                            : Mono.empty();
                });
    }

    private Boolean checkIfRelatedServiceInstanceIsActive(final AaiServiceInstanceResultModel model) {

        return ACTIVE_STATUS.equalsIgnoreCase(model.getOrchestrationStatus());
    }

    private Optional<RelationshipDict> findRelatedTo(final Relationship data) {

        return Optional.ofNullable(data.getRelationship())
                .map(Stream::of)
                .orElseGet(Stream::empty)
                .flatMap(List::stream)
                .filter(x -> RELATED_TO.equals(x.getRelatedTo()))
                .findFirst();
    }

    private Optional<String> findValue(final List<RelationshipData> data, final String key) {

        return data
                .stream()
                .filter(y -> key.equals(y.getRelationshipKey()))
                .findFirst()
                .map(RelationshipData::getRelationshipValue);
    }
  
}