9f59b1a344a7fe916027d8d8d09a9fed0c8c4f09
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2024-2025 OpenInfra Foundation Europe. All rights reserved.
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.impl.cmnotificationsubscription.ncmp;
22
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Map;
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;
35
36 @Component
37 @RequiredArgsConstructor
38 public class NcmpOutEventMapper {
39
40     /**
41      * Mapper to form a response for the client for the Cm Notification Subscription.
42      *
43      * @param subscriptionId         CM notification subscription id
44      * @param dmiSubscriptionsPerDmi contains CmNotificationSubscriptionDetails per dmi plugin
45      * @return CmNotificationSubscriptionNcmpOutEvent to sent back to the client
46      */
47     public NcmpOutEvent toNcmpOutEvent(final String subscriptionId,
48             final Map<String, DmiCmSubscriptionDetails> dmiSubscriptionsPerDmi) {
49
50         final NcmpOutEvent ncmpOutEvent = new NcmpOutEvent();
51         final Data cmSubscriptionData = new Data();
52         cmSubscriptionData.setSubscriptionId(subscriptionId);
53         populateNcmpOutEventWithCmHandleIds(dmiSubscriptionsPerDmi,
54                 cmSubscriptionData);
55         ncmpOutEvent.setData(cmSubscriptionData);
56
57         return ncmpOutEvent;
58     }
59
60     /**
61      * Mapper to form a rejected response for the client for the Cm Notification Subscription Request.
62      *
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
66      */
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);
74         return ncmpOutEvent;
75     }
76
77     private void populateNcmpOutEventWithCmHandleIds(
78             final Map<String, DmiCmSubscriptionDetails> dmiSubscriptionsPerDmi,
79             final Data cmSubscriptionData) {
80
81         final Collection<String> acceptedCmHandleIds = new HashSet<>();
82         final Collection<String> pendingCmHandleIds = new HashSet<>();
83         final Collection<String> rejectedCmHandleIds = new HashSet<>();
84
85         dmiSubscriptionsPerDmi.forEach((dmiPluginName, dmiSubscriptionDetails) -> {
86             final CmSubscriptionStatus cmSubscriptionStatus =
87                     dmiSubscriptionDetails.getCmSubscriptionStatus();
88             final List<DmiCmSubscriptionPredicate> dmiCmSubscriptionPredicates =
89                     dmiSubscriptionDetails.getDmiCmSubscriptionPredicates();
90
91             switch (cmSubscriptionStatus) {
92                 case ACCEPTED -> acceptedCmHandleIds.addAll(
93                         extractCmHandleIds(dmiCmSubscriptionPredicates));
94                 case PENDING -> pendingCmHandleIds.addAll(extractCmHandleIds(dmiCmSubscriptionPredicates));
95                 default -> rejectedCmHandleIds.addAll(extractCmHandleIds(dmiCmSubscriptionPredicates));
96             }
97         });
98
99         cmSubscriptionData.setAcceptedTargets(acceptedCmHandleIds);
100         cmSubscriptionData.setPendingTargets(pendingCmHandleIds);
101         cmSubscriptionData.setRejectedTargets(rejectedCmHandleIds);
102
103     }
104
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()));
110
111         return cmHandleIds;
112     }
113
114 }