catalog-be servlets refactoring
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / DistributionServiceServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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.openecomp.sdc.be.servlets;
22
23 import com.jcabi.aspects.Loggable;
24 import fj.data.Either;
25 import io.swagger.annotations.Api;
26 import io.swagger.annotations.ApiOperation;
27 import io.swagger.annotations.ApiResponse;
28 import io.swagger.annotations.ApiResponses;
29 import javax.inject.Inject;
30 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
31 import org.openecomp.sdc.be.components.impl.DistributionMonitoringBusinessLogic;
32 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
33 import org.openecomp.sdc.be.config.BeEcompErrorManager;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.impl.ComponentsUtils;
36 import org.openecomp.sdc.be.info.DistributionStatusListResponse;
37 import org.openecomp.sdc.be.info.DistributionStatusOfServiceListResponce;
38 import org.openecomp.sdc.be.user.UserBusinessLogic;
39 import org.openecomp.sdc.common.api.Constants;
40 import org.openecomp.sdc.common.log.wrappers.Logger;
41 import org.openecomp.sdc.exception.ResponseFormat;
42
43 import javax.inject.Singleton;
44 import javax.servlet.http.HttpServletRequest;
45 import javax.ws.rs.*;
46 import javax.ws.rs.core.Context;
47 import javax.ws.rs.core.MediaType;
48 import javax.ws.rs.core.Response;
49
50 /**
51  * Root resource (exposed at "/" path)
52  */
53 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
54 @Path("/v1/catalog")
55 @Api(value = "Distribution Service Servlet", description = "Distribution Service Servlet")
56 @Singleton
57 public class DistributionServiceServlet extends BeGenericServlet {
58     private static final Logger log = Logger.getLogger(DistributionServiceServlet.class);
59
60     @Inject
61     public DistributionServiceServlet(UserBusinessLogic userBusinessLogic,
62         ComponentsUtils componentsUtils,
63         DistributionMonitoringBusinessLogic distributionMonitoringLogic) {
64         super(userBusinessLogic, componentsUtils);
65         this.distributionMonitoringLogic = distributionMonitoringLogic;
66     }
67
68     private DistributionMonitoringBusinessLogic distributionMonitoringLogic;
69
70     @GET
71     @Path("/services/{serviceUUID}/distribution")
72     @Consumes(MediaType.APPLICATION_JSON)
73     @Produces(MediaType.APPLICATION_JSON)
74     @ApiOperation(value = "Retrieve Distributions", httpMethod = "GET", notes = "Returns list  bases on the  information extracted from  Auditing Records according to service uuid", response = DistributionStatusListResponse.class)
75     @ApiResponses(value = { @ApiResponse(code = 200, message = "Service found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Service not found") })
76     public Response getServiceById(@PathParam("serviceUUID") final String serviceUUID, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
77
78         String url = request.getMethod() + " " + request.getRequestURI();
79         log.debug("Start handle request of {}", url);
80         Response response = null;
81         ResponseFormat responseFormat = null;
82
83         try {
84             Either<DistributionStatusOfServiceListResponce, ResponseFormat> actionResponse = distributionMonitoringLogic.getListOfDistributionServiceStatus(serviceUUID, userId);
85
86             if (actionResponse.isRight()) {
87
88                 responseFormat = actionResponse.right().value();
89                 response = buildErrorResponse(responseFormat);
90             } else {
91                 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
92                 response = buildOkResponse(responseFormat, actionResponse.left().value());
93
94             }
95
96             return response;
97
98         } catch (Exception e) {
99             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Distribution list for Service");
100             log.debug("failed to get service distribution statuses", e);
101             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
102
103             response = buildErrorResponse(responseFormat);
104             return response;
105         }
106
107     }
108
109     @GET
110     @Path("/services/distribution/{did}")
111     @Consumes(MediaType.APPLICATION_JSON)
112     @Produces(MediaType.APPLICATION_JSON)
113     @ApiOperation(value = "Retrieve Distributions", httpMethod = "GET", notes = "Return  the  list  of  distribution status objects", response = DistributionStatusListResponse.class)
114     @ApiResponses(value = { @ApiResponse(code = 200, message = "Service found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Status not found") })
115     public Response getListOfDistributionStatuses(@PathParam("did") final String did, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
116
117         String url = request.getMethod() + " " + request.getRequestURI();
118         log.debug("Start handle request of {}", url);
119         Response response = null;
120         ResponseFormat responseFormat = null;
121
122         try {
123             Either<DistributionStatusListResponse, ResponseFormat> actionResponse = distributionMonitoringLogic.getListOfDistributionStatus(did, userId);
124
125             if (actionResponse.isRight()) {
126
127                 responseFormat = actionResponse.right().value();
128                 log.debug("failed to fount statuses for did {} {}", did, responseFormat);
129                 response = buildErrorResponse(responseFormat);
130             } else {
131
132                 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
133                 log.debug("success to fount statuses for did {} {}", did, actionResponse.left().value());
134                 response = buildOkResponse(responseFormat, actionResponse.left().value());
135
136             }
137
138             return response;
139
140         } catch (Exception e) {
141             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Distribution Status");
142             log.debug("failed to get distribution status ", e);
143             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
144
145             response = buildErrorResponse(responseFormat);
146             return response;
147         }
148
149     }
150
151 }