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.api.impl.events.cmsubscription.mapper;
23 import java.util.ArrayList;
24 import java.util.List;
26 import lombok.RequiredArgsConstructor;
27 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.CmNotificationSubscriptionStatus;
28 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.DmiCmNotificationSubscriptionDetails;
29 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.DmiCmNotificationSubscriptionPredicate;
30 import org.onap.cps.ncmp.events.cmsubscription_merge1_0_0.ncmp_to_client.CmNotificationSubscriptionNcmpOutEvent;
31 import org.onap.cps.ncmp.events.cmsubscription_merge1_0_0.ncmp_to_client.Data;
32 import org.springframework.stereotype.Component;
35 @RequiredArgsConstructor
36 public class CmNotificationSubscriptionNcmpOutEventMapper {
39 * Mapper to form a response for the client for the Cm Notification Subscription.
41 * @param subscriptionId Cm Notification Subscription Id
42 * @param dmiCmNotificationSubscriptionDetailsMap contains CmNotificationSubscriptionDetails per dmi plugin
43 * @return CmNotificationSubscriptionNcmpOutEvent to sent back to the client
45 public CmNotificationSubscriptionNcmpOutEvent toCmNotificationSubscriptionNcmpOutEvent(final String subscriptionId,
46 final Map<String, DmiCmNotificationSubscriptionDetails> dmiCmNotificationSubscriptionDetailsMap) {
48 final CmNotificationSubscriptionNcmpOutEvent cmNotificationSubscriptionNcmpOutEvent =
49 new CmNotificationSubscriptionNcmpOutEvent();
50 final Data cmSubscriptionData = new Data();
51 cmSubscriptionData.setSubscriptionId(subscriptionId);
52 populateCmNotificationSubscriptionNcmpOutEventWithCmHandleIds(dmiCmNotificationSubscriptionDetailsMap,
54 cmNotificationSubscriptionNcmpOutEvent.setData(cmSubscriptionData);
56 return cmNotificationSubscriptionNcmpOutEvent;
60 * Mapper to form a rejected response for the client for the Cm Notification Subscription Request.
62 * @param subscriptionId subscription id
63 * @param rejectedTargetFilters list of rejected target filters for the subscription request
64 * @return to sent back to the client
66 public CmNotificationSubscriptionNcmpOutEvent toCmNotificationSubscriptionNcmpOutEventForRejectedRequest(
67 final String subscriptionId, final List<String> rejectedTargetFilters) {
68 final CmNotificationSubscriptionNcmpOutEvent cmNotificationSubscriptionNcmpOutEvent =
69 new CmNotificationSubscriptionNcmpOutEvent();
70 final Data cmSubscriptionData = new Data();
71 cmSubscriptionData.setSubscriptionId(subscriptionId);
72 cmSubscriptionData.setRejectedTargets(rejectedTargetFilters);
73 cmNotificationSubscriptionNcmpOutEvent.setData(cmSubscriptionData);
74 return cmNotificationSubscriptionNcmpOutEvent;
77 private void populateCmNotificationSubscriptionNcmpOutEventWithCmHandleIds(
78 final Map<String, DmiCmNotificationSubscriptionDetails> dmiCmNotificationSubscriptionDetailsMap,
79 final Data cmSubscriptionData) {
81 final List<String> acceptedCmHandleIds = new ArrayList<>();
82 final List<String> pendingCmHandleIds = new ArrayList<>();
83 final List<String> rejectedCmHandleIds = new ArrayList<>();
85 dmiCmNotificationSubscriptionDetailsMap.forEach((dmiPluginName, dmiCmNotificationSubscriptionDetails) -> {
86 final CmNotificationSubscriptionStatus cmNotificationSubscriptionStatus =
87 dmiCmNotificationSubscriptionDetails.getCmNotificationSubscriptionStatus();
88 final List<DmiCmNotificationSubscriptionPredicate> dmiCmNotificationSubscriptionPredicates =
89 dmiCmNotificationSubscriptionDetails.getDmiCmNotificationSubscriptionPredicates();
91 switch (cmNotificationSubscriptionStatus) {
92 case ACCEPTED -> acceptedCmHandleIds.addAll(
93 extractCmHandleIds(dmiCmNotificationSubscriptionPredicates));
94 case PENDING -> pendingCmHandleIds.addAll(extractCmHandleIds(dmiCmNotificationSubscriptionPredicates));
95 default -> rejectedCmHandleIds.addAll(extractCmHandleIds(dmiCmNotificationSubscriptionPredicates));
99 cmSubscriptionData.setAcceptedTargets(acceptedCmHandleIds);
100 cmSubscriptionData.setPendingTargets(pendingCmHandleIds);
101 cmSubscriptionData.setRejectedTargets(rejectedCmHandleIds);
105 private List<String> extractCmHandleIds(
106 final List<DmiCmNotificationSubscriptionPredicate> dmiCmNotificationSubscriptionPredicates) {
107 final List<String> cmHandleIds = new ArrayList<>();
108 dmiCmNotificationSubscriptionPredicates.forEach(dmiCmNotificationSubscriptionPredicate -> cmHandleIds.addAll(
109 dmiCmNotificationSubscriptionPredicate.getTargetCmHandleIds()));