Support for optional rApp and rApp instance parameters
[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  * 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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========================================================================
18  */
19
20 package com.oransc.rappmanager.models.statemachine;
21
22 import com.oransc.rappmanager.models.rapp.RappEvent;
23 import com.oransc.rappmanager.models.rappinstance.RappInstance;
24 import com.oransc.rappmanager.models.rappinstance.RappInstanceState;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.UUID;
28 import lombok.RequiredArgsConstructor;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.springframework.messaging.support.MessageBuilder;
32 import org.springframework.statemachine.StateMachine;
33 import org.springframework.statemachine.config.StateMachineFactory;
34 import org.springframework.stereotype.Service;
35 import reactor.core.publisher.Mono;
36
37 @Service
38 @RequiredArgsConstructor
39 public class RappInstanceStateMachine {
40
41     Logger logger = LoggerFactory.getLogger(RappInstanceStateMachine.class);
42
43     private final StateMachineFactory<RappInstanceState, RappEvent> stateMachineFactory;
44     Map<UUID, StateMachine<RappInstanceState, RappEvent>> stateMachineMap = new HashMap<>();
45
46     public void onboardRappInstance(UUID rappInstanceId) {
47         StateMachine<RappInstanceState, RappEvent> stateMachine = stateMachineFactory.getStateMachine(rappInstanceId);
48         stateMachineMap.put(rappInstanceId, stateMachine);
49         stateMachine.startReactively().subscribe();
50     }
51
52     public void sendRappInstanceEvent(RappInstance rappInstance, RappEvent rappEvent) {
53         logger.info("Sending rapp instance event {} for {}", rappEvent.name(), rappInstance.getRappInstanceId());
54         logger.debug("State machine map is {}", stateMachineMap);
55         stateMachineMap.get(rappInstance.getRappInstanceId()).getExtendedState().getVariables()
56                 .put("sme", rappInstance.isSMEEnabled());
57         stateMachineMap.get(rappInstance.getRappInstanceId()).getExtendedState().getVariables()
58                 .put("dme", rappInstance.isDMEEnabled());
59         stateMachineMap.get(rappInstance.getRappInstanceId())
60                 .sendEvent(Mono.just(MessageBuilder.withPayload(rappEvent).build())).subscribe();
61     }
62
63     public RappInstanceState getRappInstanceState(UUID rappInstanceId) {
64         return stateMachineMap.get(rappInstanceId).getState().getId();
65     }
66
67     public void deleteRappInstance(RappInstance rappInstance) {
68         stateMachineMap.get(rappInstance.getRappInstanceId()).stopReactively().subscribe();
69         stateMachineMap.remove(rappInstance.getRappInstanceId());
70     }
71 }