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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.impl.events.avcsubscription;
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;
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;
46 @RequiredArgsConstructor
47 public class SubscriptionEventResponseOutcome {
49 private final SubscriptionPersistence subscriptionPersistence;
51 private final EventsPublisher<SubscriptionEventOutcome> outcomeEventsPublisher;
53 private final SubscriptionOutcomeMapper subscriptionOutcomeMapper;
55 @Value("${app.ncmp.avc.subscription-outcome-topic:cm-avc-subscription-response}")
56 private String subscriptionOutcomeEventTopic;
59 * This is for construction of outcome message to be published for client apps.
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.
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);
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);
86 private SubscriptionEventOutcome formSubscriptionOutcomeMessage(
87 final List<Collection<Serializable>> cmHandleIdToStatus, final String subscriptionClientId,
88 final String subscriptionName, final boolean isFullOutcomeResponse) {
90 final SubscriptionEventResponse subscriptionEventResponse = toSubscriptionEventResponse(
91 cmHandleIdToStatus, subscriptionClientId, subscriptionName);
93 final SubscriptionEventOutcome subscriptionEventOutcome =
94 subscriptionOutcomeMapper.toSubscriptionEventOutcome(subscriptionEventResponse);
96 if (isFullOutcomeResponse) {
97 subscriptionEventOutcome.setEventType(SubscriptionEventOutcome.EventType.COMPLETE_OUTCOME);
99 subscriptionEventOutcome.setEventType(SubscriptionEventOutcome.EventType.PARTIAL_OUTCOME);
102 return subscriptionEventOutcome;
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);
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);
121 if ("REJECTED".equals(item)) {
122 cmHandleIdToStatusMap.put((String) bucketIterator.next(),
123 SubscriptionStatus.REJECTED);
125 if ("ACCEPTED".equals(item)) {
126 cmHandleIdToStatusMap.put((String) bucketIterator.next(),
127 SubscriptionStatus.ACCEPTED);
131 subscriptionEventResponse.setCmHandleIdToStatus(cmHandleIdToStatusMap);
133 return subscriptionEventResponse;