09f1b09cdd5c9660b930f504d47e6ba32b40dd15
[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         setAllowed(DEPLOY);
104         setAllowed(UNDEPLOY);
105         this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
106             UPDATING, LOCKED, SUB_STATE_NONE, FAILED}, UNDEPLOY);
107         this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
108             MIGRATING, LOCKED, SUB_STATE_NONE, FAILED}, UNDEPLOY);
109
110         this.graph.put(new String[] {DELETE, LOCK_NONE, SUB_NONE,
111             DELETING, LOCK_NONE, SUB_STATE_NONE, FAILED}, DELETE);
112
113         this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, SUB_NONE,
114             DEPLOYED, LOCKING, SUB_STATE_NONE, FAILED}, UNLOCK);
115         this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, SUB_NONE,
116             DEPLOYED, UNLOCKING, SUB_STATE_NONE, FAILED}, UNLOCK);
117
118         this.graph.put(new String[] {DEPLOY_NONE, LOCK, SUB_NONE, DEPLOYED, LOCKING, SUB_STATE_NONE, FAILED}, LOCK);
119         this.graph.put(new String[] {DEPLOY_NONE, LOCK, SUB_NONE, DEPLOYED, UNLOCKING, SUB_STATE_NONE, FAILED}, LOCK);
120
121         this.graph.put(new String[] {UPDATE, LOCK_NONE, SUB_NONE, UPDATING, LOCKED, SUB_STATE_NONE, FAILED}, UPDATE);
122
123         this.graph.put(new String[] {DEPLOY_NONE, LOCK_NONE, MIGRATE_PRECHECK,
124             DEPLOYED, LOCKED, MIGRATION_PRECHECKING, FAILED}, MIGRATE_PRECHECK);
125
126         // timeout
127         this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
128             UPDATING, LOCKED, SUB_STATE_NONE, TIMEOUT}, UNDEPLOY);
129         this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
130             MIGRATING, LOCKED, SUB_STATE_NONE, TIMEOUT}, UNDEPLOY);
131         this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, SUB_NONE,
132             MIGRATION_PRECHECKING, LOCKED, SUB_STATE_NONE, TIMEOUT}, UNDEPLOY);
133
134         this.graph.put(new String[] {DELETE, LOCK_NONE, SUB_NONE,
135             DELETING, LOCK_NONE, SUB_STATE_NONE, TIMEOUT}, DELETE);
136
137         this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, SUB_NONE,
138             DEPLOYED, LOCKING, SUB_STATE_NONE, TIMEOUT}, UNLOCK);
139         this.graph.put(new String[] {DEPLOY_NONE, LOCK, SUB_NONE,
140             DEPLOYED, LOCKING, SUB_STATE_NONE, TIMEOUT}, LOCK);
141
142         this.graph.put(new String[] {DEPLOY_NONE, LOCK, SUB_NONE,
143             DEPLOYED, UNLOCKING, SUB_STATE_NONE, TIMEOUT}, LOCK);
144         this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, SUB_NONE,
145             DEPLOYED, UNLOCKING, SUB_STATE_NONE, TIMEOUT}, UNLOCK);
146
147         this.graph.put(new String[] {UPDATE, LOCK_NONE, SUB_NONE, UPDATING, LOCKED, SUB_STATE_NONE, TIMEOUT}, UPDATE);
148
149         this.graph.put(new String[] {DEPLOY_NONE, LOCK_NONE, MIGRATE_PRECHECK,
150             DEPLOYED, LOCKED, MIGRATION_PRECHECKING, TIMEOUT}, MIGRATE_PRECHECK);
151     }
152
153     private void setAllowed(String deployOrder) {
154         this.graph.put(new String[] {deployOrder, LOCK_NONE, SUB_NONE,
155             UNDEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, FAILED}, deployOrder);
156         this.graph.put(new String[] {deployOrder, LOCK_NONE, SUB_NONE,
157             UNDEPLOYING, LOCKED, SUB_STATE_NONE, FAILED}, deployOrder);
158
159         this.graph.put(new String[] {deployOrder, LOCK_NONE, SUB_NONE,
160             UNDEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, TIMEOUT}, deployOrder);
161         this.graph.put(new String[] {deployOrder, LOCK_NONE, SUB_NONE,
162             UNDEPLOYING, LOCKED, SUB_STATE_NONE, TIMEOUT}, deployOrder);
163
164         this.graph.put(new String[] {deployOrder, LOCK_NONE, SUB_NONE,
165             DEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, FAILED}, deployOrder);
166         this.graph.put(new String[] {deployOrder, LOCK_NONE, SUB_NONE,
167             DEPLOYING, LOCKED, SUB_STATE_NONE, FAILED}, deployOrder);
168
169         this.graph.put(new String[] {deployOrder, LOCK_NONE, SUB_NONE,
170             DEPLOYING, STATE_LOCKED_NONE, SUB_STATE_NONE, TIMEOUT}, deployOrder);
171         this.graph.put(new String[] {deployOrder, LOCK_NONE, SUB_NONE,
172             DEPLOYING, LOCKED, SUB_STATE_NONE, TIMEOUT}, deployOrder);
173     }
174
175     /**
176      * Check if Deploy Order and Lock Order are consistent with current DeployState and LockState.
177      *
178      * @param acDeployOrder the Deploy Ordered
179      * @param acLockOrder the Lock Ordered
180      * @param acSubOrder the Sub Ordered
181      * @param acDeployState then current Deploy State
182      * @param acLockState the current Lock State
183      * @param acSubState the current Sub State
184      * @param acStateChangeResult the current Result of the State Change
185      * @return the order (DEPLOY/UNDEPLOY/LOCK/UNLOCK) to send to participant or NONE if order is not consistent
186      */
187     public String resolve(DeployOrder acDeployOrder, LockOrder acLockOrder, SubOrder acSubOrder,
188         DeployState acDeployState, LockState acLockState, SubState acSubState, StateChangeResult acStateChangeResult) {
189         var deployOrder = acDeployOrder != null ? acDeployOrder : DeployOrder.NONE;
190         var lockOrder = acLockOrder != null ? acLockOrder : LockOrder.NONE;
191         var subOrder = acSubOrder != null ? acSubOrder : SubOrder.NONE;
192         var stateChangeResult = acStateChangeResult != null ? acStateChangeResult : StateChangeResult.NO_ERROR;
193
194         var deployState = acDeployState != null ? acDeployState : DeployState.UNDEPLOYED;
195         var lockState = acLockState != null ? acLockState : LockState.NONE;
196         var subState = acSubState != null ? acSubState : SubState.NONE;
197         return this.graph.get(new String[] {deployOrder.name(), lockOrder.name(), subOrder.name(),
198                 deployState.name(), lockState.name(), subState.name(), stateChangeResult.name()});
199     }
200 }