Add Rapp Instances
[nonrtric/plt/rappmanager.git] / rapp-manager-models / src / main / java / com / oransc / rappmanager / models / statemachine / RappInstanceStateMachine.java
1 /*-
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
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  * ============LICENSE_END========================================================================
17  */
18
19 package com.oransc.rappmanager.models.statemachine;
20
21 import com.oransc.rappmanager.models.rapp.RappEvent;
22 import com.oransc.rappmanager.models.rappinstance.RappInstance;
23 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.UUID;
27 import lombok.RequiredArgsConstructor;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.statemachine.StateMachine;
31 import org.springframework.statemachine.config.StateMachineFactory;
32 import org.springframework.stereotype.Service;
33
34 @Service
35 @RequiredArgsConstructor
36 public class RappInstanceStateMachine {
37
38     Logger logger = LoggerFactory.getLogger(RappInstanceStateMachine.class);
39
40     private final StateMachineFactory<RappInstanceState, RappEvent> stateMachineFactory;
41     Map<UUID, StateMachine<RappInstanceState, RappEvent>> stateMachineMap = new HashMap<>();
42
43     public void onboardRappInstance(UUID rappInstanceId) {
44         StateMachine<RappInstanceState, RappEvent> stateMachine = stateMachineFactory.getStateMachine(rappInstanceId);
45         stateMachineMap.put(rappInstanceId, stateMachine);
46         stateMachine.start();
47     }
48
49     public void sendRappInstanceEvent(RappInstance rappInstance, RappEvent rappEvent) {
50         logger.info("Sending rapp instance event {} for {}", rappEvent.name(), rappInstance.getRappInstanceId());
51         logger.debug("State machine map is {}", stateMachineMap);
52         stateMachineMap.get(rappInstance.getRappInstanceId()).sendEvent(rappEvent);
53     }
54
55     public RappInstanceState getRappInstanceState(UUID rappInstanceId) {
56         return stateMachineMap.get(rappInstanceId).getState().getId();
57     }
58
59     public void deleteRappInstance(RappInstance rappInstance) {
60         stateMachineMap.get(rappInstance.getRappInstanceId()).stop();
61         stateMachineMap.remove(rappInstance.getRappInstanceId());
62     }
63 }