Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-dispatcher / appc-request-handler / appc-request-handler-core / src / main / java / org / onap / appc / requesthandler / impl / AbstractRequestValidatorImpl.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.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;
48
49 import java.util.Calendar;
50 import java.util.Date;
51
52 public abstract class AbstractRequestValidatorImpl implements RequestValidator {
53
54     protected final EELFLogger logger = EELFManager.getInstance().getLogger(RequestValidatorImpl.class);
55     protected final Configuration configuration = ConfigurationFactory.getConfiguration();
56
57     LCMStateManager lcmStateManager;
58     TransactionRecorder transactionRecorder;
59
60     protected static Calendar DateToCalendar(Date date) {
61         Calendar cal = Calendar.getInstance();
62         cal.setTime(date);
63         return cal;
64     }
65
66     public void setTransactionRecorder(TransactionRecorder transactionRecorder) {
67         this.transactionRecorder = transactionRecorder;
68     }
69
70     public void setLcmStateManager(LCMStateManager lcmStateManager) {
71         this.lcmStateManager = lcmStateManager;
72     }
73
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));
79         }
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());
84             }
85
86             LoggingUtils.logErrorMessage(
87                     LoggingConstants.TargetNames.REQUEST_VALIDATOR,
88                     EELFResourceManager.format(Msg.APPC_INVALID_INPUT),
89                     this.getClass().getCanonicalName());
90
91             throw new InvalidInputException("vnfID or command is null");
92         }
93         CommonHeader commonHeader = requestContext.getCommonHeader();
94         try {
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);
99         }
100
101         Calendar inputTimeStamp = DateToCalendar(commonHeader.getTimeStamp());
102         Calendar currentTime = Calendar.getInstance();
103
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());
108             }
109             throw new InvalidInputException("Input Timestamp is of future = " + inputTimeStamp.getTime());
110         }
111
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();
116
117         logger.debug("TTL value set to (seconds) : " + ttl);
118
119         inputTimeStamp.add(Calendar.SECOND, ttl);
120
121         if (currentTime.getTime().getTime() >= inputTimeStamp.getTime().getTime()) {
122
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());
127
128             throw new RequestExpiredException("TTL Expired");
129         }
130         if (logger.isDebugEnabled()) {
131             logger.debug("Validation of the request is successful");
132         }
133     }
134 }