9fc1233cd36688cbeaecbda76c350c6d8d23fb8c
[appc.git] / appc-lifecycle-management / state-machine-lib / src / main / java / org / openecomp / appc / statemachine / impl / StateMachineImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * =============================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.statemachine.impl;
26
27 import java.util.HashSet;
28 import java.util.Set;
29
30 import org.openecomp.appc.exceptions.InvalidInputException;
31 import org.openecomp.appc.statemachine.StateMachine;
32 import org.openecomp.appc.statemachine.objects.Event;
33 import org.openecomp.appc.statemachine.objects.Response;
34 import org.openecomp.appc.statemachine.objects.State;
35 import org.openecomp.appc.statemachine.objects.StateMachineMetadata;
36 import org.openecomp.appc.statemachine.objects.StateMachineResponse;
37 import org.openecomp.appc.statemachine.objects.Transition;
38
39
40 public class StateMachineImpl implements StateMachine {
41
42     private final Set<State> states;
43
44     private final Set<Event> events;
45
46     StateMachineImpl(StateMachineMetadata metadata){
47         this.states = new HashSet<>();
48         this.states.addAll(metadata.getStates());
49         this.events = new HashSet<>();
50         this.events.addAll(metadata.getEvents());
51     }
52
53     public StateMachineResponse handleEvent(State inputState, Event event) throws InvalidInputException{
54
55         if(!validateInputs(inputState,event)){
56             throw new InvalidInputException("VNF State or incoming event is invalid. State = " +inputState + " event = " + event );
57         }
58
59         StateMachineResponse response = new StateMachineResponse();
60         State currentState = null,nextState = null;
61         for(State stateInSet:states){
62             if(stateInSet.equals(inputState)){
63                 currentState = stateInSet;
64                 break;
65             }
66         }
67         if (currentState != null) {
68             for (Transition transition : currentState.getTransitions()) {
69                 if (event.equals(transition.getEvent())) {
70                     nextState = transition.getNextState();
71                 }
72             }
73         }
74         if(nextState == null){
75             response.setResponse(Response.NO_TRANSITION_DEFINED);
76         }
77         else if(inputState.equals(nextState)){
78             response.setResponse(Response.NO_STATE_CHANGE);
79         }
80         else{
81             response.setResponse(Response.VALID_TRANSITION);
82         }
83         response.setNextState(nextState);
84         return response;
85     }
86
87     private boolean validateInputs(State state,Event event) {
88         return state != null && event != null && this.states.contains(state) && this.events.contains(event);
89     }
90
91     @Override
92     public String toString() {
93         return "StateMachineImpl{" +
94                 "states=" + states +
95                 ", events=" + events +
96                 '}';
97     }
98 }