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.onap.appc.statemachine.impl;
27 import java.util.HashSet;
28 import java.util.List;
31 import org.onap.appc.exceptions.InvalidInputException;
32 import org.onap.appc.statemachine.StateMachine;
33 import org.onap.appc.statemachine.objects.Event;
34 import org.onap.appc.statemachine.objects.Response;
35 import org.onap.appc.statemachine.objects.State;
36 import org.onap.appc.statemachine.objects.StateMachineMetadata;
37 import org.onap.appc.statemachine.objects.StateMachineResponse;
38 import org.onap.appc.statemachine.objects.Transition;
41 * Implementation of StateMachine
43 public class StateMachineImpl implements StateMachine {
44 private static final String INVALID_INPUT_FORMAT = "VNF State or incoming event is invalid. State = %s event = %s";
45 static final String TO_STRING_FORMAT = "StateMachineImpl{states=%s, events=%s}";
47 private final Set<State> states;
48 private final Set<Event> events;
50 StateMachineImpl(StateMachineMetadata metadata){
51 this.states = new HashSet<>();
52 this.states.addAll(metadata.getStates());
53 this.events = new HashSet<>();
54 this.events.addAll(metadata.getEvents());
58 public StateMachineResponse handleEvent(State inputState, Event event) throws InvalidInputException{
60 if(!validateInputs(inputState,event)){
61 throw new InvalidInputException(String.format(INVALID_INPUT_FORMAT, inputState, event));
64 StateMachineResponse response = new StateMachineResponse();
65 State currentState = null;
66 State nextState = null;
67 for(State stateInSet : states){
68 if(stateInSet.equals(inputState)){
69 currentState = stateInSet;
73 if (currentState != null) {
74 List<Transition> transitions = currentState.getTransitions();
75 if (transitions != null) {
76 for (Transition transition : currentState.getTransitions()) {
77 if (event.equals(transition.getEvent())) {
78 nextState = transition.getNextState();
83 if(nextState == null){
84 response.setResponse(Response.NO_TRANSITION_DEFINED);
86 else if(inputState.equals(nextState)){
87 response.setResponse(Response.NO_STATE_CHANGE);
90 response.setResponse(Response.VALID_TRANSITION);
92 response.setNextState(nextState);
96 boolean validateInputs(State state,Event event) {
99 && this.states.contains(state)
100 && this.events.contains(event);
104 public String toString() {
105 return String.format(TO_STRING_FORMAT, states, events);