96169f4dd0123a6a4b68f5fcfadbab5cb815564a
[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 package org.openecomp.sdc.be.servlets;
21
22 import com.jcabi.aspects.Loggable;
23 import fj.data.Either;
24 import io.swagger.v3.oas.annotations.Operation;
25 import io.swagger.v3.oas.annotations.media.ArraySchema;
26 import io.swagger.v3.oas.annotations.media.Content;
27 import io.swagger.v3.oas.annotations.media.Schema;
28 import io.swagger.v3.oas.annotations.responses.ApiResponse;
29 import io.swagger.v3.oas.annotations.servers.Server;
30 import io.swagger.v3.oas.annotations.servers.Servers;
31 import io.swagger.v3.oas.annotations.tags.Tag;
32 import io.swagger.v3.oas.annotations.tags.Tags;
33 import javax.inject.Inject;
34 import javax.servlet.http.HttpServletRequest;
35 import javax.ws.rs.Consumes;
36 import javax.ws.rs.GET;
37 import javax.ws.rs.HeaderParam;
38 import javax.ws.rs.Path;
39 import javax.ws.rs.PathParam;
40 import javax.ws.rs.Produces;
41 import javax.ws.rs.core.Context;
42 import javax.ws.rs.core.MediaType;
43 import javax.ws.rs.core.Response;
44 import org.openecomp.sdc.be.components.impl.DistributionMonitoringBusinessLogic;
45 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
46 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
47 import org.openecomp.sdc.be.config.BeEcompErrorManager;
48 import org.openecomp.sdc.be.dao.api.ActionStatus;
49 import org.openecomp.sdc.be.impl.ComponentsUtils;
50 import org.openecomp.sdc.be.info.DistributionStatusListResponse;
51 import org.openecomp.sdc.be.info.DistributionStatusOfServiceListResponce;
52 import org.openecomp.sdc.common.api.Constants;
53 import org.openecomp.sdc.common.log.wrappers.Logger;
54 import org.openecomp.sdc.exception.ResponseFormat;
55 import org.springframework.stereotype.Controller;
56
57 /**
58  * Root resource (exposed at "/" path)
59  */
60 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
61 @Path("/v1/catalog")
62 @Tags({@Tag(name = "SDCE-5 APIs")})
63 @Servers({@Server(url = "/sdc2/rest")})
64 @Controller
65 public class DistributionServiceServlet extends BeGenericServlet {
66
67     private static final Logger log = Logger.getLogger(DistributionServiceServlet.class);
68     private DistributionMonitoringBusinessLogic distributionMonitoringLogic;
69
70     @Inject
71     public DistributionServiceServlet(ComponentsUtils componentsUtils,
72                                       DistributionMonitoringBusinessLogic distributionMonitoringLogic) {
73         super(componentsUtils);
74         this.distributionMonitoringLogic = distributionMonitoringLogic;
75     }
76
77     @GET
78     @Path("/services/{serviceUUID}/distribution")
79     @Consumes(MediaType.APPLICATION_JSON)
80     @Produces(MediaType.APPLICATION_JSON)
81     @Operation(description = "Retrieve Distributions", method = "GET", summary = "Returns list  bases on the  information extracted from  Auditing Records according to service uuid", responses = {
82         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = DistributionStatusListResponse.class)))),
83         @ApiResponse(responseCode = "200", description = "Service found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
84         @ApiResponse(responseCode = "404", description = "Service not found")})
85     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
86     public Response getServiceById(@PathParam("serviceUUID") final String serviceUUID, @Context final HttpServletRequest request,
87                                    @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
88         String url = request.getMethod() + " " + request.getRequestURI();
89         log.debug("Start handle request of {}", url);
90         try {
91             Either<DistributionStatusOfServiceListResponce, ResponseFormat> actionResponse = distributionMonitoringLogic
92                 .getListOfDistributionServiceStatus(serviceUUID, userId);
93             if (actionResponse.isRight()) {
94                 return buildErrorResponse(actionResponse.right().value());
95             } else {
96                 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
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             throw e;
102         }
103     }
104
105     @GET
106     @Path("/services/distribution/{did}")
107     @Consumes(MediaType.APPLICATION_JSON)
108     @Produces(MediaType.APPLICATION_JSON)
109     @Operation(description = "Retrieve Distributions", method = "GET", summary = "Return  the  list  of  distribution status objects", responses = {
110         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = DistributionStatusListResponse.class)))),
111         @ApiResponse(responseCode = "200", description = "Service found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
112         @ApiResponse(responseCode = "404", description = "Status not found")})
113     @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
114     public Response getListOfDistributionStatuses(@PathParam("did") final String did, @Context final HttpServletRequest request,
115                                                   @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
116         String url = request.getMethod() + " " + request.getRequestURI();
117         log.debug("Start handle request of {}", url);
118         try {
119             Either<DistributionStatusListResponse, ResponseFormat> actionResponse = distributionMonitoringLogic
120                 .getListOfDistributionStatus(did, userId);
121             if (actionResponse.isRight()) {
122                 ResponseFormat responseFormat = actionResponse.right().value();
123                 log.debug("failed to fount statuses for did {} {}", did, responseFormat);
124                 return buildErrorResponse(responseFormat);
125             } else {
126                 ResponseFormat responseFormat = getComponentsUtils().getResponseFormat(ActionStatus.OK);
127                 log.debug("success to fount statuses for did {} {}", did, actionResponse.left().value());
128                 return buildOkResponse(responseFormat, actionResponse.left().value());
129             }
130         } catch (Exception e) {
131             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Distribution Status");
132             log.debug("failed to get distribution status ", e);
133             throw e;
134         }
135     }
136 }