Replace deprecated WebSecurityConfigurerAdapter
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / events / avcsubscription / SubscriptionEventResponseOutcome.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.events.avcsubscription;
22
23 import java.io.Serializable;
24 import java.util.Collection;
25 import java.util.HashMap;
26 import java.util.Iterator;
27 import java.util.List;
28 import java.util.Map;
29 import lombok.RequiredArgsConstructor;
30 import lombok.extern.slf4j.Slf4j;
31 import org.apache.kafka.common.header.Headers;
32 import org.apache.kafka.common.header.internals.RecordHeaders;
33 import org.onap.cps.ncmp.api.impl.events.EventsPublisher;
34 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence;
35 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus;
36 import org.onap.cps.ncmp.api.impl.utils.DataNodeHelper;
37 import org.onap.cps.ncmp.api.models.SubscriptionEventResponse;
38 import org.onap.cps.ncmp.events.avc.subscription.v1.SubscriptionEventOutcome;
39 import org.onap.cps.spi.model.DataNode;
40 import org.springframework.beans.factory.annotation.Value;
41 import org.springframework.stereotype.Component;
42
43 @Component
44 @Slf4j
45 @RequiredArgsConstructor
46 public class SubscriptionEventResponseOutcome {
47
48     private final SubscriptionPersistence subscriptionPersistence;
49
50     private final EventsPublisher<SubscriptionEventOutcome> outcomeEventsPublisher;
51
52     private final SubscriptionOutcomeMapper subscriptionOutcomeMapper;
53
54     @Value("${app.ncmp.avc.subscription-outcome-topic:cm-avc-subscription-response}")
55     private String subscriptionOutcomeEventTopic;
56
57     /**
58      * This is for construction of outcome message to be published for client apps.
59      *
60      * @param subscriptionClientId client id of the subscription.
61      * @param subscriptionName name of the subscription.
62      * @param isFullOutcomeResponse the flag to decide on complete or partial response to be generated.
63      */
64     public void sendResponse(final String subscriptionClientId, final String subscriptionName,
65                              final boolean isFullOutcomeResponse) {
66         final SubscriptionEventOutcome subscriptionEventOutcome = generateResponse(
67                 subscriptionClientId, subscriptionName, isFullOutcomeResponse);
68         final Headers headers = new RecordHeaders();
69         final String subscriptionEventId = subscriptionClientId + subscriptionName;
70         outcomeEventsPublisher.publishEvent(subscriptionOutcomeEventTopic,
71                 subscriptionEventId, headers, subscriptionEventOutcome);
72     }
73
74     private SubscriptionEventOutcome generateResponse(final String subscriptionClientId, final String subscriptionName,
75                                                                  final boolean isFullOutcomeResponse) {
76         final Collection<DataNode> dataNodes = subscriptionPersistence.getDataNodesForSubscriptionEvent();
77         final List<Map<String, Serializable>> dataNodeLeaves = DataNodeHelper.getDataNodeLeaves(dataNodes);
78         final List<Collection<Serializable>> cmHandleIdToStatus =
79                 DataNodeHelper.getCmHandleIdToStatus(dataNodeLeaves);
80         return formSubscriptionOutcomeMessage(cmHandleIdToStatus, subscriptionClientId, subscriptionName,
81                         isFullOutcomeResponse);
82     }
83
84
85     private SubscriptionEventOutcome formSubscriptionOutcomeMessage(
86             final List<Collection<Serializable>> cmHandleIdToStatus, final String subscriptionClientId,
87             final String subscriptionName, final boolean isFullOutcomeResponse) {
88
89         final SubscriptionEventResponse subscriptionEventResponse = toSubscriptionEventResponse(
90                 cmHandleIdToStatus, subscriptionClientId, subscriptionName);
91
92         final SubscriptionEventOutcome subscriptionEventOutcome =
93                 subscriptionOutcomeMapper.toSubscriptionEventOutcome(subscriptionEventResponse);
94
95         if (isFullOutcomeResponse) {
96             subscriptionEventOutcome.setEventType(SubscriptionEventOutcome.EventType.COMPLETE_OUTCOME);
97         } else {
98             subscriptionEventOutcome.setEventType(SubscriptionEventOutcome.EventType.PARTIAL_OUTCOME);
99         }
100
101         return subscriptionEventOutcome;
102     }
103
104     private SubscriptionEventResponse toSubscriptionEventResponse(
105             final List<Collection<Serializable>> cmHandleIdToStatus, final String subscriptionClientId,
106             final String subscriptionName) {
107         final Map<String, SubscriptionStatus> cmHandleIdToStatusMap = new HashMap<>();
108         final SubscriptionEventResponse subscriptionEventResponse = new SubscriptionEventResponse();
109         subscriptionEventResponse.setClientId(subscriptionClientId);
110         subscriptionEventResponse.setSubscriptionName(subscriptionName);
111
112         for (final Collection<Serializable> cmHandleToStatusBucket: cmHandleIdToStatus) {
113             final Iterator<Serializable> bucketIterator = cmHandleToStatusBucket.iterator();
114             while (bucketIterator.hasNext()) {
115                 final String item = (String) bucketIterator.next();
116                 if ("PENDING".equals(item)) {
117                     cmHandleIdToStatusMap.put((String) bucketIterator.next(),
118                             SubscriptionStatus.PENDING);
119                 }
120                 if ("REJECTED".equals(item)) {
121                     cmHandleIdToStatusMap.put((String) bucketIterator.next(),
122                             SubscriptionStatus.REJECTED);
123                 }
124                 if ("ACCEPTED".equals(item)) {
125                     cmHandleIdToStatusMap.put((String) bucketIterator.next(),
126                             SubscriptionStatus.ACCEPTED);
127                 }
128             }
129         }
130         subscriptionEventResponse.setCmHandleIdToStatus(cmHandleIdToStatusMap);
131
132         return subscriptionEventResponse;
133     }
134 }