Merging in bug fixes
[appc.git] / appc-dispatcher / appc-lifecycle-management / appc-lifecycle-management-core / src / main / java / org / openecomp / appc / lifecyclemanager / impl / LifecycleManagerImpl.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.lifecyclemanager.impl;
26
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.*;
38
39 import java.util.Map;
40 import java.util.concurrent.ConcurrentHashMap;
41
42
43 public class LifecycleManagerImpl implements LifecycleManager{
44
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();
51         }
52
53         @Override
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);
57                 }
58
59                 State nextState = null;
60                 StateMachine machine = null;
61                 StateMachineResponse response;
62                 try {
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);
68                         }
69                         nextState = response.getNextState();
70                 } catch (InvalidInputException e) {
71                         logger.error(e.getMessage());
72                         throw new LifecycleException(e,currentState,event);
73                 }
74                 if (logger.isTraceEnabled()) {
75                         logger.trace("Exiting from getNextState with (nextState = "+nextState.getStateName()!=null?nextState.getStateName():"null"+")");
76                 }
77                 return nextState.getStateName();
78         }
79
80         private StateMachine getStateMachine(String vnfType){
81                 if (logger.isTraceEnabled()) {
82                         logger.trace("Entering to getNextState with vnfType = "+ vnfType);
83                 }
84                 if(vnfType == null){
85                         vnfType = "DEFAULT";
86                 }
87                 StateMachine machine = stateMachineMap.get(vnfType);
88                 if(machine == null){
89                         StateMachineMetadata metadata = metadataReader.readMetadata(vnfType);
90                         machine = StateMachineFactory.getStateMachine(metadata);
91                         stateMachineMap.put(vnfType,machine);
92                 }
93
94                 logger.trace("Exiting getStateMachine with (StateMachine = "+stateMachineMap.get(vnfType)!=null?stateMachineMap.get(vnfType).toString():"null"+")");
95                 return stateMachineMap.get(vnfType);
96         }
97
98 }