2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.dg.util.impl;
27 import java.util.HashMap;
30 import java.util.regex.Pattern;
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;
40 public class InputParameterValidationImpl implements InputParameterValidation
42 private static final char NL = '\n';
43 private static final EELFLogger logger = EELFManager.getInstance().getLogger(InputParameterValidationImpl.class);
45 public InputParameterValidationImpl() {
49 @SuppressWarnings("nls")
51 public void validateAttribute(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
52 Map<String, String> contextParams=getValueFromContext(ctx);
53 boolean isSuccess = true;
55 for (String k : params.keySet()) {
56 logger.info("validating attribute " + k);
57 if (!contextParams.containsKey(k)) {
58 logger.info("missing attribute " + k);
61 if(contextParams.get(k)==null){
62 logger.info("mandatory attribute " + k+ "is null");
66 }catch (NullPointerException np) {
69 ctx.setAttribute("validateAttribute", String.valueOf(isSuccess));
72 @SuppressWarnings("nls")
74 public void validateAttributeLength(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
75 Map<String, String> contextParams=getValueFromContext(ctx);
76 boolean isSuccess =true;
78 int maxLength = Integer.parseInt(params.get("maximum_length_param"));
79 params.remove("maximum_length_param");
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");
88 }catch (NullPointerException np) {
91 ctx.setAttribute("validateAttributeLength", String.valueOf(isSuccess));
94 @SuppressWarnings("nls")
96 public void validateAttributeCharacter(Map<String, String> params, SvcLogicContext ctx) throws APPCException {
97 Map<String, String> contextParams=getValueFromContext(ctx);
98 boolean isSuccess =true;
100 String specialCharacter = params.get("special_characters");
101 String pattern = ".*[" + Pattern.quote(specialCharacter) + "].*";
102 params.remove("special_characters");
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 ");
112 }catch (NullPointerException np) {
115 ctx.setAttribute("validateAttributeCharacter", String.valueOf(isSuccess));
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()) {
125 for (String key : keys) {
126 String value = context.getAttribute(key);
127 params.put(key,value);