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