3aba39b501afa8142686f958ebd24eef04a6f97f
[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.Actions;
32 import org.onap.so.apihandlerinfra.exceptions.ApiException;
33 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
34 import org.onap.so.listener.ListenerRunner;
35 import org.onap.so.serviceinstancebeans.ServiceInstancesRequest;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38 import org.springframework.stereotype.Component;
39
40 @Component
41 public class RequestValidatorListenerRunner extends ListenerRunner {
42
43     private static Logger logger = LoggerFactory.getLogger(RequestValidatorListenerRunner.class);
44
45     protected List<RequestValidator> requestValidators;
46
47     @PostConstruct
48     protected void init() {
49         requestValidators = new ArrayList<>(
50                 Optional.ofNullable(context.getBeansOfType(RequestValidator.class)).orElse(new HashMap<>()).values());
51     }
52
53     public boolean runValidations(String requestURI, Map<String, String> instanceIdMap, ServiceInstancesRequest request,
54             Map<String, String> queryParams, Actions action) throws ApiException {
55         logger.info("Running local validations");
56         List<Pair<String, Optional<String>>> results =
57                 runValidations(requestValidators, instanceIdMap, request, queryParams, requestURI, action);
58         if (!results.isEmpty()) {
59             throw new ValidateException("Failed Validations:\n"
60                     + results.stream().map(item -> String.format("%s: %s", item.getValue0(), item.getValue1().get()))
61                             .collect(Collectors.joining("\n")),
62                     400);
63         }
64
65         return true;
66     }
67
68     protected List<Pair<String, Optional<String>>> runValidations(List<? extends RequestValidator> validators,
69             Map<String, String> instanceIdMap, ServiceInstancesRequest request, Map<String, String> queryParams,
70             String requestURI, Actions action) {
71
72         List<? extends RequestValidator> filtered =
73                 filterListeners(validators, (item -> item.shouldRunFor(requestURI, request, action)));
74
75         List<Pair<String, Optional<String>>> results = new ArrayList<>();
76         filtered.forEach(item -> results
77                 .add(new Pair<>(item.getClass().getName(), item.validate(instanceIdMap, request, queryParams))));
78
79         return results.stream().filter(item -> item.getValue1().isPresent()).collect(Collectors.toList());
80     }
81 }