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.List;
27 import lombok.RequiredArgsConstructor;
28 import lombok.extern.slf4j.Slf4j;
29 import org.apache.kafka.common.header.Headers;
30 import org.apache.kafka.common.header.internals.RecordHeaders;
31 import org.onap.cps.ncmp.api.impl.events.EventsPublisher;
32 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionPersistence;
33 import org.onap.cps.ncmp.api.impl.subscriptions.SubscriptionStatus;
34 import org.onap.cps.ncmp.api.impl.utils.DataNodeHelper;
35 import org.onap.cps.ncmp.api.models.SubscriptionEventResponse;
36 import org.onap.cps.ncmp.events.avc.subscription.v1.SubscriptionEventOutcome;
37 import org.onap.cps.spi.model.DataNode;
38 import org.springframework.beans.factory.annotation.Value;
39 import org.springframework.stereotype.Component;
43 @RequiredArgsConstructor
44 public class SubscriptionEventResponseOutcome {
46 private final SubscriptionPersistence subscriptionPersistence;
48 private final EventsPublisher<SubscriptionEventOutcome> outcomeEventsPublisher;
50 private final SubscriptionOutcomeMapper subscriptionOutcomeMapper;
52 @Value("${app.ncmp.avc.subscription-outcome-topic:cm-avc-subscription-response}")
53 private String subscriptionOutcomeEventTopic;
56 * This is for construction of outcome message to be published for client apps.
58 * @param subscriptionClientId client id of the subscription.
59 * @param subscriptionName name of the subscription.
60 * @param isFullOutcomeResponse the flag to decide on complete or partial response to be generated.
62 public void sendResponse(final String subscriptionClientId, final String subscriptionName,
63 final boolean isFullOutcomeResponse) {
64 final SubscriptionEventOutcome subscriptionEventOutcome = generateResponse(
65 subscriptionClientId, subscriptionName, isFullOutcomeResponse);
66 final Headers headers = new RecordHeaders();
67 final String subscriptionEventId = subscriptionClientId + subscriptionName;
68 outcomeEventsPublisher.publishEvent(subscriptionOutcomeEventTopic,
69 subscriptionEventId, headers, subscriptionEventOutcome);
72 private SubscriptionEventOutcome generateResponse(final String subscriptionClientId, final String subscriptionName,
73 final boolean isFullOutcomeResponse) {
74 final Collection<DataNode> dataNodes = subscriptionPersistence.getDataNodesForSubscriptionEvent();
75 final List<Map<String, Serializable>> dataNodeLeaves = DataNodeHelper.getDataNodeLeaves(dataNodes);
76 final List<Collection<Serializable>> cmHandleIdToStatus =
77 DataNodeHelper.getCmHandleIdToStatus(dataNodeLeaves);
78 return formSubscriptionOutcomeMessage(cmHandleIdToStatus, subscriptionClientId, subscriptionName,
79 isFullOutcomeResponse);
83 private SubscriptionEventOutcome formSubscriptionOutcomeMessage(
84 final List<Collection<Serializable>> cmHandleIdToStatus, final String subscriptionClientId,
85 final String subscriptionName, final boolean isFullOutcomeResponse) {
87 final SubscriptionEventResponse subscriptionEventResponse = toSubscriptionEventResponse(
88 cmHandleIdToStatus, subscriptionClientId, subscriptionName);
90 final SubscriptionEventOutcome subscriptionEventOutcome =
91 subscriptionOutcomeMapper.toSubscriptionEventOutcome(subscriptionEventResponse);
93 if (isFullOutcomeResponse) {
94 subscriptionEventOutcome.setEventType(SubscriptionEventOutcome.EventType.COMPLETE_OUTCOME);
96 subscriptionEventOutcome.setEventType(SubscriptionEventOutcome.EventType.PARTIAL_OUTCOME);
99 return subscriptionEventOutcome;
102 private SubscriptionEventResponse toSubscriptionEventResponse(
103 final List<Collection<Serializable>> cmHandleIdToStatus, final String subscriptionClientId,
104 final String subscriptionName) {
105 final Map<String, SubscriptionStatus> cmHandleIdToStatusMap =
106 DataNodeHelper.getCmHandleIdToStatusMap(cmHandleIdToStatus);
108 final SubscriptionEventResponse subscriptionEventResponse = new SubscriptionEventResponse();
109 subscriptionEventResponse.setClientId(subscriptionClientId);
110 subscriptionEventResponse.setSubscriptionName(subscriptionName);
111 subscriptionEventResponse.setCmHandleIdToStatus(cmHandleIdToStatusMap);
113 return subscriptionEventResponse;