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.requesthandler.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.apache.commons.lang.ObjectUtils;
31 import org.apache.commons.lang.StringUtils;
32 import org.openecomp.appc.requesthandler.constant.Constants;
33 import org.openecomp.appc.configuration.Configuration;
34 import org.openecomp.appc.configuration.ConfigurationFactory;
35 import org.openecomp.appc.domainmodel.lcm.CommonHeader;
36 import org.openecomp.appc.domainmodel.lcm.RequestContext;
37 import org.openecomp.appc.domainmodel.lcm.RuntimeContext;
38 import org.openecomp.appc.domainmodel.lcm.VNFContext;
39 import org.openecomp.appc.executor.UnstableVNFException;
40 import org.openecomp.appc.executor.objects.UniqueRequestIdentifier;
41 import org.openecomp.appc.i18n.Msg;
42 import org.openecomp.appc.lifecyclemanager.LifecycleManager;
43 import org.openecomp.appc.lifecyclemanager.objects.LifecycleException;
44 import org.openecomp.appc.lifecyclemanager.objects.NoTransitionDefinedException;
45 import org.openecomp.appc.logging.LoggingConstants;
46 import org.openecomp.appc.logging.LoggingUtils;
47 import org.openecomp.appc.requesthandler.LCMStateManager;
48 import org.openecomp.appc.requesthandler.exceptions.*;
49 import org.openecomp.appc.requesthandler.helper.RequestRegistry;
50 import org.openecomp.appc.requesthandler.helper.RequestValidator;
51 import org.openecomp.appc.workflow.WorkFlowManager;
52 import org.openecomp.appc.workflow.objects.WorkflowExistsOutput;
53 import org.openecomp.appc.workflow.objects.WorkflowRequest;
54 import org.openecomp.sdnc.sli.SvcLogicContext;
55 import org.openecomp.sdnc.sli.SvcLogicException;
56 import org.openecomp.sdnc.sli.SvcLogicResource;
57 import org.openecomp.sdnc.sli.aai.AAIService;
58 import org.osgi.framework.BundleContext;
59 import org.osgi.framework.FrameworkUtil;
60 import org.osgi.framework.ServiceReference;
62 import java.time.Instant;
63 import java.util.Calendar;
64 import java.util.Date;
66 public abstract class AbstractRequestValidatorImpl implements RequestValidator {
68 protected final EELFLogger logger = EELFManager.getInstance().getLogger(RequestValidatorImpl.class);
69 private final Configuration configuration = ConfigurationFactory.getConfiguration();
70 protected LifecycleManager lifecyclemanager;
71 protected LCMStateManager lcmStateManager;
72 private AAIService aaiService;
73 private WorkFlowManager workflowManager;
74 private RequestRegistry requestRegistry = new RequestRegistry();
76 protected static Calendar DateToCalendar(Date date) {
77 Calendar cal = Calendar.getInstance();
82 public void setWorkflowManager(WorkFlowManager workflowManager) {
83 this.workflowManager = workflowManager;
86 public void setLcmStateManager(LCMStateManager lcmStateManager) {
87 this.lcmStateManager = lcmStateManager;
90 public void setRequestRegistry(RequestRegistry requestRegistry) {
91 this.requestRegistry = requestRegistry;
94 public abstract void validateRequest(RuntimeContext runtimeContext) throws VNFNotFoundException, RequestExpiredException, UnstableVNFException, InvalidInputException, DuplicateRequestException, NoTransitionDefinedException, LifecycleException, WorkflowNotFoundException, DGWorkflowNotFoundException, MissingVNFDataInAAIException, LCMOperationsDisabledException;
96 private boolean isValidTTL(String ttl) {
97 if (logger.isTraceEnabled()){
98 logger.trace("Entering to isValidTTL where ttl = "+ ObjectUtils.toString(ttl));
100 if (ttl == null || ttl.length() == 0) {
101 if (logger.isTraceEnabled()) {
102 logger.trace("Exiting from isValidTT with (result = "+ ObjectUtils.toString(false)+")");
107 Integer i = Integer.parseInt(ttl);
108 if (logger.isTraceEnabled()) {
109 logger.trace("Exiting from isValidTTL with (result = "+ ObjectUtils.toString(i > 0)+")");
112 } catch (NumberFormatException e) {
113 if (logger.isTraceEnabled()) {
114 logger.trace("Exiting from isValidTTL with (result = "+ ObjectUtils.toString(false)+")");
120 protected void getAAIservice() {
121 BundleContext bctx = FrameworkUtil.getBundle(AAIService.class).getBundleContext();
122 // Get AAIadapter reference
123 ServiceReference sref = bctx.getServiceReference(AAIService.class.getName());
125 logger.info("AAIService from bundlecontext");
126 aaiService = (AAIService) bctx.getService(sref);
129 logger.info("AAIService error from bundlecontext");
130 logger.warn("Cannot find service reference for org.openecomp.sdnc.sli.aai.AAIService");
135 protected VNFContext queryAAI(String vnfId) throws VNFNotFoundException, MissingVNFDataInAAIException {
136 SvcLogicContext ctx = new SvcLogicContext();
137 ctx = getVnfdata(vnfId, "vnf", ctx);
139 VNFContext vnfContext = new VNFContext();
140 populateVnfContext(vnfContext, ctx);
145 protected void queryWFM(VNFContext vnfContext, RequestContext requestContext) throws WorkflowNotFoundException,DGWorkflowNotFoundException {
147 checkWorkflowExists(vnfContext, requestContext);
150 private void checkWorkflowExists(VNFContext vnfContext, RequestContext requestContext) throws WorkflowNotFoundException,DGWorkflowNotFoundException {
152 WorkflowExistsOutput workflowExistsOutput = workflowManager.workflowExists(getWorkflowQueryParams(vnfContext, requestContext));
153 if (!workflowExistsOutput.isMappingExist()) {
154 if (logger.isDebugEnabled()) {
155 logger.debug("WorkflowManager : Workflow not found for vnfType = " + vnfContext.getType() + ", version = " + vnfContext.getVersion() + ", command = " + requestContext.getAction().name());
158 LoggingUtils.logErrorMessage(
159 LoggingConstants.TargetNames.WORKFLOW_MANAGER,
160 EELFResourceManager.format(Msg.APPC_WORKFLOW_NOT_FOUND, vnfContext.getType(), requestContext.getAction().name()),
161 this.getClass().getCanonicalName());
164 throw new WorkflowNotFoundException("Workflow not found for vnfType = " + vnfContext.getType() + ", command = " + requestContext.getAction().name(),vnfContext.getType(),requestContext.getAction().name());
166 if (!workflowExistsOutput.isDgExist()) {
167 if (logger.isDebugEnabled()) {
168 logger.debug("WorkflowManager : DG Workflow not found for vnfType = " + vnfContext.getType() + ", version = " + vnfContext.getVersion() + ", command = " + requestContext.getAction().name()+" "+workflowExistsOutput);
172 LoggingUtils.logErrorMessage(
173 LoggingConstants.TargetNames.WORKFLOW_MANAGER,
174 EELFResourceManager.format(Msg.APPC_WORKFLOW_NOT_FOUND, vnfContext.getType(), requestContext.getAction().name()),
175 this.getClass().getCanonicalName());
178 throw new DGWorkflowNotFoundException("Workflow not found for vnfType = " + vnfContext.getType() + ", command = " + requestContext.getAction().name(),
179 workflowExistsOutput.getWorkflowModule(),workflowExistsOutput.getWorkflowName(),workflowExistsOutput.getWorkflowVersion());
183 private void populateVnfContext(VNFContext vnfContext, SvcLogicContext ctx) throws MissingVNFDataInAAIException {
184 String vnfType = ctx.getAttribute("vnf.vnf-type");
185 String orchestrationStatus = ctx.getAttribute("vnf.orchestration-status");
186 if(StringUtils.isEmpty(vnfType)){
187 throw new MissingVNFDataInAAIException("vnf-type");
189 else if(StringUtils.isEmpty(orchestrationStatus)){
190 throw new MissingVNFDataInAAIException("orchestration-status");
192 vnfContext.setType(vnfType);
193 vnfContext.setStatus(orchestrationStatus);
194 vnfContext.setId(ctx.getAttribute("vnf.vnf-id"));
197 private WorkflowRequest getWorkflowQueryParams(VNFContext vnfContext, RequestContext requestContext) {
199 WorkflowRequest workflowRequest = new WorkflowRequest();
200 workflowRequest.setVnfContext(vnfContext);
201 workflowRequest.setRequestContext(requestContext);
202 if (logger.isTraceEnabled()) {
203 logger.trace("Exiting from etWorkflowQueryParams with (WorkflowRequest = "+ ObjectUtils.toString(workflowRequest)+")");
205 return workflowRequest;
208 protected void checkForDuplicateRequest(CommonHeader header) throws DuplicateRequestException {
209 if (logger.isTraceEnabled()) {
210 logger.trace("Entering to checkForDuplicateRequest with RequestHeader = "+ ObjectUtils.toString(header));
213 UniqueRequestIdentifier requestIdentifier = new UniqueRequestIdentifier(header.getOriginatorId(), header.getRequestId(), header.getSubRequestId());
214 boolean requestAccepted = requestRegistry.registerRequest(requestIdentifier);
215 if (!requestAccepted) {
216 if (logger.isDebugEnabled()) {
217 logger.debug("Duplicate Request with " + requestIdentifier);
219 throw new DuplicateRequestException("Duplicate Request with " + requestIdentifier);
223 protected Integer readTTL(CommonHeader header) {
224 if (logger.isTraceEnabled()) {
225 logger.trace("Entering to readTTL with RequestHandlerInput = "+ ObjectUtils.toString(header));
227 if (header.getFlags()== null || !isValidTTL(String.valueOf(header.getFlags().getTtl()))) {
228 String defaultTTLStr = configuration.getProperty("org.openecomp.appc.workflow.default.ttl", String.valueOf(Constants.DEFAULT_TTL));
229 return Integer.parseInt(defaultTTLStr);
231 if (logger.isTraceEnabled()) {
232 logger.trace("Exiting from readTTL with (TTL = "+ ObjectUtils.toString(header.getFlags().getTtl())+")");
234 return header.getFlags().getTtl();
237 private SvcLogicContext getVnfdata(String vnf_id, String prefix, SvcLogicContext ctx) throws VNFNotFoundException {
238 if (logger.isTraceEnabled()) {
239 logger.trace("Entering to getVnfdata with vnfid = "+ ObjectUtils.toString(vnf_id) + ", prefix = "+ ObjectUtils.toString(prefix)+ ", SvcLogicContext"+ ObjectUtils.toString(ctx));
242 String key = "vnf-id = '" + vnf_id + "'";
243 logger.debug("inside getVnfdata=== " + key);
245 Instant beginTimestamp = Instant.now();
246 SvcLogicResource.QueryStatus response = aaiService.query("generic-vnf", false, null, key, prefix, null, ctx);
247 Instant endTimestamp = Instant.now();
248 String status = SvcLogicResource.QueryStatus.SUCCESS.equals(response) ? LoggingConstants.StatusCodes.COMPLETE : LoggingConstants.StatusCodes.ERROR;
249 LoggingUtils.logMetricsMessage(
252 LoggingConstants.TargetNames.AAI,
253 LoggingConstants.TargetServiceNames.AAIServiceNames.QUERY,
257 this.getClass().getCanonicalName());
258 if (SvcLogicResource.QueryStatus.NOT_FOUND.equals(response)) {
259 throw new VNFNotFoundException("VNF not found for vnf_id = " + vnf_id);
260 } else if (SvcLogicResource.QueryStatus.FAILURE.equals(response)) {
261 throw new RuntimeException("Error Querying AAI with vnfID = " + vnf_id);
263 logger.info("AAIResponse: " + response.toString());
264 } catch (SvcLogicException e) {
266 LoggingUtils.logErrorMessage(
267 LoggingConstants.TargetServiceNames.AAIServiceNames.GET_VNF_DATA,
268 "Error in getVnfdata" + e,
269 this.getClass().getCanonicalName());
271 throw new RuntimeException(e);
273 if (logger.isTraceEnabled()) {
274 logger.trace("Exiting from getVnfdata with (SvcLogicContext = "+ ObjectUtils.toString(ctx)+")");
279 protected void validateInput(RequestContext requestContext)
280 throws RequestExpiredException, InvalidInputException, DuplicateRequestException {
281 if (logger.isTraceEnabled()){
282 logger.trace("Entering to validateInput with RequestHandlerInput = "+ ObjectUtils.toString(requestContext));
284 if (requestContext.getActionIdentifiers().getVnfId() == null || requestContext.getAction() == null
285 || requestContext.getActionIdentifiers().getVnfId().length() == 0 || requestContext.getAction().name().length() == 0 ||
286 null == requestContext.getCommonHeader().getApiVer()) {
287 if (logger.isDebugEnabled()) {
288 logger.debug("vnfID = " + requestContext.getActionIdentifiers().getVnfId() + ", action = " + requestContext.getAction().name());
291 LoggingUtils.logErrorMessage(
292 LoggingConstants.TargetNames.REQUEST_VALIDATOR,
293 EELFResourceManager.format(Msg.APPC_INVALID_INPUT),
294 this.getClass().getCanonicalName());
296 throw new InvalidInputException("vnfID or command is null");
298 CommonHeader commonHeader = requestContext.getCommonHeader();
300 checkForDuplicateRequest(commonHeader);
302 Calendar inputTimeStamp = DateToCalendar(Date.from(commonHeader.getTimeStamp()));
303 Calendar currentTime = Calendar.getInstance();
305 // If input timestamp is of future, we reject the request
306 if (inputTimeStamp.getTime().getTime() > currentTime.getTime().getTime()) {
307 if (logger.isDebugEnabled()) {
308 logger.debug("Input Timestamp is of future = " + inputTimeStamp.getTime());
310 throw new InvalidInputException("Input Timestamp is of future = " + inputTimeStamp.getTime());
312 Integer ttl = readTTL(commonHeader);
313 logger.debug("TTL value set to (seconds) : " + ttl);
314 inputTimeStamp.add(Calendar.SECOND, ttl);
315 if (currentTime.getTime().getTime() >= inputTimeStamp.getTime().getTime()) {
317 LoggingUtils.logErrorMessage(
318 LoggingConstants.TargetNames.REQUEST_VALIDATOR,
319 "TTL Expired: Current time - " + currentTime.getTime().getTime() + " Request time: " + inputTimeStamp.getTime().getTime() + " with TTL value: " + ttl,
320 this.getClass().getCanonicalName());
322 throw new RequestExpiredException("TTL Expired");
324 if (logger.isDebugEnabled()) {
325 logger.debug("Validation of the request is successful");