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;
29 import static org.onap.pnfsimulator.message.MessageConstants.VES_SERVER_URL;
31 import com.google.common.collect.ImmutableMap;
32 import java.util.List;
34 import java.util.Map.Entry;
35 import java.util.function.BiPredicate;
36 import java.util.stream.Collectors;
37 import java.util.stream.Stream;
38 import org.apache.commons.lang3.StringUtils;
39 import org.json.JSONObject;
42 public class ParamsValidator {
44 private final static String MISSING_PARAMS_MESSAGE = "Following mandatory params are missing:\n";
46 private final Map<String, BiPredicate<String, JSONObject>> simulatorParamsValidators = ImmutableMap
47 .<String, BiPredicate<String, JSONObject>>builder()
48 .put(VES_SERVER_URL, this::isDefined)
49 .put(TEST_DURATION, this::isNumeric)
50 .put(MESSAGE_INTERVAL, this::isNumeric)
53 private final Map<String, BiPredicate<String, JSONObject>> messageParamsValidators = ImmutableMap
54 .<String, BiPredicate<String, JSONObject>>builder()
55 .put(PNF_SERIAL_NUMBER, this::isDefined)
56 .put(PNF_VENDOR_NAME, this::isDefined)
57 .put(PNF_OAM_IPV4_ADDRESS, this::isDefined)
58 .put(PNF_OAM_IPV6_ADDRESS, this::isDefined)
61 private JSONObject simulatorParams;
62 private JSONObject messageParams;
64 private ParamsValidator(JSONObject simulatorParams, JSONObject messageParams) {
65 this.simulatorParams = simulatorParams;
66 this.messageParams = messageParams;
69 public static ParamsValidator forParams(JSONObject simulatorParams, JSONObject messageParams) {
70 return new ParamsValidator(simulatorParams, messageParams);
73 public void validate() throws ValidationException {
75 Stream<String> missingSimulatorParams = simulatorParamsValidators
78 .filter(entry -> !entry.getValue().test(entry.getKey(), simulatorParams))
81 Stream<String> missingMessageParams = messageParamsValidators
84 .filter(entry -> !entry.getValue().test(entry.getKey(), messageParams))
87 List<String> missingParams = Stream
88 .concat(missingMessageParams, missingSimulatorParams)
89 .collect(Collectors.toList());
91 resolveMissingIP(missingParams);
93 if (!missingParams.isEmpty()) {
94 throw new ValidationException(constructMessage(missingParams));
98 private String constructMessage(List<String> missingParams) {
100 return MISSING_PARAMS_MESSAGE + missingParams
102 .collect(Collectors.joining("\n"));
105 private boolean isNumeric(String param, JSONObject container) {
106 return isDefined(param, container) && StringUtils.isNumeric(container.getString(param));
109 private boolean isDefined(String param, JSONObject container) {
111 return container.has(param) && !container.getString(param).isEmpty();
114 private void resolveMissingIP(List<String> missingParams) {
115 // if only one IP is missing clear the error
116 if (!(missingParams.contains(PNF_OAM_IPV4_ADDRESS) && missingParams.contains(PNF_OAM_IPV6_ADDRESS))) {
117 missingParams.remove(PNF_OAM_IPV4_ADDRESS);
118 missingParams.remove(PNF_OAM_IPV6_ADDRESS);