2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.openecomp.appc.lifecyclemanager.impl;
25 import java.util.concurrent.ConcurrentHashMap;
27 import org.openecomp.appc.i18n.Msg;
28 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
29 import org.openecomp.appc.lifecyclemanager.helper.MetadataReader;
30 import org.openecomp.appc.lifecyclemanager.objects.*;
31 import org.openecomp.appc.statemachine.*;
32 import org.openecomp.appc.statemachine.impl.StateMachineFactory;
33 import org.openecomp.appc.statemachine.objects.*;
34 import com.att.eelf.configuration.EELFLogger;
35 import com.att.eelf.configuration.EELFManager;
36 import com.att.eelf.i18n.EELFResourceManager;
39 public class LifecycleManagerImpl implements LifecycleManager{
41 private MetadataReader metadataReader;
42 private static Map<String,StateMachine> stateMachineMap = new ConcurrentHashMap<String,StateMachine>();
43 private static final EELFLogger logger = EELFManager.getInstance().getLogger(LifecycleManagerImpl.class);
44 private static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
45 public LifecycleManagerImpl(){
46 this.metadataReader = new MetadataReader();
50 public String getNextState(String vnfType, String currentState, String event) throws NoTransitionDefinedException,LifecycleException{
51 if (logger.isTraceEnabled()) {
52 logger.trace("Entering to getNextState with vnfType = "+ vnfType + ", currentState = " + currentState + ", event = " + event);
55 State nextState = null;
56 StateMachine machine = null;
57 StateMachineResponse response;
59 machine = this.getStateMachine(vnfType);
60 response = machine.handleEvent(new State(currentState),new Event(event));
61 if(Response.NO_TRANSITION_DEFINED.equals(response.getResponse())){
62 errorLogger.error(EELFResourceManager.format(Msg.VF_ILLEGAL_COMMAND, vnfType,event,currentState));
63 throw new NoTransitionDefinedException("No Transition Defined for currentState = " + currentState + ", event = " + event,currentState,event);
65 nextState = response.getNextState();
66 } catch (InvalidInputException e) {
67 logger.error(e.getMessage());
68 throw new LifecycleException(e,currentState,event);
70 if (logger.isTraceEnabled()) {
71 logger.trace("Exiting from getNextState with (nextState = "+nextState.getStateName()!=null?nextState.getStateName():"null"+")");
73 return nextState.getStateName();
76 private StateMachine getStateMachine(String vnfType){
77 if (logger.isTraceEnabled()) {
78 logger.trace("Entering to getNextState with vnfType = "+ vnfType);
83 StateMachine machine = stateMachineMap.get(vnfType);
85 StateMachineMetadata metadata = metadataReader.readMetadata(vnfType);
86 machine = StateMachineFactory.getStateMachine(metadata);
87 stateMachineMap.put(vnfType,machine);
90 logger.trace("Exiting getStateMachine with (StateMachine = "+stateMachineMap.get(vnfType)!=null?stateMachineMap.get(vnfType).toString():"null"+")");
91 return stateMachineMap.get(vnfType);