2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.openecomp.sdc.be.servlets;
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.Parameter;
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.servers.Servers;
32 import io.swagger.v3.oas.annotations.tags.Tag;
33 import io.swagger.v3.oas.annotations.tags.Tags;
34 import java.io.IOException;
35 import javax.inject.Inject;
36 import javax.servlet.http.HttpServletRequest;
37 import javax.ws.rs.Consumes;
38 import javax.ws.rs.DELETE;
39 import javax.ws.rs.GET;
40 import javax.ws.rs.HeaderParam;
41 import javax.ws.rs.POST;
42 import javax.ws.rs.PUT;
43 import javax.ws.rs.Path;
44 import javax.ws.rs.PathParam;
45 import javax.ws.rs.Produces;
46 import javax.ws.rs.core.Context;
47 import javax.ws.rs.core.MediaType;
48 import javax.ws.rs.core.Response;
49 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
50 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
51 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
52 import org.openecomp.sdc.be.config.BeEcompErrorManager;
53 import org.openecomp.sdc.be.dao.api.ActionStatus;
54 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
55 import org.openecomp.sdc.be.impl.ComponentsUtils;
56 import org.openecomp.sdc.be.impl.ServletUtils;
57 import org.openecomp.sdc.be.info.GroupDefinitionInfo;
58 import org.openecomp.sdc.be.model.GroupDefinition;
59 import org.openecomp.sdc.be.model.Resource;
60 import org.openecomp.sdc.be.model.User;
61 import org.openecomp.sdc.common.api.Constants;
62 import org.openecomp.sdc.common.log.wrappers.Logger;
63 import org.openecomp.sdc.exception.ResponseFormat;
64 import org.springframework.stereotype.Controller;
67 * Root resource (exposed at "/" path)
69 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
70 @Consumes(MediaType.APPLICATION_JSON)
71 @Produces(MediaType.APPLICATION_JSON)
73 @Tags({@Tag(name = "SDCE-2 APIs")})
74 @Servers({@Server(url = "/sdc2/rest")})
76 public class GroupServlet extends AbstractValidationsServlet {
78 public static final String START_HANDLE_REQUEST = "Start handle request of {}";
79 private static final Logger log = Logger.getLogger(GroupServlet.class);
80 private final GroupBusinessLogic groupBL;
83 public GroupServlet(GroupBusinessLogic groupBL, ComponentInstanceBusinessLogic componentInstanceBL,
84 ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager) {
85 super(componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
86 this.groupBL = groupBL;
90 @Path("/{containerComponentType}/{componentId}/groups/{groupType}")
91 @Operation(description = "Create group ", method = "POST", summary = "Creates new group in component and returns it", responses = {
92 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = GroupDefinition.class)))),
93 @ApiResponse(responseCode = "201", description = "Group created"),
94 @ApiResponse(responseCode = "400", description = "field name invalid type/length, characters; mandatory field is absent, already exists (name)"),
95 @ApiResponse(responseCode = "403", description = "Restricted operation"),
96 @ApiResponse(responseCode = "404", description = "Component not found"), @ApiResponse(responseCode = "500", description = "Internal Error")})
97 public Response createGroup(@PathParam("containerComponentType") final String containerComponentType,
98 @PathParam("componentId") final String componentId, @PathParam("groupType") final String type,
99 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
100 String url = request.getMethod() + " " + request.getRequestURI();
101 log.debug("(post) Start handle request of {}", url);
102 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
103 GroupDefinition groupDefinition = groupBL.createGroup(componentId, componentTypeEnum, type, userId);
104 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), groupDefinition);
108 @Path("/{containerComponentType}/{componentId}/groups/{groupId}")
109 @Operation(description = "Get group artifacts ", method = "GET", summary = "Returns artifacts metadata according to groupId", responses = {
110 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
111 @ApiResponse(responseCode = "200", description = "group found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
112 @ApiResponse(responseCode = "404", description = "Group not found")})
113 public Response getGroupById(@PathParam("containerComponentType") final String containerComponentType,
114 @PathParam("componentId") final String componentId, @PathParam("groupId") final String groupId,
115 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
116 String url = request.getMethod() + " " + request.getRequestURI();
117 log.debug("(get) Start handle request of {}", url);
119 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
120 Either<GroupDefinitionInfo, ResponseFormat> actionResponse = groupBL
121 .getGroupWithArtifactsById(componentTypeEnum, componentId, groupId, userId, false);
122 if (actionResponse.isRight()) {
123 log.debug("failed to get all non abstract {}", containerComponentType);
124 return buildErrorResponse(actionResponse.right().value());
126 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
127 } catch (Exception e) {
128 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
129 log.debug("getGroupArtifactById unexpected exception", e);
135 @Path("/{containerComponentType}/{componentId}/groups/{groupUniqueId}")
136 @Operation(description = "Delete Group", method = "DELETE", summary = "Returns deleted group id", responses = {
137 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
138 @ApiResponse(responseCode = "201", description = "ResourceInstance deleted"),
139 @ApiResponse(responseCode = "400", description = "field name invalid type/length, characters; mandatory field is absent, already exists (name)"),
140 @ApiResponse(responseCode = "403", description = "Restricted operation"),
141 @ApiResponse(responseCode = "404", description = "Component not found"), @ApiResponse(responseCode = "500", description = "Internal Error")})
142 public Response deleteGroup(@PathParam("containerComponentType") final String containerComponentType,
143 @PathParam("componentId") final String componentId, @PathParam("groupUniqueId") final String groupId,
144 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
145 String url = request.getMethod() + " " + request.getRequestURI();
146 log.debug(START_HANDLE_REQUEST, url);
147 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
148 GroupDefinition groupDefinition = groupBL.deleteGroup(componentId, componentTypeEnum, groupId, userId);
149 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), groupDefinition.getUniqueId());
153 @Path("/{containerComponentType}/{componentId}/groups/{groupId}")
154 @Operation(description = "Update Group metadata", method = "PUT", summary = "Returns updated Group", responses = {
155 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
156 @ApiResponse(responseCode = "200", description = "Group updated"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
157 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
158 @ApiResponse(responseCode = "404", description = "component / group Not found")})
159 public Response updateGroup(@PathParam("containerComponentType") final String containerComponentType,
160 @PathParam("componentId") final String componentId, @PathParam("groupId") final String groupId,
161 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
162 @Parameter(description = "GroupDefinition", required = true) GroupDefinition groupData,
163 @Context final HttpServletRequest request) {
164 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
165 GroupDefinition updatedGroup = groupBL.updateGroup(componentId, componentTypeEnum, groupId, userId, groupData);
166 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), updatedGroup);
170 @Path("/{containerComponentType}/{componentId}/groups/{groupUniqueId}/metadata")
171 @Consumes(MediaType.APPLICATION_JSON)
172 @Produces(MediaType.APPLICATION_JSON)
173 @Operation(description = "Update Group Metadata", method = "PUT", summary = "Returns updated group definition", responses = {
174 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = GroupDefinition.class)))),
175 @ApiResponse(responseCode = "200", description = "Group Updated"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
176 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
177 public Response updateGroupMetadata(@PathParam("containerComponentType") final String containerComponentType,
178 @PathParam("componentId") final String componentId, @PathParam("groupUniqueId") final String groupUniqueId,
179 @Parameter(description = "Service object to be Updated", required = true) String data,
180 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId)
182 String url = request.getMethod() + " " + request.getRequestURI();
183 log.debug(START_HANDLE_REQUEST, url);
184 User user = new User();
185 user.setUserId(userId);
186 log.debug("modifier id is {}", userId);
188 Either<GroupDefinition, ResponseFormat> convertResponse = parseToObject(data, () -> GroupDefinition.class);
189 if (convertResponse.isRight()) {
190 log.debug("failed to parse group");
191 return buildErrorResponse(convertResponse.right().value());
193 GroupDefinition updatedGroup = convertResponse.left().value();
194 // Update GroupDefinition
195 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
196 Either<GroupDefinition, ResponseFormat> actionResponse = groupBL
197 .validateAndUpdateGroupMetadata(componentId, user, componentTypeEnum, updatedGroup, true, true);
198 if (actionResponse.isRight()) {
199 log.debug("failed to update GroupDefinition");
200 return buildErrorResponse(actionResponse.right().value());
202 GroupDefinition group = actionResponse.left().value();
203 Object result = RepresentationUtils.toRepresentation(group);
204 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
205 } catch (Exception e) {
206 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Group Metadata");
207 log.debug("update group metadata failed with exception", e);