2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 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.impl.cmnotificationsubscription.ncmp;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.List;
28 import lombok.RequiredArgsConstructor;
29 import org.onap.cps.ncmp.impl.cmnotificationsubscription.models.CmSubscriptionStatus;
30 import org.onap.cps.ncmp.impl.cmnotificationsubscription.models.DmiCmSubscriptionDetails;
31 import org.onap.cps.ncmp.impl.cmnotificationsubscription.models.DmiCmSubscriptionPredicate;
32 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_client.Data;
33 import org.onap.cps.ncmp.impl.cmnotificationsubscription_1_0_0.ncmp_to_client.NcmpOutEvent;
34 import org.springframework.stereotype.Component;
37 @RequiredArgsConstructor
38 public class NcmpOutEventMapper {
41 * Mapper to form a response for the client for the Cm Notification Subscription.
43 * @param subscriptionId Cm Notification Subscription Id
44 * @param dmiSubscriptionsPerDmi contains CmNotificationSubscriptionDetails per dmi plugin
45 * @return CmNotificationSubscriptionNcmpOutEvent to sent back to the client
47 public NcmpOutEvent toNcmpOutEvent(final String subscriptionId,
48 final Map<String, DmiCmSubscriptionDetails> dmiSubscriptionsPerDmi) {
50 final NcmpOutEvent ncmpOutEvent = new NcmpOutEvent();
51 final Data cmSubscriptionData = new Data();
52 cmSubscriptionData.setSubscriptionId(subscriptionId);
53 populateNcmpOutEventWithCmHandleIds(dmiSubscriptionsPerDmi,
55 ncmpOutEvent.setData(cmSubscriptionData);
61 * Mapper to form a rejected response for the client for the Cm Notification Subscription Request.
63 * @param subscriptionId subscription id
64 * @param rejectedTargetFilters list of rejected target filters for the subscription request
65 * @return to sent back to the client
67 public NcmpOutEvent toNcmpOutEventForRejectedRequest(final String subscriptionId,
68 final List<String> rejectedTargetFilters) {
69 final NcmpOutEvent ncmpOutEvent = new NcmpOutEvent();
70 final Data cmSubscriptionData = new Data();
71 cmSubscriptionData.setSubscriptionId(subscriptionId);
72 cmSubscriptionData.setRejectedTargets(rejectedTargetFilters);
73 ncmpOutEvent.setData(cmSubscriptionData);
77 private void populateNcmpOutEventWithCmHandleIds(
78 final Map<String, DmiCmSubscriptionDetails> dmiSubscriptionsPerDmi,
79 final Data cmSubscriptionData) {
81 final Collection<String> acceptedCmHandleIds = new HashSet<>();
82 final Collection<String> pendingCmHandleIds = new HashSet<>();
83 final Collection<String> rejectedCmHandleIds = new HashSet<>();
85 dmiSubscriptionsPerDmi.forEach((dmiPluginName, dmiSubscriptionDetails) -> {
86 final CmSubscriptionStatus cmSubscriptionStatus =
87 dmiSubscriptionDetails.getCmSubscriptionStatus();
88 final List<DmiCmSubscriptionPredicate> dmiCmSubscriptionPredicates =
89 dmiSubscriptionDetails.getDmiCmSubscriptionPredicates();
91 switch (cmSubscriptionStatus) {
92 case ACCEPTED -> acceptedCmHandleIds.addAll(
93 extractCmHandleIds(dmiCmSubscriptionPredicates));
94 case PENDING -> pendingCmHandleIds.addAll(extractCmHandleIds(dmiCmSubscriptionPredicates));
95 default -> rejectedCmHandleIds.addAll(extractCmHandleIds(dmiCmSubscriptionPredicates));
99 cmSubscriptionData.setAcceptedTargets(acceptedCmHandleIds);
100 cmSubscriptionData.setPendingTargets(pendingCmHandleIds);
101 cmSubscriptionData.setRejectedTargets(rejectedCmHandleIds);
105 private List<String> extractCmHandleIds(
106 final List<DmiCmSubscriptionPredicate> dmiCmSubscriptionPredicates) {
107 final List<String> cmHandleIds = new ArrayList<>();
108 dmiCmSubscriptionPredicates.forEach(dmiSubscriptionPredicate -> cmHandleIds.addAll(
109 dmiSubscriptionPredicate.getTargetCmHandleIds()));