9239b3ae90c5e8be7b7828921d69c05818757e6f
[policy/clamp.git] /
1 /*-
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
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 SUB_STATE_NONE = SubState.NONE.name();
46
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();
52
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();
56
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();
60
61     // list of results
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";
73
74     /**
75      * Construct.
76      */
77     public AcInstanceStateResolver() {
78         this.graph = new StateDefinition<>(7, NONE);
79
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);
101
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);
107
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);
116
117         this.graph.put(new String[] {DELETE, LOCK_NONE, SUB_NONE,
118             DELETING, LOCK_NONE, SUB_STATE_NONE, FAILED}, DELETE);
119
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);
124
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);
127
128         this.graph.put(new String[] {UPDATE, LOCK_NONE, SUB_NONE, UPDATING, LOCKED, SUB_STATE_NONE, FAILED}, UPDATE);
129
130         this.graph.put(new String[] {DEPLOY_NONE, LOCK_NONE, MIGRATE_PRECHECK,
131             DEPLOYED, LOCKED, MIGRATION_PRECHECKING, FAILED}, MIGRATE_PRECHECK);
132
133         // timeout
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);
138
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             UNDEPLOYING, LOCKED, 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);
151
152         this.graph.put(new String[] {DELETE, LOCK_NONE, SUB_NONE,
153             DELETING, LOCK_NONE, SUB_STATE_NONE, TIMEOUT}, DELETE);
154
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);
159
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);
164
165         this.graph.put(new String[] {UPDATE, LOCK_NONE, SUB_NONE, UPDATING, LOCKED, SUB_STATE_NONE, TIMEOUT}, UPDATE);
166
167         this.graph.put(new String[] {DEPLOY_NONE, LOCK_NONE, MIGRATE_PRECHECK,
168             DEPLOYED, LOCKED, MIGRATION_PRECHECKING, TIMEOUT}, MIGRATE_PRECHECK);
169     }
170
171     /**
172      * Check if Deploy Order and Lock Order are consistent with current DeployState and LockState.
173      *
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
182      */
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;
189
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()});
195     }
196 }