Updating licenses in all files
[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  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.dg.util.impl;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.Set;
28 import java.util.regex.Pattern;
29
30 import org.openecomp.appc.dg.util.InputParameterValidation;
31 import org.openecomp.appc.exceptions.APPCException;
32 import com.att.eelf.configuration.EELFLogger;
33 import com.att.eelf.configuration.EELFManager;
34 import org.openecomp.sdnc.sli.SvcLogicContext;
35
36
37
38 public class InputParameterValidationImpl implements InputParameterValidation
39 {
40     private static final char NL = '\n';
41     private static final EELFLogger logger = EELFManager.getInstance().getLogger(InputParameterValidationImpl.class);
42
43     public InputParameterValidationImpl() {
44     }
45
46
47     @SuppressWarnings("nls")
48     @Override
49     public void validateAttribute(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
50         Map<String, String> contextParams=getValueFromContext(ctx);
51         boolean isSuccess = true;
52         try {
53             for (String k : params.keySet()) {
54                 logger.info("validating attribute  " + k);
55                 if (!contextParams.containsKey(k)) {
56                     logger.info("missing attribute  " + k);
57                     isSuccess =false;
58                 }
59                 if(contextParams.get(k)==null){
60                     logger.info("mandatory attribute " + k+ "is null");
61                     isSuccess =false;
62                 }
63             }
64         }catch (NullPointerException np) {
65             isSuccess =false;
66         }
67         ctx.setAttribute("validateAttribute", String.valueOf(isSuccess));
68     }
69
70     @SuppressWarnings("nls")
71     @Override
72     public void validateAttributeLength(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
73         Map<String, String> contextParams=getValueFromContext(ctx);
74         boolean isSuccess =true;
75         try {
76             int maxLength = Integer.parseInt(params.get("maximum_length_param"));
77             params.remove("maximum_length_param");
78
79             for (String k : params.keySet()) {
80                 logger.info("validating attribute  " + k);
81                 if(contextParams.get(k).length() > maxLength){
82                     logger.info("attribute " + k+ "'s length is exceeding Maximum limit of " + maxLength +" character");
83                     isSuccess=false;
84                 }
85             }
86         }catch (NullPointerException np) {
87             isSuccess=false;
88         }
89         ctx.setAttribute("validateAttributeLength", String.valueOf(isSuccess));
90     }
91
92     @SuppressWarnings("nls")
93     @Override
94     public void validateAttributeCharacter(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
95         Map<String, String> contextParams=getValueFromContext(ctx);
96         boolean isSuccess =true;
97         try {
98             String specialCharacter = params.get("special_characters");
99             String pattern = ".*[" + Pattern.quote(specialCharacter) + "].*";
100             params.remove("special_characters");
101
102             for (String k : params.keySet()) {
103                 logger.info("validating attribute  " + k);
104                 if(contextParams.get(k).matches(pattern)){
105                     logger.info("attribute " + k + " contains any of these " + specialCharacter + " special character ");
106                     isSuccess =false;
107                 }
108
109             }
110         }catch (NullPointerException np) {
111             isSuccess =false;
112         }
113         ctx.setAttribute("validateAttributeCharacter", String.valueOf(isSuccess));
114     }
115
116
117     private Map<String, String> getValueFromContext(SvcLogicContext context) {
118         Set<String> keys = context.getAttributeKeySet();
119         Map<String, String> params = new HashMap<String, String>();
120         StringBuilder builder = new StringBuilder();
121         if (keys != null && !keys.isEmpty()) {
122             builder.append(NL);
123             for (String key : keys) {
124                 String value = context.getAttribute(key);
125                 params.put(key,value);
126
127             }
128         }
129         return params;
130
131     }
132
133
134 }