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