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