defa804ca6a2cb685401c00003ae625e9b62a7e5
[policy/clamp.git] /
1 /*-
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
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.policy.clamp.models.acm.persistence.provider;
22
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;
32
33 @Component
34 public class AcInstanceStateResolver {
35     private final StateDefinition<String> graph;
36
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 MIGRATION_REVERTING = DeployState.MIGRATION_REVERTING.name();
46     private static final String SUB_STATE_NONE = SubState.NONE.name();
47     private static final String PREPARING = SubState.PREPARING.name();
48     private static final String REVIEWING = SubState.REVIEWING.name();
49
50     private static final String LOCKED = LockState.LOCKED.name();
51     private static final String LOCKING = LockState.LOCKING.name();
52     private static final String UNLOCKED = LockState.UNLOCKED.name();
53     private static final String UNLOCKING = LockState.UNLOCKING.name();
54     private static final String STATE_LOCKED_NONE = LockState.NONE.name();
55
56     private static final String DEPLOY_NONE = DeployOrder.NONE.name();
57     private static final String LOCK_NONE = LockOrder.NONE.name();
58     private static final String SUB_NONE = SubOrder.NONE.name();
59
60     private static final String NO_ERROR = StateChangeResult.NO_ERROR.name();
61     private static final String FAILED = StateChangeResult.FAILED.name();
62     private static final String TIMEOUT = StateChangeResult.TIMEOUT.name();
63
64     // list of results
65     public static final String DEPLOY = DeployOrder.DEPLOY.name();
66     public static final String UNDEPLOY = DeployOrder.UNDEPLOY.name();
67     public static final String DELETE = DeployOrder.DELETE.name();
68     public static final String LOCK = LockOrder.LOCK.name();
69     public static final String UNLOCK = LockOrder.UNLOCK.name();
70     public static final String MIGRATE = DeployOrder.MIGRATE.name();
71     public static final String MIGRATE_PRECHECK = SubOrder.MIGRATE_PRECHECK.name();
72     public static final String PREPARE = SubOrder.PREPARE.name();
73     public static final String REVIEW = SubOrder.REVIEW.name();
74     public static final String UPDATE = DeployOrder.UPDATE.name();
75     public static final String NONE = "NONE";
76
77     /**
78      * Construct.
79      */
80     public AcInstanceStateResolver() {
81         this.graph = new StateDefinition<>(7, NONE);
82
83         // make an order when there are no fails
84         addDeployOrder(DEPLOY, UNDEPLOYED, STATE_LOCKED_NONE);
85         addDeployOrder(UNDEPLOY, DEPLOYED, LOCKED);
86         addDeployOrder(DELETE, UNDEPLOYED, STATE_LOCKED_NONE);
87         addDeployOrder(MIGRATE, DEPLOYED, LOCKED);
88         addDeployOrder(UPDATE, DEPLOYED, LOCKED);
89
90         addLockOrder(UNLOCK, LOCKED);
91         addLockOrder(LOCK, UNLOCKED);
92
93         addSubOrder(REVIEW, DEPLOYED, LOCKED);
94         addSubOrder(PREPARE, UNDEPLOYED, STATE_LOCKED_NONE);
95         addSubOrder(MIGRATE_PRECHECK, DEPLOYED, LOCKED);
96
97         // failed or timeout scenario
98         setAllowed(DEPLOY);
99         setAllowed(UNDEPLOY);
100
101         // undeploy order in a failed or timeout scenario
102         addDeployOrderWithFail(UNDEPLOY, UPDATING, LOCKED, SUB_STATE_NONE);
103         addDeployOrderWithFail(UNDEPLOY, MIGRATING, LOCKED, SUB_STATE_NONE);
104         addDeployOrderWithFail(UNDEPLOY, MIGRATION_PRECHECKING, LOCKED, SUB_STATE_NONE);
105         addDeployOrderWithFail(UNDEPLOY, REVIEWING, LOCKED, SUB_STATE_NONE);
106
107         // delete order in a failed or timeout scenario
108         addDeployOrderWithFail(DELETE, DELETING, LOCK_NONE, SUB_STATE_NONE);
109         addDeployOrderWithFail(DELETE, UNDEPLOYED, LOCK_NONE, PREPARING);
110
111         // update order in a failed or timeout scenario
112         addDeployOrderWithFail(UPDATE, UPDATING, LOCKED, SUB_STATE_NONE);
113
114         // unlock order in a failed or timeout scenario
115         addLockOrderWithFail(UNLOCK, LOCKING);
116         addLockOrderWithFail(UNLOCK, UNLOCKING);
117
118         // lock order in a failed or timeout scenario
119         addLockOrderWithFail(LOCK, LOCKING);
120         addLockOrderWithFail(LOCK, UNLOCKING);
121
122         // migrate-precheck order in a failed or timeout scenario
123         addSubOrderWithFail(MIGRATE_PRECHECK, DEPLOYED, LOCKED, MIGRATION_PRECHECKING);
124
125         // prepare order in a failed or timeout scenario
126         addSubOrderWithFail(PREPARE, UNDEPLOYED, LOCK_NONE, PREPARING);
127
128         // review order in a failed or timeout scenario
129         addSubOrderWithFail(REVIEW, DEPLOYED, LOCKED, REVIEWING);
130
131         // rollback
132         addDeployOrderWithFail(MIGRATION_REVERTING, MIGRATING, LOCKED, SUB_STATE_NONE);
133         addDeployOrderWithFail(UNDEPLOY, MIGRATION_REVERTING, LOCKED, SUB_STATE_NONE);
134     }
135
136     private void addDeployOrder(String deployOrder, String deployState, String lockState) {
137         this.graph.put(new String[] {
138             deployOrder, LOCK_NONE, SUB_NONE, deployState, lockState, STATE_LOCKED_NONE, NO_ERROR}, deployOrder);
139     }
140
141     private void addDeployOrderWithFail(String deployOrder, String deployState, String lockState, String subState) {
142         this.graph.put(new String[] {
143             deployOrder, LOCK_NONE, SUB_NONE, deployState, lockState, subState, FAILED}, deployOrder);
144         this.graph.put(new String[] {
145             deployOrder, LOCK_NONE, SUB_NONE, deployState, lockState, subState, TIMEOUT}, deployOrder);
146     }
147
148     private void addSubOrder(String subOrder, String deployState, String lockState) {
149         this.graph.put(new String[] {
150             DEPLOY_NONE, LOCK_NONE, subOrder, deployState, lockState, SUB_STATE_NONE, NO_ERROR}, subOrder);
151     }
152
153     private void addSubOrderWithFail(String subOrder, String deployState, String lockState, String subState) {
154         this.graph.put(new String[] {
155             DEPLOY_NONE, LOCK_NONE, subOrder, deployState, lockState, subState, FAILED}, subOrder);
156         this.graph.put(new String[] {
157             DEPLOY_NONE, LOCK_NONE, subOrder, deployState, lockState, subState, TIMEOUT}, subOrder);
158     }
159
160     private void addLockOrder(String lockOrder, String lockState) {
161         this.graph.put(new String[] {
162             DEPLOY_NONE, lockOrder, SUB_NONE, DEPLOYED, lockState, SUB_STATE_NONE, NO_ERROR}, lockOrder);
163     }
164
165     private void addLockOrderWithFail(String lockOrder, String lockState) {
166         this.graph.put(new String[] {
167             DEPLOY_NONE, lockOrder, SUB_NONE, DEPLOYED, lockState, SUB_STATE_NONE, FAILED}, lockOrder);
168         this.graph.put(new String[] {
169             DEPLOY_NONE, lockOrder, SUB_NONE, DEPLOYED, lockState, SUB_STATE_NONE, TIMEOUT}, lockOrder);
170     }
171
172     private void setAllowed(String deployOrder) {
173         addDeployOrderWithFail(deployOrder, UNDEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE);
174         addDeployOrderWithFail(deployOrder, UNDEPLOYING, LOCKED, SUB_STATE_NONE);
175         addDeployOrderWithFail(deployOrder, DEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE);
176         addDeployOrderWithFail(deployOrder, DEPLOYING, LOCKED, SUB_STATE_NONE);
177     }
178
179     /**
180      * Check if Deploy Order and Lock Order are consistent with current DeployState and LockState.
181      *
182      * @param acDeployOrder the Deploy Ordered
183      * @param acLockOrder the Lock Ordered
184      * @param acSubOrder the Sub Ordered
185      * @param acDeployState the current Deploy State
186      * @param acLockState the current Lock State
187      * @param acSubState the current Sub State
188      * @param acStateChangeResult the current Result of the State Change
189      * @return the order (DEPLOY/UNDEPLOY/LOCK/UNLOCK) to send to participant or NONE if order is not consistent
190      */
191     public String resolve(DeployOrder acDeployOrder, LockOrder acLockOrder, SubOrder acSubOrder,
192         DeployState acDeployState, LockState acLockState, SubState acSubState, StateChangeResult acStateChangeResult) {
193         var deployOrder = acDeployOrder != null ? acDeployOrder : DeployOrder.NONE;
194         var lockOrder = acLockOrder != null ? acLockOrder : LockOrder.NONE;
195         var subOrder = acSubOrder != null ? acSubOrder : SubOrder.NONE;
196         var stateChangeResult = acStateChangeResult != null ? acStateChangeResult : StateChangeResult.NO_ERROR;
197
198         var deployState = acDeployState != null ? acDeployState : DeployState.UNDEPLOYED;
199         var lockState = acLockState != null ? acLockState : LockState.NONE;
200         var subState = acSubState != null ? acSubState : SubState.NONE;
201         return this.graph.get(new String[] {deployOrder.name(), lockOrder.name(), subOrder.name(),
202                 deployState.name(), lockState.name(), subState.name(), stateChangeResult.name()});
203     }
204 }