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.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;
40 public class StateMachineImpl implements StateMachine {
42 private final Set<State> states;
44 private final Set<Event> events;
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());
53 public StateMachineResponse handleEvent(State inputState, Event event) throws InvalidInputException{
55 if(!validateInputs(inputState,event)){
56 throw new InvalidInputException("VNF State or incoming event is invalid. State = " +inputState + " event = " + event );
59 StateMachineResponse response = new StateMachineResponse();
60 State currentState = null,nextState = null;
61 for(State stateInSet:states){
62 if(stateInSet.equals(inputState)){
63 currentState = stateInSet;
67 if (currentState != null) {
68 for (Transition transition : currentState.getTransitions()) {
69 if (event.equals(transition.getEvent())) {
70 nextState = transition.getNextState();
74 if(nextState == null){
75 response.setResponse(Response.NO_TRANSITION_DEFINED);
77 else if(inputState.equals(nextState)){
78 response.setResponse(Response.NO_STATE_CHANGE);
81 response.setResponse(Response.VALID_TRANSITION);
83 response.setNextState(nextState);
87 private boolean validateInputs(State state,Event event) {
88 return state != null && event != null && this.states.contains(state) && this.events.contains(event);
92 public String toString() {
93 return "StateMachineImpl{" +
95 ", events=" + events +