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.openecomp.appc.lifecyclemanager.impl;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import com.att.eelf.i18n.EELFResourceManager;
30 import org.openecomp.appc.i18n.Msg;
31 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
32 import org.openecomp.appc.lifecyclemanager.helper.MetadataReader;
33 import org.openecomp.appc.lifecyclemanager.objects.LifecycleException;
34 import org.openecomp.appc.lifecyclemanager.objects.NoTransitionDefinedException;
35 import org.openecomp.appc.statemachine.StateMachine;
36 import org.openecomp.appc.statemachine.impl.StateMachineFactory;
37 import org.openecomp.appc.statemachine.objects.*;
40 import java.util.concurrent.ConcurrentHashMap;
43 public class LifecycleManagerImpl implements LifecycleManager{
45 private MetadataReader metadataReader;
46 private static Map<String,StateMachine> stateMachineMap = new ConcurrentHashMap<String,StateMachine>();
47 private static final EELFLogger logger = EELFManager.getInstance().getLogger(LifecycleManagerImpl.class);
48 private static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
49 public LifecycleManagerImpl(){
50 this.metadataReader = new MetadataReader();
54 public String getNextState(String vnfType, String currentState, String event) throws NoTransitionDefinedException,LifecycleException{
55 if (logger.isTraceEnabled()) {
56 logger.trace("Entering to getNextState with vnfType = "+ vnfType + ", currentState = " + currentState + ", event = " + event);
59 State nextState = null;
60 StateMachine machine = null;
61 StateMachineResponse response;
63 machine = this.getStateMachine(vnfType);
64 response = machine.handleEvent(new State(currentState.toLowerCase()),new Event(event));
65 if(Response.NO_TRANSITION_DEFINED.equals(response.getResponse())){
66 errorLogger.error(EELFResourceManager.format(Msg.VF_ILLEGAL_COMMAND, vnfType,event,currentState));
67 throw new NoTransitionDefinedException("No Transition Defined for currentState = " + currentState + ", event = " + event,currentState,event);
69 nextState = response.getNextState();
70 } catch (InvalidInputException e) {
71 logger.error(e.getMessage());
72 throw new LifecycleException(e,currentState,event);
74 if (logger.isTraceEnabled()) {
75 logger.trace("Exiting from getNextState with (nextState = "+nextState.getStateName()!=null?nextState.getStateName():"null"+")");
77 return nextState.getStateName();
80 private StateMachine getStateMachine(String vnfType){
81 if (logger.isTraceEnabled()) {
82 logger.trace("Entering to getNextState with vnfType = "+ vnfType);
87 StateMachine machine = stateMachineMap.get(vnfType);
89 StateMachineMetadata metadata = metadataReader.readMetadata(vnfType);
90 machine = StateMachineFactory.getStateMachine(metadata);
91 stateMachineMap.put(vnfType,machine);
94 logger.trace("Exiting getStateMachine with (StateMachine = "+stateMachineMap.get(vnfType)!=null?stateMachineMap.get(vnfType).toString():"null"+")");
95 return stateMachineMap.get(vnfType);