8724adc5933e175aa268941e5bce7da68f6ca4d2
[appc.git] / appc-dispatcher / appc-request-handler / appc-request-handler-core / src / main / java / org / openecomp / appc / requesthandler / impl / RequestValidatorImpl.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.onap.appc.requesthandler.impl;
26
27 import com.att.eelf.i18n.EELFResourceManager;
28 import org.apache.commons.lang.ObjectUtils;
29 import org.onap.appc.domainmodel.lcm.RuntimeContext;
30 import org.onap.appc.domainmodel.lcm.VNFContext;
31 import org.onap.appc.domainmodel.lcm.VNFOperation;
32 import org.onap.appc.executor.UnstableVNFException;
33 import org.onap.appc.i18n.Msg;
34 import org.onap.appc.lifecyclemanager.LifecycleManager;
35 import org.onap.appc.lifecyclemanager.objects.LifecycleException;
36 import org.onap.appc.lifecyclemanager.objects.NoTransitionDefinedException;
37 import org.onap.appc.logging.LoggingConstants;
38 import org.onap.appc.logging.LoggingUtils;
39 import org.onap.appc.requesthandler.LCMStateManager;
40 import org.onap.appc.requesthandler.exceptions.*;
41 import org.onap.appc.workingstatemanager.WorkingStateManager;
42
43
44 public class RequestValidatorImpl extends AbstractRequestValidatorImpl {
45
46     private WorkingStateManager workingStateManager;
47     private LCMStateManager lcmStateManager;
48
49     public void setLifecyclemanager(LifecycleManager lifecyclemanager) {
50         this.lifecyclemanager = lifecyclemanager;
51     }
52
53     public void setWorkingStateManager(WorkingStateManager workingStateManager) {
54         this.workingStateManager = workingStateManager;
55     }
56
57     public void setLcmStateManager(LCMStateManager lcmStateManager) {
58         this.lcmStateManager = lcmStateManager;
59     }
60
61     public RequestValidatorImpl() {
62     }
63
64     @Override
65     public void validateRequest(RuntimeContext runtimeContext)
66             throws VNFNotFoundException, RequestExpiredException, UnstableVNFException, InvalidInputException,
67             DuplicateRequestException, NoTransitionDefinedException, LifecycleException, WorkflowNotFoundException,
68             DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException {
69         if (logger.isTraceEnabled()){
70             logger.trace("Entering to validateRequest with RequestHandlerInput = "+ ObjectUtils.toString(runtimeContext));
71         }
72         if(!lcmStateManager.isLCMOperationEnabled()) {
73             LoggingUtils.logErrorMessage(
74                     LoggingConstants.TargetNames.REQUEST_VALIDATOR,
75                     EELFResourceManager.format(Msg.LCM_OPERATIONS_DISABLED),
76                     this.getClass().getCanonicalName());
77             throw new LCMOperationsDisabledException("APPC LCM operations have been administratively disabled");
78         }
79
80         getAAIservice();
81         validateInput(runtimeContext.getRequestContext());
82         checkVNFWorkingState(runtimeContext);
83         String vnfId = runtimeContext.getRequestContext().getActionIdentifiers().getVnfId();
84         VNFContext vnfContext = queryAAI(vnfId);
85         runtimeContext.setVnfContext(vnfContext);
86
87         queryLCM(runtimeContext.getVnfContext().getStatus(), runtimeContext.getRequestContext().getAction());
88         VNFOperation operation = runtimeContext.getRequestContext().getAction();
89         if(!operation.isBuiltIn()) {
90             // for built-in operations skip WF presence check
91             queryWFM(vnfContext, runtimeContext.getRequestContext());
92         }
93     }
94
95
96     private String queryLCM(String orchestrationStatus, VNFOperation action) throws LifecycleException, NoTransitionDefinedException {
97         if (logger.isTraceEnabled()) {
98             logger.trace("Entering to queryLCM  with Orchestration Status = "+ ObjectUtils.toString(orchestrationStatus)+
99                     ", command = "+ ObjectUtils.toString(action));
100         }
101
102         String nextState = lifecyclemanager.getNextState(null, orchestrationStatus, action.name());
103         if (logger.isDebugEnabled()) {
104             logger.trace("Exiting from queryLCM with (LCMResponse = "+ ObjectUtils.toString(nextState)+")");
105         }
106         return nextState;
107     }
108
109
110     private void checkVNFWorkingState(RuntimeContext runtimeContext) throws UnstableVNFException {
111
112         if (logger.isTraceEnabled()) {
113             logger.trace("Entering to checkVNFWorkingState with RequestHandlerInput = "+ ObjectUtils.toString(runtimeContext.getRequestContext()));
114         }
115         boolean forceFlag = runtimeContext.getRequestContext().getCommonHeader().getFlags() != null && runtimeContext.getRequestContext().getCommonHeader().getFlags().isForce();
116         String vnfId = runtimeContext.getRequestContext().getActionIdentifiers().getVnfId();
117
118         if (logger.isDebugEnabled()) {
119             logger.debug("forceFlag = " + forceFlag);
120         }
121         boolean isVNFStable = workingStateManager.isVNFStable(vnfId);
122         if (!isVNFStable && !forceFlag) {
123             if (logger.isDebugEnabled()) {
124                 logger.debug("VNF is not stable for VNF ID = " + vnfId);
125             }
126             throw new UnstableVNFException("VNF is not stable for vnfID = " + vnfId);
127         }
128
129     }
130
131
132 }