2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Copyright (C) 2017 Amdocs
\r
8 * =============================================================================
\r
9 * Licensed under the Apache License, Version 2.0 (the "License");
\r
10 * you may not use this file except in compliance with the License.
\r
11 * You may obtain a copy of the License at
\r
13 * http://www.apache.org/licenses/LICENSE-2.0
\r
15 * Unless required by applicable law or agreed to in writing, software
\r
16 * distributed under the License is distributed on an "AS IS" BASIS,
\r
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
18 * See the License for the specific language governing permissions and
\r
19 * limitations under the License.
\r
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 * ============LICENSE_END=========================================================
\r
25 package org.openecomp.appc.statemachine.impl;
\r
27 import java.util.HashSet;
\r
28 import java.util.Set;
\r
30 import org.openecomp.appc.exceptions.InvalidInputException;
\r
31 import org.openecomp.appc.statemachine.StateMachine;
\r
32 import org.openecomp.appc.statemachine.objects.Event;
\r
33 import org.openecomp.appc.statemachine.objects.Response;
\r
34 import org.openecomp.appc.statemachine.objects.State;
\r
35 import org.openecomp.appc.statemachine.objects.StateMachineMetadata;
\r
36 import org.openecomp.appc.statemachine.objects.StateMachineResponse;
\r
37 import org.openecomp.appc.statemachine.objects.Transition;
\r
40 public class StateMachineImpl implements StateMachine {
\r
42 private final Set<State> states;
\r
44 private final Set<Event> events;
\r
46 StateMachineImpl(StateMachineMetadata metadata){
\r
47 this.states = new HashSet<>();
\r
48 this.states.addAll(metadata.getStates());
\r
49 this.events = new HashSet<>();
\r
50 this.events.addAll(metadata.getEvents());
\r
53 public StateMachineResponse handleEvent(State inputState, Event event) throws InvalidInputException{
\r
55 if(!validateInputs(inputState,event)){
\r
56 throw new InvalidInputException("VNF State or incoming event is invalid. State = " +inputState + " event = " + event );
\r
59 StateMachineResponse response = new StateMachineResponse();
\r
60 State currentState = null,nextState = null;
\r
61 for(State stateInSet:states){
\r
62 if(stateInSet.equals(inputState)){
\r
63 currentState = stateInSet;
\r
67 if (currentState != null) {
\r
68 for (Transition transition : currentState.getTransitions()) {
\r
69 if (event.equals(transition.getEvent())) {
\r
70 nextState = transition.getNextState();
\r
74 if(nextState == null){
\r
75 response.setResponse(Response.NO_TRANSITION_DEFINED);
\r
77 else if(inputState.equals(nextState)){
\r
78 response.setResponse(Response.NO_STATE_CHANGE);
\r
81 response.setResponse(Response.VALID_TRANSITION);
\r
83 response.setNextState(nextState);
\r
87 private boolean validateInputs(State state,Event event) {
\r
88 return state != null && event != null && this.states.contains(state) && this.events.contains(event);
\r
92 public String toString() {
\r
93 return "StateMachineImpl{" +
\r
94 "states=" + states +
\r
95 ", events=" + events +
\r