3c8459473545d595e16bd2991f1e525b1e50364a
[integration.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * org.onap.integration
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.pnfsimulator.simulator.validation;
22
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;
30
31 import com.google.common.collect.ImmutableMap;
32 import java.util.List;
33 import java.util.Map;
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;
40
41
42 public class ParamsValidator {
43
44     private final static String MISSING_PARAMS_MESSAGE = "Following mandatory params are missing:\n";
45
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)
51         .build();
52
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)
59         .build();
60
61     private JSONObject simulatorParams;
62     private JSONObject messageParams;
63
64     private ParamsValidator(JSONObject simulatorParams, JSONObject messageParams) {
65         this.simulatorParams = simulatorParams;
66         this.messageParams = messageParams;
67     }
68
69     public static ParamsValidator forParams(JSONObject simulatorParams, JSONObject messageParams) {
70         return new ParamsValidator(simulatorParams, messageParams);
71     }
72
73     public void validate() throws ValidationException {
74
75         Stream<String> missingSimulatorParams = simulatorParamsValidators
76             .entrySet()
77             .stream()
78             .filter(entry -> !entry.getValue().test(entry.getKey(), simulatorParams))
79             .map(Entry::getKey);
80
81         Stream<String> missingMessageParams = messageParamsValidators
82             .entrySet()
83             .stream()
84             .filter(entry -> !entry.getValue().test(entry.getKey(), messageParams))
85             .map(Entry::getKey);
86
87         List<String> missingParams = Stream
88             .concat(missingMessageParams, missingSimulatorParams)
89             .collect(Collectors.toList());
90
91         resolveMissingIP(missingParams);
92
93         if (!missingParams.isEmpty()) {
94             throw new ValidationException(constructMessage(missingParams));
95         }
96     }
97
98     private String constructMessage(List<String> missingParams) {
99
100         return MISSING_PARAMS_MESSAGE + missingParams
101             .stream()
102             .collect(Collectors.joining("\n"));
103     }
104
105     private boolean isNumeric(String param, JSONObject container) {
106         return isDefined(param, container) && StringUtils.isNumeric(container.getString(param));
107     }
108
109     private boolean isDefined(String param, JSONObject container) {
110
111         return container.has(param) && !container.getString(param).isEmpty();
112     }
113
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);
119         }
120     }
121 }