57408660a650a752ade7920f4855adfb325cc94f
[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
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;
36
37
38 public class ParamsValidator {
39
40     private final static String MISSING_PARAMS_ERROR = "Some mandatory params are missing";
41     private static ParamsValidator instance;
42
43
44     public static ParamsValidator getInstance() {
45         if (instance == null) {
46             instance = new ParamsValidator();
47         }
48         return instance;
49     }
50
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)
60             .build();
61
62         List<String> missingParams = new ArrayList<>();
63
64         paramValidators.forEach((param, validator) -> {
65             if (validator.test(params, param)) {
66                 missingParams.add(param);
67             }
68         });
69
70         clearIPError(missingParams);
71         if (!missingParams.isEmpty()) {
72             throw new ValidationException(constructMessage(missingParams));
73         }
74     }
75
76     private String constructMessage(List<String> missingParams) {
77         StringBuilder msg = new StringBuilder(MISSING_PARAMS_ERROR);
78
79         missingParams.forEach(param -> {
80             msg.append('\n');
81             msg.append(param);
82         });
83
84         return msg.toString();
85     }
86
87     private boolean isNotNumeric(JSONObject params, String param) {
88         return nullOrEmpty(params, param) || !StringUtils.isNumeric(params.getString(param));
89     }
90
91     private boolean nullOrEmpty(JSONObject params, String param) {
92         return !params.has(param) || params.getString(param).isEmpty();
93     }
94
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);
100         }
101     }
102 }