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=========================================================
21 package org.openecomp.sdc.be.servlets;
23 import java.util.List;
25 import javax.inject.Singleton;
26 import javax.servlet.ServletContext;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.ws.rs.Consumes;
29 import javax.ws.rs.DELETE;
30 import javax.ws.rs.GET;
31 import javax.ws.rs.HeaderParam;
32 import javax.ws.rs.POST;
33 import javax.ws.rs.PUT;
34 import javax.ws.rs.Path;
35 import javax.ws.rs.PathParam;
36 import javax.ws.rs.Produces;
37 import javax.ws.rs.core.Context;
38 import javax.ws.rs.core.MediaType;
39 import javax.ws.rs.core.Response;
41 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
42 import org.openecomp.sdc.be.config.BeEcompErrorManager;
43 import org.openecomp.sdc.be.dao.api.ActionStatus;
44 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
45 import org.openecomp.sdc.be.info.GroupDefinitionInfo;
46 import org.openecomp.sdc.be.model.GroupDefinition;
47 import org.openecomp.sdc.be.model.GroupProperty;
48 import org.openecomp.sdc.be.model.Resource;
49 import org.openecomp.sdc.be.model.User;
50 import org.openecomp.sdc.common.api.Constants;
51 import org.openecomp.sdc.exception.ResponseFormat;
52 import org.slf4j.Logger;
53 import org.slf4j.LoggerFactory;
55 import com.google.common.reflect.TypeToken;
56 import com.jcabi.aspects.Loggable;
58 import fj.data.Either;
59 import io.swagger.annotations.Api;
60 import io.swagger.annotations.ApiOperation;
61 import io.swagger.annotations.ApiParam;
62 import io.swagger.annotations.ApiResponse;
63 import io.swagger.annotations.ApiResponses;
66 * Root resource (exposed at "/" path)
68 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
69 @Consumes(MediaType.APPLICATION_JSON)
70 @Produces(MediaType.APPLICATION_JSON)
72 @Api(value = "Resource Group Servlet")
74 public class GroupServlet extends AbstractValidationsServlet {
76 private static final Logger log = LoggerFactory.getLogger(GroupServlet.class);
77 public static final String START_HANDLE_REQUEST = "Start handle request of {}";
80 @Path("/{containerComponentType}/{componentId}/groups/{groupType}")
81 @ApiOperation(value = "Create group ", httpMethod = "POST", notes = "Creates new group in component and returns it", response = GroupDefinition.class)
82 @ApiResponses(value = {
83 @ApiResponse(code = 201, message = "Group created"),
84 @ApiResponse(code = 400, message = "field name invalid type/length, characters; mandatory field is absent, already exists (name)"),
85 @ApiResponse(code = 403, message = "Restricted operation"),
86 @ApiResponse(code = 404, message = "Component not found"),
87 @ApiResponse(code = 500, message = "Internal Error")
89 public Response createGroup(@PathParam("containerComponentType") final String containerComponentType,
90 @PathParam("componentId") final String componentId,
91 @PathParam("groupType") final String type,
92 @Context final HttpServletRequest request,
93 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
94 ServletContext context = request.getSession().getServletContext();
95 String url = request.getMethod() + " " + request.getRequestURI();
96 log.debug("(post) Start handle request of {}", url);
98 GroupBusinessLogic businessLogic = getGroupBL(context);
99 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
100 GroupDefinition groupDefinition = businessLogic
101 .createGroup(type, componentTypeEnum, componentId, userId);
103 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.CREATED),
108 @Path("/{containerComponentType}/{componentId}/groups/{groupId}")
109 @ApiOperation(value = "Get group artifacts ", httpMethod = "GET", notes = "Returns artifacts metadata according to groupId", response = Resource.class)
110 @ApiResponses(value = {@ApiResponse(code = 200, message = "group found"),
111 @ApiResponse(code = 403, message = "Restricted operation"),
112 @ApiResponse(code = 404, message = "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,
116 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
117 ServletContext context = request.getSession().getServletContext();
118 String url = request.getMethod() + " " + request.getRequestURI();
119 log.debug("(get) Start handle request of {}", url);
123 GroupBusinessLogic businessLogic = this.getGroupBL(context);
124 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
125 Either<GroupDefinitionInfo, ResponseFormat> actionResponse = businessLogic
126 .getGroupWithArtifactsById(componentTypeEnum, componentId, groupId, userId, false);
128 if (actionResponse.isRight()) {
129 log.debug("failed to get all non abstract {}", containerComponentType);
130 return buildErrorResponse(actionResponse.right().value());
133 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
134 actionResponse.left().value());
136 } catch (Exception e) {
137 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("getGroupArtifactById");
138 log.debug("getGroupArtifactById unexpected exception", e);
139 return buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));
145 @Path("/{containerComponentType}/{componentId}/groups/{groupUniqueId}")
146 @ApiOperation(value = "Delete Group", httpMethod = "DELETE", notes = "Returns deleted group id", response = Response.class)
147 @ApiResponses(value = {
148 @ApiResponse(code = 201, message = "ResourceInstance deleted"),
149 @ApiResponse(code = 400, message = "field name invalid type/length, characters; mandatory field is absent, already exists (name)"),
150 @ApiResponse(code = 403, message = "Restricted operation"),
151 @ApiResponse(code = 404, message = "Component not found"),
152 @ApiResponse(code = 500, message = "Internal Error")
154 public Response deleteGroup(@PathParam("containerComponentType") final String containerComponentType,
155 @PathParam("componentId") final String componentId,
156 @PathParam("groupUniqueId") final String groupId,
157 @Context final HttpServletRequest request,
158 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
159 ServletContext context = request.getSession().getServletContext();
160 String url = request.getMethod() + " " + request.getRequestURI();
161 log.debug(START_HANDLE_REQUEST, url);
162 GroupBusinessLogic businessLogic = this.getGroupBL(context);
163 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
164 GroupDefinition groupDefinition = businessLogic
165 .deleteGroup(componentTypeEnum, componentId, groupId, userId);
167 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.NO_CONTENT), groupDefinition.getUniqueId());
171 @Path("/{containerComponentType}/{componentId}/groups/{groupId}")
172 @ApiOperation(value = "Update Group metadata", httpMethod = "PUT", notes = "Returns updated Group", response = Response.class)
173 @ApiResponses(value = {
174 @ApiResponse(code = 200, message = "Group updated"),
175 @ApiResponse(code = 403, message = "Restricted operation"),
176 @ApiResponse(code = 400, message = "Invalid content / Missing content"),
177 @ApiResponse(code = 404, message = "component / group Not found")})
178 public Response updateGroup(@PathParam("containerComponentType") final String containerComponentType,
179 @PathParam("componentId") final String componentId,
180 @PathParam("groupId") final String groupId,
181 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
182 @ApiParam(value = "GroupDefinition", required = true) GroupDefinition groupData,
183 @Context final HttpServletRequest request) {
184 ServletContext context = request.getSession().getServletContext();
185 GroupBusinessLogic businessLogic = this.getGroupBL(context);
186 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
187 GroupDefinition updatedGroup = businessLogic.updateGroup(componentTypeEnum, componentId, groupId, userId, groupData);
188 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), updatedGroup);
192 * Updates List of properties on a group (only values)
193 * @param containerComponentType
195 * @param groupUniqueId
202 @Path("/{containerComponentType}/{componentId}/groups/{groupUniqueId}/properties")
203 @ApiOperation(value = "Updates List of properties on a group (only values)", httpMethod = "PUT", notes = "Returns updated list of properties", response = GroupDefinition.class)
204 @ApiResponses(value = { @ApiResponse(code = 200, message = "Group Updated"),
205 @ApiResponse(code = 403, message = "Restricted operation"),
206 @ApiResponse(code = 400, message = "Invalid content / Missing content") })
207 public Response updateGroupProperties(@PathParam("containerComponentType") final String containerComponentType,
208 @PathParam("componentId") final String componentId, @PathParam("groupUniqueId") final String groupUniqueId,
209 @ApiParam(value = "Group Properties to be Updated", required = true) String data,
210 @Context final HttpServletRequest request,
211 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
214 ServletContext context = request.getSession().getServletContext();
215 String url = request.getMethod() + " " + request.getRequestURI();
216 log.debug(START_HANDLE_REQUEST, url);
218 User user = new User();
219 user.setUserId(userId);
220 log.debug("modifier id is {}", userId);
222 Response response = null;
225 GroupBusinessLogic businessLogic = getGroupBL(context);
226 Either<List<GroupProperty>, ResponseFormat> convertResponse = parseListOfObjects(data,
227 new TypeToken<List<GroupProperty>>() {
230 if (convertResponse.isRight()) {
231 log.debug("failed to parse group Property");
232 response = buildErrorResponse(convertResponse.right().value());
235 List<GroupProperty> groupPropertyToUpdate = convertResponse.left().value();
237 // Update GroupDefinition
238 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(containerComponentType);
239 Either<List<GroupProperty>, ResponseFormat> actionResponse = businessLogic.validateAndUpdateGroupProperties(
240 componentId, groupUniqueId, user, componentTypeEnum, groupPropertyToUpdate, false);
242 if (actionResponse.isRight()) {
243 log.debug("failed to update GroupDefinition");
244 response = buildErrorResponse(actionResponse.right().value());
248 List<GroupProperty> groupProperty = actionResponse.left().value();
249 Object result = RepresentationUtils.toRepresentation(groupProperty);
250 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), result);
252 } catch (Exception e) {
253 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Update Group Properties");
254 log.debug("update group properties failed with exception", e);
255 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.GENERAL_ERROR));