0ac269639eabb2a7a758d210e9c67887af57dce2
[appc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
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=========================================================
20  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.lifecyclemanager.impl;
24
25 import java.util.Map;
26 import java.util.concurrent.ConcurrentHashMap;
27
28 import org.openecomp.appc.i18n.Msg;
29 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
30 import org.openecomp.appc.lifecyclemanager.helper.MetadataReader;
31 import org.openecomp.appc.lifecyclemanager.objects.*;
32 import org.openecomp.appc.statemachine.*;
33 import org.openecomp.appc.statemachine.impl.StateMachineFactory;
34 import org.openecomp.appc.statemachine.objects.*;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import com.att.eelf.i18n.EELFResourceManager;
38
39
40 public class LifecycleManagerImpl implements LifecycleManager{
41
42         private MetadataReader metadataReader;
43         private static Map<String,StateMachine> stateMachineMap = new ConcurrentHashMap<String,StateMachine>();
44         private static final EELFLogger logger = EELFManager.getInstance().getLogger(LifecycleManagerImpl.class);
45         private static EELFLogger errorLogger = EELFManager.getInstance().getErrorLogger();
46         public LifecycleManagerImpl(){
47                 this.metadataReader = new MetadataReader();
48         }
49
50         @Override
51         public String getNextState(String vnfType, String currentState, String event) throws NoTransitionDefinedException,LifecycleException{
52                 if (logger.isTraceEnabled()) {
53                         logger.trace("Entering to getNextState with vnfType = "+ vnfType +      ", currentState = " + currentState + ", event = " + event);
54                 }
55
56                 State nextState = null;
57                 StateMachine machine = null;
58                 StateMachineResponse response;
59                 try {
60                         machine = this.getStateMachine(vnfType);
61                         response = machine.handleEvent(new State(currentState),new Event(event));
62                         if(Response.NO_TRANSITION_DEFINED.equals(response.getResponse())){
63                                 errorLogger.error(EELFResourceManager.format(Msg.VF_ILLEGAL_COMMAND, vnfType,event,currentState));
64                                 throw new NoTransitionDefinedException("No Transition Defined for currentState = " +  currentState + ", event = " + event,currentState,event);
65                         }
66                         nextState = response.getNextState();
67                 } catch (InvalidInputException e) {
68                         logger.error(e.getMessage());
69                         throw new LifecycleException(e,currentState,event);
70                 }
71                 if (logger.isTraceEnabled()) {
72                         logger.trace("Exiting from getNextState with (nextState = "+nextState.getStateName()!=null?nextState.getStateName():"null"+")");
73                 }
74                 return nextState.getStateName();
75         }
76
77         private StateMachine getStateMachine(String vnfType){
78                 if (logger.isTraceEnabled()) {
79                         logger.trace("Entering to getNextState with vnfType = "+ vnfType);
80                 }
81                 if(vnfType == null){
82                         vnfType = "DEFAULT";
83                 }
84                 StateMachine machine = stateMachineMap.get(vnfType);
85                 if(machine == null){
86                         StateMachineMetadata metadata = metadataReader.readMetadata(vnfType);
87                         machine = StateMachineFactory.getStateMachine(metadata);
88                         stateMachineMap.put(vnfType,machine);
89                 }
90
91                 logger.trace("Exiting getStateMachine with (StateMachine = "+stateMachineMap.get(vnfType)!=null?stateMachineMap.get(vnfType).toString():"null"+")");
92                 return stateMachineMap.get(vnfType);
93         }
94
95 }