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.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.ACMUNDEPLOYFAILED)
64 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.SMEUNDEPLOYFAILED)
67 .source(RappInstanceState.DEPLOYED).target(RappInstanceState.UNDEPLOYING).event(RappEvent.UNDEPLOYING)
70 .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.ACMDEPLOYED)
71 .guard(deployedGuard())
74 .source(RappInstanceState.DEPLOYING).target(RappInstanceState.DEPLOYED).event(RappEvent.SMEDEPLOYED)
75 .guard(deployedGuard())
78 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.ACMUNDEPLOYED)
79 .guard(undeployedGuard())
82 .source(RappInstanceState.UNDEPLOYING).target(RappInstanceState.UNDEPLOYED).event(RappEvent.SMEUNDEPLOYED)
83 .guard(undeployedGuard());
89 public Guard<RappInstanceState, RappEvent> deployedGuard() {
90 return stateContext -> {
91 stateContext.getExtendedState().getVariables().put(stateContext.getEvent(), true);
92 return stateContext.getExtendedState().getVariables().get(RappEvent.ACMDEPLOYED) != null
93 && stateContext.getExtendedState().getVariables().get(RappEvent.SMEDEPLOYED) != null;
98 public Guard<RappInstanceState, RappEvent> undeployedGuard() {
99 return stateContext -> {
100 stateContext.getExtendedState().getVariables().put(stateContext.getEvent(), true);
101 return stateContext.getExtendedState().getVariables().get(RappEvent.ACMUNDEPLOYED) != null
102 && stateContext.getExtendedState().getVariables().get(RappEvent.SMEUNDEPLOYED) != null;