Merge "Add controller which create error reports"
[vid.git] / vid-app-common / src / main / java / org / onap / vid / reports / DeploymentReportGenerator.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.reports;
22
23 import com.google.common.collect.ImmutableMap;
24 import org.onap.vid.asdc.AsdcCatalogException;
25 import org.onap.vid.model.errorReport.ReportCreationParameters;
26 import org.onap.vid.mso.MsoBusinessLogic;
27 import org.onap.vid.mso.MsoResponseWrapper;
28 import org.onap.vid.services.VidService;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.stereotype.Service;
31
32 import javax.servlet.http.HttpServletRequest;
33 import java.util.HashMap;
34 import java.util.Map;
35
36 @Service
37 public class DeploymentReportGenerator implements ReportGenerator {
38
39         private final VidService vidService;
40         private final MsoBusinessLogic msoBusinessLogic;
41
42         @Autowired
43         public DeploymentReportGenerator(VidService vidService, MsoBusinessLogic msoBusinessLogic) {
44                 this.vidService = vidService;
45                 this.msoBusinessLogic = msoBusinessLogic;
46         }
47
48         @Override
49         public Map<String, Object> apply(HttpServletRequest request, ReportCreationParameters creationParameters) {
50                 return ImmutableMap.<String, Object>builder()
51                                 .put("serviceInstanceInfo", getOrchestrationRequestFromMso(creationParameters.getRequestId()))
52                                 .put("serviceDetails", getServiceDetails(creationParameters.getServiceUuid()))
53                                 .build();
54         }
55
56         @Override
57         public boolean canGenerate(ReportCreationParameters creationParameters) {
58                 return creationParameters.getRequestId() != null && creationParameters.getServiceUuid() != null;
59         }
60
61         MsoResponseWrapper getOrchestrationRequestFromMso(String requestId) {
62                 return msoBusinessLogic.getOrchestrationRequest(requestId);
63         }
64
65         Map<String, Object> getServiceDetails(String serviceUuid) {
66                 Map<String, Object> serviceDetails;
67                 try {
68                         org.onap.vid.model.Service serviceModel = vidService.getService(serviceUuid).getService();
69                         serviceDetails = ImmutableMap.of("details", serviceModel);
70                 } catch (AsdcCatalogException | NullPointerException e) {
71                         serviceDetails = generateServiceDetailsExceptionResponse(serviceUuid, e);
72                 }
73                 return serviceDetails;
74         }
75
76         Map<String, Object> generateServiceDetailsExceptionResponse(String serviceUuid, Exception e) {
77                 Map<String, Object> result = new HashMap<>();
78                 if (e.getClass() == NullPointerException.class) {
79                         result.put("message", "Service details for given uuid were not found");
80                 }
81                 result.put("exception", e.toString());
82                 result.put("serviceUuid", serviceUuid);
83                 return result;
84         }
85 }