2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2023 Nordix Foundation. All rights reserved.
4 * Copyright (C) 2024 OpenInfra Foundation Europe. All rights reserved.
5 * ===============================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END========================================================================
20 package com.oransc.rappmanager.models.statemachine;
22 import com.oransc.rappmanager.models.rapp.RappEvent;
23 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
24 import java.util.EnumSet;
25 import org.springframework.context.annotation.Bean;
26 import org.springframework.context.annotation.Configuration;
27 import org.springframework.statemachine.StateContext;
28 import org.springframework.statemachine.config.EnableStateMachineFactory;
29 import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
30 import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
31 import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
32 import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
33 import org.springframework.statemachine.guard.Guard;
36 @EnableStateMachineFactory
37 public class RappInstanceStateMachineConfig extends EnumStateMachineConfigurerAdapter<RappInstanceState, RappEvent> {
40 public void configure(StateMachineStateConfigurer<RappInstanceState, RappEvent> states) throws Exception {
41 states.withStates().initial(RappInstanceState.UNDEPLOYED).states(EnumSet.allOf(RappInstanceState.class));
45 public void configure(StateMachineConfigurationConfigurer<RappInstanceState, RappEvent> config) throws Exception {
46 config.withConfiguration();
51 public void configure(StateMachineTransitionConfigurer<RappInstanceState, RappEvent> transitions) throws Exception {
54 .source(RappInstanceState.UNDEPLOYED).target(RappInstanceState.DEPLOYING).event(RappEvent.DEPLOYING)
57 .source(RappInstanceState.DEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.ACMDEPLOYFAILED)
60 .source(RappInstanceState.DEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.SMEDEPLOYFAILED)
63 .source(RappInstanceState.DEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.DMEDEPLOYFAILED)
66 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.ACMUNDEPLOYFAILED)
69 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.SMEUNDEPLOYFAILED)
72 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.DMEUNDEPLOYFAILED)
75 .source(RappInstanceState.DEPLOYED).target(RappInstanceState.UNDEPLOYING).event(RappEvent.UNDEPLOYING)
78 .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.ACMDEPLOYED)
79 .guard(deployedGuard())
82 .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.SMEDEPLOYED)
83 .guard(deployedGuard())
86 .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.DMEDEPLOYED)
87 .guard(deployedGuard())
90 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.ACMUNDEPLOYED)
91 .guard(undeployedGuard())
94 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.SMEUNDEPLOYED)
95 .guard(undeployedGuard())
98 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.DMEUNDEPLOYED)
99 .guard(undeployedGuard());
105 public Guard<RappInstanceState, RappEvent> deployedGuard() {
106 return stateContext -> {
107 stateContext.getExtendedState().getVariables().put(stateContext.getEvent(), true);
108 return stateContext.getExtendedState().getVariables().get(RappEvent.ACMDEPLOYED) != null && smeGuard(
109 stateContext, RappEvent.SMEDEPLOYED) && dmeGuard(stateContext, RappEvent.DMEDEPLOYED);
113 boolean smeGuard(StateContext<RappInstanceState, RappEvent> stateContext, RappEvent rappEvent) {
114 Boolean smeEnabled = (Boolean) stateContext.getExtendedState().getVariables().get("sme");
115 if (smeEnabled != null && smeEnabled.equals(Boolean.TRUE)) {
116 return stateContext.getExtendedState().getVariables().get(rappEvent) != null;
121 boolean dmeGuard(StateContext<RappInstanceState, RappEvent> stateContext, RappEvent rappEvent) {
122 Boolean dmeEnabled = (Boolean) stateContext.getExtendedState().getVariables().get("dme");
123 if (dmeEnabled != null && dmeEnabled.equals(Boolean.TRUE)) {
124 return stateContext.getExtendedState().getVariables().get(rappEvent) != null;
130 public Guard<RappInstanceState, RappEvent> undeployedGuard() {
131 return stateContext -> {
132 stateContext.getExtendedState().getVariables().put(stateContext.getEvent(), true);
133 return stateContext.getExtendedState().getVariables().get(RappEvent.ACMUNDEPLOYED) != null && smeGuard(
134 stateContext, RappEvent.SMEUNDEPLOYED) && dmeGuard(stateContext, RappEvent.DMEUNDEPLOYED);