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
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.deprecated.cmsubscription;
23 import java.util.List;
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;
37 @Mapper(componentModel = "spring")
38 public interface CmSubscriptionEventToCmSubscriptionNcmpOutEventMapper {
40 @Mapping(source = "cmSubscriptionStatus", target = "data.additionalInfo",
41 qualifiedByName = "mapCmSubscriptionStatusToAdditionalInfo")
42 CmSubscriptionNcmpOutEvent toCmSubscriptionNcmpOutEvent(CmSubscriptionEvent cmSubscriptionEvent);
45 * Maps list of SubscriptionStatus to an AdditionalInfo.
47 * @param cmSubscriptionStatusList containing details
48 * @return an AdditionalInfo
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");
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);
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);
70 final AdditionalInfo additionalInfo = new AdditionalInfo();
71 additionalInfo.setRejected(rejectedCmHandles);
72 additionalInfo.setPending(pendingCmHandles);
74 return additionalInfo;
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));
84 private static Map<String, List<String>> getCmHandlesPerDetails(
85 final Map<String, List<CmSubscriptionStatus>> cmSubscriptionsPerDetails) {
86 return cmSubscriptionsPerDetails.entrySet().stream()
87 .collect(Collectors.toMap(
89 entry -> entry.getValue().stream()
90 .map(CmSubscriptionStatus::getId)
91 .collect(Collectors.toList())
95 private static List<AdditionalInfoDetail> getAdditionalInfoDetailList(
96 final Map<String, List<String>> cmHandlesPerDetails) {
97 return cmHandlesPerDetails.entrySet().stream()
99 final AdditionalInfoDetail detail = new AdditionalInfoDetail();
100 detail.setDetails(entry.getKey());
101 detail.setTargets(entry.getValue());
103 }).collect(Collectors.toList());