4e21c13f433a8a5a233da165dcac3821e17b6962
[appc.git] / appc-lifecycle-management / state-machine-lib / src / main / java / org / openecomp / 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.openecomp.appc.statemachine.impl;
26
27 import java.util.HashSet;
28 import java.util.Set;
29
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;
38
39 /**
40  * Implementation of StateMachine
41  */
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}";
45
46     private final Set<State> states;
47     private final Set<Event> events;
48
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());
54     }
55
56     @Override
57     public StateMachineResponse handleEvent(State inputState, Event event) throws InvalidInputException{
58
59         if(!validateInputs(inputState,event)){
60             throw new InvalidInputException(String.format(invalidInputFormat, inputState, event));
61         }
62
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;
69                 break;
70             }
71         }
72         if (currentState != null) {
73             for (Transition transition : currentState.getTransitions()) {
74                 if (event.equals(transition.getEvent())) {
75                     nextState = transition.getNextState();
76                 }
77             }
78         }
79         if(nextState == null){
80             response.setResponse(Response.NO_TRANSITION_DEFINED);
81         }
82         else if(inputState.equals(nextState)){
83             response.setResponse(Response.NO_STATE_CHANGE);
84         }
85         else{
86             response.setResponse(Response.VALID_TRANSITION);
87         }
88         response.setNextState(nextState);
89         return response;
90     }
91
92     boolean validateInputs(State state,Event event) {
93         return state != null
94                 && event != null
95                 && this.states.contains(state)
96                 && this.events.contains(event);
97     }
98
99     @Override
100     public String toString() {
101         return String.format(toStringFormat, states, events);
102     }
103 }