Mapper to form CmNotificationSubscriptionNcmpOutEvent
[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     private void populateCmNotificationSubscriptionNcmpOutEventWithCmHandleIds(
60             final Map<String, DmiCmNotificationSubscriptionDetails> dmiCmNotificationSubscriptionDetailsMap,
61             final Data cmSubscriptionData) {
62
63         final List<String> acceptedCmHandleIds = new ArrayList<>();
64         final List<String> pendingCmHandleIds = new ArrayList<>();
65         final List<String> rejectedCmHandleIds = new ArrayList<>();
66
67         dmiCmNotificationSubscriptionDetailsMap.forEach((dmiPluginName, dmiCmNotificationSubscriptionDetails) -> {
68             final CmNotificationSubscriptionStatus cmNotificationSubscriptionStatus =
69                     dmiCmNotificationSubscriptionDetails.getCmNotificationSubscriptionStatus();
70             final List<DmiCmNotificationSubscriptionPredicate> dmiCmNotificationSubscriptionPredicates =
71                     dmiCmNotificationSubscriptionDetails.getDmiCmNotificationSubscriptionPredicates();
72
73             switch (cmNotificationSubscriptionStatus) {
74                 case ACCEPTED -> acceptedCmHandleIds.addAll(
75                         extractCmHandleIds(dmiCmNotificationSubscriptionPredicates));
76                 case PENDING -> pendingCmHandleIds.addAll(extractCmHandleIds(dmiCmNotificationSubscriptionPredicates));
77                 default -> rejectedCmHandleIds.addAll(extractCmHandleIds(dmiCmNotificationSubscriptionPredicates));
78             }
79         });
80
81         cmSubscriptionData.setAcceptedTargets(acceptedCmHandleIds);
82         cmSubscriptionData.setPendingTargets(pendingCmHandleIds);
83         cmSubscriptionData.setRejectedTargets(rejectedCmHandleIds);
84
85     }
86
87     private List<String> extractCmHandleIds(
88             final List<DmiCmNotificationSubscriptionPredicate> dmiCmNotificationSubscriptionPredicates) {
89         final List<String> cmHandleIds = new ArrayList<>();
90         dmiCmNotificationSubscriptionPredicates.forEach(dmiCmNotificationSubscriptionPredicate -> cmHandleIds.addAll(
91                 dmiCmNotificationSubscriptionPredicate.getTargetCmHandleIds()));
92
93         return cmHandleIds;
94     }
95
96 }