e2f4fdf9be2ccc7a1b29c677b0c22545432d0c1e
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2023 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.messages.rest.instantiation.DeployOrder;
27 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
28 import org.onap.policy.clamp.models.acm.utils.StateDefinition;
29 import org.springframework.stereotype.Component;
30
31 @Component
32 public class AcInstanceStateResolver {
33     private final StateDefinition<String> graph;
34
35     private static final String DEPLOYED = DeployState.DEPLOYED.name();
36     private static final String DEPLOYING = DeployState.DEPLOYING.name();
37     private static final String UNDEPLOYED = DeployState.UNDEPLOYED.name();
38     private static final String UNDEPLOYING = DeployState.UNDEPLOYING.name();
39     private static final String UPDATING = DeployState.UPDATING.name();
40     private static final String DELETING = DeployState.DELETING.name();
41
42     private static final String LOCKED = LockState.LOCKED.name();
43     private static final String LOCKING = LockState.LOCKING.name();
44     private static final String UNLOCKED = LockState.UNLOCKED.name();
45     private static final String UNLOCKING = LockState.UNLOCKING.name();
46     private static final String STATE_LOCKED_NONE = LockState.NONE.name();
47
48     private static final String DEPLOY_NONE = DeployOrder.NONE.name();
49     private static final String LOCK_NONE = LockOrder.NONE.name();
50
51     private static final String NO_ERROR = StateChangeResult.NO_ERROR.name();
52     private static final String FAILED = StateChangeResult.FAILED.name();
53     private static final String TIMEOUT = StateChangeResult.TIMEOUT.name();
54
55     // list of results
56     public static final String DEPLOY = DeployOrder.DEPLOY.name();
57     public static final String UNDEPLOY = DeployOrder.UNDEPLOY.name();
58     public static final String DELETE = DeployOrder.DELETE.name();
59     public static final String LOCK = LockOrder.LOCK.name();
60     public static final String UNLOCK = LockOrder.UNLOCK.name();
61     public static final String NONE = "NONE";
62
63     /**
64      * Construct.
65      */
66     public AcInstanceStateResolver() {
67         this.graph = new StateDefinition<>(5, NONE);
68
69         // no error
70         this.graph.put(new String[] {DEPLOY, LOCK_NONE, UNDEPLOYED, STATE_LOCKED_NONE, NO_ERROR}, DEPLOY);
71         this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, DEPLOYED, LOCKED, NO_ERROR}, UNDEPLOY);
72         this.graph.put(new String[] {DELETE, LOCK_NONE, UNDEPLOYED, LOCK_NONE, NO_ERROR}, DELETE);
73         this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, DEPLOYED, LOCKED, NO_ERROR}, UNLOCK);
74         this.graph.put(new String[] {DEPLOY_NONE, LOCK, DEPLOYED, UNLOCKED, NO_ERROR}, LOCK);
75
76         // failed
77         this.graph.put(new String[] {DEPLOY, LOCK_NONE, UNDEPLOYING, STATE_LOCKED_NONE, FAILED}, DEPLOY);
78         this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, UNDEPLOYING, STATE_LOCKED_NONE, FAILED}, UNDEPLOY);
79         this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, UPDATING, STATE_LOCKED_NONE, FAILED}, UNDEPLOY);
80
81         this.graph.put(new String[] {DEPLOY, LOCK_NONE, DEPLOYING, STATE_LOCKED_NONE, FAILED}, DEPLOY);
82         this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, DEPLOYING, STATE_LOCKED_NONE, FAILED}, UNDEPLOY);
83
84         this.graph.put(new String[] {DELETE, LOCK_NONE, DELETING, LOCK_NONE, FAILED}, DELETE);
85
86         this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, DEPLOYED, LOCKING, FAILED}, UNLOCK);
87         this.graph.put(new String[] {DEPLOY_NONE, LOCK, DEPLOYED, LOCKING, FAILED}, LOCK);
88
89         this.graph.put(new String[] {DEPLOY_NONE, LOCK, DEPLOYED, UNLOCKING, FAILED}, LOCK);
90         this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, DEPLOYED, UNLOCKING, FAILED}, UNLOCK);
91
92         // timeout
93         this.graph.put(new String[] {DEPLOY, LOCK_NONE, UNDEPLOYING, STATE_LOCKED_NONE, TIMEOUT}, DEPLOY);
94         this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, UNDEPLOYING, STATE_LOCKED_NONE, TIMEOUT}, UNDEPLOY);
95         this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, UPDATING, STATE_LOCKED_NONE, TIMEOUT}, UNDEPLOY);
96
97         this.graph.put(new String[] {DEPLOY, LOCK_NONE, DEPLOYING, STATE_LOCKED_NONE, TIMEOUT}, DEPLOY);
98         this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, DEPLOYING, STATE_LOCKED_NONE, TIMEOUT}, UNDEPLOY);
99
100         this.graph.put(new String[] {DELETE, LOCK_NONE, DELETING, LOCK_NONE, TIMEOUT}, DELETE);
101
102         this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, DEPLOYED, LOCKING, TIMEOUT}, UNLOCK);
103         this.graph.put(new String[] {DEPLOY_NONE, LOCK, DEPLOYED, LOCKING, TIMEOUT}, LOCK);
104
105         this.graph.put(new String[] {DEPLOY_NONE, LOCK, DEPLOYED, UNLOCKING, TIMEOUT}, LOCK);
106         this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, DEPLOYED, UNLOCKING, TIMEOUT}, UNLOCK);
107     }
108
109     /**
110      * Check if Deploy Order and Lock Order are consistent with current DeployState and LockState.
111      *
112      * @param acDeployOrder the Deploy Ordered
113      * @param acLockOrder the Lock Ordered
114      * @param acDeployState then current Deploy State
115      * @param acLockState the current Lock State
116      * @param acStateChangeResult the current Result of the State Change
117      * @return the order (DEPLOY/UNDEPLOY/LOCK/UNLOCK) to send to participant or NONE if order is not consistent
118      */
119     public String resolve(DeployOrder acDeployOrder, LockOrder acLockOrder, DeployState acDeployState,
120             LockState acLockState, StateChangeResult acStateChangeResult) {
121         var deployOrder = acDeployOrder != null ? acDeployOrder : DeployOrder.NONE;
122         var lockOrder = acLockOrder != null ? acLockOrder : LockOrder.NONE;
123         var stateChangeResult = acStateChangeResult != null ? acStateChangeResult : StateChangeResult.NO_ERROR;
124
125         var deployState = acDeployState != null ? acDeployState : DeployState.UNDEPLOYED;
126         var lockState = acLockState != null ? acLockState : LockState.NONE;
127         return this.graph.get(new String[] {deployOrder.name(), lockOrder.name(), deployState.name(), lockState.name(),
128                 stateChangeResult.name()});
129     }
130 }