aboutsummaryrefslogtreecommitdiffstats
path: root/models/src/main/java/org/onap/policy/clamp/models/acm/persistence/provider/AcInstanceStateResolver.java
blob: 659169b9f2d2b398db64f20ab402ebd622c1ece0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.clamp.models.acm.persistence.provider;

import org.onap.policy.clamp.models.acm.concepts.DeployState;
import org.onap.policy.clamp.models.acm.concepts.LockState;
import org.onap.policy.clamp.models.acm.concepts.StateChangeResult;
import org.onap.policy.clamp.models.acm.messages.rest.instantiation.DeployOrder;
import org.onap.policy.clamp.models.acm.messages.rest.instantiation.LockOrder;
import org.onap.policy.clamp.models.acm.utils.StateDefinition;
import org.springframework.stereotype.Component;

@Component
public class AcInstanceStateResolver {
    private final StateDefinition<String> graph;

    private static final String DEPLOYED = DeployState.DEPLOYED.name();
    private static final String DEPLOYING = DeployState.DEPLOYING.name();
    private static final String UNDEPLOYED = DeployState.UNDEPLOYED.name();
    private static final String UNDEPLOYING = DeployState.UNDEPLOYING.name();
    private static final String UPDATING = DeployState.UPDATING.name();
    private static final String DELETING = DeployState.DELETING.name();

    private static final String LOCKED = LockState.LOCKED.name();
    private static final String LOCKING = LockState.LOCKING.name();
    private static final String UNLOCKED = LockState.UNLOCKED.name();
    private static final String UNLOCKING = LockState.UNLOCKING.name();
    private static final String STATE_LOCKED_NONE = LockState.NONE.name();

    private static final String DEPLOY_NONE = DeployOrder.NONE.name();
    private static final String LOCK_NONE = LockOrder.NONE.name();

    private static final String NO_ERROR = StateChangeResult.NO_ERROR.name();
    private static final String FAILED = StateChangeResult.FAILED.name();

    // list of results
    public static final String DEPLOY = DeployOrder.DEPLOY.name();
    public static final String UNDEPLOY = DeployOrder.UNDEPLOY.name();
    public static final String DELETE = DeployOrder.DELETE.name();
    public static final String LOCK = LockOrder.LOCK.name();
    public static final String UNLOCK = LockOrder.UNLOCK.name();
    public static final String NONE = "NONE";

    /**
     * Construct.
     */
    public AcInstanceStateResolver() {
        this.graph = new StateDefinition<>(5, NONE);

        // no error
        this.graph.put(new String[] {DEPLOY, LOCK_NONE, UNDEPLOYED, STATE_LOCKED_NONE, NO_ERROR}, DEPLOY);
        this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, DEPLOYED, LOCKED, NO_ERROR}, UNDEPLOY);
        this.graph.put(new String[] {DELETE, LOCK_NONE, UNDEPLOYED, LOCK_NONE, NO_ERROR}, DELETE);
        this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, DEPLOYED, LOCKED, NO_ERROR}, UNLOCK);
        this.graph.put(new String[] {DEPLOY_NONE, LOCK, DEPLOYED, UNLOCKED, NO_ERROR}, LOCK);

        // failed
        this.graph.put(new String[] {DEPLOY, LOCK_NONE, UNDEPLOYING, STATE_LOCKED_NONE, FAILED}, DEPLOY);
        this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, UNDEPLOYING, STATE_LOCKED_NONE, FAILED}, UNDEPLOY);
        this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, UPDATING, STATE_LOCKED_NONE, FAILED}, UNDEPLOY);

        this.graph.put(new String[] {DEPLOY, LOCK_NONE, DEPLOYING, STATE_LOCKED_NONE, FAILED}, DEPLOY);
        this.graph.put(new String[] {UNDEPLOY, LOCK_NONE, DEPLOYING, STATE_LOCKED_NONE, FAILED}, UNDEPLOY);

        this.graph.put(new String[] {DELETE, LOCK_NONE, DELETING, LOCK_NONE, FAILED}, DELETE);

        this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, DEPLOYED, LOCKING, FAILED}, UNLOCK);
        this.graph.put(new String[] {DEPLOY_NONE, LOCK, DEPLOYED, LOCKING, FAILED}, LOCK);

        this.graph.put(new String[] {DEPLOY_NONE, LOCK, DEPLOYED, UNLOCKING, FAILED}, LOCK);
        this.graph.put(new String[] {DEPLOY_NONE, UNLOCK, DEPLOYED, UNLOCKING, FAILED}, UNLOCK);
    }

    /**
     * Check if Deploy Order and Lock Order are consistent with current DeployState and LockState.
     *
     * @param acDeployOrder the Deploy Ordered
     * @param acLockOrder the Lock Ordered
     * @param acDeployState then current Deploy State
     * @param acLockState the current Lock State
     * @param acStateChangeResult the current Result of the State Change
     * @return the order (DEPLOY/UNDEPLOY/LOCK/UNLOCK) to send to participant or NONE if order is not consistent
     */
    public String resolve(DeployOrder acDeployOrder, LockOrder acLockOrder, DeployState acDeployState,
            LockState acLockState, StateChangeResult acStateChangeResult) {
        var deployOrder = acDeployOrder != null ? acDeployOrder : DeployOrder.NONE;
        var lockOrder = acLockOrder != null ? acLockOrder : LockOrder.NONE;
        var stateChangeResult = acStateChangeResult != null ? acStateChangeResult : StateChangeResult.NO_ERROR;

        var deployState = acDeployState != null ? acDeployState : DeployState.UNDEPLOYED;
        var lockState = acLockState != null ? acLockState : LockState.NONE;
        return this.graph.get(new String[] {deployOrder.name(), lockOrder.name(), deployState.name(), lockState.name(),
                stateChangeResult.name()});
    }
}