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.components.impl.aaf.AafPermission;
53 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
54 import org.openecomp.sdc.be.config.BeEcompErrorManager;
55 import org.openecomp.sdc.be.dao.api.ActionStatus;
56 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
57 import org.openecomp.sdc.be.impl.ComponentsUtils;
58 import org.openecomp.sdc.be.impl.ServletUtils;
59 import org.openecomp.sdc.be.info.GroupDefinitionInfo;
60 import org.openecomp.sdc.be.model.GroupDefinition;
61 import org.openecomp.sdc.be.model.Resource;
62 import org.openecomp.sdc.be.model.User;
63 import org.openecomp.sdc.common.api.Constants;
64 import org.openecomp.sdc.common.log.wrappers.Logger;
65 import org.openecomp.sdc.exception.ResponseFormat;
66 import org.springframework.stereotype.Controller;
69 * Root resource (exposed at "/" path)
71 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
72 @Consumes(MediaType.APPLICATION_JSON)
73 @Produces(MediaType.APPLICATION_JSON)
75 @Tags({@Tag(name = "SDCE-2 APIs")})
76 @Servers({@Server(url = "/sdc2/rest")})
78 public class GroupServlet extends AbstractValidationsServlet {
80 public static final String START_HANDLE_REQUEST = "Start handle request of {}";
81 private static final Logger log = Logger.getLogger(GroupServlet.class);
82 private final GroupBusinessLogic groupBL;
85 public GroupServlet(GroupBusinessLogic groupBL, ComponentInstanceBusinessLogic componentInstanceBL,
86 ComponentsUtils componentsUtils, ServletUtils servletUtils, ResourceImportManager resourceImportManager) {
87 super(componentInstanceBL, componentsUtils, servletUtils, resourceImportManager);
88 this.groupBL = groupBL;
92 @Path("/{containerComponentType}/{componentId}/groups/{groupType}")
93 @Operation(description = "Create group ", method = "POST", summary = "Creates new group in component and returns it", responses = {
94 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = GroupDefinition.class)))),
95 @ApiResponse(responseCode = "201", description = "Group created"),
96 @ApiResponse(responseCode = "400", description = "field name invalid type/length, characters; mandatory field is absent, already exists (name)"),
97 @ApiResponse(responseCode = "403", description = "Restricted operation"),
98 @ApiResponse(responseCode = "404", description = "Component not found"), @ApiResponse(responseCode = "500", description = "Internal Error")})
99 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
100 public Response createGroup(@PathParam("containerComponentType") final String containerComponentType,
101 @PathParam("componentId") final String componentId, @PathParam("groupType") final String type,
102 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
103 String url = request.getMethod() + " " + request.getRequestURI();
104 log.debug("(post) Start handle request of {}", url);
105 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
106 GroupDefinition groupDefinition = groupBL.createGroup(componentId, componentTypeEnum, type, userId);
107 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED), groupDefinition);
111 @Path("/{containerComponentType}/{componentId}/groups/{groupId}")
112 @Operation(description = "Get group artifacts ", method = "GET", summary = "Returns artifacts metadata according to groupId", responses = {
113 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
114 @ApiResponse(responseCode = "200", description = "group found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
115 @ApiResponse(responseCode = "404", description = "Group not found")})
116 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
117 public Response getGroupById(@PathParam("containerComponentType") final String containerComponentType,
118 @PathParam("componentId") final String componentId, @PathParam("groupId") final String groupId,
119 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
120 String url = request.getMethod() + " " + request.getRequestURI();
121 log.debug("(get) Start handle request of {}", url);
123 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
124 Either<GroupDefinitionInfo, ResponseFormat> actionResponse = groupBL
125 .getGroupWithArtifactsById(componentTypeEnum, componentId, groupId, userId, false);
126 if (actionResponse.isRight()) {
127 log.debug("failed to get all non abstract {}", containerComponentType);
128 return buildErrorResponse(actionResponse.right().value());
130 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), actionResponse.left().value());
131 } catch (Exception e) {
132 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
133 log.debug("getGroupArtifactById unexpected exception", e);
139 @Path("/{containerComponentType}/{componentId}/groups/{groupUniqueId}")
140 @Operation(description = "Delete Group", method = "DELETE", summary = "Returns deleted group id", responses = {
141 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
142 @ApiResponse(responseCode = "201", description = "ResourceInstance deleted"),
143 @ApiResponse(responseCode = "400", description = "field name invalid type/length, characters; mandatory field is absent, already exists (name)"),
144 @ApiResponse(responseCode = "403", description = "Restricted operation"),
145 @ApiResponse(responseCode = "404", description = "Component not found"), @ApiResponse(responseCode = "500", description = "Internal Error")})
146 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
147 public Response deleteGroup(@PathParam("containerComponentType") final String containerComponentType,
148 @PathParam("componentId") final String componentId, @PathParam("groupUniqueId") final String groupId,
149 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
150 String url = request.getMethod() + " " + request.getRequestURI();
151 log.debug(START_HANDLE_REQUEST, url);
152 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
153 GroupDefinition groupDefinition = groupBL.deleteGroup(componentId, componentTypeEnum, groupId, userId);
154 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), groupDefinition.getUniqueId());
158 @Path("/{containerComponentType}/{componentId}/groups/{groupId}")
159 @Operation(description = "Update Group metadata", method = "PUT", summary = "Returns updated Group", responses = {
160 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Response.class)))),
161 @ApiResponse(responseCode = "200", description = "Group updated"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
162 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content"),
163 @ApiResponse(responseCode = "404", description = "component / group Not found")})
164 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
165 public Response updateGroup(@PathParam("containerComponentType") final String containerComponentType,
166 @PathParam("componentId") final String componentId, @PathParam("groupId") final String groupId,
167 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
168 @Parameter(description = "GroupDefinition", required = true) GroupDefinition groupData,
169 @Context final HttpServletRequest request) {
170 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
171 GroupDefinition updatedGroup = groupBL.updateGroup(componentId, componentTypeEnum, groupId, userId, groupData);
172 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), updatedGroup);
176 @Path("/{containerComponentType}/{componentId}/groups/{groupUniqueId}/metadata")
177 @Consumes(MediaType.APPLICATION_JSON)
178 @Produces(MediaType.APPLICATION_JSON)
179 @Operation(description = "Update Group Metadata", method = "PUT", summary = "Returns updated group definition", responses = {
180 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = GroupDefinition.class)))),
181 @ApiResponse(responseCode = "200", description = "Group Updated"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
182 @ApiResponse(responseCode = "400", description = "Invalid content / Missing content")})
183 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
184 public Response updateGroupMetadata(@PathParam("containerComponentType") final String containerComponentType,
185 @PathParam("componentId") final String componentId, @PathParam("groupUniqueId") final String groupUniqueId,
186 @Parameter(description = "Service object to be Updated", required = true) String data,
187 @Context final HttpServletRequest request, @HeaderParam(value = Constants.USER_ID_HEADER) String userId)
189 String url = request.getMethod() + " " + request.getRequestURI();
190 log.debug(START_HANDLE_REQUEST, url);
191 User user = new User();
192 user.setUserId(userId);
193 log.debug("modifier id is {}", userId);
195 Either<GroupDefinition, ResponseFormat> convertResponse = parseToObject(data, () -> GroupDefinition.class);
196 if (convertResponse.isRight()) {
197 log.debug("failed to parse group");
198 return buildErrorResponse(convertResponse.right().value());
200 GroupDefinition updatedGroup = convertResponse.left().value();
201 // Update GroupDefinition
202 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
203 Either<GroupDefinition, ResponseFormat> actionResponse = groupBL
204 .validateAndUpdateGroupMetadata(componentId, user, componentTypeEnum, updatedGroup, true, true);
205 if (actionResponse.isRight()) {
206 log.debug("failed to update GroupDefinition");
207 return buildErrorResponse(actionResponse.right().value());
209 GroupDefinition group = actionResponse.left().value();
210 Object result = RepresentationUtils.toRepresentation(group);
211 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
212 } catch (Exception e) {
213 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Group Metadata");
214 log.debug("update group metadata failed with exception", e);