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