2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023-2025 OpenInfra Foundation Europe. All rights reserved.
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.policy.clamp.models.acm.persistence.provider;
23 import org.onap.policy.clamp.models.acm.concepts.DeployState;
24 import org.onap.policy.clamp.models.acm.concepts.LockState;
25 import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
26 import org.onap.policy.clamp.models.acm.concepts.SubState;
27 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
28 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
29 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.SubOrder;
30 import org.onap.policy.clamp.models.acm.utils.StateDefinition;
31 import org.springframework.stereotype.Component;
34 public class AcInstanceStateResolver {
35 private final StateDefinition<String> graph;
37 private static final String DEPLOYED = DeployState.DEPLOYED.name();
38 private static final String DEPLOYING = DeployState.DEPLOYING.name();
39 private static final String UNDEPLOYED = DeployState.UNDEPLOYED.name();
40 private static final String UNDEPLOYING = DeployState.UNDEPLOYING.name();
41 private static final String UPDATING = DeployState.UPDATING.name();
42 private static final String DELETING = DeployState.DELETING.name();
43 private static final String MIGRATING = DeployState.MIGRATING.name();
44 private static final String MIGRATION_PRECHECKING = SubState.MIGRATION_PRECHECKING.name();
45 private static final String SUB_STATE_NONE = SubState.NONE.name();
46 private static final String PREPARING = SubState.PREPARING.name();
47 private static final String REVIEWING = SubState.REVIEWING.name();
49 private static final String LOCKED = LockState.LOCKED.name();
50 private static final String LOCKING = LockState.LOCKING.name();
51 private static final String UNLOCKED = LockState.UNLOCKED.name();
52 private static final String UNLOCKING = LockState.UNLOCKING.name();
53 private static final String STATE_LOCKED_NONE = LockState.NONE.name();
55 private static final String DEPLOY_NONE = DeployOrder.NONE.name();
56 private static final String LOCK_NONE = LockOrder.NONE.name();
57 private static final String SUB_NONE = SubOrder.NONE.name();
59 private static final String NO_ERROR = StateChangeResult.NO_ERROR.name();
60 private static final String FAILED = StateChangeResult.FAILED.name();
61 private static final String TIMEOUT = StateChangeResult.TIMEOUT.name();
64 public static final String DEPLOY = DeployOrder.DEPLOY.name();
65 public static final String UNDEPLOY = DeployOrder.UNDEPLOY.name();
66 public static final String DELETE = DeployOrder.DELETE.name();
67 public static final String LOCK = LockOrder.LOCK.name();
68 public static final String UNLOCK = LockOrder.UNLOCK.name();
69 public static final String MIGRATE = DeployOrder.MIGRATE.name();
70 public static final String MIGRATE_PRECHECK = SubOrder.MIGRATE_PRECHECK.name();
71 public static final String PREPARE = SubOrder.PREPARE.name();
72 public static final String REVIEW = SubOrder.REVIEW.name();
73 public static final String UPDATE = DeployOrder.UPDATE.name();
74 public static final String NONE = "NONE";
79 public AcInstanceStateResolver() {
80 this.graph = new StateDefinition<>(7, NONE);
82 // make an order when there are no fails
83 addDeployOrder(DEPLOY, UNDEPLOYED, STATE_LOCKED_NONE);
84 addDeployOrder(UNDEPLOY, DEPLOYED, LOCKED);
85 addDeployOrder(DELETE, UNDEPLOYED, STATE_LOCKED_NONE);
86 addDeployOrder(MIGRATE, DEPLOYED, LOCKED);
87 addDeployOrder(UPDATE, DEPLOYED, LOCKED);
89 addLockOrder(UNLOCK, LOCKED);
90 addLockOrder(LOCK, UNLOCKED);
92 addSubOrder(REVIEW, DEPLOYED, LOCKED);
93 addSubOrder(PREPARE, UNDEPLOYED, STATE_LOCKED_NONE);
94 addSubOrder(MIGRATE_PRECHECK, DEPLOYED, LOCKED);
96 // failed or timeout scenario
100 // undeploy order in a failed or timeout scenario
101 addDeployOrderWithFail(UNDEPLOY, UPDATING, LOCKED, SUB_STATE_NONE);
102 addDeployOrderWithFail(UNDEPLOY, MIGRATING, LOCKED, SUB_STATE_NONE);
103 addDeployOrderWithFail(UNDEPLOY, MIGRATION_PRECHECKING, LOCKED, SUB_STATE_NONE);
104 addDeployOrderWithFail(UNDEPLOY, REVIEWING, LOCKED, SUB_STATE_NONE);
106 // delete order in a failed or timeout scenario
107 addDeployOrderWithFail(DELETE, DELETING, LOCK_NONE, SUB_STATE_NONE);
108 addDeployOrderWithFail(DELETE, UNDEPLOYED, LOCK_NONE, PREPARING);
110 // update order in a failed or timeout scenario
111 addDeployOrderWithFail(UPDATE, UPDATING, LOCKED, SUB_STATE_NONE);
113 // unlock order in a failed or timeout scenario
114 addLockOrderWithFail(UNLOCK, LOCKING);
115 addLockOrderWithFail(UNLOCK, UNLOCKING);
117 // lock order in a failed or timeout scenario
118 addLockOrderWithFail(LOCK, LOCKING);
119 addLockOrderWithFail(LOCK, UNLOCKING);
121 // migrate-precheck order in a failed or timeout scenario
122 addSubOrderWithFail(MIGRATE_PRECHECK, DEPLOYED, LOCKED, MIGRATION_PRECHECKING);
124 // prepare order in a failed or timeout scenario
125 addSubOrderWithFail(PREPARE, UNDEPLOYED, LOCK_NONE, PREPARING);
127 // review order in a failed or timeout scenario
128 addSubOrderWithFail(REVIEW, DEPLOYED, LOCKED, REVIEWING);
131 private void addDeployOrder(String deployOrder, String deployState, String lockState) {
132 this.graph.put(new String[] {
133 deployOrder, LOCK_NONE, SUB_NONE, deployState, lockState, STATE_LOCKED_NONE, NO_ERROR}, deployOrder);
136 private void addDeployOrderWithFail(String deployOrder, String deployState, String lockState, String subState) {
137 this.graph.put(new String[] {
138 deployOrder, LOCK_NONE, SUB_NONE, deployState, lockState, subState, FAILED}, deployOrder);
139 this.graph.put(new String[] {
140 deployOrder, LOCK_NONE, SUB_NONE, deployState, lockState, subState, TIMEOUT}, deployOrder);
143 private void addSubOrder(String subOrder, String deployState, String lockState) {
144 this.graph.put(new String[] {
145 DEPLOY_NONE, LOCK_NONE, subOrder, deployState, lockState, SUB_STATE_NONE, NO_ERROR}, subOrder);
148 private void addSubOrderWithFail(String subOrder, String deployState, String lockState, String subState) {
149 this.graph.put(new String[] {
150 DEPLOY_NONE, LOCK_NONE, subOrder, deployState, lockState, subState, FAILED}, subOrder);
151 this.graph.put(new String[] {
152 DEPLOY_NONE, LOCK_NONE, subOrder, deployState, lockState, subState, TIMEOUT}, subOrder);
155 private void addLockOrder(String lockOrder, String lockState) {
156 this.graph.put(new String[] {
157 DEPLOY_NONE, lockOrder, SUB_NONE, DEPLOYED, lockState, SUB_STATE_NONE, NO_ERROR}, lockOrder);
160 private void addLockOrderWithFail(String lockOrder, String lockState) {
161 this.graph.put(new String[] {
162 DEPLOY_NONE, lockOrder, SUB_NONE, DEPLOYED, lockState, SUB_STATE_NONE, FAILED}, lockOrder);
163 this.graph.put(new String[] {
164 DEPLOY_NONE, lockOrder, SUB_NONE, DEPLOYED, lockState, SUB_STATE_NONE, TIMEOUT}, lockOrder);
167 private void setAllowed(String deployOrder) {
168 addDeployOrderWithFail(deployOrder, UNDEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE);
169 addDeployOrderWithFail(deployOrder, UNDEPLOYING, LOCKED, SUB_STATE_NONE);
170 addDeployOrderWithFail(deployOrder, DEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE);
171 addDeployOrderWithFail(deployOrder, DEPLOYING, LOCKED, SUB_STATE_NONE);
175 * Check if Deploy Order and Lock Order are consistent with current DeployState and LockState.
177 * @param acDeployOrder the Deploy Ordered
178 * @param acLockOrder the Lock Ordered
179 * @param acSubOrder the Sub Ordered
180 * @param acDeployState then current Deploy State
181 * @param acLockState the current Lock State
182 * @param acSubState the current Sub State
183 * @param acStateChangeResult the current Result of the State Change
184 * @return the order (DEPLOY/UNDEPLOY/LOCK/UNLOCK) to send to participant or NONE if order is not consistent
186 public String resolve(DeployOrder acDeployOrder, LockOrder acLockOrder, SubOrder acSubOrder,
187 DeployState acDeployState, LockState acLockState, SubState acSubState, StateChangeResult acStateChangeResult) {
188 var deployOrder = acDeployOrder != null ? acDeployOrder : DeployOrder.NONE;
189 var lockOrder = acLockOrder != null ? acLockOrder : LockOrder.NONE;
190 var subOrder = acSubOrder != null ? acSubOrder : SubOrder.NONE;
191 var stateChangeResult = acStateChangeResult != null ? acStateChangeResult : StateChangeResult.NO_ERROR;
193 var deployState = acDeployState != null ? acDeployState : DeployState.UNDEPLOYED;
194 var lockState = acLockState != null ? acLockState : LockState.NONE;
195 var subState = acSubState != null ? acSubState : SubState.NONE;
196 return this.graph.get(new String[] {deployOrder.name(), lockOrder.name(), subOrder.name(),
197 deployState.name(), lockState.name(), subState.name(), stateChangeResult.name()});