650251f5ae337633d9a4e2b7cb344214dec6a47a
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / event / lcm / LcmEventsCmHandleStateHandlerImpl.java
1 /*
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
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.event.lcm;
22
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;
26
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.ncmp.cmhandle.event.lcm.LcmEvent;
36 import org.springframework.beans.factory.annotation.Value;
37 import org.springframework.stereotype.Service;
38
39 @Slf4j
40 @Service
41 @RequiredArgsConstructor
42 public class LcmEventsCmHandleStateHandlerImpl implements LcmEventsCmHandleStateHandler {
43
44     private final InventoryPersistence inventoryPersistence;
45     private final LcmEventsCreator lcmEventsCreator;
46     private final LcmEventsService lcmEventsService;
47
48     @Value("${data-sync.cache.enabled:false}")
49     private boolean isGlobalDataSyncCacheEnabled;
50
51
52     @Override
53     public void updateCmHandleState(final YangModelCmHandle yangModelCmHandle,
54             final CmHandleState targetCmHandleState) {
55
56         if (yangModelCmHandle.getCompositeState().getCmHandleState() == targetCmHandleState) {
57             log.debug("CmHandle with id : {} already in state : {}", yangModelCmHandle.getId(), targetCmHandleState);
58         } else {
59             updateToSpecifiedCmHandleState(yangModelCmHandle, targetCmHandleState);
60             publishLcmEvent(yangModelCmHandle);
61         }
62
63     }
64
65     private void updateToSpecifiedCmHandleState(final YangModelCmHandle yangModelCmHandle,
66             final CmHandleState targetCmHandleState) {
67
68         if (READY == targetCmHandleState) {
69             CompositeStateUtils.setCompositeStateToReadyWithInitialDataStoreSyncState(isGlobalDataSyncCacheEnabled)
70                     .accept(yangModelCmHandle.getCompositeState());
71             inventoryPersistence.saveCmHandleState(yangModelCmHandle.getId(), yangModelCmHandle.getCompositeState());
72         } else if (ADVISED == targetCmHandleState) {
73             if (yangModelCmHandle.getCompositeState().getCmHandleState() == LOCKED) {
74                 retryCmHandle(yangModelCmHandle);
75             } else {
76                 registerNewCmHandle(yangModelCmHandle);
77             }
78         } else {
79             CompositeStateUtils.setCompositeState(targetCmHandleState).accept(yangModelCmHandle.getCompositeState());
80             inventoryPersistence.saveCmHandleState(yangModelCmHandle.getId(), yangModelCmHandle.getCompositeState());
81         }
82
83     }
84
85     private void retryCmHandle(final YangModelCmHandle yangModelCmHandle) {
86         CompositeStateUtils.setCompositeStateForRetry().accept(yangModelCmHandle.getCompositeState());
87         inventoryPersistence.saveCmHandleState(yangModelCmHandle.getId(), yangModelCmHandle.getCompositeState());
88     }
89
90     private void registerNewCmHandle(final YangModelCmHandle yangModelCmHandle) {
91         CompositeStateUtils.setCompositeState(ADVISED).accept(yangModelCmHandle.getCompositeState());
92         inventoryPersistence.saveCmHandle(yangModelCmHandle);
93     }
94
95     private void publishLcmEvent(final YangModelCmHandle yangModelCmHandle) {
96         final NcmpServiceCmHandle ncmpServiceCmHandle =
97                 YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(yangModelCmHandle);
98         final String cmHandleId = ncmpServiceCmHandle.getCmHandleId();
99         final LcmEvent lcmEvent = lcmEventsCreator.populateLcmEvent(cmHandleId);
100         lcmEventsService.publishLcmEvent(cmHandleId, lcmEvent);
101     }
102 }