2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.openecomp.sdc.be.servlets;
\r
23 import javax.inject.Inject;
\r
24 import javax.inject.Singleton;
\r
25 import javax.servlet.http.HttpServletRequest;
\r
26 import javax.ws.rs.Consumes;
\r
27 import javax.ws.rs.DELETE;
\r
28 import javax.ws.rs.GET;
\r
29 import javax.ws.rs.HeaderParam;
\r
30 import javax.ws.rs.POST;
\r
31 import javax.ws.rs.PUT;
\r
32 import javax.ws.rs.Path;
\r
33 import javax.ws.rs.PathParam;
\r
34 import javax.ws.rs.Produces;
\r
35 import javax.ws.rs.core.Context;
\r
36 import javax.ws.rs.core.MediaType;
\r
37 import javax.ws.rs.core.Response;
\r
38 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
\r
39 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
\r
40 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
\r
41 import org.openecomp.sdc.be.config.BeEcompErrorManager;
\r
42 import org.openecomp.sdc.be.dao.api.ActionStatus;
\r
43 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
\r
44 import org.openecomp.sdc.be.impl.ComponentsUtils;
\r
45 import org.openecomp.sdc.be.impl.ServletUtils;
\r
46 import org.openecomp.sdc.be.info.GroupDefinitionInfo;
\r
47 import org.openecomp.sdc.be.model.GroupDefinition;
\r
48 import org.openecomp.sdc.be.model.Resource;
\r
49 import org.openecomp.sdc.be.model.User;
\r
50 import org.openecomp.sdc.be.user.UserBusinessLogic;
\r
51 import org.openecomp.sdc.common.api.Constants;
\r
52 import org.openecomp.sdc.common.log.wrappers.Logger;
\r
53 import org.openecomp.sdc.exception.ResponseFormat;
\r
54 import com.jcabi.aspects.Loggable;
\r
55 import fj.data.Either;
\r
56 import io.swagger.v3.oas.annotations.OpenAPIDefinition;
\r
57 import io.swagger.v3.oas.annotations.Operation;
\r
58 import io.swagger.v3.oas.annotations.Parameter;
\r
59 import io.swagger.v3.oas.annotations.info.Info;
\r
60 import io.swagger.v3.oas.annotations.media.ArraySchema;
\r
61 import io.swagger.v3.oas.annotations.media.Content;
\r
62 import io.swagger.v3.oas.annotations.media.Schema;
\r
63 import io.swagger.v3.oas.annotations.responses.ApiResponse;
\r
64 import io.swagger.v3.oas.annotations.responses.ApiResponses;
\r
67 * Root resource (exposed at "/" path)
\r
69 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
\r
70 @Consumes(MediaType.APPLICATION_JSON)
\r
71 @Produces(MediaType.APPLICATION_JSON)
\r
72 @Path("/v1/catalog")
\r
73 @OpenAPIDefinition(info = @Info(title = "Group Servlet"))
\r
75 public class GroupServlet extends AbstractValidationsServlet {
\r
77 private static final Logger log = Logger.getLogger(GroupServlet.class);
\r
78 public static final String START_HANDLE_REQUEST = "Start handle request of {}";
\r
79 private final GroupBusinessLogic groupBL;
\r
82 public GroupServlet(UserBusinessLogic userBusinessLogic, GroupBusinessLogic groupBL,
\r
83 ComponentInstanceBusinessLogic componentInstanceBL, ComponentsUtils componentsUtils,
\r
84 ServletUtils servletUtils, ResourceImportManager resourceImportManager) {
\r
85 super(userBusinessLogic, componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
\r
86 this.groupBL = groupBL;
\r
90 @Path("/{containerComponentType}/{componentId}/groups/{groupType}")
\r
91 @Operation(description = "Create group ", method = "POST",
\r
92 summary = "Creates new group in component and returns it", responses = @ApiResponse(
\r
93 content = @Content(array = @ArraySchema(schema = @Schema(implementation = GroupDefinition.class)))))
\r
94 @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "Group created"), @ApiResponse(
\r
95 responseCode = "400",
\r
96 description = "field name invalid type/length, characters; mandatory field is absent, already exists (name)"),
\r
97 @ApiResponse(responseCode = "403", description = "Restricted operation"),
\r
98 @ApiResponse(responseCode = "404", description = "Component not found"),
\r
99 @ApiResponse(responseCode = "500", description = "Internal Error")})
\r
100 public Response createGroup(@PathParam("containerComponentType") final String containerComponentType,
\r
101 @PathParam("componentId") final String componentId, @PathParam("groupType") final String type,
\r
102 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
\r
103 String url = request.getMethod() + " " + request.getRequestURI();
\r
104 log.debug("(post) Start handle request of {}", url);
\r
106 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
\r
107 GroupDefinition groupDefinition = groupBL.createGroup(componentId, componentTypeEnum, type, userId);
\r
109 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), groupDefinition);
\r
113 @Path("/{containerComponentType}/{componentId}/groups/{groupId}")
\r
114 @Operation(description = "Get group artifacts ", method = "GET",
\r
115 summary = "Returns artifacts metadata according to groupId", responses = @ApiResponse(
\r
116 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))))
\r
117 @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "group found"),
\r
118 @ApiResponse(responseCode = "403", description = "Restricted operation"),
\r
119 @ApiResponse(responseCode = "404", description = "Group not found")})
\r
120 public Response getGroupById(@PathParam("containerComponentType") final String containerComponentType,
\r
121 @PathParam("componentId") final String componentId, @PathParam("groupId") final String groupId,
\r
122 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
\r
123 String url = request.getMethod() + " " + request.getRequestURI();
\r
124 log.debug("(get) Start handle request of {}", url);
\r
128 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
\r
129 Either<GroupDefinitionInfo, ResponseFormat> actionResponse =
\r
130 groupBL.getGroupWithArtifactsById(componentTypeEnum, componentId, groupId, userId, false);
\r
132 if (actionResponse.isRight()) {
\r
133 log.debug("failed to get all non abstract {}", containerComponentType);
\r
134 return buildErrorResponse(actionResponse.right().value());
\r
137 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
\r
138 actionResponse.left().value());
\r
140 } catch (Exception e) {
\r
141 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
\r
142 log.debug("getGroupArtifactById unexpected exception", e);
\r
143 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
\r
149 @Path("/{containerComponentType}/{componentId}/groups/{groupUniqueId}")
\r
150 @Operation(description = "Delete Group", method = "DELETE", summary = "Returns deleted group id",
\r
151 responses = @ApiResponse(
\r
152 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
\r
153 @ApiResponses(value = {@ApiResponse(responseCode = "201", description = "ResourceInstance deleted"), @ApiResponse(
\r
154 responseCode = "400",
\r
155 description = "field name invalid type/length, characters; mandatory field is absent, already exists (name)"),
\r
156 @ApiResponse(responseCode = "403", description = "Restricted operation"),
\r
157 @ApiResponse(responseCode = "404", description = "Component not found"),
\r
158 @ApiResponse(responseCode = "500", description = "Internal Error")})
\r
159 public Response deleteGroup(@PathParam("containerComponentType") final String containerComponentType,
\r
160 @PathParam("componentId") final String componentId, @PathParam("groupUniqueId") final String groupId,
\r
161 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
\r
162 String url = request.getMethod() + " " + request.getRequestURI();
\r
163 log.debug(START_HANDLE_REQUEST, url);
\r
164 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
\r
165 GroupDefinition groupDefinition = groupBL.deleteGroup(componentId, componentTypeEnum, groupId, userId);
\r
167 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT),
\r
168 groupDefinition.getUniqueId());
\r
172 @Path("/{containerComponentType}/{componentId}/groups/{groupId}")
\r
173 @Operation(description = "Update Group metadata", method = "PUT", summary = "Returns updated Group",
\r
174 responses = @ApiResponse(
\r
175 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))))
\r
176 @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Group updated"),
\r
177 @ApiResponse(responseCode = "403", description = "Restricted operation"),
\r
178 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
\r
179 @ApiResponse(responseCode = "404", description = "component / group Not found")})
\r
180 public Response updateGroup(@PathParam("containerComponentType") final String containerComponentType,
\r
181 @PathParam("componentId") final String componentId, @PathParam("groupId") final String groupId,
\r
182 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
\r
183 @Parameter(description = "GroupDefinition", required = true) GroupDefinition groupData,
\r
184 @Context final HttpServletRequest request) {
\r
185 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
\r
186 GroupDefinition updatedGroup = groupBL.updateGroup(componentId, componentTypeEnum, groupId, userId, groupData);
\r
187 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), updatedGroup);
\r
191 @Path("/{containerComponentType}/{componentId}/groups/{groupUniqueId}/metadata")
\r
192 @Consumes(MediaType.APPLICATION_JSON)
\r
193 @Produces(MediaType.APPLICATION_JSON)
\r
194 @Operation(description = "Update Group Metadata", method = "PUT", summary = "Returns updated group definition",
\r
195 responses = @ApiResponse(
\r
196 content = @Content(array = @ArraySchema(schema = @Schema(implementation = GroupDefinition.class)))))
\r
197 @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "Group Updated"),
\r
198 @ApiResponse(responseCode = "403", description = "Restricted operation"),
\r
199 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
\r
200 public Response updateGroupMetadata(@PathParam("containerComponentType") final String containerComponentType,
\r
201 @PathParam("componentId") final String componentId, @PathParam("groupUniqueId") final String groupUniqueId,
\r
202 @Parameter(description = "Service object to be Updated", required = true) String data,
\r
203 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
\r
205 String url = request.getMethod() + " " + request.getRequestURI();
\r
206 log.debug(START_HANDLE_REQUEST, url);
\r
208 User user = new User();
\r
209 user.setUserId(userId);
\r
210 log.debug("modifier id is {}", userId);
\r
212 Response response = null;
\r
215 Either<GroupDefinition, ResponseFormat> convertResponse = parseToObject(data, () -> GroupDefinition.class);
\r
216 if (convertResponse.isRight()) {
\r
217 log.debug("failed to parse group");
\r
218 response = buildErrorResponse(convertResponse.right().value());
\r
221 GroupDefinition updatedGroup = convertResponse.left().value();
\r
223 // Update GroupDefinition
\r
224 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
\r
225 Either<GroupDefinition, ResponseFormat> actionResponse = groupBL.validateAndUpdateGroupMetadata(componentId,
\r
226 user, componentTypeEnum, updatedGroup, true, true);
\r
228 if (actionResponse.isRight()) {
\r
229 log.debug("failed to update GroupDefinition");
\r
230 response = buildErrorResponse(actionResponse.right().value());
\r
234 GroupDefinition group = actionResponse.left().value();
\r
235 Object result = RepresentationUtils.toRepresentation(group);
\r
236 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
\r
238 } catch (Exception e) {
\r
239 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Group Metadata");
\r
240 log.debug("update group metadata failed with exception", e);
\r
241 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
\r