75864a9bac07228513ff7eb14de95c77277ede14
[appc.git] / appc-lifecycle-management / state-machine-lib / src / main / java / org / onap / appc / statemachine / impl / StateMachineImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.onap.appc.statemachine.impl;
26
27 import java.util.HashSet;
28 import java.util.List;
29 import java.util.Set;
30
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;
39
40 /**
41  * Implementation of StateMachine
42  */
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}";
46
47     private final Set<State> states;
48     private final Set<Event> events;
49
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());
55     }
56
57     @Override
58     public StateMachineResponse handleEvent(State inputState, Event event) throws InvalidInputException{
59
60         if(!validateInputs(inputState,event)){
61             throw new InvalidInputException(String.format(INVALID_INPUT_FORMAT, inputState, event));
62         }
63
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;
70                 break;
71             }
72         }
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();
79                 }
80             }
81         }
82       }
83         if(nextState == null){
84             response.setResponse(Response.NO_TRANSITION_DEFINED);
85         }
86         else if(inputState.equals(nextState)){
87             response.setResponse(Response.NO_STATE_CHANGE);
88         }
89         else{
90             response.setResponse(Response.VALID_TRANSITION);
91         }
92         response.setNextState(nextState);
93         return response;
94     }
95
96     boolean validateInputs(State state,Event event) {
97         return state != null
98                 && event != null
99                 && this.states.contains(state)
100                 && this.events.contains(event);
101     }
102
103     @Override
104     public String toString() {
105         return String.format(TO_STRING_FORMAT, states, events);
106     }
107 }