Merge "Patch # 3: Data operation response event (NCMP → Client App) to comply with...
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / events / lcm / LcmEventsCmHandleStateHandlerAsyncHelper.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2023 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.lcm;
22
23 import java.util.Collection;
24 import lombok.RequiredArgsConstructor;
25 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
26 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
27 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
28 import org.onap.cps.ncmp.events.lcm.v1.LcmEvent;
29 import org.onap.cps.ncmp.events.lcm.v1.LcmEventHeader;
30 import org.springframework.scheduling.annotation.Async;
31 import org.springframework.stereotype.Service;
32
33 @Service
34 @RequiredArgsConstructor
35 public class LcmEventsCmHandleStateHandlerAsyncHelper {
36
37     private final LcmEventsCreator lcmEventsCreator;
38     private final LcmEventsService lcmEventsService;
39
40     /**
41      * Publish LCM Event in asynchronous manner.
42      *
43      * @param targetNcmpServiceCmHandle  target NcmpServiceCmHandle
44      * @param currentNcmpServiceCmHandle current NcmpServiceCmHandle
45      */
46     @Async("notificationExecutor")
47     public void publishLcmEventAsynchronously(final NcmpServiceCmHandle targetNcmpServiceCmHandle,
48                                               final NcmpServiceCmHandle currentNcmpServiceCmHandle) {
49         publishLcmEvent(targetNcmpServiceCmHandle, currentNcmpServiceCmHandle);
50     }
51
52     /**
53      * Publish LcmEvent in batches and in asynchronous manner.
54      *
55      * @param cmHandleTransitionPairs Pair of existing and modified cm handle represented as YangModelCmHandle
56      */
57     @Async("notificationExecutor")
58     public void publishLcmEventBatchAsynchronously(
59             final Collection<LcmEventsCmHandleStateHandlerImpl.CmHandleTransitionPair> cmHandleTransitionPairs) {
60         cmHandleTransitionPairs.forEach(cmHandleTransitionPair -> publishLcmEvent(
61                 toNcmpServiceCmHandle(cmHandleTransitionPair.getTargetYangModelCmHandle()),
62                 toNcmpServiceCmHandle(cmHandleTransitionPair.getCurrentYangModelCmHandle())));
63     }
64
65     private void publishLcmEvent(final NcmpServiceCmHandle targetNcmpServiceCmHandle,
66                                  final NcmpServiceCmHandle existingNcmpServiceCmHandle) {
67         final String cmHandleId = targetNcmpServiceCmHandle.getCmHandleId();
68         final LcmEventHeader lcmEventHeader =
69                 lcmEventsCreator.populateLcmEventHeader(cmHandleId, targetNcmpServiceCmHandle,
70                         existingNcmpServiceCmHandle);
71         final LcmEvent lcmEvent =
72                 lcmEventsCreator.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle);
73         lcmEventsService.publishLcmEvent(cmHandleId, lcmEvent, lcmEventHeader);
74     }
75
76     private static NcmpServiceCmHandle toNcmpServiceCmHandle(final YangModelCmHandle yangModelCmHandle) {
77         return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(yangModelCmHandle);
78     }
79 }