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 static org.openecomp.sdc.common.util.GeneralUtility.getCategorizedComponents;
24 import com.jcabi.aspects.Loggable;
25 import fj.data.Either;
26 import io.swagger.v3.oas.annotations.Operation;
27 import io.swagger.v3.oas.annotations.Parameter;
28 import io.swagger.v3.oas.annotations.media.ArraySchema;
29 import io.swagger.v3.oas.annotations.media.Content;
30 import io.swagger.v3.oas.annotations.media.Schema;
31 import io.swagger.v3.oas.annotations.responses.ApiResponse;
32 import io.swagger.v3.oas.annotations.servers.Server;
33 import io.swagger.v3.oas.annotations.servers.Servers;
34 import io.swagger.v3.oas.annotations.tags.Tag;
35 import io.swagger.v3.oas.annotations.tags.Tags;
36 import java.io.IOException;
37 import java.util.ArrayList;
38 import java.util.EnumMap;
39 import java.util.List;
41 import javax.inject.Inject;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.ws.rs.Consumes;
44 import javax.ws.rs.GET;
45 import javax.ws.rs.HeaderParam;
46 import javax.ws.rs.POST;
47 import javax.ws.rs.Path;
48 import javax.ws.rs.PathParam;
49 import javax.ws.rs.Produces;
50 import javax.ws.rs.QueryParam;
51 import javax.ws.rs.core.Context;
52 import javax.ws.rs.core.MediaType;
53 import javax.ws.rs.core.Response;
54 import org.apache.commons.collections.CollectionUtils;
55 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogic;
56 import org.openecomp.sdc.be.components.impl.ComponentBusinessLogicProvider;
57 import org.openecomp.sdc.be.config.BeEcompErrorManager;
58 import org.openecomp.sdc.be.dao.api.ActionStatus;
59 import org.openecomp.sdc.be.datamodel.api.HighestFilterEnum;
60 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
61 import org.openecomp.sdc.be.datatypes.enums.FilterKeyEnum;
62 import org.openecomp.sdc.be.impl.ComponentsUtils;
63 import org.openecomp.sdc.be.mixin.GroupCompositionMixin;
64 import org.openecomp.sdc.be.mixin.PolicyCompositionMixin;
65 import org.openecomp.sdc.be.model.Component;
66 import org.openecomp.sdc.be.model.ComponentInstance;
67 import org.openecomp.sdc.be.model.IComponentInstanceConnectedElement;
68 import org.openecomp.sdc.be.model.Resource;
69 import org.openecomp.sdc.be.model.User;
70 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
71 import org.openecomp.sdc.be.ui.model.UiLeftPaletteComponent;
72 import org.openecomp.sdc.be.view.ResponseView;
73 import org.openecomp.sdc.common.api.Constants;
74 import org.openecomp.sdc.common.log.wrappers.Logger;
75 import org.openecomp.sdc.common.util.ValidationUtils;
76 import org.openecomp.sdc.exception.ResponseFormat;
77 import org.springframework.stereotype.Controller;
79 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
81 @Tags({@Tag(name = "SDCE-2 APIs")})
82 @Servers({@Server(url = "/sdc2/rest")})
84 public class ComponentServlet extends BeGenericServlet {
86 private static final String GET_CERTIFIED_NOT_ABSTRACT_COMPONENTS_FAILED_WITH_EXCEPTION = "getCertifiedNotAbstractComponents failed with exception";
87 private static final String GET_CERTIFIED_NON_ABSTRACT = "Get Certified Non Abstract";
88 private static final String FAILED_TO_GET_ALL_NON_ABSTRACT = "failed to get all non abstract {}";
89 private static final String START_HANDLE_REQUEST_OF = "Start handle request of {}";
90 private static final Logger log = Logger.getLogger(ComponentServlet.class);
91 private final ComponentBusinessLogicProvider componentBusinessLogicProvider;
94 public ComponentServlet(ComponentsUtils componentsUtils,
95 ComponentBusinessLogicProvider componentBusinessLogicProvider) {
96 super(componentsUtils);
97 this.componentBusinessLogicProvider = componentBusinessLogicProvider;
101 @Path("/{componentType}/{componentUuid}/conformanceLevelValidation")
102 @Consumes(MediaType.APPLICATION_JSON)
103 @Produces(MediaType.APPLICATION_JSON)
104 @Operation(description = "Validate Component Conformance Level", method = "GET", summary = "Returns the result according to conformance level in BE config", responses = {
105 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
106 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
107 @ApiResponse(responseCode = "404", description = "Component not found")})
108 public Response conformanceLevelValidation(@PathParam("componentType") final String componentType,
109 @PathParam("componentUuid") final String componentUuid, @Context final HttpServletRequest request,
110 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
112 String url = request.getMethod() + " " + request.getRequestURI();
113 log.debug(START_HANDLE_REQUEST_OF, url);
114 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
115 if (componentTypeEnum != null) {
116 ComponentBusinessLogic compBL = componentBusinessLogicProvider.getInstance(componentTypeEnum);
117 Either<Boolean, ResponseFormat> eitherConformanceLevel = compBL.validateConformanceLevel(componentUuid, componentTypeEnum, userId);
118 if (eitherConformanceLevel.isRight()) {
119 response = buildErrorResponse(eitherConformanceLevel.right().value());
121 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
122 gson.toJson(eitherConformanceLevel.left().value()));
125 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
131 @Path("/{componentType}/{componentId}/requirmentsCapabilities")
132 @Consumes(MediaType.APPLICATION_JSON)
133 @Produces(MediaType.APPLICATION_JSON)
134 @Operation(description = "Get Component Requirments And Capabilities", method = "GET", summary = "Returns Requirements And Capabilities according to componentId", responses = {
135 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
136 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
137 @ApiResponse(responseCode = "404", description = "Component not found")})
138 public Response getRequirementAndCapabilities(@PathParam("componentType") final String componentType,
139 @PathParam("componentId") final String componentId, @Context final HttpServletRequest request,
140 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
142 String url = request.getMethod() + " " + request.getRequestURI();
143 log.debug(START_HANDLE_REQUEST_OF, url);
144 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
145 if (componentTypeEnum != null) {
147 ComponentBusinessLogic compBL = componentBusinessLogicProvider.getInstance(componentTypeEnum);
148 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
149 RepresentationUtils.toRepresentation(compBL.getRequirementsAndCapabilities(componentId, componentTypeEnum, userId)));
150 } catch (IOException e) {
151 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Capabilities and requirements for " + componentId);
152 log.debug("getRequirementAndCapabilities failed with exception", e);
156 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
162 @Path("/{componentType}/latestversion/notabstract")
163 @Consumes(MediaType.APPLICATION_JSON)
164 @Produces(MediaType.APPLICATION_JSON)
165 @Operation(description = "Get Component Requirments And Capabilities", method = "GET", summary = "Returns Requirments And Capabilities according to componentId", responses = {
166 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
167 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
168 @ApiResponse(responseCode = "404", description = "Component not found")})
169 public Response getLatestVersionNotAbstractCheckoutComponents(@PathParam("componentType") final String componentType,
170 @Context final HttpServletRequest request,
171 @QueryParam("internalComponentType") String internalComponentType,
172 @QueryParam("componentUids") List<String> componentUids,
173 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
174 String url = request.getMethod() + " " + request.getRequestURI();
175 log.debug("(get) Start handle request of {}", url);
177 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
178 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
179 log.debug("Received componentUids size is {}", componentUids == null ? 0 : componentUids.size());
180 Either<List<Component>, ResponseFormat> actionResponse = businessLogic
181 .getLatestVersionNotAbstractComponents(false, componentTypeEnum, internalComponentType, componentUids, userId);
182 if (actionResponse.isRight()) {
183 log.debug(FAILED_TO_GET_ALL_NON_ABSTRACT, componentType);
184 return buildErrorResponse(actionResponse.right().value());
186 Object components = RepresentationUtils.toRepresentation(actionResponse.left().value());
187 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), components);
188 } catch (Exception e) {
189 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_CERTIFIED_NON_ABSTRACT + componentType);
190 log.debug(GET_CERTIFIED_NOT_ABSTRACT_COMPONENTS_FAILED_WITH_EXCEPTION, e);
196 @Path("/{componentType}/latestversion/notabstract")
197 @Consumes(MediaType.APPLICATION_JSON)
198 @Produces(MediaType.APPLICATION_JSON)
199 @Operation(description = "Get Component Requirments And Capabilities", method = "GET", summary = "Returns Requirments And Capabilities according to componentId", responses = {
200 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
201 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
202 @ApiResponse(responseCode = "404", description = "Component not found")})
203 public Response getLatestVersionNotAbstractCheckoutComponentsByBody(@PathParam("componentType") final String componentType,
204 @Context final HttpServletRequest request,
205 @QueryParam("internalComponentType") String internalComponentType,
206 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
207 @Parameter(description = "Consumer Object to be created", required = true) List<String> data)
209 String url = request.getMethod() + " " + request.getRequestURI();
210 log.debug("(GET) Start handle request of {}", url);
213 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
214 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
215 if (log.isDebugEnabled()) {
216 log.debug("Received componentUids size is {}", data == null ? 0 : data.size());
218 Either<List<Component>, ResponseFormat> actionResponse = businessLogic
219 .getLatestVersionNotAbstractComponents(false, componentTypeEnum, internalComponentType, data, userId);
220 if (actionResponse.isRight()) {
221 log.debug(FAILED_TO_GET_ALL_NON_ABSTRACT, componentType);
222 return buildErrorResponse(actionResponse.right().value());
224 Object components = RepresentationUtils.toRepresentation(actionResponse.left().value());
225 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), components);
226 } catch (Exception e) {
227 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_CERTIFIED_NON_ABSTRACT + componentType);
228 log.debug(GET_CERTIFIED_NOT_ABSTRACT_COMPONENTS_FAILED_WITH_EXCEPTION, e);
234 @Path("/{componentType}/latestversion/notabstract/metadata")
235 @Consumes(MediaType.APPLICATION_JSON)
236 @Produces(MediaType.APPLICATION_JSON)
237 @Operation(description = "Get Component uid only", method = "GET", summary = "Returns componentId", responses = {
238 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
239 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
240 @ApiResponse(responseCode = "404", description = "Component not found")})
241 public Response getLatestVersionNotAbstractCheckoutComponentsIdesOnly(@PathParam("componentType") final String componentType,
242 @Context final HttpServletRequest request,
243 @QueryParam("internalComponentType") String internalComponentType,
244 @QueryParam("componentModel") String internalComponentModel,
245 @QueryParam("includeNormativeExtensionModels") boolean includeNormativeExtensionModels,
246 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
247 @Parameter(description = "uid list", required = true) String data)
249 String url = request.getMethod() + " " + request.getRequestURI();
250 log.debug("(get) Start handle request of {}", url);
252 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
253 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
254 if (internalComponentModel != null) {
255 internalComponentModel = ValidationUtils.sanitizeInputString(internalComponentModel.trim());
257 Either<List<Component>, ResponseFormat> actionResponse = businessLogic
258 .getLatestVersionNotAbstractComponentsMetadata(false, HighestFilterEnum.HIGHEST_ONLY, componentTypeEnum,
259 internalComponentType, userId, internalComponentModel, includeNormativeExtensionModels);
260 if (actionResponse.isRight()) {
261 log.debug(FAILED_TO_GET_ALL_NON_ABSTRACT, componentType);
262 return buildErrorResponse(actionResponse.right().value());
264 List<UiLeftPaletteComponent> uiLeftPaletteComponents = getComponentsUtils()
265 .convertComponentToUiLeftPaletteComponentObject(actionResponse.left().value());
266 Map<String, Map<String, List<UiLeftPaletteComponent>>> categorizedComponents = getCategorizedComponents(uiLeftPaletteComponents);
267 Object components = RepresentationUtils.toRepresentation(categorizedComponents);
268 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), components);
269 } catch (Exception e) {
270 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_CERTIFIED_NON_ABSTRACT + componentType);
271 log.debug(GET_CERTIFIED_NOT_ABSTRACT_COMPONENTS_FAILED_WITH_EXCEPTION, e);
277 @Path("/{componentType}/{componentId}/componentInstances")
278 @Consumes(MediaType.APPLICATION_JSON)
279 @Produces(MediaType.APPLICATION_JSON)
280 @Operation(description = "Get Component instances", method = "GET", summary = "Returns component instances", responses = {
281 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
282 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
283 @ApiResponse(responseCode = "404", description = "Component not found")})
284 public Response getComponentInstancesFilteredByPropertiesAndInputs(@PathParam("componentType") final String componentType,
285 @PathParam("componentId") final String componentId,
286 @Context final HttpServletRequest request,
287 @QueryParam("searchText") String searchText,
288 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
289 @Parameter(description = "uid" + " " + "list", required = true) String data)
291 String url = request.getMethod() + " " + request.getRequestURI();
292 log.debug("(GET) Start handle request of {}", url);
294 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
295 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
296 Either<List<ComponentInstance>, ResponseFormat> actionResponse = businessLogic
297 .getComponentInstancesFilteredByPropertiesAndInputs(componentId, userId);
298 if (actionResponse.isRight()) {
299 log.debug("failed to get all component instances filtered by properties and inputs", componentType);
300 return buildErrorResponse(actionResponse.right().value());
302 Object components = RepresentationUtils.toRepresentation(actionResponse.left().value());
303 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), components);
304 } catch (Exception e) {
305 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Component Instances filtered by properties & inputs" + componentType);
306 log.debug("getComponentInstancesFilteredByPropertiesAndInputs failed with exception", e);
312 * 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. for example: list
313 * of the string "properties, inputs" will return component with the list of properties and inputs.
315 * @param componentType
317 * @param dataParamsToReturn
323 @Path("/{componentType}/{componentId}/filteredDataByParams")
324 @Consumes(MediaType.APPLICATION_JSON)
325 @Produces(MediaType.APPLICATION_JSON)
326 @Operation(description = "Retrieve Resource", method = "GET", summary = "Returns resource according to resourceId", responses = {
327 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
328 @ApiResponse(responseCode = "200", description = "Resource found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
329 @ApiResponse(responseCode = "404", description = "Resource not found")})
330 @ResponseView(mixin = {GroupCompositionMixin.class, PolicyCompositionMixin.class})
331 public Response getComponentDataFilteredByParams(@PathParam("componentType") final String componentType,
332 @PathParam("componentId") final String componentId,
333 @QueryParam("include") final List<String> dataParamsToReturn,
334 @Context final HttpServletRequest request,
335 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
336 String url = request.getMethod() + " " + request.getRequestURI();
337 log.debug(START_HANDLE_REQUEST_OF, url);
339 User modifier = new User();
340 modifier.setUserId(userId);
341 log.debug("modifier id is {}", userId);
343 String resourceIdLower = componentId.toLowerCase();
344 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
345 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
346 log.trace("get component with id {} filtered by ui params", componentId);
347 Either<UiComponentDataTransfer, ResponseFormat> actionResponse = businessLogic
348 .getComponentDataFilteredByParams(resourceIdLower, modifier, dataParamsToReturn);
349 if (actionResponse.isRight()) {
350 log.debug("failed to get component data filtered by ui params");
351 return buildErrorResponse(actionResponse.right().value());
353 RepresentationUtils.toRepresentation(actionResponse.left().value());
354 return buildOkResponse(actionResponse.left().value());
355 } catch (Exception e) {
356 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get component filtered by ui params");
357 log.debug("get resource failed with exception", e);
363 @Path("/{componentType}/{componentId}/filteredproperties/{propertyNameFragment}")
364 @Consumes(MediaType.APPLICATION_JSON)
365 @Produces(MediaType.APPLICATION_JSON)
366 @Operation(description = "Retrieve properties belonging to component instances of specific component by name and optionally resource type", method = "GET", summary = "Returns properties belonging to component instances of specific component by name and optionally resource type", responses = {
367 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Map.class)))),
368 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
369 @ApiResponse(responseCode = "404", description = "Component not found")})
370 public Response getFilteredComponentInstanceProperties(@PathParam("componentType") final String componentType,
371 @PathParam("componentId") final String componentId,
372 @PathParam("propertyNameFragment") final String propertyNameFragment,
373 @QueryParam("resourceType") List<String> resourceTypes,
374 @Context final HttpServletRequest request,
375 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
376 User user = new User();
377 user.setUserId(userId);
378 log.debug("User Id is {}", userId);
381 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
382 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
383 Map<FilterKeyEnum, List<String>> filters = new EnumMap<>(FilterKeyEnum.class);
384 List<String> propertyNameFragments = new ArrayList<>();
385 propertyNameFragments.add(propertyNameFragment);
386 filters.put(FilterKeyEnum.NAME_FRAGMENT, propertyNameFragments);
387 if (CollectionUtils.isNotEmpty(resourceTypes)) {
388 filters.put(FilterKeyEnum.RESOURCE_TYPE, resourceTypes);
390 Either<Map<String, List<IComponentInstanceConnectedElement>>, ResponseFormat> actionResponse = businessLogic
391 .getFilteredComponentInstanceProperties(componentId, filters, userId);
392 if (actionResponse.isRight()) {
393 response = buildErrorResponse(actionResponse.right().value());
396 Object resource = RepresentationUtils.toRepresentation(actionResponse.left().value());
397 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), resource);
398 } catch (Exception e) {
399 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Filtered Component Instance Properties");
400 log.debug("Getting of filtered component instance properties failed with exception", e);