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 PREPARING = SubState.PREPARING.name();
46 private static final String REVIEWING = SubState.REVIEWING.name();
47 private static final String SUB_STATE_NONE = SubState.NONE.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 this.graph.put(new String[] {DEPLOY, LOCK_NONE, SUB_NONE,
84 UNDEPLOYED, STATE_LOCKED_NONE, SUB_STATE_NONE, NO_ERROR}, DEPLOY);
85 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
86 DEPLOYED, LOCKED, SUB_STATE_NONE, NO_ERROR}, UNDEPLOY);
87 this.graph.put(new String[] {DELETE, LOCK_NONE, SUB_NONE,
88 UNDEPLOYED, LOCK_NONE, SUB_STATE_NONE, NO_ERROR}, DELETE);
89 this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, SUB_NONE,
90 DEPLOYED, LOCKED, SUB_STATE_NONE, NO_ERROR}, UNLOCK);
91 this.graph.put(new String[] {DEPLOY_NONE, LOCK, SUB_NONE,
92 DEPLOYED, UNLOCKED, SUB_STATE_NONE, NO_ERROR}, LOCK);
93 this.graph.put(new String[] {MIGRATE, LOCK_NONE, SUB_NONE,
94 DEPLOYED, LOCKED, SUB_STATE_NONE, NO_ERROR}, MIGRATE);
95 this.graph.put(new String[] {UPDATE, LOCK_NONE, SUB_NONE,
96 DEPLOYED, LOCKED, SUB_STATE_NONE, NO_ERROR}, UPDATE);
97 this.graph.put(new String[] {DEPLOY_NONE, LOCK_NONE, REVIEW,
98 DEPLOYED, LOCKED, SUB_STATE_NONE, NO_ERROR}, REVIEW);
99 this.graph.put(new String[] {DEPLOY_NONE, LOCK_NONE, PREPARE,
100 UNDEPLOYED, STATE_LOCKED_NONE, SUB_STATE_NONE, NO_ERROR}, PREPARE);
101 this.graph.put(new String[] {DEPLOY_NONE, LOCK_NONE, MIGRATE_PRECHECK,
102 DEPLOYED, LOCKED, SUB_STATE_NONE, NO_ERROR}, MIGRATE_PRECHECK);
104 // make an order in a failed scenario
105 this.graph.put(new String[] {DEPLOY, LOCK_NONE, SUB_NONE,
106 UNDEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, FAILED}, DEPLOY);
107 this.graph.put(new String[] {DEPLOY, LOCK_NONE, SUB_NONE,
108 DEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, FAILED}, DEPLOY);
110 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
111 UNDEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, FAILED}, UNDEPLOY);
112 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
113 UPDATING, LOCKED, SUB_STATE_NONE, FAILED}, UNDEPLOY);
114 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
115 MIGRATING, LOCKED, SUB_STATE_NONE, FAILED}, UNDEPLOY);
116 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
117 DEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, FAILED}, UNDEPLOY);
119 this.graph.put(new String[] {DELETE, LOCK_NONE, SUB_NONE,
120 DELETING, LOCK_NONE, SUB_STATE_NONE, FAILED}, DELETE);
122 this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, SUB_NONE,
123 DEPLOYED, LOCKING, SUB_STATE_NONE, FAILED}, UNLOCK);
124 this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, SUB_NONE,
125 DEPLOYED, UNLOCKING, SUB_STATE_NONE, FAILED}, UNLOCK);
127 this.graph.put(new String[] {DEPLOY_NONE, LOCK, SUB_NONE, DEPLOYED, LOCKING, SUB_STATE_NONE, FAILED}, LOCK);
128 this.graph.put(new String[] {DEPLOY_NONE, LOCK, SUB_NONE, DEPLOYED, UNLOCKING, SUB_STATE_NONE, FAILED}, LOCK);
130 this.graph.put(new String[] {UPDATE, LOCK_NONE, SUB_NONE, UPDATING, LOCKED, SUB_STATE_NONE, FAILED}, UPDATE);
132 this.graph.put(new String[] {DEPLOY_NONE, LOCK_NONE, MIGRATE_PRECHECK,
133 DEPLOYED, LOCKED, MIGRATION_PRECHECKING, FAILED}, MIGRATE_PRECHECK);
136 this.graph.put(new String[] {DEPLOY, LOCK_NONE, SUB_NONE,
137 UNDEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, TIMEOUT}, DEPLOY);
138 this.graph.put(new String[] {DEPLOY, LOCK_NONE, SUB_NONE,
139 DEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, TIMEOUT}, DEPLOY);
141 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
142 UNDEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, TIMEOUT}, UNDEPLOY);
143 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
144 UPDATING, LOCKED, SUB_STATE_NONE, TIMEOUT}, UNDEPLOY);
145 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
146 MIGRATING, LOCKED, SUB_STATE_NONE, TIMEOUT}, UNDEPLOY);
147 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
148 MIGRATION_PRECHECKING, LOCKED, SUB_STATE_NONE, TIMEOUT}, UNDEPLOY);
149 this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
150 DEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, TIMEOUT}, UNDEPLOY);
152 this.graph.put(new String[] {DELETE, LOCK_NONE, SUB_NONE,
153 DELETING, LOCK_NONE, SUB_STATE_NONE, TIMEOUT}, DELETE);
155 this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, SUB_NONE,
156 DEPLOYED, LOCKING, SUB_STATE_NONE, TIMEOUT}, UNLOCK);
157 this.graph.put(new String[] {DEPLOY_NONE, LOCK, SUB_NONE,
158 DEPLOYED, LOCKING, SUB_STATE_NONE, TIMEOUT}, LOCK);
160 this.graph.put(new String[] {DEPLOY_NONE, LOCK, SUB_NONE,
161 DEPLOYED, UNLOCKING, SUB_STATE_NONE, TIMEOUT}, LOCK);
162 this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, SUB_NONE,
163 DEPLOYED, UNLOCKING, SUB_STATE_NONE, TIMEOUT}, UNLOCK);
165 this.graph.put(new String[] {UPDATE, LOCK_NONE, SUB_NONE, UPDATING, LOCKED, SUB_STATE_NONE, TIMEOUT}, UPDATE);
167 this.graph.put(new String[] {DEPLOY_NONE, LOCK_NONE, MIGRATE_PRECHECK,
168 DEPLOYED, LOCKED, MIGRATION_PRECHECKING, TIMEOUT}, MIGRATE_PRECHECK);
172 * Check if Deploy Order and Lock Order are consistent with current DeployState and LockState.
174 * @param acDeployOrder the Deploy Ordered
175 * @param acLockOrder the Lock Ordered
176 * @param acSubOrder the Sub Ordered
177 * @param acDeployState then current Deploy State
178 * @param acLockState the current Lock State
179 * @param acSubState the current Sub State
180 * @param acStateChangeResult the current Result of the State Change
181 * @return the order (DEPLOY/UNDEPLOY/LOCK/UNLOCK) to send to participant or NONE if order is not consistent
183 public String resolve(DeployOrder acDeployOrder, LockOrder acLockOrder, SubOrder acSubOrder,
184 DeployState acDeployState, LockState acLockState, SubState acSubState, StateChangeResult acStateChangeResult) {
185 var deployOrder = acDeployOrder != null ? acDeployOrder : DeployOrder.NONE;
186 var lockOrder = acLockOrder != null ? acLockOrder : LockOrder.NONE;
187 var subOrder = acSubOrder != null ? acSubOrder : SubOrder.NONE;
188 var stateChangeResult = acStateChangeResult != null ? acStateChangeResult : StateChangeResult.NO_ERROR;
190 var deployState = acDeployState != null ? acDeployState : DeployState.UNDEPLOYED;
191 var lockState = acLockState != null ? acLockState : LockState.NONE;
192 var subState = acSubState != null ? acSubState : SubState.NONE;
193 return this.graph.get(new String[] {deployOrder.name(), lockOrder.name(), subOrder.name(),
194 deployState.name(), lockState.name(), subState.name(), stateChangeResult.name()});