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