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 * Implementation of StateMachine
42 public class StateMachineImpl implements StateMachine {
43 private static final String invalidInputFormat = "VNF State or incoming event is invalid. State = %s event = %s";
44 static final String toStringFormat = "StateMachineImpl{states=%s, events=%s}";
46 private final Set<State> states;
47 private final Set<Event> events;
49 StateMachineImpl(StateMachineMetadata metadata){
50 this.states = new HashSet<>();
51 this.states.addAll(metadata.getStates());
52 this.events = new HashSet<>();
53 this.events.addAll(metadata.getEvents());
57 public StateMachineResponse handleEvent(State inputState, Event event) throws InvalidInputException{
59 if(!validateInputs(inputState,event)){
60 throw new InvalidInputException(String.format(invalidInputFormat, inputState, event));
63 StateMachineResponse response = new StateMachineResponse();
64 State currentState = null;
65 State nextState = null;
66 for(State stateInSet:states){
67 if(stateInSet.equals(inputState)){
68 currentState = stateInSet;
72 if (currentState != null) {
73 for (Transition transition : currentState.getTransitions()) {
74 if (event.equals(transition.getEvent())) {
75 nextState = transition.getNextState();
79 if(nextState == null){
80 response.setResponse(Response.NO_TRANSITION_DEFINED);
82 else if(inputState.equals(nextState)){
83 response.setResponse(Response.NO_STATE_CHANGE);
86 response.setResponse(Response.VALID_TRANSITION);
88 response.setNextState(nextState);
92 boolean validateInputs(State state,Event event) {
95 && this.states.contains(state)
96 && this.events.contains(event);
100 public String toString() {
101 return String.format(toStringFormat, states, events);