9d5bc1575aa5cb06530d640fcea1f67869d0a048
[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 LcmEventsCreator lcmEventsCreator;
39     private final LcmEventsProducer lcmEventsProducer;
40
41     /**
42      * Publish 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 publishLcmEventBatchAsynchronously(final Collection<CmHandleTransitionPair> cmHandleTransitionPairs) {
48         cmHandleTransitionPairs.forEach(cmHandleTransitionPair -> publishLcmEvent(
49                 toNcmpServiceCmHandle(cmHandleTransitionPair.getTargetYangModelCmHandle()),
50                 toNcmpServiceCmHandle(cmHandleTransitionPair.getCurrentYangModelCmHandle())));
51     }
52
53     private void publishLcmEvent(final NcmpServiceCmHandle targetNcmpServiceCmHandle,
54                                  final NcmpServiceCmHandle existingNcmpServiceCmHandle) {
55         final String cmHandleId = targetNcmpServiceCmHandle.getCmHandleId();
56         final LcmEventHeader lcmEventHeader =
57                 lcmEventsCreator.populateLcmEventHeader(cmHandleId, targetNcmpServiceCmHandle,
58                         existingNcmpServiceCmHandle);
59         final LcmEvent lcmEvent =
60                 lcmEventsCreator.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle);
61         lcmEventsProducer.publishLcmEvent(cmHandleId, lcmEvent, lcmEventHeader);
62     }
63
64     private static NcmpServiceCmHandle toNcmpServiceCmHandle(final YangModelCmHandle yangModelCmHandle) {
65         return YangDataConverter.toNcmpServiceCmHandle(yangModelCmHandle);
66     }
67 }