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;
23 import static org.onap.cps.ncmp.api.inventory.CmHandleState.ADVISED;
24 import static org.onap.cps.ncmp.api.inventory.CmHandleState.LOCKED;
25 import static org.onap.cps.ncmp.api.inventory.CmHandleState.READY;
27 import lombok.RequiredArgsConstructor;
28 import lombok.extern.slf4j.Slf4j;
29 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
30 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
31 import org.onap.cps.ncmp.api.inventory.CmHandleState;
32 import org.onap.cps.ncmp.api.inventory.CompositeStateUtils;
33 import org.onap.cps.ncmp.api.inventory.InventoryPersistence;
34 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
35 import org.onap.cps.utils.JsonObjectMapper;
36 import org.onap.ncmp.cmhandle.lcm.event.NcmpEvent;
37 import org.springframework.beans.factory.annotation.Value;
38 import org.springframework.stereotype.Service;
42 @RequiredArgsConstructor
43 public class NcmpEventsCmHandleStateHandlerImpl implements NcmpEventsCmHandleStateHandler {
45 private final InventoryPersistence inventoryPersistence;
46 private final NcmpEventsCreator ncmpEventsCreator;
47 private final JsonObjectMapper jsonObjectMapper;
48 private final NcmpEventsService ncmpEventsService;
50 @Value("${data-sync.cache.enabled:false}")
51 private boolean isGlobalDataSyncCacheEnabled;
55 public void updateCmHandleState(final YangModelCmHandle yangModelCmHandle,
56 final CmHandleState targetCmHandleState) {
58 if (yangModelCmHandle.getCompositeState().getCmHandleState() == targetCmHandleState) {
59 log.debug("CmHandle with id : {} already in state : {}", yangModelCmHandle.getId(), targetCmHandleState);
61 updateToSpecifiedCmHandleState(yangModelCmHandle, targetCmHandleState);
62 publishNcmpEvent(yangModelCmHandle);
67 private void updateToSpecifiedCmHandleState(final YangModelCmHandle yangModelCmHandle,
68 final CmHandleState targetCmHandleState) {
70 if (READY == targetCmHandleState) {
71 CompositeStateUtils.setCompositeStateToReadyWithInitialDataStoreSyncState(isGlobalDataSyncCacheEnabled)
72 .accept(yangModelCmHandle.getCompositeState());
73 inventoryPersistence.saveCmHandleState(yangModelCmHandle.getId(), yangModelCmHandle.getCompositeState());
74 } else if (ADVISED == targetCmHandleState) {
75 if (yangModelCmHandle.getCompositeState().getCmHandleState() == LOCKED) {
76 retryCmHandle(yangModelCmHandle);
78 registerNewCmHandle(yangModelCmHandle);
81 CompositeStateUtils.setCompositeState(targetCmHandleState).accept(yangModelCmHandle.getCompositeState());
82 inventoryPersistence.saveCmHandleState(yangModelCmHandle.getId(), yangModelCmHandle.getCompositeState());
87 private void retryCmHandle(final YangModelCmHandle yangModelCmHandle) {
88 CompositeStateUtils.setCompositeStateForRetry()
89 .accept(yangModelCmHandle.getCompositeState());
90 inventoryPersistence.saveCmHandleState(yangModelCmHandle.getId(), yangModelCmHandle.getCompositeState());
93 private void registerNewCmHandle(final YangModelCmHandle yangModelCmHandle) {
94 CompositeStateUtils.setCompositeState(ADVISED).accept(yangModelCmHandle.getCompositeState());
95 inventoryPersistence.saveListElements(
96 String.format("{\"cm-handles\":[%s]}", jsonObjectMapper.asJsonString(yangModelCmHandle)));
99 private void publishNcmpEvent(final YangModelCmHandle yangModelCmHandle) {
100 final NcmpServiceCmHandle ncmpServiceCmHandle =
101 YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(yangModelCmHandle);
102 final String cmHandleId = ncmpServiceCmHandle.getCmHandleId();
103 final NcmpEvent ncmpEvent = ncmpEventsCreator.populateNcmpEvent(cmHandleId, ncmpServiceCmHandle);
104 ncmpEventsService.publishNcmpEvent(cmHandleId, ncmpEvent);