2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T 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.so.apihandlerinfra.infra.rest.validators;
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
27 import java.util.Optional;
28 import java.util.stream.Collectors;
29 import javax.annotation.PostConstruct;
30 import org.javatuples.Pair;
31 import org.onap.so.apihandlerinfra.exceptions.ApiException;
32 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
33 import org.onap.so.listener.ListenerRunner;
34 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.springframework.stereotype.Component;
40 public class RequestValidatorListenerRunner extends ListenerRunner {
42 private static Logger logger = LoggerFactory.getLogger(RequestValidatorListenerRunner.class);
44 protected List<RequestValidator> requestValidators;
47 protected void init() {
48 requestValidators = new ArrayList<>(
49 Optional.ofNullable(context.getBeansOfType(RequestValidator.class)).orElse(new HashMap<>()).values());
52 public boolean runValidations(String requestURI, Map<String, String> instanceIdMap, ServiceInstancesRequest request,
53 Map<String, String> queryParams) throws ApiException {
54 logger.info("Running local validations");
55 List<Pair<String, Optional<String>>> results =
56 runValidations(requestValidators, instanceIdMap, request, queryParams, requestURI);
57 if (!results.isEmpty()) {
58 throw new ValidateException("Failed Validations:\n"
59 + results.stream().map(item -> String.format("%s: %s", item.getValue0(), item.getValue1().get()))
60 .collect(Collectors.joining("\n")),
67 protected List<Pair<String, Optional<String>>> runValidations(List<? extends RequestValidator> validators,
68 Map<String, String> instanceIdMap, ServiceInstancesRequest request, Map<String, String> queryParams,
71 List<? extends RequestValidator> filtered =
72 filterListeners(validators, (item -> item.shouldRunFor(requestURI, request)));
74 List<Pair<String, Optional<String>>> results = new ArrayList<>();
75 filtered.forEach(item -> results
76 .add(new Pair<>(item.getClass().getName(), item.validate(instanceIdMap, request, queryParams))));
78 return results.stream().filter(item -> item.getValue1().isPresent()).collect(Collectors.toList());