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.components.impl.aaf.AafPermission;
58 import org.openecomp.sdc.be.components.impl.aaf.PermissionAllowed;
59 import org.openecomp.sdc.be.config.BeEcompErrorManager;
60 import org.openecomp.sdc.be.dao.api.ActionStatus;
61 import org.openecomp.sdc.be.datamodel.api.HighestFilterEnum;
62 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
63 import org.openecomp.sdc.be.datatypes.enums.FilterKeyEnum;
64 import org.openecomp.sdc.be.impl.ComponentsUtils;
65 import org.openecomp.sdc.be.mixin.GroupCompositionMixin;
66 import org.openecomp.sdc.be.mixin.PolicyCompositionMixin;
67 import org.openecomp.sdc.be.model.Component;
68 import org.openecomp.sdc.be.model.ComponentInstance;
69 import org.openecomp.sdc.be.model.IComponentInstanceConnectedElement;
70 import org.openecomp.sdc.be.model.Resource;
71 import org.openecomp.sdc.be.model.User;
72 import org.openecomp.sdc.be.ui.model.UiComponentDataTransfer;
73 import org.openecomp.sdc.be.ui.model.UiLeftPaletteComponent;
74 import org.openecomp.sdc.be.user.UserBusinessLogic;
75 import org.openecomp.sdc.be.view.ResponseView;
76 import org.openecomp.sdc.common.api.Constants;
77 import org.openecomp.sdc.common.log.wrappers.Logger;
78 import org.openecomp.sdc.exception.ResponseFormat;
79 import org.springframework.stereotype.Controller;
81 @Loggable(prepend = true, value = Loggable.DEBUG, trim = false)
83 @Tags({@Tag(name = "SDCE-2 APIs")})
84 @Servers({@Server(url = "/sdc2/rest")})
86 public class ComponentServlet extends BeGenericServlet {
88 private static final String GET_CERTIFIED_NOT_ABSTRACT_COMPONENTS_FAILED_WITH_EXCEPTION = "getCertifiedNotAbstractComponents failed with exception";
89 private static final String GET_CERTIFIED_NON_ABSTRACT = "Get Certified Non Abstract";
90 private static final String FAILED_TO_GET_ALL_NON_ABSTRACT = "failed to get all non abstract {}";
91 private static final String START_HANDLE_REQUEST_OF = "Start handle request of {}";
92 private static final Logger log = Logger.getLogger(ComponentServlet.class);
93 private final ComponentBusinessLogicProvider componentBusinessLogicProvider;
96 public ComponentServlet(UserBusinessLogic userBusinessLogic, ComponentsUtils componentsUtils,
97 ComponentBusinessLogicProvider componentBusinessLogicProvider) {
98 super(userBusinessLogic, componentsUtils);
99 this.componentBusinessLogicProvider = componentBusinessLogicProvider;
103 @Path("/{componentType}/{componentUuid}/conformanceLevelValidation")
104 @Consumes(MediaType.APPLICATION_JSON)
105 @Produces(MediaType.APPLICATION_JSON)
106 @Operation(description = "Validate Component Conformance Level", method = "GET", summary = "Returns the result according to conformance level in BE config", responses = {
107 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
108 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
109 @ApiResponse(responseCode = "404", description = "Component not found")})
110 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
111 public Response conformanceLevelValidation(@PathParam("componentType") final String componentType,
112 @PathParam("componentUuid") final String componentUuid, @Context final HttpServletRequest request,
113 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) {
115 String url = request.getMethod() + " " + request.getRequestURI();
116 log.debug(START_HANDLE_REQUEST_OF, url);
117 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
118 if (componentTypeEnum != null) {
119 ComponentBusinessLogic compBL = componentBusinessLogicProvider.getInstance(componentTypeEnum);
120 Either<Boolean, ResponseFormat> eitherConformanceLevel = compBL.validateConformanceLevel(componentUuid, componentTypeEnum, userId);
121 if (eitherConformanceLevel.isRight()) {
122 response = buildErrorResponse(eitherConformanceLevel.right().value());
124 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
125 gson.toJson(eitherConformanceLevel.left().value()));
128 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
134 @Path("/{componentType}/{componentId}/requirmentsCapabilities")
135 @Consumes(MediaType.APPLICATION_JSON)
136 @Produces(MediaType.APPLICATION_JSON)
137 @Operation(description = "Get Component Requirments And Capabilities", method = "GET", summary = "Returns Requirements And Capabilities according to componentId", responses = {
138 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
139 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
140 @ApiResponse(responseCode = "404", description = "Component not found")})
141 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
142 public Response getRequirementAndCapabilities(@PathParam("componentType") final String componentType,
143 @PathParam("componentId") final String componentId, @Context final HttpServletRequest request,
144 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
146 String url = request.getMethod() + " " + request.getRequestURI();
147 log.debug(START_HANDLE_REQUEST_OF, url);
148 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
149 if (componentTypeEnum != null) {
151 ComponentBusinessLogic compBL = componentBusinessLogicProvider.getInstance(componentTypeEnum);
152 response = buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK),
153 RepresentationUtils.toRepresentation(compBL.getRequirementsAndCapabilities(componentId, componentTypeEnum, userId)));
154 } catch (IOException e) {
155 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Capabilities and requirements for " + componentId);
156 log.debug("getRequirementAndCapabilities failed with exception", e);
160 response = buildErrorResponse(getComponentsUtils().getResponseFormat(ActionStatus.INVALID_CONTENT));
166 @Path("/{componentType}/latestversion/notabstract")
167 @Consumes(MediaType.APPLICATION_JSON)
168 @Produces(MediaType.APPLICATION_JSON)
169 @Operation(description = "Get Component Requirments And Capabilities", method = "GET", summary = "Returns Requirments And Capabilities according to componentId", responses = {
170 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
171 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
172 @ApiResponse(responseCode = "404", description = "Component not found")})
173 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
174 public Response getLatestVersionNotAbstractCheckoutComponents(@PathParam("componentType") final String componentType,
175 @Context final HttpServletRequest request,
176 @QueryParam("internalComponentType") String internalComponentType,
177 @QueryParam("componentUids") List<String> componentUids,
178 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
179 String url = request.getMethod() + " " + request.getRequestURI();
180 log.debug("(get) Start handle request of {}", url);
182 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
183 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
184 log.debug("Received componentUids size is {}", componentUids == null ? 0 : componentUids.size());
185 Either<List<Component>, ResponseFormat> actionResponse = businessLogic
186 .getLatestVersionNotAbstractComponents(false, componentTypeEnum, internalComponentType, componentUids, userId);
187 if (actionResponse.isRight()) {
188 log.debug(FAILED_TO_GET_ALL_NON_ABSTRACT, componentType);
189 return buildErrorResponse(actionResponse.right().value());
191 Object components = RepresentationUtils.toRepresentation(actionResponse.left().value());
192 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), components);
193 } catch (Exception e) {
194 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_CERTIFIED_NON_ABSTRACT + componentType);
195 log.debug(GET_CERTIFIED_NOT_ABSTRACT_COMPONENTS_FAILED_WITH_EXCEPTION, e);
201 @Path("/{componentType}/latestversion/notabstract")
202 @Consumes(MediaType.APPLICATION_JSON)
203 @Produces(MediaType.APPLICATION_JSON)
204 @Operation(description = "Get Component Requirments And Capabilities", method = "GET", summary = "Returns Requirments And Capabilities according to componentId", responses = {
205 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
206 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
207 @ApiResponse(responseCode = "404", description = "Component not found")})
208 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
209 public Response getLatestVersionNotAbstractCheckoutComponentsByBody(@PathParam("componentType") final String componentType,
210 @Context final HttpServletRequest request,
211 @QueryParam("internalComponentType") String internalComponentType,
212 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
213 @Parameter(description = "Consumer Object to be created", required = true) List<String> data)
215 String url = request.getMethod() + " " + request.getRequestURI();
216 log.debug("(GET) Start handle request of {}", url);
219 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
220 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
221 if (log.isDebugEnabled()) {
222 log.debug("Received componentUids size is {}", data == null ? 0 : data.size());
224 Either<List<Component>, ResponseFormat> actionResponse = businessLogic
225 .getLatestVersionNotAbstractComponents(false, componentTypeEnum, internalComponentType, data, userId);
226 if (actionResponse.isRight()) {
227 log.debug(FAILED_TO_GET_ALL_NON_ABSTRACT, componentType);
228 return buildErrorResponse(actionResponse.right().value());
230 Object components = RepresentationUtils.toRepresentation(actionResponse.left().value());
231 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), components);
232 } catch (Exception e) {
233 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_CERTIFIED_NON_ABSTRACT + componentType);
234 log.debug(GET_CERTIFIED_NOT_ABSTRACT_COMPONENTS_FAILED_WITH_EXCEPTION, e);
240 @Path("/{componentType}/latestversion/notabstract/metadata")
241 @Consumes(MediaType.APPLICATION_JSON)
242 @Produces(MediaType.APPLICATION_JSON)
243 @Operation(description = "Get Component uid only", method = "GET", summary = "Returns componentId", responses = {
244 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
245 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
246 @ApiResponse(responseCode = "404", description = "Component not found")})
247 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
248 public Response getLatestVersionNotAbstractCheckoutComponentsIdesOnly(@PathParam("componentType") final String componentType,
249 @Context final HttpServletRequest request,
250 @QueryParam("internalComponentType") String internalComponentType,
251 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
252 @Parameter(description = "uid list", required = true) String data)
254 String url = request.getMethod() + " " + request.getRequestURI();
255 log.debug("(get) Start handle request of {}", url);
257 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
258 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
259 Either<List<Component>, ResponseFormat> actionResponse = businessLogic
260 .getLatestVersionNotAbstractComponentsMetadata(false, HighestFilterEnum.HIGHEST_ONLY, componentTypeEnum, internalComponentType,
262 if (actionResponse.isRight()) {
263 log.debug(FAILED_TO_GET_ALL_NON_ABSTRACT, componentType);
264 return buildErrorResponse(actionResponse.right().value());
266 List<UiLeftPaletteComponent> uiLeftPaletteComponents = getComponentsUtils()
267 .convertComponentToUiLeftPaletteComponentObject(actionResponse.left().value());
268 Map<String, Map<String, List<UiLeftPaletteComponent>>> categorizedComponents = getCategorizedComponents(uiLeftPaletteComponents);
269 Object components = RepresentationUtils.toRepresentation(categorizedComponents);
270 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), components);
271 } catch (Exception e) {
272 BeEcompErrorManager.getInstance().logBeRestApiGeneralError(GET_CERTIFIED_NON_ABSTRACT + componentType);
273 log.debug(GET_CERTIFIED_NOT_ABSTRACT_COMPONENTS_FAILED_WITH_EXCEPTION, e);
279 @Path("/{componentType}/{componentId}/componentInstances")
280 @Consumes(MediaType.APPLICATION_JSON)
281 @Produces(MediaType.APPLICATION_JSON)
282 @Operation(description = "Get Component instances", method = "GET", summary = "Returns component instances", responses = {
283 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
284 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
285 @ApiResponse(responseCode = "404", description = "Component not found")})
286 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
287 public Response getComponentInstancesFilteredByPropertiesAndInputs(@PathParam("componentType") final String componentType,
288 @PathParam("componentId") final String componentId,
289 @Context final HttpServletRequest request,
290 @QueryParam("searchText") String searchText,
291 @HeaderParam(value = Constants.USER_ID_HEADER) String userId,
292 @Parameter(description = "uid" + " " + "list", required = true) String data)
294 String url = request.getMethod() + " " + request.getRequestURI();
295 log.debug("(GET) Start handle request of {}", url);
297 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
298 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
299 Either<List<ComponentInstance>, ResponseFormat> actionResponse = businessLogic
300 .getComponentInstancesFilteredByPropertiesAndInputs(componentId, userId);
301 if (actionResponse.isRight()) {
302 log.debug("failed to get all component instances filtered by properties and inputs", componentType);
303 return buildErrorResponse(actionResponse.right().value());
305 Object components = RepresentationUtils.toRepresentation(actionResponse.left().value());
306 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), components);
307 } catch (Exception e) {
308 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Component Instances filtered by properties & inputs" + componentType);
309 log.debug("getComponentInstancesFilteredByPropertiesAndInputs failed with exception", e);
315 * 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
316 * of the string "properties, inputs" will return component with the list of properties and inputs.
318 * @param componentType
320 * @param dataParamsToReturn
326 @Path("/{componentType}/{componentId}/filteredDataByParams")
327 @Consumes(MediaType.APPLICATION_JSON)
328 @Produces(MediaType.APPLICATION_JSON)
329 @Operation(description = "Retrieve Resource", method = "GET", summary = "Returns resource according to resourceId", responses = {
330 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Resource.class)))),
331 @ApiResponse(responseCode = "200", description = "Resource found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
332 @ApiResponse(responseCode = "404", description = "Resource not found")})
333 @ResponseView(mixin = {GroupCompositionMixin.class, PolicyCompositionMixin.class})
334 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
335 public Response getComponentDataFilteredByParams(@PathParam("componentType") final String componentType,
336 @PathParam("componentId") final String componentId,
337 @QueryParam("include") final List<String> dataParamsToReturn,
338 @Context final HttpServletRequest request,
339 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
340 String url = request.getMethod() + " " + request.getRequestURI();
341 log.debug(START_HANDLE_REQUEST_OF, url);
343 User modifier = new User();
344 modifier.setUserId(userId);
345 log.debug("modifier id is {}", userId);
347 String resourceIdLower = componentId.toLowerCase();
348 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
349 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
350 log.trace("get component with id {} filtered by ui params", componentId);
351 Either<UiComponentDataTransfer, ResponseFormat> actionResponse = businessLogic
352 .getComponentDataFilteredByParams(resourceIdLower, modifier, dataParamsToReturn);
353 if (actionResponse.isRight()) {
354 log.debug("failed to get component data filtered by ui params");
355 return buildErrorResponse(actionResponse.right().value());
357 RepresentationUtils.toRepresentation(actionResponse.left().value());
358 return buildOkResponse(actionResponse.left().value());
359 } catch (Exception e) {
360 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get component filtered by ui params");
361 log.debug("get resource failed with exception", e);
367 @Path("/{componentType}/{componentId}/filteredproperties/{propertyNameFragment}")
368 @Consumes(MediaType.APPLICATION_JSON)
369 @Produces(MediaType.APPLICATION_JSON)
370 @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 = {
371 @ApiResponse(content = @Content(array = @ArraySchema(schema = @Schema(implementation = Map.class)))),
372 @ApiResponse(responseCode = "200", description = "Component found"), @ApiResponse(responseCode = "403", description = "Restricted operation"),
373 @ApiResponse(responseCode = "404", description = "Component not found")})
374 @PermissionAllowed(AafPermission.PermNames.INTERNAL_ALL_VALUE)
375 public Response getFilteredComponentInstanceProperties(@PathParam("componentType") final String componentType,
376 @PathParam("componentId") final String componentId,
377 @PathParam("propertyNameFragment") final String propertyNameFragment,
378 @QueryParam("resourceType") List<String> resourceTypes,
379 @Context final HttpServletRequest request,
380 @HeaderParam(value = Constants.USER_ID_HEADER) String userId) throws IOException {
381 User user = new User();
382 user.setUserId(userId);
383 log.debug("User Id is {}", userId);
386 ComponentTypeEnum componentTypeEnum = ComponentTypeEnum.findByParamName(componentType);
387 ComponentBusinessLogic businessLogic = componentBusinessLogicProvider.getInstance(componentTypeEnum);
388 Map<FilterKeyEnum, List<String>> filters = new EnumMap<>(FilterKeyEnum.class);
389 List<String> propertyNameFragments = new ArrayList<>();
390 propertyNameFragments.add(propertyNameFragment);
391 filters.put(FilterKeyEnum.NAME_FRAGMENT, propertyNameFragments);
392 if (CollectionUtils.isNotEmpty(resourceTypes)) {
393 filters.put(FilterKeyEnum.RESOURCE_TYPE, resourceTypes);
395 Either<Map<String, List<IComponentInstanceConnectedElement>>, ResponseFormat> actionResponse = businessLogic
396 .getFilteredComponentInstanceProperties(componentId, filters, userId);
397 if (actionResponse.isRight()) {
398 response = buildErrorResponse(actionResponse.right().value());
401 Object resource = RepresentationUtils.toRepresentation(actionResponse.left().value());
402 return buildOkResponse(getComponentsUtils().getResponseFormat(ActionStatus.OK), resource);
403 } catch (Exception e) {
404 BeEcompErrorManager.getInstance().logBeRestApiGeneralError("Get Filtered Component Instance Properties");
405 log.debug("Getting of filtered component instance properties failed with exception", e);