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 com.jcabi.aspects.Loggable;
24 import fj.data.Either;
25 import io.swagger.v3.oas.annotations.Operation;
26 import io.swagger.v3.oas.annotations.Parameter;
27 import io.swagger.v3.oas.annotations.media.ArraySchema;
28 import io.swagger.v3.oas.annotations.media.Content;
29 import io.swagger.v3.oas.annotations.media.Schema;
30 import io.swagger.v3.oas.annotations.responses.ApiResponse;
31 import io.swagger.v3.oas.annotations.servers.Server;
32 import io.swagger.v3.oas.annotations.servers.Servers;
33 import io.swagger.v3.oas.annotations.tags.Tag;
34 import io.swagger.v3.oas.annotations.tags.Tags;
35 import org.apache.commons.collections.CollectionUtils;
36 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogicProvider;
38 import org.openecomp.sdc.be.components.impl.aaf.AafPermission;
39 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
40 import org.openecomp.sdc.be.config.BeEcompErrorManager;
41 import org.openecomp.sdc.be.dao.api.ActionStatus;
42 import org.openecomp.sdc.be.datamodel.api.HighestFilterEnum;
43 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
44 import org.openecomp.sdc.be.datatypes.enums.FilterKeyEnum;
45 import org.openecomp.sdc.be.impl.ComponentsUtils;
46 import org.openecomp.sdc.be.mixin.GroupCompositionMixin;
47 import org.openecomp.sdc.be.mixin.PolicyCompositionMixin;
48 import org.openecomp.sdc.be.model.Component;
49 import org.openecomp.sdc.be.model.ComponentInstance;
50 import org.openecomp.sdc.be.model.IComponentInstanceConnectedElement;
51 import org.openecomp.sdc.be.model.Resource;
52 import org.openecomp.sdc.be.model.User;
53 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
54 import org.openecomp.sdc.be.ui.model.UiLeftPaletteComponent;
55 import org.openecomp.sdc.be.user.UserBusinessLogic;
56 import org.openecomp.sdc.be.view.ResponseView;
57 import org.openecomp.sdc.common.api.Constants;
58 import org.openecomp.sdc.common.log.wrappers.Logger;
59 import org.openecomp.sdc.exception.ResponseFormat;
60 import org.springframework.stereotype.Controller;
62 import javax.inject.Inject;
63 import javax.servlet.http.HttpServletRequest;
64 import javax.ws.rs.Consumes;
65 import javax.ws.rs.GET;
66 import javax.ws.rs.HeaderParam;
67 import javax.ws.rs.POST;
68 import javax.ws.rs.Path;
69 import javax.ws.rs.PathParam;
70 import javax.ws.rs.Produces;
71 import javax.ws.rs.QueryParam;
72 import javax.ws.rs.core.Context;
73 import javax.ws.rs.core.MediaType;
74 import javax.ws.rs.core.Response;
75 import java.io.IOException;
76 import java.util.ArrayList;
77 import java.util.EnumMap;
78 import java.util.List;
81 import static org.openecomp.sdc.common.util.GeneralUtility.getCategorizedComponents;
83 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
85 @Tags({@Tag(name = "SDCE-2 APIs")})
86 @Servers({@Server(url = "/sdc2/rest")})
88 public class ComponentServlet extends BeGenericServlet {
89 private static final String GET_CERTIFIED_NOT_ABSTRACT_COMPONENTS_FAILED_WITH_EXCEPTION = "getCertifiedNotAbstractComponents failed with exception";
91 private static final String GET_CERTIFIED_NON_ABSTRACT = "Get Certified Non Abstract";
93 private static final String FAILED_TO_GET_ALL_NON_ABSTRACT = "failed to get all non abstract {}";
95 private static final String START_HANDLE_REQUEST_OF = "Start handle request of {}";
97 private static final Logger log = Logger.getLogger(ComponentServlet.class);
99 private final ComponentBusinessLogicProvider componentBusinessLogicProvider;
102 public ComponentServlet(UserBusinessLogic userBusinessLogic,
103 ComponentsUtils componentsUtils,
104 ComponentBusinessLogicProvider componentBusinessLogicProvider) {
105 super(userBusinessLogic, componentsUtils);
106 this.componentBusinessLogicProvider = componentBusinessLogicProvider;
110 @Path("/{componentType}/{componentUuid}/conformanceLevelValidation")
111 @Consumes(MediaType.APPLICATION_JSON)
112 @Produces(MediaType.APPLICATION_JSON)
113 @Operation(description = "Validate Component Conformance Level", method = "GET",
114 summary = "Returns the result according to conformance level in BE config", responses = {
115 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
116 @ApiResponse(responseCode = "200", description = "Component found"),
117 @ApiResponse(responseCode = "403", description = "Restricted operation"),
118 @ApiResponse(responseCode = "404", description = "Component not found")})
119 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
120 public Response conformanceLevelValidation(@PathParam("componentType") final String componentType,
121 @PathParam("componentUuid") final String componentUuid, @Context final HttpServletRequest request,
122 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
125 String url = request.getMethod() + " " + request.getRequestURI();
126 log.debug(START_HANDLE_REQUEST_OF, url);
128 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
129 if (componentTypeEnum != null) {
130 ComponentBusinessLogic compBL = componentBusinessLogicProvider.getInstance(componentTypeEnum);
131 Either<Boolean, ResponseFormat> eitherConformanceLevel = compBL.validateConformanceLevel(componentUuid, componentTypeEnum, userId);
132 if (eitherConformanceLevel.isRight()) {
133 response = buildErrorResponse(eitherConformanceLevel.right().value());
135 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), gson.toJson(eitherConformanceLevel.left().value()));
138 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
145 @Path("/{componentType}/{componentId}/requirmentsCapabilities")
146 @Consumes(MediaType.APPLICATION_JSON)
147 @Produces(MediaType.APPLICATION_JSON)
148 @Operation(description = "Get Component Requirments And Capabilities", method = "GET",
149 summary = "Returns Requirements And Capabilities according to componentId", responses = {
150 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
151 @ApiResponse(responseCode = "200", description = "Component found"),
152 @ApiResponse(responseCode = "403", description = "Restricted operation"),
153 @ApiResponse(responseCode = "404", description = "Component not found")})
154 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
155 public Response getRequirementAndCapabilities(@PathParam("componentType") final String componentType,
156 @PathParam("componentId") final String componentId, @Context final HttpServletRequest request,
157 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
160 String url = request.getMethod() + " " + request.getRequestURI();
161 log.debug(START_HANDLE_REQUEST_OF, url);
163 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
164 if (componentTypeEnum != null) {
166 ComponentBusinessLogic compBL = componentBusinessLogicProvider.getInstance(componentTypeEnum);
167 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
168 RepresentationUtils.toRepresentation(compBL.getRequirementsAndCapabilities(componentId, componentTypeEnum, userId)));
169 } catch (IOException e) {
170 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Capabilities and requirements for " + componentId);
171 log.debug("getRequirementAndCapabilities failed with exception", e);
175 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
182 @Path("/{componentType}/latestversion/notabstract")
183 @Consumes(MediaType.APPLICATION_JSON)
184 @Produces(MediaType.APPLICATION_JSON)
185 @Operation(description = "Get Component Requirments And Capabilities", method = "GET",
186 summary = "Returns Requirments And Capabilities according to componentId", responses = {
187 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
188 @ApiResponse(responseCode = "200", description = "Component found"),
189 @ApiResponse(responseCode = "403", description = "Restricted operation"),
190 @ApiResponse(responseCode = "404", description = "Component not found")})
191 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
192 public Response getLatestVersionNotAbstractCheckoutComponents(
193 @PathParam("componentType") final String componentType, @Context final HttpServletRequest request,
194 @QueryParam("internalComponentType") String internalComponentType,
195 @QueryParam("componentUids") List<String> componentUids,
196 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
198 String url = request.getMethod() + " " + request.getRequestURI();
199 log.debug("(get) Start handle request of {}", url);
203 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
204 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
206 log.debug("Received componentUids size is {}", componentUids == null ? 0 : componentUids.size());
208 Either<List<Component>, ResponseFormat> actionResponse = businessLogic.getLatestVersionNotAbstractComponents(false, componentTypeEnum, internalComponentType, componentUids, userId);
210 if (actionResponse.isRight()) {
211 log.debug(FAILED_TO_GET_ALL_NON_ABSTRACT, componentType);
212 return buildErrorResponse(actionResponse.right().value());
214 Object components = RepresentationUtils.toRepresentation(actionResponse.left().value());
215 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), components);
217 } catch (Exception e) {
218 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_CERTIFIED_NON_ABSTRACT + componentType);
219 log.debug(GET_CERTIFIED_NOT_ABSTRACT_COMPONENTS_FAILED_WITH_EXCEPTION, e);
226 @Path("/{componentType}/latestversion/notabstract")
227 @Consumes(MediaType.APPLICATION_JSON)
228 @Produces(MediaType.APPLICATION_JSON)
229 @Operation(description = "Get Component Requirments And Capabilities", method = "GET",
230 summary = "Returns Requirments And Capabilities according to componentId", responses = {
231 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
232 @ApiResponse(responseCode = "200", description = "Component found"),
233 @ApiResponse(responseCode = "403", description = "Restricted operation"),
234 @ApiResponse(responseCode = "404", description = "Component not found")})
235 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
236 public Response getLatestVersionNotAbstractCheckoutComponentsByBody(
237 @PathParam("componentType") final String componentType, @Context final HttpServletRequest request,
238 @QueryParam("internalComponentType") String internalComponentType,
239 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
240 @Parameter(description = "Consumer Object to be created", required = true) List<String> data) throws IOException {
242 String url = request.getMethod() + " " + request.getRequestURI();
243 log.debug("(GET) Start handle request of {}", url);
248 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
249 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
250 if (log.isDebugEnabled()) {
251 log.debug("Received componentUids size is {}", data == null ? 0 : data.size());
254 Either<List<Component>, ResponseFormat> actionResponse = businessLogic.getLatestVersionNotAbstractComponents(false, componentTypeEnum, internalComponentType, data, userId);
256 if (actionResponse.isRight()) {
257 log.debug(FAILED_TO_GET_ALL_NON_ABSTRACT, componentType);
258 return buildErrorResponse(actionResponse.right().value());
261 Object components = RepresentationUtils.toRepresentation(actionResponse.left().value());
262 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), components);
266 } catch (Exception e) {
267 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_CERTIFIED_NON_ABSTRACT + componentType);
268 log.debug(GET_CERTIFIED_NOT_ABSTRACT_COMPONENTS_FAILED_WITH_EXCEPTION, e);
275 @Path("/{componentType}/latestversion/notabstract/metadata")
276 @Consumes(MediaType.APPLICATION_JSON)
277 @Produces(MediaType.APPLICATION_JSON)
278 @Operation(description = "Get Component uid only", method = "GET", summary = "Returns componentId", responses = {
279 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
280 @ApiResponse(responseCode = "200", description = "Component found"),
281 @ApiResponse(responseCode = "403", description = "Restricted operation"),
282 @ApiResponse(responseCode = "404", description = "Component not found")})
283 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
284 public Response getLatestVersionNotAbstractCheckoutComponentsIdesOnly(
285 @PathParam("componentType") final String componentType, @Context final HttpServletRequest request,
286 @QueryParam("internalComponentType") String internalComponentType,
287 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
288 @Parameter(description = "uid list", required = true) String data) throws IOException {
290 String url = request.getMethod() + " " + request.getRequestURI();
291 log.debug("(get) Start handle request of {}", url);
293 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
294 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
296 Either<List<Component>, ResponseFormat> actionResponse = businessLogic.getLatestVersionNotAbstractComponentsMetadata(false, HighestFilterEnum.HIGHEST_ONLY, componentTypeEnum, internalComponentType, userId);
297 if (actionResponse.isRight()) {
298 log.debug(FAILED_TO_GET_ALL_NON_ABSTRACT, componentType);
299 return buildErrorResponse(actionResponse.right().value());
301 List<UiLeftPaletteComponent> uiLeftPaletteComponents = getComponentsUtils().convertComponentToUiLeftPaletteComponentObject(actionResponse.left().value());
302 Map<String, Map<String, List<UiLeftPaletteComponent>>> categorizedComponents = getCategorizedComponents(uiLeftPaletteComponents);
303 Object components = RepresentationUtils.toRepresentation(categorizedComponents);
304 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), components);
306 } catch (Exception e) {
307 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_CERTIFIED_NON_ABSTRACT + componentType);
308 log.debug(GET_CERTIFIED_NOT_ABSTRACT_COMPONENTS_FAILED_WITH_EXCEPTION, e);
315 @Path("/{componentType}/{componentId}/componentInstances")
316 @Consumes(MediaType.APPLICATION_JSON)
317 @Produces(MediaType.APPLICATION_JSON)
318 @Operation(description = "Get Component instances", method = "GET", summary = "Returns component instances",
319 responses = {@ApiResponse(
320 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
321 @ApiResponse(responseCode = "200", description = "Component found"),
322 @ApiResponse(responseCode = "403", description = "Restricted operation"),
323 @ApiResponse(responseCode = "404", description = "Component not found")})
324 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
325 public Response getComponentInstancesFilteredByPropertiesAndInputs(
326 @PathParam("componentType") final String componentType, @PathParam("componentId") final String componentId,
327 @Context final HttpServletRequest request, @QueryParam("searchText") String searchText,
328 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
329 @Parameter(description = "uid" + " " + "list", required = true) String data) throws IOException {
331 String url = request.getMethod() + " " + request.getRequestURI();
332 log.debug("(GET) Start handle request of {}", url);
334 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
335 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
337 Either<List<ComponentInstance>, ResponseFormat> actionResponse = businessLogic.getComponentInstancesFilteredByPropertiesAndInputs(componentId, userId);
338 if (actionResponse.isRight()) {
339 log.debug("failed to get all component instances filtered by properties and inputs", componentType);
340 return buildErrorResponse(actionResponse.right().value());
342 Object components = RepresentationUtils.toRepresentation(actionResponse.left().value());
343 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), components);
345 } catch (Exception e) {
346 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Component Instances filtered by properties & inputs" + componentType);
347 log.debug("getComponentInstancesFilteredByPropertiesAndInputs failed with exception", e);
355 * This API is a generic api for ui - the api get a list of strings and return the data on the component according to to list.
356 * for example: list of the string "properties, inputs" will return component with the list of properties and inputs.
358 * @param componentType
360 * @param dataParamsToReturn
368 @Path("/{componentType}/{componentId}/filteredDataByParams")
369 @Consumes(MediaType.APPLICATION_JSON)
370 @Produces(MediaType.APPLICATION_JSON)
371 @Operation(description = "Retrieve Resource", method = "GET", summary = "Returns resource according to resourceId",
372 responses = {@ApiResponse(
373 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
374 @ApiResponse(responseCode = "200", description = "Resource found"),
375 @ApiResponse(responseCode = "403", description = "Restricted operation"),
376 @ApiResponse(responseCode = "404", description = "Resource not found")})
377 @ResponseView(mixin = {GroupCompositionMixin.class, PolicyCompositionMixin.class})
378 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
379 public Response getComponentDataFilteredByParams(@PathParam("componentType") final String componentType,
380 @PathParam("componentId") final String componentId,
381 @QueryParam("include") final List<String> dataParamsToReturn, @Context final HttpServletRequest request,
382 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
384 String url = request.getMethod() + " " + request.getRequestURI();
385 log.debug(START_HANDLE_REQUEST_OF , url);
388 User modifier = new User();
389 modifier.setUserId(userId);
390 log.debug("modifier id is {}" , userId);
393 String resourceIdLower = componentId.toLowerCase();
394 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
395 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
397 log.trace("get component with id {} filtered by ui params", componentId);
398 Either<UiComponentDataTransfer, ResponseFormat> actionResponse = businessLogic.getComponentDataFilteredByParams(resourceIdLower, modifier, dataParamsToReturn);
400 if (actionResponse.isRight()) {
401 log.debug("failed to get component data filtered by ui params");
402 return buildErrorResponse(actionResponse.right().value());
404 RepresentationUtils.toRepresentation(actionResponse.left().value());
405 return buildOkResponse(actionResponse.left().value());
407 } catch (Exception e) {
408 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get component filtered by ui params");
409 log.debug("get resource failed with exception", e);
416 @Path("/{componentType}/{componentId}/filteredproperties/{propertyNameFragment}")
417 @Consumes(MediaType.APPLICATION_JSON)
418 @Produces(MediaType.APPLICATION_JSON)
420 description = "Retrieve properties belonging to component instances of specific component by name and optionally resource type",
422 summary = "Returns properties belonging to component instances of specific component by name and optionally resource type",
423 responses = {@ApiResponse(
424 content = @Content(array = @ArraySchema(schema = @Schema(implementation = Map.class)))),
425 @ApiResponse(responseCode = "200", description = "Component found"),
426 @ApiResponse(responseCode = "403", description = "Restricted operation"),
427 @ApiResponse(responseCode = "404", description = "Component not found")})
428 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
429 public Response getFilteredComponentInstanceProperties(
430 @PathParam("componentType") final String componentType,
431 @PathParam("componentId") final String componentId,
432 @PathParam("propertyNameFragment") final String propertyNameFragment,
433 @QueryParam("resourceType") List<String> resourceTypes,
434 @Context final HttpServletRequest request,
435 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
437 User user = new User();
438 user.setUserId(userId);
439 log.debug("User Id is {}" , userId);
442 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
443 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
444 Map<FilterKeyEnum, List<String>> filters = new EnumMap<>(FilterKeyEnum.class);
445 List<String> propertyNameFragments = new ArrayList<>();
446 propertyNameFragments.add(propertyNameFragment);
447 filters.put(FilterKeyEnum.NAME_FRAGMENT, propertyNameFragments);
448 if(CollectionUtils.isNotEmpty(resourceTypes)){
449 filters.put(FilterKeyEnum.RESOURCE_TYPE, resourceTypes);
451 Either<Map<String, List<IComponentInstanceConnectedElement>>, ResponseFormat> actionResponse = businessLogic.getFilteredComponentInstanceProperties(componentId, filters, userId);
452 if (actionResponse.isRight()) {
453 response = buildErrorResponse(actionResponse.right().value());
456 Object resource = RepresentationUtils.toRepresentation(actionResponse.left().value());
457 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), resource);
459 } catch (Exception e) {
460 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Filtered Component Instance Properties");
461 log.debug("Getting of filtered component instance properties failed with exception", e);