2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.openecomp.appc.dg.util.impl;
24 import java.util.HashMap;
27 import java.util.regex.Pattern;
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;
37 public class InputParameterValidationImpl implements InputParameterValidation
39 private static final char NL = '\n';
40 private static final EELFLogger logger = EELFManager.getInstance().getLogger(InputParameterValidationImpl.class);
42 public InputParameterValidationImpl() {
46 @SuppressWarnings("nls")
48 public void validateAttribute(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
49 Map<String, String> contextParams=getValueFromContext(ctx);
50 boolean isSuccess = true;
52 for (String k : params.keySet()) {
53 logger.info("validating attribute " + k);
54 if (!contextParams.containsKey(k)) {
55 logger.info("missing attribute " + k);
58 if(contextParams.get(k)==null){
59 logger.info("mandatory attribute " + k+ "is null");
63 }catch (NullPointerException np) {
66 ctx.setAttribute("validateAttribute", String.valueOf(isSuccess));
69 @SuppressWarnings("nls")
71 public void validateAttributeLength(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
72 Map<String, String> contextParams=getValueFromContext(ctx);
73 boolean isSuccess =true;
75 int maxLength = Integer.parseInt(params.get("maximum_length_param"));
76 params.remove("maximum_length_param");
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");
85 }catch (NullPointerException np) {
88 ctx.setAttribute("validateAttributeLength", String.valueOf(isSuccess));
91 @SuppressWarnings("nls")
93 public void validateAttributeCharacter(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
94 Map<String, String> contextParams=getValueFromContext(ctx);
95 boolean isSuccess =true;
97 String specialCharacter = params.get("special_characters");
98 String pattern = ".*[" + Pattern.quote(specialCharacter) + "].*";
99 params.remove("special_characters");
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 ");
109 }catch (NullPointerException np) {
112 ctx.setAttribute("validateAttributeCharacter", String.valueOf(isSuccess));
116 private Map 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()) {
122 for (String key : keys) {
123 String value = context.getAttribute(key);
124 params.put(key,value);