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.onap.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.onap.appc.domainmodel.lcm.RequestContext;
33 import org.onap.appc.domainmodel.lcm.RuntimeContext;
34 import org.onap.appc.domainmodel.lcm.TransactionRecord;
35 import org.onap.appc.exceptions.APPCException;
36 import org.onap.appc.requesthandler.constant.Constants;
37 import org.onap.appc.configuration.Configuration;
38 import org.onap.appc.configuration.ConfigurationFactory;
39 import org.onap.appc.domainmodel.lcm.CommonHeader;
40 import org.onap.appc.exceptions.InvalidInputException;
41 import org.onap.appc.i18n.Msg;
42 import org.onap.appc.logging.LoggingConstants;
43 import org.onap.appc.logging.LoggingUtils;
44 import org.onap.appc.requesthandler.LCMStateManager;
45 import org.onap.appc.requesthandler.exceptions.*;
46 import org.onap.appc.requesthandler.helper.RequestValidator;
47 import org.onap.appc.transactionrecorder.TransactionRecorder;
49 import java.util.Calendar;
50 import java.util.Date;
52 public abstract class AbstractRequestValidatorImpl implements RequestValidator {
54 protected final EELFLogger logger = EELFManager.getInstance().getLogger(RequestValidatorImpl.class);
55 protected final Configuration configuration = ConfigurationFactory.getConfiguration();
57 LCMStateManager lcmStateManager;
58 TransactionRecorder transactionRecorder;
60 protected static Calendar DateToCalendar(Date date) {
61 Calendar cal = Calendar.getInstance();
66 public void setTransactionRecorder(TransactionRecorder transactionRecorder) {
67 this.transactionRecorder = transactionRecorder;
70 public void setLcmStateManager(LCMStateManager lcmStateManager) {
71 this.lcmStateManager = lcmStateManager;
74 protected void validateInput(RuntimeContext runtimeContext)
75 throws RequestExpiredException, InvalidInputException, DuplicateRequestException {
76 RequestContext requestContext = runtimeContext.getRequestContext();
77 if (logger.isTraceEnabled()){
78 logger.trace("Entering to validateInput with RequestHandlerInput = "+ ObjectUtils.toString(requestContext));
80 if (StringUtils.isEmpty(requestContext.getActionIdentifiers().getVnfId()) || requestContext.getAction() == null
81 || StringUtils.isEmpty(requestContext.getAction().name()) || StringUtils.isEmpty(requestContext.getCommonHeader().getApiVer())){
82 if (logger.isDebugEnabled()) {
83 logger.debug("vnfID = " + requestContext.getActionIdentifiers().getVnfId() + ", action = " + requestContext.getAction().name());
86 LoggingUtils.logErrorMessage(
87 LoggingConstants.TargetNames.REQUEST_VALIDATOR,
88 EELFResourceManager.format(Msg.APPC_INVALID_INPUT),
89 this.getClass().getCanonicalName());
91 throw new InvalidInputException("vnfID or command is null");
93 CommonHeader commonHeader = requestContext.getCommonHeader();
95 if(transactionRecorder.isTransactionDuplicate(runtimeContext.getTransactionRecord()))
96 throw new DuplicateRequestException("Duplicate Request with");
97 } catch (APPCException e) {
98 logger.error("Error accessing database for transaction data",e);
101 Calendar inputTimeStamp = DateToCalendar(commonHeader.getTimeStamp());
102 Calendar currentTime = Calendar.getInstance();
104 // If input timestamp is of future, we reject the request
105 if (inputTimeStamp.getTime().getTime() > currentTime.getTime().getTime()) {
106 if (logger.isDebugEnabled()) {
107 logger.debug("Input Timestamp is of future = " + inputTimeStamp.getTime());
109 throw new InvalidInputException("Input Timestamp is of future = " + inputTimeStamp.getTime());
112 // Set ttl value from commonHeader. If not available set it to default
113 Integer ttl = (commonHeader.getFlags()== null || commonHeader.getFlags().getTtl() <= 0 ) ?
114 Integer.parseInt(configuration.getProperty(Constants.DEFAULT_TTL_KEY, String.valueOf(Constants.DEFAULT_TTL))):
115 commonHeader.getFlags().getTtl();
117 logger.debug("TTL value set to (seconds) : " + ttl);
119 inputTimeStamp.add(Calendar.SECOND, ttl);
121 if (currentTime.getTime().getTime() >= inputTimeStamp.getTime().getTime()) {
123 LoggingUtils.logErrorMessage(
124 LoggingConstants.TargetNames.REQUEST_VALIDATOR,
125 "TTL Expired: Current time - " + currentTime.getTime().getTime() + " Request time: " + inputTimeStamp.getTime().getTime() + " with TTL value: " + ttl,
126 this.getClass().getCanonicalName());
128 throw new RequestExpiredException("TTL Expired");
130 if (logger.isDebugEnabled()) {
131 logger.debug("Validation of the request is successful");