LcmEvent state handler refactoring
[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.DELETED;
25 import static org.onap.cps.ncmp.api.inventory.CmHandleState.LOCKED;
26 import static org.onap.cps.ncmp.api.inventory.CmHandleState.READY;
27
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.stereotype.Service;
39
40 @Slf4j
41 @Service
42 @RequiredArgsConstructor
43 public class LcmEventsCmHandleStateHandlerImpl implements LcmEventsCmHandleStateHandler {
44
45     private final InventoryPersistence inventoryPersistence;
46     private final LcmEventsCreator lcmEventsCreator;
47     private final LcmEventsService lcmEventsService;
48
49     @Override
50     public void updateCmHandleState(final YangModelCmHandle yangModelCmHandle,
51             final CmHandleState targetCmHandleState) {
52
53         final CompositeState compositeState = yangModelCmHandle.getCompositeState();
54
55         if (compositeState != null && compositeState.getCmHandleState() == targetCmHandleState) {
56             log.debug("CmHandle with id : {} already in state : {}", yangModelCmHandle.getId(), targetCmHandleState);
57         } else {
58             final NcmpServiceCmHandle existingNcmpServiceCmHandle =
59                     new NcmpServiceCmHandle(toNcmpServiceCmHandle(yangModelCmHandle));
60             updateToSpecifiedCmHandleState(yangModelCmHandle, targetCmHandleState);
61             final NcmpServiceCmHandle targetNcmpServiceCmHandle = toNcmpServiceCmHandle(yangModelCmHandle);
62             publishLcmEvent(targetNcmpServiceCmHandle, existingNcmpServiceCmHandle);
63         }
64
65     }
66
67     private void updateToSpecifiedCmHandleState(final YangModelCmHandle yangModelCmHandle,
68             final CmHandleState targetCmHandleState) {
69
70         if (READY == targetCmHandleState) {
71             CompositeStateUtils.setCompositeStateToReadyWithInitialDataStoreSyncState()
72                     .accept(yangModelCmHandle.getCompositeState());
73             inventoryPersistence.saveCmHandleState(yangModelCmHandle.getId(), yangModelCmHandle.getCompositeState());
74         } else if (ADVISED == targetCmHandleState) {
75             if (yangModelCmHandle.getCompositeState() == null) {
76                 registerNewCmHandle(yangModelCmHandle);
77             } else if (yangModelCmHandle.getCompositeState().getCmHandleState() == LOCKED) {
78                 retryCmHandle(yangModelCmHandle);
79             }
80         } else if (DELETED == targetCmHandleState) {
81             setCmHandleState(yangModelCmHandle, targetCmHandleState);
82         } else {
83             updateAndSaveCmHandleState(yangModelCmHandle, targetCmHandleState);
84         }
85     }
86
87     private void retryCmHandle(final YangModelCmHandle yangModelCmHandle) {
88         CompositeStateUtils.setCompositeStateForRetry().accept(yangModelCmHandle.getCompositeState());
89         inventoryPersistence.saveCmHandleState(yangModelCmHandle.getId(), yangModelCmHandle.getCompositeState());
90     }
91
92     private void registerNewCmHandle(final YangModelCmHandle yangModelCmHandle) {
93         yangModelCmHandle.setCompositeState(new CompositeState());
94         setCmHandleState(yangModelCmHandle, ADVISED);
95         inventoryPersistence.saveCmHandle(yangModelCmHandle);
96     }
97
98     private void publishLcmEvent(final NcmpServiceCmHandle targetNcmpServiceCmHandle,
99             final NcmpServiceCmHandle existingNcmpServiceCmHandle) {
100         final String cmHandleId = targetNcmpServiceCmHandle.getCmHandleId();
101         final LcmEvent lcmEvent =
102                 lcmEventsCreator.populateLcmEvent(cmHandleId, targetNcmpServiceCmHandle, existingNcmpServiceCmHandle);
103         lcmEventsService.publishLcmEvent(cmHandleId, lcmEvent);
104     }
105
106     private void updateAndSaveCmHandleState(final YangModelCmHandle yangModelCmHandle,
107             final CmHandleState targetCmHandleState) {
108         setCmHandleState(yangModelCmHandle, targetCmHandleState);
109         inventoryPersistence.saveCmHandleState(yangModelCmHandle.getId(), yangModelCmHandle.getCompositeState());
110     }
111
112     private void setCmHandleState(final YangModelCmHandle yangModelCmHandle, final CmHandleState targetCmHandleState) {
113         CompositeStateUtils.setCompositeState(targetCmHandleState).accept(yangModelCmHandle.getCompositeState());
114     }
115
116     private NcmpServiceCmHandle toNcmpServiceCmHandle(final YangModelCmHandle yangModelCmHandle) {
117         return YangDataConverter.convertYangModelCmHandleToNcmpServiceCmHandle(yangModelCmHandle);
118     }
119 }