2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 NOKIA Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.pnfsimulator.simulator.validation;
23 import static org.onap.pnfsimulator.message.MessageConstants.MESSAGE_INTERVAL;
24 import static org.onap.pnfsimulator.message.MessageConstants.PNF_OAM_IPV4_ADDRESS;
25 import static org.onap.pnfsimulator.message.MessageConstants.PNF_OAM_IPV6_ADDRESS;
26 import static org.onap.pnfsimulator.message.MessageConstants.PNF_SERIAL_NUMBER;
27 import static org.onap.pnfsimulator.message.MessageConstants.PNF_VENDOR_NAME;
28 import static org.onap.pnfsimulator.message.MessageConstants.TEST_DURATION;
30 import com.google.common.collect.ImmutableMap;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.function.BiPredicate;
34 import org.apache.commons.lang3.StringUtils;
35 import org.json.JSONObject;
38 public class ParamsValidator {
40 private final static String MISSING_PARAMS_ERROR = "Some mandatory params are missing";
41 private static ParamsValidator instance;
44 public static ParamsValidator getInstance() {
45 if (instance == null) {
46 instance = new ParamsValidator();
51 public void validate(JSONObject params) throws ValidationException {
52 ImmutableMap<String, BiPredicate<JSONObject, String>> paramValidators = ImmutableMap
53 .<String, BiPredicate<JSONObject, String>>builder()
54 .put(TEST_DURATION, this::isNotNumeric)
55 .put(MESSAGE_INTERVAL, this::isNotNumeric)
56 .put(PNF_SERIAL_NUMBER, this::nullOrEmpty)
57 .put(PNF_VENDOR_NAME, this::nullOrEmpty)
58 .put(PNF_OAM_IPV4_ADDRESS, this::nullOrEmpty)
59 .put(PNF_OAM_IPV6_ADDRESS, this::nullOrEmpty)
62 List<String> missingParams = new ArrayList<>();
64 paramValidators.forEach((param, validator) -> {
65 if (validator.test(params, param)) {
66 missingParams.add(param);
70 clearIPError(missingParams);
71 if (!missingParams.isEmpty()) {
72 throw new ValidationException(constructMessage(missingParams));
76 private String constructMessage(List<String> missingParams) {
77 StringBuilder msg = new StringBuilder(MISSING_PARAMS_ERROR);
79 missingParams.forEach(param -> {
84 return msg.toString();
87 private boolean isNotNumeric(JSONObject params, String param) {
88 return nullOrEmpty(params, param) || !StringUtils.isNumeric(params.getString(param));
91 private boolean nullOrEmpty(JSONObject params, String param) {
92 return !params.has(param) || params.getString(param).isEmpty();
95 private void clearIPError(List<String> missingParams) {
96 // if only one IP is missing clear the error
97 if (!(missingParams.contains(PNF_OAM_IPV4_ADDRESS) && missingParams.contains(PNF_OAM_IPV6_ADDRESS))) {
98 missingParams.remove(PNF_OAM_IPV4_ADDRESS);
99 missingParams.remove(PNF_OAM_IPV6_ADDRESS);