2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.openecomp.appc.statemachine.impl;
27 import java.util.HashSet;
30 import org.openecomp.appc.statemachine.StateMachine;
31 import org.openecomp.appc.statemachine.objects.*;
34 public class StateMachineImpl implements StateMachine {
36 private final Set<State> states;
38 private final Set<Event> events;
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());
47 public StateMachineResponse handleEvent(State inputState, Event event) throws InvalidInputException{
49 if(!validateInputs(inputState,event)){
50 throw new InvalidInputException("VNF State or incoming event is invalid. State = " +inputState + " event = " + event );
53 StateMachineResponse response = new StateMachineResponse();
54 State currentState = null,nextState = null;
55 for(State stateInSet:states){
56 if(stateInSet.equals(inputState)){
57 currentState = stateInSet;
61 for(Transition transition : currentState.getTransitions()){
62 if(event.equals(transition.getEvent())){
63 nextState = transition.getNextState();
66 if(nextState == null){
67 response.setResponse(Response.NO_TRANSITION_DEFINED);
69 else if(inputState.equals(nextState)){
70 response.setResponse(Response.NO_STATE_CHANGE);
73 response.setResponse(Response.VALID_TRANSITION);
75 response.setNextState(nextState);
79 private boolean validateInputs(State state,Event event){
80 if(state ==null || event == null){
83 if(!(this.states.contains(state) && this.events.contains(event))){
90 public String toString() {
91 return "StateMachineImpl{" +
93 ", events=" + events +