6cdb781f1aad3610d6ce30bed9091850b94c3a5b
[integration.git] /
1 package org.onap.pnfsimulator.simulator.validation;
2
3 import static org.onap.pnfsimulator.message.MessageConstants.MESSAGE_INTERVAL;
4 import static org.onap.pnfsimulator.message.MessageConstants.PNF_OAM_IPV4_ADDRESS;
5 import static org.onap.pnfsimulator.message.MessageConstants.PNF_OAM_IPV6_ADDRESS;
6 import static org.onap.pnfsimulator.message.MessageConstants.PNF_SERIAL_NUMBER;
7 import static org.onap.pnfsimulator.message.MessageConstants.PNF_VENDOR_NAME;
8 import static org.onap.pnfsimulator.message.MessageConstants.TEST_DURATION;
9
10 import com.google.common.collect.ImmutableMap;
11 import java.util.List;
12 import java.util.Map;
13 import java.util.Map.Entry;
14 import java.util.function.Predicate;
15 import java.util.stream.Collectors;
16 import org.apache.commons.lang3.StringUtils;
17 import org.json.JSONObject;
18
19
20 public class ParamsValidator {
21
22     private final static String MISSING_PARAMS_MESSAGE = "Following mandatory params are missing:\n";
23     private final Map<String, Predicate<String>> validators = ImmutableMap
24         .<String, Predicate<String>>builder()
25         .put(TEST_DURATION, this::isNumeric)
26         .put(MESSAGE_INTERVAL, this::isNumeric)
27         .put(PNF_SERIAL_NUMBER, this::isDefined)
28         .put(PNF_VENDOR_NAME, this::isDefined)
29         .put(PNF_OAM_IPV4_ADDRESS, this::isDefined)
30         .put(PNF_OAM_IPV6_ADDRESS, this::isDefined)
31         .build();
32
33     private JSONObject subject;
34
35     private ParamsValidator(JSONObject paramsObject) {
36         subject = paramsObject;
37     }
38
39     public static ParamsValidator forObject(JSONObject configObject) {
40         return new ParamsValidator(configObject);
41     }
42
43     public void validate() throws ValidationException {
44
45         List<String> missingParams = validators
46             .entrySet()
47             .stream()
48             .filter(entry -> !entry.getValue().test(entry.getKey()))
49             .map(Entry::getKey)
50             .collect(Collectors.toList());
51
52         resolveMissingIP(missingParams);
53
54         if (!missingParams.isEmpty()) {
55             throw new ValidationException(constructMessage(missingParams));
56         }
57     }
58
59     private String constructMessage(List<String> missingParams) {
60
61         return MISSING_PARAMS_MESSAGE + missingParams
62             .stream()
63             .collect(Collectors.joining("\n"));
64     }
65
66     private boolean isNumeric(String param) {
67         return isDefined(param) && StringUtils.isNumeric(subject.getString(param));
68     }
69
70     private boolean isDefined(String param) {
71         return subject.has(param) && !subject.getString(param).isEmpty();
72     }
73
74     private void resolveMissingIP(List<String> missingParams) {
75         // if only one IP is missing clear the error
76         if (!(missingParams.contains(PNF_OAM_IPV4_ADDRESS) && missingParams.contains(PNF_OAM_IPV6_ADDRESS))) {
77             missingParams.remove(PNF_OAM_IPV4_ADDRESS);
78             missingParams.remove(PNF_OAM_IPV6_ADDRESS);
79         }
80     }
81 }