Applying license changes to all files
[appc.git] / appc-dispatcher / appc-dispatcher-common / 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.statemachine.StateMachine;
31 import org.openecomp.appc.statemachine.objects.*;
32
33
34 public class StateMachineImpl implements StateMachine {
35
36         private final Set<State> states;
37
38         private final Set<Event> events;
39
40         StateMachineImpl(StateMachineMetadata metadata){
41                 this.states = new HashSet<State>();
42                 this.states.addAll(metadata.getStates());
43                 this.events = new HashSet<Event>();
44                 this.events.addAll(metadata.getEvents());
45         }
46
47         public StateMachineResponse handleEvent(State inputState, Event event) throws InvalidInputException{
48
49                 if(!validateInputs(inputState,event)){
50                         throw new InvalidInputException("VNF State or incoming event is invalid. State = " +inputState + " event = " + event );
51                 }
52
53                 StateMachineResponse response = new StateMachineResponse();
54                 State currentState = null,nextState = null;
55                 for(State stateInSet:states){
56                         if(stateInSet.equals(inputState)){
57                                 currentState = stateInSet;
58                                 break;
59                         }
60                 }
61                 for(Transition transition : currentState.getTransitions()){
62                         if(event.equals(transition.getEvent())){
63                                 nextState = transition.getNextState();
64                         }
65                 }
66                 if(nextState == null){
67                         response.setResponse(Response.NO_TRANSITION_DEFINED);
68                 }
69                 else if(inputState.equals(nextState)){
70                         response.setResponse(Response.NO_STATE_CHANGE);
71                 }
72                 else{
73                         response.setResponse(Response.VALID_TRANSITION);
74                 }
75                 response.setNextState(nextState);
76                 return response;
77         }
78
79         private boolean validateInputs(State state,Event event){
80                 if(state ==null || event == null){
81                         return false;
82                 }
83                 if(!(this.states.contains(state) && this.events.contains(event))){
84                         return false;
85                 }
86                 return true;
87         }
88
89         @Override
90         public String toString() {
91                 return "StateMachineImpl{" +
92                                 "states=" + states +
93                                 ", events=" + events +
94                                 '}';
95         }
96 }