Merge "Parameterize raw generic type CommandTask<M>"
[appc.git] / appc-dg-util / appc-dg-util-bundle / src / main / java / org / openecomp / appc / dg / util / impl / InputParameterValidationImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * openECOMP : APP-C
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                                              reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.appc.dg.util.impl;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.Set;
27 import java.util.regex.Pattern;
28
29 import org.openecomp.appc.dg.util.InputParameterValidation;
30 import org.openecomp.appc.exceptions.APPCException;
31 import com.att.eelf.configuration.EELFLogger;
32 import com.att.eelf.configuration.EELFManager;
33 import org.openecomp.sdnc.sli.SvcLogicContext;
34
35
36
37 public class InputParameterValidationImpl implements InputParameterValidation
38 {
39     private static final char NL = '\n';
40     private static final EELFLogger logger = EELFManager.getInstance().getLogger(InputParameterValidationImpl.class);
41
42     public InputParameterValidationImpl() {
43     }
44
45
46     @SuppressWarnings("nls")
47     @Override
48     public void validateAttribute(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
49         Map<String, String> contextParams=getValueFromContext(ctx);
50         boolean isSuccess = true;
51         try {
52             for (String k : params.keySet()) {
53                 logger.info("validating attribute  " + k);
54                 if (!contextParams.containsKey(k)) {
55                     logger.info("missing attribute  " + k);
56                     isSuccess =false;
57                 }
58                 if(contextParams.get(k)==null){
59                     logger.info("mandatory attribute " + k+ "is null");
60                     isSuccess =false;
61                 }
62             }
63         }catch (NullPointerException np) {
64             isSuccess =false;
65         }
66         ctx.setAttribute("validateAttribute", String.valueOf(isSuccess));
67     }
68
69     @SuppressWarnings("nls")
70     @Override
71     public void validateAttributeLength(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
72         Map<String, String> contextParams=getValueFromContext(ctx);
73         boolean isSuccess =true;
74         try {
75             int maxLength = Integer.parseInt(params.get("maximum_length_param"));
76             params.remove("maximum_length_param");
77
78             for (String k : params.keySet()) {
79                 logger.info("validating attribute  " + k);
80                 if(contextParams.get(k).length() > maxLength){
81                     logger.info("attribute " + k+ "'s length is exceeding Maximum limit of " + maxLength +" character");
82                     isSuccess=false;
83                 }
84             }
85         }catch (NullPointerException np) {
86             isSuccess=false;
87         }
88         ctx.setAttribute("validateAttributeLength", String.valueOf(isSuccess));
89     }
90
91     @SuppressWarnings("nls")
92     @Override
93     public void validateAttributeCharacter(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
94         Map<String, String> contextParams=getValueFromContext(ctx);
95         boolean isSuccess =true;
96         try {
97             String specialCharacter = params.get("special_characters");
98             String pattern = ".*[" + Pattern.quote(specialCharacter) + "].*";
99             params.remove("special_characters");
100
101             for (String k : params.keySet()) {
102                 logger.info("validating attribute  " + k);
103                 if(contextParams.get(k).matches(pattern)){
104                     logger.info("attribute " + k + " contains any of these " + specialCharacter + " special character ");
105                     isSuccess =false;
106                 }
107
108             }
109         }catch (NullPointerException np) {
110             isSuccess =false;
111         }
112         ctx.setAttribute("validateAttributeCharacter", String.valueOf(isSuccess));
113     }
114
115
116     private Map<String, String> getValueFromContext(SvcLogicContext context) {
117         Set<String> keys = context.getAttributeKeySet();
118         Map<String, String> params = new HashMap<String, String>();
119         StringBuilder builder = new StringBuilder();
120         if (keys != null && !keys.isEmpty()) {
121             builder.append(NL);
122             for (String key : keys) {
123                 String value = context.getAttribute(key);
124                 params.put(key,value);
125
126             }
127         }
128         return params;
129
130     }
131
132
133 }