6717f8f7c57805079259370a8dffe89818c3ddab
[cps.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2023-2025 OpenInfra Foundation Europe. All rights reserved.
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.impl.inventory.sync.lcm;
22
23 import java.util.Collection;
24 import lombok.RequiredArgsConstructor;
25 import org.onap.cps.ncmp.api.inventory.models.NcmpServiceCmHandle;
26 import org.onap.cps.ncmp.events.lcm.v1.LcmEvent;
27 import org.onap.cps.ncmp.events.lcm.v1.LcmEventHeader;
28 import org.onap.cps.ncmp.impl.inventory.models.YangModelCmHandle;
29 import org.onap.cps.ncmp.impl.inventory.sync.lcm.LcmEventsCmHandleStateHandlerImpl.CmHandleTransitionPair;
30 import org.onap.cps.ncmp.impl.utils.YangDataConverter;
31 import org.springframework.scheduling.annotation.Async;
32 import org.springframework.stereotype.Service;
33
34 @Service
35 @RequiredArgsConstructor
36 public class LcmEventsCmHandleStateHandlerAsyncHelper {
37
38     private final LcmEventsProducerHelper lcmEventsProducerHelper;
39     private final LcmEventsProducer lcmEventsProducer;
40
41     /**
42      * Send LcmEvent in batches and in asynchronous manner.
43      *
44      * @param cmHandleTransitionPairs Pair of existing and modified cm handle represented as YangModelCmHandle
45      */
46     @Async("notificationExecutor")
47     public void sendLcmEventBatchAsynchronously(final Collection<CmHandleTransitionPair> cmHandleTransitionPairs) {
48         cmHandleTransitionPairs.forEach(cmHandleTransitionPair -> sendLcmEvent(
49                 toNcmpServiceCmHandle(cmHandleTransitionPair.getTargetYangModelCmHandle()),
50                 toNcmpServiceCmHandle(cmHandleTransitionPair.getCurrentYangModelCmHandle())));
51     }
52
53     private void sendLcmEvent(final NcmpServiceCmHandle targetNcmpServiceCmHandle,
54                               final NcmpServiceCmHandle existingNcmpServiceCmHandle) {
55         final String cmHandleId = targetNcmpServiceCmHandle.getCmHandleId();
56         final LcmEventHeader lcmEventHeader =
57                 lcmEventsProducerHelper.populateLcmEventHeader(cmHandleId, targetNcmpServiceCmHandle,
58                         existingNcmpServiceCmHandle);
59         final LcmEvent lcmEvent =
60                 lcmEventsProducerHelper.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle,
61                         existingNcmpServiceCmHandle);
62         lcmEventsProducer.sendLcmEvent(cmHandleId, lcmEvent, lcmEventHeader);
63     }
64
65     private static NcmpServiceCmHandle toNcmpServiceCmHandle(final YangModelCmHandle yangModelCmHandle) {
66         return YangDataConverter.toNcmpServiceCmHandle(yangModelCmHandle);
67     }
68 }