Sync Integ to Master
[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 org.openecomp.sdc.be.components.impl.DistributionMonitoringBusinessLogic;
30 import org.openecomp.sdc.be.config.BeEcompErrorManager;
31 import org.openecomp.sdc.be.dao.api.ActionStatus;
32 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
33 import org.openecomp.sdc.be.info.DistributionStatusListResponse;
34 import org.openecomp.sdc.be.info.DistributionStatusOfServiceListResponce;
35 import org.openecomp.sdc.common.api.Constants;
36 import org.openecomp.sdc.common.config.EcompErrorName;
37 import org.openecomp.sdc.exception.ResponseFormat;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40 import org.springframework.web.context.WebApplicationContext;
41
42 import javax.annotation.Resource;
43 import javax.inject.Singleton;
44 import javax.servlet.ServletContext;
45 import javax.servlet.http.HttpServletRequest;
46 import javax.ws.rs.*;
47 import javax.ws.rs.core.Context;
48 import javax.ws.rs.core.MediaType;
49 import javax.ws.rs.core.Response;
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 = LoggerFactory.getLogger(DistributionServiceServlet.class);
59
60     @Resource
61     private DistributionMonitoringBusinessLogic distributionMonitoringLogic;
62
63     @GET
64     @Path("/services/{serviceUUID}/distribution")
65     @Consumes(MediaType.APPLICATION_JSON)
66     @Produces(MediaType.APPLICATION_JSON)
67     @ApiOperation(value = "Retrieve Distributions", httpMethod = "GET", notes = "Returns list  bases on the  information extracted from  Auditing Records according to service uuid", response = DistributionStatusListResponse.class)
68     @ApiResponses(value = { @ApiResponse(code = 200, message = "Service found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Service not found") })
69     public Response getServiceById(@PathParam("serviceUUID") final String serviceUUID, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
70
71         init(request);
72         String url = request.getMethod() + " " + request.getRequestURI();
73         log.debug("Start handle request of {}", url);
74         Response response = null;
75         ResponseFormat responseFormat = null;
76
77         try {
78             Either<DistributionStatusOfServiceListResponce, ResponseFormat> actionResponse = distributionMonitoringLogic.getListOfDistributionServiceStatus(serviceUUID, userId);
79
80             if (actionResponse.isRight()) {
81
82                 responseFormat = actionResponse.right().value();
83                 response = buildErrorResponse(responseFormat);
84             } else {
85                 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
86                 response = buildOkResponse(responseFormat, actionResponse.left().value());
87
88             }
89
90             return response;
91
92         } catch (Exception e) {
93             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Distribution list for Service");
94             log.debug("failed to get service distribution statuses", e);
95             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
96
97             response = buildErrorResponse(responseFormat);
98             return response;
99         }
100
101     }
102
103     @GET
104     @Path("/services/distribution/{did}")
105     @Consumes(MediaType.APPLICATION_JSON)
106     @Produces(MediaType.APPLICATION_JSON)
107     @ApiOperation(value = "Retrieve Distributions", httpMethod = "GET", notes = "Return  the  list  of  distribution status objects", response = DistributionStatusListResponse.class)
108     @ApiResponses(value = { @ApiResponse(code = 200, message = "Service found"), @ApiResponse(code = 403, message = "Restricted operation"), @ApiResponse(code = 404, message = "Status not found") })
109     public Response getListOfDistributionStatuses(@PathParam("did") final String did, @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
110
111         init(request);
112         String url = request.getMethod() + " " + request.getRequestURI();
113         log.debug("Start handle request of {}", url);
114         Response response = null;
115         ResponseFormat responseFormat = null;
116
117         try {
118             Either<DistributionStatusListResponse, ResponseFormat> actionResponse = distributionMonitoringLogic.getListOfDistributionStatus(did, userId);
119
120             if (actionResponse.isRight()) {
121
122                 responseFormat = actionResponse.right().value();
123                 log.debug("failed to fount statuses for did {} {}", did, responseFormat);
124                 response = buildErrorResponse(responseFormat);
125             } else {
126
127                 responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
128                 log.debug("success to fount statuses for did {} {}", did, actionResponse.left().value());
129                 response = buildOkResponse(responseFormat, actionResponse.left().value());
130
131             }
132
133             return response;
134
135         } catch (Exception e) {
136             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Distribution Status");
137             log.debug("failed to get distribution status ", e);
138             responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR);
139
140             response = buildErrorResponse(responseFormat);
141             return response;
142         }
143
144     }
145
146     private void init(HttpServletRequest request) {
147         if (distributionMonitoringLogic == null) {
148             distributionMonitoringLogic = getDistributionBL(request.getSession().getServletContext());
149         }
150     }
151
152     private DistributionMonitoringBusinessLogic getDistributionBL(ServletContext context) {
153         WebAppContextWrapper webApplicationContextWrapper = (WebAppContextWrapper) context.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR);
154         WebApplicationContext webApplicationContext = webApplicationContextWrapper.getWebAppContext(context);
155         DistributionMonitoringBusinessLogic distributionBl = webApplicationContext.getBean(DistributionMonitoringBusinessLogic.class);
156         return distributionBl;
157     }
158
159 }