2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2023-2024 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.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();
47 private static final String LOCKED = LockState.LOCKED.name();
48 private static final String LOCKING = LockState.LOCKING.name();
49 private static final String UNLOCKED = LockState.UNLOCKED.name();
50 private static final String UNLOCKING = LockState.UNLOCKING.name();
51 private static final String STATE_LOCKED_NONE = LockState.NONE.name();
53 private static final String DEPLOY_NONE = DeployOrder.NONE.name();
54 private static final String LOCK_NONE = LockOrder.NONE.name();
55 private static final String SUB_NONE = SubOrder.NONE.name();
57 private static final String NO_ERROR = StateChangeResult.NO_ERROR.name();
58 private static final String FAILED = StateChangeResult.FAILED.name();
59 private static final String TIMEOUT = StateChangeResult.TIMEOUT.name();
62 public static final String DEPLOY = DeployOrder.DEPLOY.name();
63 public static final String UNDEPLOY = DeployOrder.UNDEPLOY.name();
64 public static final String DELETE = DeployOrder.DELETE.name();
65 public static final String LOCK = LockOrder.LOCK.name();
66 public static final String UNLOCK = LockOrder.UNLOCK.name();
67 public static final String MIGRATE = DeployOrder.MIGRATE.name();
68 public static final String MIGRATE_PRECHECK = SubOrder.MIGRATE_PRECHECK.name();
69 public static final String PREPARE = SubOrder.PREPARE.name();
70 public static final String REVIEW = SubOrder.REVIEW.name();
71 public static final String UPDATE = DeployOrder.UPDATE.name();
72 public static final String NONE = "NONE";
77 public AcInstanceStateResolver() {
78 this.graph = new StateDefinition<>(7, NONE);
80 // make an order when there are no fails
81 this.graph.put(new String[] {DEPLOY, LOCK_NONE, SUB_NONE,
82 UNDEPLOYED, STATE_LOCKED_NONE, SUB_STATE_NONE, NO_ERROR}, DEPLOY);
83 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
84 DEPLOYED, LOCKED, SUB_STATE_NONE, NO_ERROR}, UNDEPLOY);
85 this.graph.put(new String[] {DELETE, LOCK_NONE, SUB_NONE,
86 UNDEPLOYED, LOCK_NONE, SUB_STATE_NONE, NO_ERROR}, DELETE);
87 this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, SUB_NONE,
88 DEPLOYED, LOCKED, SUB_STATE_NONE, NO_ERROR}, UNLOCK);
89 this.graph.put(new String[] {DEPLOY_NONE, LOCK, SUB_NONE,
90 DEPLOYED, UNLOCKED, SUB_STATE_NONE, NO_ERROR}, LOCK);
91 this.graph.put(new String[] {MIGRATE, LOCK_NONE, SUB_NONE,
92 DEPLOYED, LOCKED, SUB_STATE_NONE, NO_ERROR}, MIGRATE);
93 this.graph.put(new String[] {UPDATE, LOCK_NONE, SUB_NONE,
94 DEPLOYED, LOCKED, SUB_STATE_NONE, NO_ERROR}, UPDATE);
95 this.graph.put(new String[] {DEPLOY_NONE, LOCK_NONE, REVIEW,
96 DEPLOYED, LOCKED, SUB_STATE_NONE, NO_ERROR}, REVIEW);
97 this.graph.put(new String[] {DEPLOY_NONE, LOCK_NONE, PREPARE,
98 UNDEPLOYED, STATE_LOCKED_NONE, SUB_STATE_NONE, NO_ERROR}, PREPARE);
99 this.graph.put(new String[] {DEPLOY_NONE, LOCK_NONE, MIGRATE_PRECHECK,
100 DEPLOYED, LOCKED, SUB_STATE_NONE, NO_ERROR}, MIGRATE_PRECHECK);
102 // make an order in a failed scenario
103 this.graph.put(new String[] {DEPLOY, LOCK_NONE, SUB_NONE,
104 UNDEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, FAILED}, DEPLOY);
105 this.graph.put(new String[] {DEPLOY, LOCK_NONE, SUB_NONE,
106 DEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, FAILED}, DEPLOY);
108 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
109 UNDEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, FAILED}, UNDEPLOY);
110 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
111 UPDATING, LOCKED, SUB_STATE_NONE, FAILED}, UNDEPLOY);
112 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
113 MIGRATING, LOCKED, SUB_STATE_NONE, FAILED}, UNDEPLOY);
114 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
115 DEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, FAILED}, UNDEPLOY);
117 this.graph.put(new String[] {DELETE, LOCK_NONE, SUB_NONE,
118 DELETING, LOCK_NONE, SUB_STATE_NONE, FAILED}, DELETE);
120 this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, SUB_NONE,
121 DEPLOYED, LOCKING, SUB_STATE_NONE, FAILED}, UNLOCK);
122 this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, SUB_NONE,
123 DEPLOYED, UNLOCKING, SUB_STATE_NONE, FAILED}, UNLOCK);
125 this.graph.put(new String[] {DEPLOY_NONE, LOCK, SUB_NONE, DEPLOYED, LOCKING, SUB_STATE_NONE, FAILED}, LOCK);
126 this.graph.put(new String[] {DEPLOY_NONE, LOCK, SUB_NONE, DEPLOYED, UNLOCKING, SUB_STATE_NONE, FAILED}, LOCK);
128 this.graph.put(new String[] {UPDATE, LOCK_NONE, SUB_NONE, UPDATING, LOCKED, SUB_STATE_NONE, FAILED}, UPDATE);
130 this.graph.put(new String[] {DEPLOY_NONE, LOCK_NONE, MIGRATE_PRECHECK,
131 DEPLOYED, LOCKED, MIGRATION_PRECHECKING, FAILED}, MIGRATE_PRECHECK);
134 this.graph.put(new String[] {DEPLOY, LOCK_NONE, SUB_NONE,
135 UNDEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, TIMEOUT}, DEPLOY);
136 this.graph.put(new String[] {DEPLOY, LOCK_NONE, SUB_NONE,
137 DEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, TIMEOUT}, DEPLOY);
139 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
140 UNDEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, TIMEOUT}, UNDEPLOY);
141 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
142 UPDATING, LOCKED, SUB_STATE_NONE, TIMEOUT}, UNDEPLOY);
143 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
144 MIGRATING, LOCKED, SUB_STATE_NONE, TIMEOUT}, UNDEPLOY);
145 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
146 MIGRATION_PRECHECKING, LOCKED, SUB_STATE_NONE, TIMEOUT}, UNDEPLOY);
147 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
148 DEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, TIMEOUT}, UNDEPLOY);
150 this.graph.put(new String[] {DELETE, LOCK_NONE, SUB_NONE,
151 DELETING, LOCK_NONE, SUB_STATE_NONE, TIMEOUT}, DELETE);
153 this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, SUB_NONE,
154 DEPLOYED, LOCKING, SUB_STATE_NONE, TIMEOUT}, UNLOCK);
155 this.graph.put(new String[] {DEPLOY_NONE, LOCK, SUB_NONE,
156 DEPLOYED, LOCKING, SUB_STATE_NONE, TIMEOUT}, LOCK);
158 this.graph.put(new String[] {DEPLOY_NONE, LOCK, SUB_NONE,
159 DEPLOYED, UNLOCKING, SUB_STATE_NONE, TIMEOUT}, LOCK);
160 this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, SUB_NONE,
161 DEPLOYED, UNLOCKING, SUB_STATE_NONE, TIMEOUT}, UNLOCK);
163 this.graph.put(new String[] {UPDATE, LOCK_NONE, SUB_NONE, UPDATING, LOCKED, SUB_STATE_NONE, TIMEOUT}, UPDATE);
165 this.graph.put(new String[] {DEPLOY_NONE, LOCK_NONE, MIGRATE_PRECHECK,
166 DEPLOYED, LOCKED, MIGRATION_PRECHECKING, TIMEOUT}, MIGRATE_PRECHECK);
170 * Check if Deploy Order and Lock Order are consistent with current DeployState and LockState.
172 * @param acDeployOrder the Deploy Ordered
173 * @param acLockOrder the Lock Ordered
174 * @param acSubOrder the Sub Ordered
175 * @param acDeployState then current Deploy State
176 * @param acLockState the current Lock State
177 * @param acSubState the current Sub State
178 * @param acStateChangeResult the current Result of the State Change
179 * @return the order (DEPLOY/UNDEPLOY/LOCK/UNLOCK) to send to participant or NONE if order is not consistent
181 public String resolve(DeployOrder acDeployOrder, LockOrder acLockOrder, SubOrder acSubOrder,
182 DeployState acDeployState, LockState acLockState, SubState acSubState, StateChangeResult acStateChangeResult) {
183 var deployOrder = acDeployOrder != null ? acDeployOrder : DeployOrder.NONE;
184 var lockOrder = acLockOrder != null ? acLockOrder : LockOrder.NONE;
185 var subOrder = acSubOrder != null ? acSubOrder : SubOrder.NONE;
186 var stateChangeResult = acStateChangeResult != null ? acStateChangeResult : StateChangeResult.NO_ERROR;
188 var deployState = acDeployState != null ? acDeployState : DeployState.UNDEPLOYED;
189 var lockState = acLockState != null ? acLockState : LockState.NONE;
190 var subState = acSubState != null ? acSubState : SubState.NONE;
191 return this.graph.get(new String[] {deployOrder.name(), lockOrder.name(), subOrder.name(),
192 deployState.name(), lockState.name(), subState.name(), stateChangeResult.name()});