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.sdcrests.vsp.rest.services;
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
25 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManagerFactory;
26 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
27 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
28 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
30 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
31 import org.openecomp.sdc.versioning.dao.types.Version;
32 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentCreationDto;
33 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentDto;
34 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentRequestDto;
35 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityResponseDto;
36 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityValidationDataDto;
37 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireResponseDto;
38 import org.openecomp.sdcrests.vsp.rest.Components;
39 import org.openecomp.sdcrests.vsp.rest.mapping.MapComponentDataToComponentDto;
40 import org.openecomp.sdcrests.vsp.rest.mapping.MapComponentEntityToComponentCreationDto;
41 import org.openecomp.sdcrests.vsp.rest.mapping.MapComponentEntityToComponentDto;
42 import org.openecomp.sdcrests.vsp.rest.mapping.MapComponentRequestDtoToComponentEntity;
43 import org.openecomp.sdcrests.vsp.rest.mapping.MapCompositionEntityResponseToDto;
44 import org.openecomp.sdcrests.vsp.rest.mapping.MapCompositionEntityValidationDataToDto;
45 import org.openecomp.sdcrests.vsp.rest.mapping.MapQuestionnaireResponseToQuestionnaireResponseDto;
46 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
47 import org.springframework.context.annotation.Scope;
48 import org.springframework.stereotype.Service;
50 import javax.inject.Named;
51 import javax.ws.rs.core.Response;
52 import java.util.Collection;
55 @Service("components")
56 @Scope(value = "prototype")
57 public class ComponentsImpl implements Components {
58 private ComponentManager componentManager =
59 ComponentManagerFactory.getInstance().createInterface();
62 public Response list(String vspId, String versionId, String user) {
64 Collection<ComponentEntity> components =
65 componentManager.listComponents(vspId, new Version(versionId));
67 MapComponentEntityToComponentDto mapper = new MapComponentEntityToComponentDto();
68 GenericCollectionWrapper<ComponentDto> results = new GenericCollectionWrapper<>();
69 for (ComponentEntity component : components) {
70 results.add(mapper.applyMapping(component, ComponentDto.class));
73 return Response.ok(results).build();
77 public Response deleteList(String vspId, String versionId, String user) {
78 componentManager.deleteComponents(vspId, new Version(versionId));
79 return Response.ok().build();
83 public Response create(ComponentRequestDto request, String vspId, String versionId, String user) {
85 ComponentEntity component =
86 new MapComponentRequestDtoToComponentEntity().applyMapping(request, ComponentEntity.class);
87 component.setVspId(vspId);
88 component.setVersion(new Version(versionId));
90 ComponentEntity createdComponent = componentManager.createComponent(component);
91 MapComponentEntityToComponentCreationDto mapping =
92 new MapComponentEntityToComponentCreationDto();
93 ComponentCreationDto createdComponentDto = mapping.applyMapping(createdComponent,
94 ComponentCreationDto.class);
96 .ok(createdComponent != null ? createdComponentDto : null)
101 public Response get(String vspId, String versionId, String componentId, String user) {
103 CompositionEntityResponse<ComponentData> response =
104 componentManager.getComponent(vspId, new Version(versionId), componentId);
106 CompositionEntityResponseDto<ComponentDto> responseDto = new CompositionEntityResponseDto<>();
107 new MapCompositionEntityResponseToDto<>(new MapComponentDataToComponentDto(),
108 ComponentDto.class).doMapping(response, responseDto);
109 return Response.ok(responseDto).build();
113 public Response delete(String vspId, String versionId, String componentId, String user) {
114 componentManager.deleteComponent(vspId, new Version(versionId), componentId);
115 return Response.ok().build();
119 public Response update(ComponentRequestDto request, String vspId, String versionId,
122 ComponentEntity componentEntity =
123 new MapComponentRequestDtoToComponentEntity().applyMapping(request, ComponentEntity.class);
124 componentEntity.setVspId(vspId);
125 componentEntity.setVersion(new Version(versionId));
126 componentEntity.setId(componentId);
128 CompositionEntityValidationData validationData =
129 componentManager.updateComponent(componentEntity);
130 return validationData != null && CollectionUtils.isNotEmpty(validationData.getErrors())
131 ? Response.status(Response.Status.EXPECTATION_FAILED).entity(
132 new MapCompositionEntityValidationDataToDto().applyMapping(validationData,
133 CompositionEntityValidationDataDto.class)).build() : Response.ok().build();
137 public Response getQuestionnaire(String vspId, String versionId, String componentId,
139 QuestionnaireResponse questionnaireResponse =
140 componentManager.getQuestionnaire(vspId, new Version(versionId), componentId);
142 QuestionnaireResponseDto result = new MapQuestionnaireResponseToQuestionnaireResponseDto()
143 .applyMapping(questionnaireResponse, QuestionnaireResponseDto.class);
144 return Response.ok(result).build();
148 public Response updateQuestionnaire(String questionnaireData, String vspId, String versionId,
149 String componentId, String user) {
151 .updateQuestionnaire(vspId, new Version(versionId), componentId, questionnaireData);
152 return Response.ok().build();