Reject create request with duplicated subscriptionId
[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.Set;
26 import java.util.stream.Collectors;
27 import lombok.RequiredArgsConstructor;
28 import org.onap.cps.ncmp.api.impl.events.cmsubscription.CmNotificationSubscriptionNcmpOutEventProducer;
29 import org.onap.cps.ncmp.api.impl.events.cmsubscription.DmiCmNotificationSubscriptionCacheHandler;
30 import org.onap.cps.ncmp.api.impl.events.cmsubscription.mapper.CmNotificationSubscriptionNcmpOutEventMapper;
31 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.client_to_ncmp.CmNotificationSubscriptionNcmpInEvent;
32 import org.onap.cps.ncmp.events.cmnotificationsubscription_merge1_0_0.client_to_ncmp.Predicate;
33 import org.onap.cps.ncmp.events.cmsubscription_merge1_0_0.ncmp_to_client.CmNotificationSubscriptionNcmpOutEvent;
34 import org.springframework.stereotype.Service;
35
36 @Service
37 @RequiredArgsConstructor
38 public class CmNotificationSubscriptionHandlerServiceImpl implements CmNotificationSubscriptionHandlerService {
39
40     private final CmNotificationSubscriptionPersistenceService cmNotificationSubscriptionPersistenceService;
41     private final CmNotificationSubscriptionNcmpOutEventMapper cmNotificationSubscriptionNcmpOutEventMapper;
42     private final CmNotificationSubscriptionNcmpOutEventProducer cmNotificationSubscriptionNcmpOutEventProducer;
43     private final DmiCmNotificationSubscriptionCacheHandler dmiCmNotificationSubscriptionCacheHandler;
44
45     @Override
46     public void processSubscriptionCreateRequest(
47         final CmNotificationSubscriptionNcmpInEvent cmNotificationSubscriptionNcmpInEvent) {
48         final String subscriptionId = cmNotificationSubscriptionNcmpInEvent.getData().getSubscriptionId();
49         final List<Predicate> predicates = cmNotificationSubscriptionNcmpInEvent.getData().getPredicates();
50
51         if (cmNotificationSubscriptionPersistenceService.isUniqueSubscriptionId(subscriptionId)) {
52             dmiCmNotificationSubscriptionCacheHandler.add(subscriptionId, predicates);
53         } else {
54             final Set<String> subscriptionTargetFilters = predicates.stream().flatMap(
55                 predicate -> predicate.getTargetFilter().stream()).collect(Collectors.toSet());
56             rejectAndPublishCmNotificationSubscriptionCreateRequest(subscriptionId,
57                     new ArrayList<>(subscriptionTargetFilters));
58         }
59     }
60
61     private void rejectAndPublishCmNotificationSubscriptionCreateRequest(final String subscriptionId,
62                                                                          final List<String> subscriptionTargetFilters) {
63         final CmNotificationSubscriptionNcmpOutEvent cmNotificationSubscriptionNcmpOutEvent =
64             cmNotificationSubscriptionNcmpOutEventMapper
65                 .toCmNotificationSubscriptionNcmpOutEventForRejectedRequest(subscriptionId,
66                     subscriptionTargetFilters);
67         cmNotificationSubscriptionNcmpOutEventProducer.publishCmNotificationSubscriptionNcmpOutEvent(subscriptionId,
68             "subscriptionCreateResponse", cmNotificationSubscriptionNcmpOutEvent, false);
69     }
70 }