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
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.cmsubscription.service;
23 import java.util.ArrayList;
24 import java.util.List;
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.CmNotificationSubscriptionStatus;
34 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.DmiCmNotificationSubscriptionDetails;
35 import org.onap.cps.ncmp.api.impl.events.cmsubscription.model.DmiCmNotificationSubscriptionPredicate;
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;
42 @RequiredArgsConstructor
43 public class CmNotificationSubscriptionHandlerServiceImpl implements CmNotificationSubscriptionHandlerService {
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;
52 public void processSubscriptionCreateRequest(final String subscriptionId, final List<Predicate> predicates) {
53 if (cmNotificationSubscriptionPersistenceService.isUniqueSubscriptionId(subscriptionId)) {
54 dmiCmNotificationSubscriptionCacheHandler.add(subscriptionId, predicates);
55 handleCmNotificationSubscriptionDelta(subscriptionId);
56 scheduleCmNotificationSubscriptionNcmpOutEventResponse(subscriptionId,
57 "subscriptionCreateResponse");
59 rejectAndPublishCmNotificationSubscriptionCreateRequest(subscriptionId, predicates);
64 public void processSubscriptionDeleteRequest(final String subscriptionId, final List<Predicate> predicates) {
65 dmiCmNotificationSubscriptionCacheHandler.add(subscriptionId, predicates);
66 sendSubscriptionDeleteRequestToDmi(subscriptionId);
67 scheduleCmNotificationSubscriptionNcmpOutEventResponse(subscriptionId, "subscriptionDeleteResponse");
70 private void scheduleCmNotificationSubscriptionNcmpOutEventResponse(final String subscriptionId,
71 final String eventType) {
72 cmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionNcmpOutEvent(subscriptionId,
73 eventType, null, true);
76 private void rejectAndPublishCmNotificationSubscriptionCreateRequest(final String subscriptionId,
77 final List<Predicate> predicates) {
78 final Set<String> subscriptionTargetFilters =
79 predicates.stream().flatMap(predicate -> predicate.getTargetFilter().stream())
80 .collect(Collectors.toSet());
81 final CmNotificationSubscriptionNcmpOutEvent cmNotificationSubscriptionNcmpOutEvent =
82 cmNotificationSubscriptionMappersHandler.toCmNotificationSubscriptionNcmpOutEventForRejectedRequest(
83 subscriptionId, new ArrayList<>(subscriptionTargetFilters));
84 cmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionNcmpOutEvent(subscriptionId,
85 "subscriptionCreateResponse", cmNotificationSubscriptionNcmpOutEvent, false);
88 private void handleCmNotificationSubscriptionDelta(final String subscriptionId) {
89 final Map<String, DmiCmNotificationSubscriptionDetails> dmiCmNotificationSubscriptionDetailsMap =
90 dmiCmNotificationSubscriptionCacheHandler.get(subscriptionId);
91 dmiCmNotificationSubscriptionDetailsMap.forEach((dmiPluginName, dmiCmNotificationSubscriptionDetails) -> {
92 final List<DmiCmNotificationSubscriptionPredicate> dmiCmNotificationSubscriptionPredicates =
93 cmNotificationSubscriptionDelta.getDelta(
94 dmiCmNotificationSubscriptionDetails.getDmiCmNotificationSubscriptionPredicates());
96 if (dmiCmNotificationSubscriptionPredicates.isEmpty()) {
97 acceptAndPublishCmNotificationSubscriptionNcmpOutEventPerDmi(subscriptionId, dmiPluginName);
99 publishCmNotificationSubscriptionDmiInEventPerDmi(subscriptionId, dmiPluginName,
100 dmiCmNotificationSubscriptionPredicates);
105 private void publishCmNotificationSubscriptionDmiInEventPerDmi(final String subscriptionId,
106 final String dmiPluginName,
107 final List<DmiCmNotificationSubscriptionPredicate>
108 dmiCmNotificationSubscriptionPredicates) {
109 final CmNotificationSubscriptionDmiInEvent cmNotificationSubscriptionDmiInEvent =
110 cmNotificationSubscriptionMappersHandler.toCmNotificationSubscriptionDmiInEvent(
111 dmiCmNotificationSubscriptionPredicates);
112 cmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionDmiInEvent(subscriptionId,
113 dmiPluginName, "subscriptionCreateRequest", cmNotificationSubscriptionDmiInEvent);
116 private void acceptAndPublishCmNotificationSubscriptionNcmpOutEventPerDmi(final String subscriptionId,
117 final String dmiPluginName) {
118 dmiCmNotificationSubscriptionCacheHandler.updateDmiCmNotificationSubscriptionStatusPerDmi(subscriptionId,
119 dmiPluginName, CmNotificationSubscriptionStatus.ACCEPTED);
120 dmiCmNotificationSubscriptionCacheHandler.persistIntoDatabasePerDmi(subscriptionId, dmiPluginName);
123 private void sendSubscriptionDeleteRequestToDmi(final String subscriptionId) {
124 final Map<String, DmiCmNotificationSubscriptionDetails> dmiCmNotificationSubscriptionDetailsMap =
125 dmiCmNotificationSubscriptionCacheHandler.get(subscriptionId);
126 dmiCmNotificationSubscriptionDetailsMap.forEach((dmiPluginName, dmiCmNotificationSubscriptionDetails) -> {
127 final CmNotificationSubscriptionDmiInEvent cmNotificationSubscriptionDmiInEvent =
128 cmNotificationSubscriptionMappersHandler.toCmNotificationSubscriptionDmiInEvent(
129 dmiCmNotificationSubscriptionDetails.getDmiCmNotificationSubscriptionPredicates());
130 cmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionDmiInEvent(subscriptionId,
131 dmiPluginName, "subscriptionDeleteRequest", cmNotificationSubscriptionDmiInEvent);