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.CmNotificationSubscriptionNcmpInEvent;
37 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.client_to_ncmp.Predicate;
38 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.ncmp_to_dmi.CmNotificationSubscriptionDmiInEvent;
39 import org.onap.cps.ncmp.events.cmsubscription_merge1_0_0.ncmp_to_client.CmNotificationSubscriptionNcmpOutEvent;
40 import org.springframework.stereotype.Service;
43 @RequiredArgsConstructor
44 public class CmNotificationSubscriptionHandlerServiceImpl implements CmNotificationSubscriptionHandlerService {
46 private final CmNotificationSubscriptionPersistenceService cmNotificationSubscriptionPersistenceService;
47 private final CmNotificationSubscriptionDelta cmNotificationSubscriptionDelta;
48 private final CmNotificationSubscriptionMappersHandler cmNotificationSubscriptionMappersHandler;
49 private final CmNotificationSubscriptionEventsHandler cmNotificationSubscriptionEventsHandler;
50 private final DmiCmNotificationSubscriptionCacheHandler dmiCmNotificationSubscriptionCacheHandler;
53 public void processSubscriptionCreateRequest(
54 final CmNotificationSubscriptionNcmpInEvent cmNotificationSubscriptionNcmpInEvent) {
55 final String subscriptionId = cmNotificationSubscriptionNcmpInEvent.getData().getSubscriptionId();
56 final List<Predicate> predicates = cmNotificationSubscriptionNcmpInEvent.getData().getPredicates();
58 if (cmNotificationSubscriptionPersistenceService.isUniqueSubscriptionId(subscriptionId)) {
59 dmiCmNotificationSubscriptionCacheHandler.add(subscriptionId, predicates);
60 handleCmNotificationSubscriptionDelta(subscriptionId);
61 scheduleCmNotificationSubscriptionNcmpOutEventResponse(subscriptionId);
63 rejectAndPublishCmNotificationSubscriptionCreateRequest(subscriptionId, predicates);
67 private void scheduleCmNotificationSubscriptionNcmpOutEventResponse(final String subscriptionId) {
68 cmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionNcmpOutEvent(subscriptionId,
69 "subscriptionCreateResponse", null, true);
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);
84 private void handleCmNotificationSubscriptionDelta(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());
92 if (dmiCmNotificationSubscriptionPredicates.isEmpty()) {
93 acceptAndPublishCmNotificationSubscriptionNcmpOutEventPerDmi(subscriptionId, dmiPluginName);
95 publishCmNotificationSubscriptionDmiInEventPerDmi(subscriptionId, dmiPluginName,
96 dmiCmNotificationSubscriptionPredicates);
101 private void publishCmNotificationSubscriptionDmiInEventPerDmi(final String subscriptionId,
102 final String dmiPluginName,
103 final List<DmiCmNotificationSubscriptionPredicate> dmiCmNotificationSubscriptionPredicates) {
104 final CmNotificationSubscriptionDmiInEvent cmNotificationSubscriptionDmiInEvent =
105 cmNotificationSubscriptionMappersHandler.toCmNotificationSubscriptionDmiInEvent(
106 dmiCmNotificationSubscriptionPredicates);
107 cmNotificationSubscriptionEventsHandler.publishCmNotificationSubscriptionDmiInEvent(subscriptionId,
108 dmiPluginName, "subscriptionCreateRequest", cmNotificationSubscriptionDmiInEvent);
111 private void acceptAndPublishCmNotificationSubscriptionNcmpOutEventPerDmi(final String subscriptionId,
112 final String dmiPluginName) {
113 dmiCmNotificationSubscriptionCacheHandler.updateDmiCmNotificationSubscriptionStatusPerDmi(subscriptionId,
114 dmiPluginName, CmNotificationSubscriptionStatus.ACCEPTED);
115 dmiCmNotificationSubscriptionCacheHandler.persistIntoDatabasePerDmi(subscriptionId, dmiPluginName);