VfModule and VolumeGroup RequestParameters: introduce objects hierarchy
[vid.git] / vid-app-common / src / main / java / org / onap / vid / controller / ErrorReportController.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2019 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.vid.controller;
22
23 import org.jetbrains.annotations.NotNull;
24 import org.onap.portalsdk.core.controller.RestrictedBaseController;
25 import org.onap.vid.model.errorReport.ReportCreationParameters;
26 import org.onap.vid.reports.ReportGenerator;
27 import org.springframework.beans.factory.annotation.Autowired;
28 import org.springframework.web.bind.annotation.PostMapping;
29 import org.springframework.web.bind.annotation.RequestBody;
30 import org.springframework.web.bind.annotation.RestController;
31
32 import javax.servlet.http.HttpServletRequest;
33 import java.util.Collection;
34 import java.util.HashMap;
35 import java.util.List;
36 import java.util.Map;
37 import java.util.function.BinaryOperator;
38
39 @RestController
40 public class ErrorReportController extends RestrictedBaseController {
41
42         private final List<ReportGenerator> providers;
43
44         @Autowired
45         public ErrorReportController(List<ReportGenerator> reportGenerators) {
46                 providers = reportGenerators;
47         }
48
49         @PostMapping(value = "error-report")
50         public Map<String, Object> getErrorReport(HttpServletRequest request,
51                                                   @RequestBody ReportCreationParameters creationParameters) {
52                 return generateReportsData(request, creationParameters);
53         }
54
55         @NotNull
56         HashMap<String, Object> generateReportsData(HttpServletRequest request, ReportCreationParameters creationParameters) {
57                 return providers
58                                 .stream()
59                                 .filter(provider -> provider.canGenerate(creationParameters))
60                                 .map(provider -> provider.apply(request, creationParameters))
61                                 .map(Map::entrySet)
62                                 .flatMap(Collection::parallelStream)
63                                 .reduce(new HashMap<>(), this::putAndGet, concatenateMap());
64         }
65
66         @NotNull
67         private HashMap<String, Object> putAndGet(HashMap<String, Object> map, Map.Entry<String, Object> entry) {
68                 map.put(entry.getKey(), entry.getValue());
69                 return map;
70         }
71
72         @NotNull
73         private BinaryOperator<HashMap<String, Object>> concatenateMap() {
74                 return (map1, map2) -> {
75                         map1.putAll(map2);
76                         return map1;
77                 };
78         }
79
80 }