Subscription Create Event Outcome Kafka Part
[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.event.avc.SubscriptionOutcomeMapper;
34 import org.onap.cps.ncmp.api.impl.events.EventsPublisher;
35 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence;
36 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus;
37 import org.onap.cps.ncmp.api.impl.utils.DataNodeHelper;
38 import org.onap.cps.ncmp.api.models.SubscriptionEventResponse;
39 import org.onap.cps.ncmp.events.avc.subscription.v1.SubscriptionEventOutcome;
40 import org.onap.cps.spi.model.DataNode;
41 import org.springframework.beans.factory.annotation.Value;
42 import org.springframework.stereotype.Component;
43
44 @Component
45 @Slf4j
46 @RequiredArgsConstructor
47 public class SubscriptionEventResponseOutcome {
48
49     private final SubscriptionPersistence subscriptionPersistence;
50
51     private final EventsPublisher<SubscriptionEventOutcome> outcomeEventsPublisher;
52
53     private final SubscriptionOutcomeMapper subscriptionOutcomeMapper;
54
55     @Value("${app.ncmp.avc.subscription-outcome-topic:cm-avc-subscription-response}")
56     private String subscriptionOutcomeEventTopic;
57
58     /**
59      * This is for construction of outcome message to be published for client apps.
60      *
61      * @param subscriptionClientId client id of the subscription.
62      * @param subscriptionName name of the subscription.
63      * @param isFullOutcomeResponse the flag to decide on complete or partial response to be generated.
64      */
65     public void sendResponse(final String subscriptionClientId, final String subscriptionName,
66                              final boolean isFullOutcomeResponse) {
67         final SubscriptionEventOutcome subscriptionEventOutcome = generateResponse(
68                 subscriptionClientId, subscriptionName, isFullOutcomeResponse);
69         final Headers headers = new RecordHeaders();
70         final String subscriptionEventId = subscriptionClientId + subscriptionName;
71         outcomeEventsPublisher.publishEvent(subscriptionOutcomeEventTopic,
72                 subscriptionEventId, headers, subscriptionEventOutcome);
73     }
74
75     private SubscriptionEventOutcome generateResponse(final String subscriptionClientId, final String subscriptionName,
76                                                                  final boolean isFullOutcomeResponse) {
77         final Collection<DataNode> dataNodes = subscriptionPersistence.getDataNodesForSubscriptionEvent();
78         final List<Map<String, Serializable>> dataNodeLeaves = DataNodeHelper.getDataNodeLeaves(dataNodes);
79         final List<Collection<Serializable>> cmHandleIdToStatus =
80                 DataNodeHelper.getCmHandleIdToStatus(dataNodeLeaves);
81         return formSubscriptionOutcomeMessage(cmHandleIdToStatus, subscriptionClientId, subscriptionName,
82                         isFullOutcomeResponse);
83     }
84
85
86     private SubscriptionEventOutcome formSubscriptionOutcomeMessage(
87             final List<Collection<Serializable>> cmHandleIdToStatus, final String subscriptionClientId,
88             final String subscriptionName, final boolean isFullOutcomeResponse) {
89
90         final SubscriptionEventResponse subscriptionEventResponse = toSubscriptionEventResponse(
91                 cmHandleIdToStatus, subscriptionClientId, subscriptionName);
92
93         final SubscriptionEventOutcome subscriptionEventOutcome =
94                 subscriptionOutcomeMapper.toSubscriptionEventOutcome(subscriptionEventResponse);
95
96         if (isFullOutcomeResponse) {
97             subscriptionEventOutcome.setEventType(SubscriptionEventOutcome.EventType.COMPLETE_OUTCOME);
98         } else {
99             subscriptionEventOutcome.setEventType(SubscriptionEventOutcome.EventType.PARTIAL_OUTCOME);
100         }
101
102         return subscriptionEventOutcome;
103     }
104
105     private SubscriptionEventResponse toSubscriptionEventResponse(
106             final List<Collection<Serializable>> cmHandleIdToStatus, final String subscriptionClientId,
107             final String subscriptionName) {
108         final Map<String, SubscriptionStatus> cmHandleIdToStatusMap = new HashMap<>();
109         final SubscriptionEventResponse subscriptionEventResponse = new SubscriptionEventResponse();
110         subscriptionEventResponse.setClientId(subscriptionClientId);
111         subscriptionEventResponse.setSubscriptionName(subscriptionName);
112
113         for (final Collection<Serializable> cmHandleToStatusBucket: cmHandleIdToStatus) {
114             final Iterator<Serializable> bucketIterator = cmHandleToStatusBucket.iterator();
115             while (bucketIterator.hasNext()) {
116                 final String item = (String) bucketIterator.next();
117                 if ("PENDING".equals(item)) {
118                     cmHandleIdToStatusMap.put((String) bucketIterator.next(),
119                             SubscriptionStatus.PENDING);
120                 }
121                 if ("REJECTED".equals(item)) {
122                     cmHandleIdToStatusMap.put((String) bucketIterator.next(),
123                             SubscriptionStatus.REJECTED);
124                 }
125                 if ("ACCEPTED".equals(item)) {
126                     cmHandleIdToStatusMap.put((String) bucketIterator.next(),
127                             SubscriptionStatus.ACCEPTED);
128                 }
129             }
130         }
131         subscriptionEventResponse.setCmHandleIdToStatus(cmHandleIdToStatusMap);
132
133         return subscriptionEventResponse;
134     }
135 }