8676b3397dbf3e9bdfdef961698739510b370f79
[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.messages.rest.instantiation.DeployOrder;
26 import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
27 import org.onap.policy.clamp.models.acm.utils.StateDefinition;
28 import org.springframework.stereotype.Component;
29
30 @Component
31 public class AcInstanceStateResolver {
32     private final StateDefinition<String> graph;
33
34     private static final String DEPLOYED = DeployState.DEPLOYED.name();
35     private static final String UNDEPLOYED = DeployState.UNDEPLOYED.name();
36
37     private static final String LOCKED = LockState.LOCKED.name();
38     private static final String UNLOCKED = LockState.UNLOCKED.name();
39     private static final String STATE_LOCKED_NONE = LockState.NONE.name();
40
41     private static final String DEPLOY_NONE = DeployOrder.NONE.name();
42     private static final String LOCK_NONE = LockOrder.NONE.name();
43
44     // list of results
45     public static final String DEPLOY = DeployOrder.DEPLOY.name();
46     public static final String UNDEPLOY = DeployOrder.UNDEPLOY.name();
47     public static final String DELETE = DeployOrder.DELETE.name();
48     public static final String LOCK = LockOrder.LOCK.name();
49     public static final String UNLOCK = LockOrder.UNLOCK.name();
50     public static final String NONE = "NONE";
51
52     /**
53      * Construct.
54      */
55     public AcInstanceStateResolver() {
56         this.graph = new StateDefinition<>(4, NONE);
57
58         this.graph.put(new String[] {DEPLOY, LOCK_NONE, UNDEPLOYED, STATE_LOCKED_NONE}, DEPLOY);
59         this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, DEPLOYED, LOCKED}, UNDEPLOY);
60         this.graph.put(new String[] {DELETE, LOCK_NONE, UNDEPLOYED, LOCK_NONE}, DELETE);
61         this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, DEPLOYED, LOCKED}, UNLOCK);
62         this.graph.put(new String[] {DEPLOY_NONE, LOCK, DEPLOYED, UNLOCKED}, LOCK);
63     }
64
65     /**
66      * Check if Deploy Order and Lock Order are consistent with current DeployState and LockState.
67      *
68      * @param acDeployOrder the Deploy Ordered
69      * @param acLockOrder the Lock Ordered
70      * @param acDeployState then current Deploy State
71      * @param acLockState the  current Lock State
72      * @return the order (DEPLOY/UNDEPLOY/LOCK/UNLOCK) to send to participant or NONE if order is not consistent
73      */
74     public String resolve(DeployOrder acDeployOrder, LockOrder acLockOrder, DeployState acDeployState,
75             LockState acLockState) {
76         var deployOrder = acDeployOrder != null ? acDeployOrder : DeployOrder.NONE;
77         var lockOrder = acLockOrder != null ? acLockOrder : LockOrder.NONE;
78
79         var deployState = acDeployState != null ? acDeployState : DeployState.UNDEPLOYED;
80         var lockState = acLockState != null ? acLockState : LockState.NONE;
81         return this.graph
82                 .get(new String[] {deployOrder.name(), lockOrder.name(), deployState.name(), lockState.name()});
83     }
84 }