Merge "[k6] Refactor k6 tests for CM handle searches"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / events / cmsubscription / service / CmNotificationSubscriptionHandlerServiceImpl.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.service;
22
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.stream.Collectors;
28 import lombok.RequiredArgsConstructor;
29 import org.onap.cps.ncmp.api.impl.events.cmsubscription.CmNotificationSubscriptionDelta;
30 import org.onap.cps.ncmp.api.impl.events.cmsubscription.CmNotificationSubscriptionEventsHandler;
31 import org.onap.cps.ncmp.api.impl.events.cmsubscription.CmNotificationSubscriptionMappersHandler;
32 import org.onap.cps.ncmp.api.impl.events.cmsubscription.DmiCmNotificationSubscriptionCacheHandler;
33 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.DmiCmNotificationSubscriptionDetails;
34 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.DmiCmNotificationSubscriptionPredicate;
35 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.client_to_ncmp.CmNotificationSubscriptionNcmpInEvent;
36 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.client_to_ncmp.Predicate;
37 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.ncmp_to_dmi.CmNotificationSubscriptionDmiInEvent;
38 import org.onap.cps.ncmp.events.cmsubscription_merge1_0_0.ncmp_to_client.CmNotificationSubscriptionNcmpOutEvent;
39 import org.springframework.stereotype.Service;
40
41 @Service
42 @RequiredArgsConstructor
43 public class CmNotificationSubscriptionHandlerServiceImpl implements CmNotificationSubscriptionHandlerService {
44
45     private final CmNotificationSubscriptionPersistenceService cmNotificationSubscriptionPersistenceService;
46     private final CmNotificationSubscriptionDelta cmNotificationSubscriptionDelta;
47     private final CmNotificationSubscriptionMappersHandler cmNotificationSubscriptionMappersHandler;
48     private final CmNotificationSubscriptionEventsHandler cmNotificationSubscriptionEventsHandler;
49     private final DmiCmNotificationSubscriptionCacheHandler dmiCmNotificationSubscriptionCacheHandler;
50
51     @Override
52     public void processSubscriptionCreateRequest(
53             final CmNotificationSubscriptionNcmpInEvent cmNotificationSubscriptionNcmpInEvent) {
54         final String subscriptionId = cmNotificationSubscriptionNcmpInEvent.getData().getSubscriptionId();
55         final List<Predicate> predicates = cmNotificationSubscriptionNcmpInEvent.getData().getPredicates();
56
57         if (cmNotificationSubscriptionPersistenceService.isUniqueSubscriptionId(subscriptionId)) {
58             dmiCmNotificationSubscriptionCacheHandler.add(subscriptionId, predicates);
59             sendSubscriptionCreateRequestToDmi(subscriptionId);
60             scheduleCmNotificationSubscriptionNcmpOutEventResponse(subscriptionId);
61         } else {
62             rejectAndPublishCmNotificationSubscriptionCreateRequest(subscriptionId,
63                     predicates);
64         }
65     }
66
67     private void scheduleCmNotificationSubscriptionNcmpOutEventResponse(final String subscriptionId) {
68         cmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionNcmpOutEvent(subscriptionId,
69                 "subscriptionCreateResponse", null, true);
70     }
71
72     private void rejectAndPublishCmNotificationSubscriptionCreateRequest(final String subscriptionId,
73             final List<Predicate> predicates) {
74         final Set<String> subscriptionTargetFilters =
75                 predicates.stream().flatMap(predicate -> predicate.getTargetFilter().stream())
76                         .collect(Collectors.toSet());
77         final CmNotificationSubscriptionNcmpOutEvent cmNotificationSubscriptionNcmpOutEvent =
78                 cmNotificationSubscriptionMappersHandler.toCmNotificationSubscriptionNcmpOutEventForRejectedRequest(
79                         subscriptionId, new ArrayList<>(subscriptionTargetFilters));
80         cmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionNcmpOutEvent(subscriptionId,
81                 "subscriptionCreateResponse", cmNotificationSubscriptionNcmpOutEvent, false);
82     }
83
84     private void sendSubscriptionCreateRequestToDmi(final String subscriptionId) {
85         final Map<String, DmiCmNotificationSubscriptionDetails> dmiCmNotificationSubscriptionDetailsMap =
86                 dmiCmNotificationSubscriptionCacheHandler.get(subscriptionId);
87         dmiCmNotificationSubscriptionDetailsMap.forEach((dmiPluginName, dmiCmNotificationSubscriptionDetails) -> {
88             final List<DmiCmNotificationSubscriptionPredicate> dmiCmNotificationSubscriptionPredicates =
89                     cmNotificationSubscriptionDelta.getDelta(
90                             dmiCmNotificationSubscriptionDetails.getDmiCmNotificationSubscriptionPredicates());
91             final CmNotificationSubscriptionDmiInEvent cmNotificationSubscriptionDmiInEvent =
92                     cmNotificationSubscriptionMappersHandler.toCmNotificationSubscriptionDmiInEvent(
93                             dmiCmNotificationSubscriptionPredicates);
94             cmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionDmiInEvent(subscriptionId,
95                     dmiPluginName, "subscriptionCreateRequest", cmNotificationSubscriptionDmiInEvent);
96         });
97     }
98 }