2 * ============LICENSE_START======================================================================
3 * Copyright (C) 2023 Nordix Foundation. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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 * ============LICENSE_END========================================================================
19 package com.oransc.rappmanager.models.statemachine;
21 import com.oransc.rappmanager.models.rapp.RappEvent;
22 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
23 import java.util.EnumSet;
24 import org.springframework.context.annotation.Bean;
25 import org.springframework.context.annotation.Configuration;
26 import org.springframework.statemachine.config.EnableStateMachineFactory;
27 import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
28 import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
29 import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
30 import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
31 import org.springframework.statemachine.guard.Guard;
34 @EnableStateMachineFactory
35 public class RappInstanceStateMachineConfig extends EnumStateMachineConfigurerAdapter<RappInstanceState, RappEvent> {
38 public void configure(StateMachineStateConfigurer<RappInstanceState, RappEvent> states) throws Exception {
39 states.withStates().initial(RappInstanceState.UNDEPLOYED).states(EnumSet.allOf(RappInstanceState.class));
43 public void configure(StateMachineConfigurationConfigurer<RappInstanceState, RappEvent> config) throws Exception {
44 config.withConfiguration();
49 public void configure(StateMachineTransitionConfigurer<RappInstanceState, RappEvent> transitions) throws Exception {
52 .source(RappInstanceState.UNDEPLOYED).target(RappInstanceState.DEPLOYING).event(RappEvent.DEPLOYING)
55 .source(RappInstanceState.DEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.ACMDEPLOYFAILED)
58 .source(RappInstanceState.DEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.SMEDEPLOYFAILED)
61 .source(RappInstanceState.DEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.DMEDEPLOYFAILED)
64 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.ACMUNDEPLOYFAILED)
67 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.SMEUNDEPLOYFAILED)
70 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.DMEUNDEPLOYFAILED)
73 .source(RappInstanceState.DEPLOYED).target(RappInstanceState.UNDEPLOYING).event(RappEvent.UNDEPLOYING)
76 .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.ACMDEPLOYED)
77 .guard(deployedGuard())
80 .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.SMEDEPLOYED)
81 .guard(deployedGuard())
84 .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.DMEDEPLOYED)
85 .guard(deployedGuard())
88 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.ACMUNDEPLOYED)
89 .guard(undeployedGuard())
92 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.SMEUNDEPLOYED)
93 .guard(undeployedGuard())
96 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.DMEUNDEPLOYED)
97 .guard(undeployedGuard());
103 public Guard<RappInstanceState, RappEvent> deployedGuard() {
104 return stateContext -> {
105 stateContext.getExtendedState().getVariables().put(stateContext.getEvent(), true);
106 return stateContext.getExtendedState().getVariables().get(RappEvent.ACMDEPLOYED) != null
107 && stateContext.getExtendedState().getVariables().get(RappEvent.SMEDEPLOYED) != null
108 && stateContext.getExtendedState().getVariables().get(RappEvent.DMEDEPLOYED) != null;
113 public Guard<RappInstanceState, RappEvent> undeployedGuard() {
114 return stateContext -> {
115 stateContext.getExtendedState().getVariables().put(stateContext.getEvent(), true);
116 return stateContext.getExtendedState().getVariables().get(RappEvent.ACMUNDEPLOYED) != null
117 && stateContext.getExtendedState().getVariables().get(RappEvent.SMEUNDEPLOYED) != null
118 && stateContext.getExtendedState().getVariables().get(RappEvent.DMEUNDEPLOYED) != null;