ea217516917a9e4b125f61f4d29c808886367d55
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / events / cmsubscription / mapper / CmNotificationSubscriptionNcmpOutEventMapper.java
1 /*
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
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.api.impl.events.cmsubscription.mapper;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
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;
33
34 @Component
35 @RequiredArgsConstructor
36 public class CmNotificationSubscriptionNcmpOutEventMapper {
37
38     /**
39      * Mapper to form a response for the client for the Cm Notification Subscription.
40      *
41      * @param subscriptionId                          Cm Notification Subscription Id
42      * @param dmiCmNotificationSubscriptionDetailsMap contains CmNotificationSubscriptionDetails per dmi plugin
43      * @return CmNotificationSubscriptionNcmpOutEvent to sent back to the client
44      */
45     public CmNotificationSubscriptionNcmpOutEvent toCmNotificationSubscriptionNcmpOutEvent(final String subscriptionId,
46             final Map<String, DmiCmNotificationSubscriptionDetails> dmiCmNotificationSubscriptionDetailsMap) {
47
48         final CmNotificationSubscriptionNcmpOutEvent cmNotificationSubscriptionNcmpOutEvent =
49                 new CmNotificationSubscriptionNcmpOutEvent();
50         final Data cmSubscriptionData = new Data();
51         cmSubscriptionData.setSubscriptionId(subscriptionId);
52         populateCmNotificationSubscriptionNcmpOutEventWithCmHandleIds(dmiCmNotificationSubscriptionDetailsMap,
53                 cmSubscriptionData);
54         cmNotificationSubscriptionNcmpOutEvent.setData(cmSubscriptionData);
55
56         return cmNotificationSubscriptionNcmpOutEvent;
57     }
58
59     /**
60      * Mapper to form a rejected response for the client for the Cm Notification Subscription Request.
61      *
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
65      */
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;
75     }
76
77     private void populateCmNotificationSubscriptionNcmpOutEventWithCmHandleIds(
78             final Map<String, DmiCmNotificationSubscriptionDetails> dmiCmNotificationSubscriptionDetailsMap,
79             final Data cmSubscriptionData) {
80
81         final List<String> acceptedCmHandleIds = new ArrayList<>();
82         final List<String> pendingCmHandleIds = new ArrayList<>();
83         final List<String> rejectedCmHandleIds = new ArrayList<>();
84
85         dmiCmNotificationSubscriptionDetailsMap.forEach((dmiPluginName, dmiCmNotificationSubscriptionDetails) -> {
86             final CmNotificationSubscriptionStatus cmNotificationSubscriptionStatus =
87                     dmiCmNotificationSubscriptionDetails.getCmNotificationSubscriptionStatus();
88             final List<DmiCmNotificationSubscriptionPredicate> dmiCmNotificationSubscriptionPredicates =
89                     dmiCmNotificationSubscriptionDetails.getDmiCmNotificationSubscriptionPredicates();
90
91             switch (cmNotificationSubscriptionStatus) {
92                 case ACCEPTED -> acceptedCmHandleIds.addAll(
93                         extractCmHandleIds(dmiCmNotificationSubscriptionPredicates));
94                 case PENDING -> pendingCmHandleIds.addAll(extractCmHandleIds(dmiCmNotificationSubscriptionPredicates));
95                 default -> rejectedCmHandleIds.addAll(extractCmHandleIds(dmiCmNotificationSubscriptionPredicates));
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<DmiCmNotificationSubscriptionPredicate> dmiCmNotificationSubscriptionPredicates) {
107         final List<String> cmHandleIds = new ArrayList<>();
108         dmiCmNotificationSubscriptionPredicates.forEach(dmiCmNotificationSubscriptionPredicate -> cmHandleIds.addAll(
109                 dmiCmNotificationSubscriptionPredicate.getTargetCmHandleIds()));
110
111         return cmHandleIds;
112     }
113
114 }