2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 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.event.lcm;
23 import static org.onap.cps.ncmp.api.inventory.CmHandleState.ADVISED;
24 import static org.onap.cps.ncmp.api.inventory.CmHandleState.DELETED;
25 import static org.onap.cps.ncmp.api.inventory.CmHandleState.LOCKED;
26 import static org.onap.cps.ncmp.api.inventory.CmHandleState.READY;
28 import lombok.RequiredArgsConstructor;
29 import lombok.extern.slf4j.Slf4j;
30 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
31 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
32 import org.onap.cps.ncmp.api.inventory.CmHandleState;
33 import org.onap.cps.ncmp.api.inventory.CompositeState;
34 import org.onap.cps.ncmp.api.inventory.CompositeStateUtils;
35 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
36 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
37 import org.onap.ncmp.cmhandle.event.lcm.LcmEvent;
38 import org.springframework.scheduling.annotation.Async;
39 import org.springframework.stereotype.Service;
43 @RequiredArgsConstructor
44 public class LcmEventsCmHandleStateHandlerImpl implements LcmEventsCmHandleStateHandler {
46 private final InventoryPersistence inventoryPersistence;
47 private final LcmEventsCreator lcmEventsCreator;
48 private final LcmEventsService lcmEventsService;
51 public void updateCmHandleState(final YangModelCmHandle yangModelCmHandle,
52 final CmHandleState targetCmHandleState) {
54 final CompositeState compositeState = yangModelCmHandle.getCompositeState();
56 if (compositeState != null && compositeState.getCmHandleState() == targetCmHandleState) {
57 log.debug("CmHandle with id : {} already in state : {}", yangModelCmHandle.getId(), targetCmHandleState);
59 final NcmpServiceCmHandle existingNcmpServiceCmHandle =
60 new NcmpServiceCmHandle(toNcmpServiceCmHandle(yangModelCmHandle));
61 updateToSpecifiedCmHandleState(yangModelCmHandle, targetCmHandleState);
62 final NcmpServiceCmHandle targetNcmpServiceCmHandle = toNcmpServiceCmHandle(yangModelCmHandle);
63 publishLcmEvent(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle);
68 @Async("notificationExecutor")
70 public void publishLcmEvent(final NcmpServiceCmHandle targetNcmpServiceCmHandle,
71 final NcmpServiceCmHandle existingNcmpServiceCmHandle) {
72 final String cmHandleId = targetNcmpServiceCmHandle.getCmHandleId();
73 final LcmEvent lcmEvent =
74 lcmEventsCreator.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle);
75 lcmEventsService.publishLcmEvent(cmHandleId, lcmEvent);
78 private void updateToSpecifiedCmHandleState(final YangModelCmHandle yangModelCmHandle,
79 final CmHandleState targetCmHandleState) {
81 if (READY == targetCmHandleState) {
82 CompositeStateUtils.setCompositeStateToReadyWithInitialDataStoreSyncState()
83 .accept(yangModelCmHandle.getCompositeState());
84 inventoryPersistence.saveCmHandleState(yangModelCmHandle.getId(), yangModelCmHandle.getCompositeState());
85 } else if (ADVISED == targetCmHandleState) {
86 if (yangModelCmHandle.getCompositeState() == null) {
87 registerNewCmHandle(yangModelCmHandle);
88 } else if (yangModelCmHandle.getCompositeState().getCmHandleState() == LOCKED) {
89 retryCmHandle(yangModelCmHandle);
91 } else if (DELETED == targetCmHandleState) {
92 setCmHandleState(yangModelCmHandle, targetCmHandleState);
94 updateAndSaveCmHandleState(yangModelCmHandle, targetCmHandleState);
98 private void retryCmHandle(final YangModelCmHandle yangModelCmHandle) {
99 CompositeStateUtils.setCompositeStateForRetry().accept(yangModelCmHandle.getCompositeState());
100 inventoryPersistence.saveCmHandleState(yangModelCmHandle.getId(), yangModelCmHandle.getCompositeState());
103 private void registerNewCmHandle(final YangModelCmHandle yangModelCmHandle) {
104 yangModelCmHandle.setCompositeState(new CompositeState());
105 setCmHandleState(yangModelCmHandle, ADVISED);
106 inventoryPersistence.saveCmHandle(yangModelCmHandle);
109 private void updateAndSaveCmHandleState(final YangModelCmHandle yangModelCmHandle,
110 final CmHandleState targetCmHandleState) {
111 setCmHandleState(yangModelCmHandle, targetCmHandleState);
112 inventoryPersistence.saveCmHandleState(yangModelCmHandle.getId(), yangModelCmHandle.getCompositeState());
115 private void setCmHandleState(final YangModelCmHandle yangModelCmHandle, final CmHandleState targetCmHandleState) {
116 CompositeStateUtils.setCompositeState(targetCmHandleState).accept(yangModelCmHandle.getCompositeState());
119 private NcmpServiceCmHandle toNcmpServiceCmHandle(final YangModelCmHandle yangModelCmHandle) {
120 return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(yangModelCmHandle);