Update Model to allow Persisting of alternateId
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / events / deprecated / cmsubscription / CmSubscriptionEventToCmSubscriptionNcmpOutEventMapper.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2023 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.deprecated.cmsubscription;
22
23 import java.util.List;
24 import java.util.Map;
25 import java.util.stream.Collectors;
26 import org.mapstruct.Mapper;
27 import org.mapstruct.Mapping;
28 import org.mapstruct.Named;
29 import org.onap.cps.ncmp.api.impl.deprecated.subscriptions.SubscriptionStatus;
30 import org.onap.cps.ncmp.api.models.CmSubscriptionEvent;
31 import org.onap.cps.ncmp.api.models.CmSubscriptionStatus;
32 import org.onap.cps.ncmp.events.cmsubscription1_0_0.ncmp_to_client.AdditionalInfo;
33 import org.onap.cps.ncmp.events.cmsubscription1_0_0.ncmp_to_client.AdditionalInfoDetail;
34 import org.onap.cps.ncmp.events.cmsubscription1_0_0.ncmp_to_client.CmSubscriptionNcmpOutEvent;
35 import org.onap.cps.spi.exceptions.DataValidationException;
36
37 @Mapper(componentModel = "spring")
38 public interface CmSubscriptionEventToCmSubscriptionNcmpOutEventMapper {
39
40     @Mapping(source = "cmSubscriptionStatus", target = "data.additionalInfo",
41             qualifiedByName = "mapCmSubscriptionStatusToAdditionalInfo")
42     CmSubscriptionNcmpOutEvent toCmSubscriptionNcmpOutEvent(CmSubscriptionEvent cmSubscriptionEvent);
43
44     /**
45      * Maps list of SubscriptionStatus to an AdditionalInfo.
46      *
47      * @param cmSubscriptionStatusList containing details
48      * @return an AdditionalInfo
49      */
50     @Named("mapCmSubscriptionStatusToAdditionalInfo")
51     default AdditionalInfo mapCmSubscriptionStatusToAdditionalInfo(
52             final List<CmSubscriptionStatus> cmSubscriptionStatusList) {
53         if (cmSubscriptionStatusList == null || cmSubscriptionStatusList.isEmpty()) {
54             throw new DataValidationException("Invalid cmSubscriptionStatusList",
55                     "CmSubscriptionStatus list cannot be null or empty");
56         }
57
58         final Map<String, List<CmSubscriptionStatus>> rejectedSubscriptionsPerDetails =
59                 getSubscriptionsPerDetails(cmSubscriptionStatusList, SubscriptionStatus.REJECTED);
60         final Map<String, List<String>> rejectedCmHandlesPerDetails =
61                 getCmHandlesPerDetails(rejectedSubscriptionsPerDetails);
62         final List<AdditionalInfoDetail> rejectedCmHandles = getAdditionalInfoDetailList(rejectedCmHandlesPerDetails);
63
64         final Map<String, List<CmSubscriptionStatus>> pendingSubscriptionsPerDetails =
65                 getSubscriptionsPerDetails(cmSubscriptionStatusList, SubscriptionStatus.PENDING);
66         final Map<String, List<String>> pendingCmHandlesPerDetails =
67                 getCmHandlesPerDetails(pendingSubscriptionsPerDetails);
68         final List<AdditionalInfoDetail> pendingCmHandles = getAdditionalInfoDetailList(pendingCmHandlesPerDetails);
69
70         final AdditionalInfo additionalInfo = new AdditionalInfo();
71         additionalInfo.setRejected(rejectedCmHandles);
72         additionalInfo.setPending(pendingCmHandles);
73
74         return additionalInfo;
75     }
76
77     private static Map<String, List<CmSubscriptionStatus>> getSubscriptionsPerDetails(
78             final List<CmSubscriptionStatus> cmSubscriptionStatusList, final SubscriptionStatus status) {
79         return cmSubscriptionStatusList.stream()
80                 .filter(subscriptionStatus -> subscriptionStatus.getStatus() == status)
81                 .collect(Collectors.groupingBy(CmSubscriptionStatus::getDetails));
82     }
83
84     private static Map<String, List<String>> getCmHandlesPerDetails(
85             final Map<String, List<CmSubscriptionStatus>> cmSubscriptionsPerDetails) {
86         return cmSubscriptionsPerDetails.entrySet().stream()
87                 .collect(Collectors.toMap(
88                         Map.Entry::getKey,
89                         entry -> entry.getValue().stream()
90                                 .map(CmSubscriptionStatus::getId)
91                                 .collect(Collectors.toList())
92                 ));
93     }
94
95     private static List<AdditionalInfoDetail> getAdditionalInfoDetailList(
96             final Map<String, List<String>> cmHandlesPerDetails) {
97         return cmHandlesPerDetails.entrySet().stream()
98                 .map(entry -> {
99                     final AdditionalInfoDetail detail = new AdditionalInfoDetail();
100                     detail.setDetails(entry.getKey());
101                     detail.setTargets(entry.getValue());
102                     return detail;
103                 }).collect(Collectors.toList());
104     }
105 }