d689c6b7a5d7baa81944869387835762cafca8fe
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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.so.apihandlerinfra.infra.rest.validators;
22
23 import java.util.ArrayList;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
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;
38
39 @Component
40 public class RequestValidatorListenerRunner extends ListenerRunner {
41
42     private static Logger logger = LoggerFactory.getLogger(RequestValidatorListenerRunner.class);
43
44     protected List<RequestValidator> requestValidators;
45
46     @PostConstruct
47     protected void init() {
48         requestValidators = new ArrayList<>(
49                 Optional.ofNullable(context.getBeansOfType(RequestValidator.class)).orElse(new HashMap<>()).values());
50     }
51
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")),
61                     400);
62         }
63
64         return true;
65     }
66
67     protected List<Pair<String, Optional<String>>> runValidations(List<? extends RequestValidator> validators,
68             Map<String, String> instanceIdMap, ServiceInstancesRequest request, Map<String, String> queryParams,
69             String requestURI) {
70
71         List<? extends RequestValidator> filtered =
72                 filterListeners(validators, (item -> item.shouldRunFor(requestURI, request)));
73
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))));
77
78         return results.stream().filter(item -> item.getValue1().isPresent()).collect(Collectors.toList());
79     }
80 }