Implement Attributes/Outputs BE (part 1)
[sdc.git] / catalog-be / src / main / java / org / openecomp / sdc / be / servlets / OutputsServlet.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2021, Nordix Foundation. 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.v3.oas.annotations.Operation;
26 import io.swagger.v3.oas.annotations.media.ArraySchema;
27 import io.swagger.v3.oas.annotations.media.Content;
28 import io.swagger.v3.oas.annotations.media.Schema;
29 import io.swagger.v3.oas.annotations.responses.ApiResponse;
30 import io.swagger.v3.oas.annotations.servers.Server;
31 import io.swagger.v3.oas.annotations.tags.Tag;
32 import java.io.IOException;
33 import java.util.List;
34 import javax.inject.Inject;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.ws.rs.Consumes;
37 import javax.ws.rs.GET;
38 import javax.ws.rs.HeaderParam;
39 import javax.ws.rs.Path;
40 import javax.ws.rs.PathParam;
41 import javax.ws.rs.Produces;
42 import javax.ws.rs.core.Context;
43 import javax.ws.rs.core.MediaType;
44 import javax.ws.rs.core.Response;
45 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
46 import org.openecomp.sdc.be.components.impl.OutputsBusinessLogic;
47 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
48 import org.openecomp.sdc.be.config.BeEcompErrorManager;
49 import org.openecomp.sdc.be.dao.api.ActionStatus;
50 import org.openecomp.sdc.be.impl.ComponentsUtils;
51 import org.openecomp.sdc.be.impl.ServletUtils;
52 import org.openecomp.sdc.be.model.ComponentInstanceOutput;
53 import org.openecomp.sdc.be.model.Resource;
54 import org.openecomp.sdc.be.user.UserBusinessLogic;
55 import org.openecomp.sdc.common.api.Constants;
56 import org.openecomp.sdc.common.log.wrappers.Logger;
57 import org.openecomp.sdc.exception.ResponseFormat;
58 import org.springframework.stereotype.Controller;
59
60 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
61 @Tag(name = "SDC Internal APIs")
62 @Server(url = "/sdc2/rest")
63 @Path("/v1/catalog")
64 @Controller
65 @Consumes(MediaType.APPLICATION_JSON)
66 @Produces(MediaType.APPLICATION_JSON)
67 public class OutputsServlet extends AbstractValidationsServlet {
68
69     private static final Logger log = Logger.getLogger(OutputsServlet.class);
70     private static final String START_HANDLE_REQUEST_OF = "(get) Start handle request of {}";
71
72     private final OutputsBusinessLogic outputsBusinessLogic;
73
74     @Inject
75     public OutputsServlet(final UserBusinessLogic userBusinessLogic,
76                           final OutputsBusinessLogic outputsBusinessLogic,
77                           final ComponentInstanceBusinessLogic componentInstanceBL,
78                           final ComponentsUtils componentsUtils,
79                           final ServletUtils servletUtils,
80                           final ResourceImportManager resourceImportManager) {
81         super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
82         this.outputsBusinessLogic = outputsBusinessLogic;
83     }
84
85     @GET
86     @Path("/{componentType}/{componentId}/componentInstances/{instanceId}/{originComponentUid}/outputs")
87     @Operation(description = "Get Outputs only", method = "GET", summary = "Returns Outputs list", responses = {
88         @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
89         @ApiResponse(responseCode = "200", description = "Component found"),
90         @ApiResponse(responseCode = "403", description = "Restricted operation"),
91         @ApiResponse(responseCode = "404", description = "Component not found")})
92     public Response getComponentInstanceOutputs(@PathParam("componentType") final String componentType,
93                                                 @PathParam("componentId") final String componentId,
94                                                 @PathParam("instanceId") final String instanceId,
95                                                 @PathParam("originComponentUid") final String originComponentUid,
96                                                 @Context final HttpServletRequest request,
97                                                 @HeaderParam(value = Constants.USER_ID_HEADER) final String userId) throws IOException {
98
99         final String url = request.getMethod() + " " + request.getRequestURI();
100         log.debug(START_HANDLE_REQUEST_OF, url);
101
102         try {
103             final Either<List<ComponentInstanceOutput>, ResponseFormat> outputsResponse =
104                 outputsBusinessLogic.getComponentInstanceOutputs(userId, componentId, instanceId);
105             if (outputsResponse.isRight()) {
106                 log.debug("failed to get component instance outputs {}", componentType);
107                 return buildErrorResponse(outputsResponse.right().value());
108             }
109             final Object outputs = RepresentationUtils.toRepresentation(outputsResponse.left().value());
110             return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), outputs);
111
112         } catch (final Exception e) {
113             BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Outputs " + componentType);
114             log.debug("getOutputs failed with exception", e);
115             throw e;
116         }
117     }
118
119 }