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.*;
33 import org.openecomp.sdcrests.vsp.rest.Components;
34 import org.openecomp.sdcrests.vsp.rest.mapping.*;
35 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
36 import org.springframework.context.annotation.Scope;
37 import org.springframework.stereotype.Service;
39 import javax.inject.Named;
40 import javax.ws.rs.core.Response;
41 import java.util.Collection;
44 @Service("components")
45 @Scope(value = "prototype")
46 public class ComponentsImpl implements Components {
47 private ComponentManager componentManager =
48 ComponentManagerFactory.getInstance().createInterface();
51 public Response list(String vspId, String versionId, String user) {
53 Collection<ComponentEntity> components =
54 componentManager.listComponents(vspId, new Version(versionId));
56 MapComponentEntityToComponentDto mapper = new MapComponentEntityToComponentDto();
57 GenericCollectionWrapper<ComponentDto> results = new GenericCollectionWrapper<>();
58 for (ComponentEntity component : components) {
59 results.add(mapper.applyMapping(component, ComponentDto.class));
62 return Response.ok(results).build();
66 public Response deleteList(String vspId, String versionId, String user) {
67 componentManager.deleteComponents(vspId, new Version(versionId));
68 return Response.ok().build();
72 public Response create(ComponentRequestDto request, String vspId, String versionId, String user) {
74 ComponentEntity component =
75 new MapComponentRequestDtoToComponentEntity().applyMapping(request, ComponentEntity.class);
76 component.setVspId(vspId);
77 component.setVersion(new Version(versionId));
79 ComponentEntity createdComponent = componentManager.createComponent(component);
80 MapComponentEntityToComponentCreationDto mapping =
81 new MapComponentEntityToComponentCreationDto();
82 ComponentCreationDto createdComponentDto = mapping.applyMapping(createdComponent,
83 ComponentCreationDto.class);
85 .ok(createdComponent != null ? createdComponentDto : null)
90 public Response get(String vspId, String versionId, String componentId, String user) {
92 CompositionEntityResponse<ComponentData> response =
93 componentManager.getComponent(vspId, new Version(versionId), componentId);
95 CompositionEntityResponseDto<ComponentDto> responseDto = new CompositionEntityResponseDto<>();
96 new MapCompositionEntityResponseToDto<>(new MapComponentDataToComponentDto(),
97 ComponentDto.class).doMapping(response, responseDto);
98 return Response.ok(responseDto).build();
102 public Response delete(String vspId, String versionId, String componentId, String user) {
103 componentManager.deleteComponent(vspId, new Version(versionId), componentId);
104 return Response.ok().build();
108 public Response update(ComponentRequestDto request, String vspId, String versionId,
111 ComponentEntity componentEntity =
112 new MapComponentRequestDtoToComponentEntity().applyMapping(request, ComponentEntity.class);
113 componentEntity.setVspId(vspId);
114 componentEntity.setVersion(new Version(versionId));
115 componentEntity.setId(componentId);
117 CompositionEntityValidationData validationData =
118 componentManager.updateComponent(componentEntity);
119 return validationData != null && CollectionUtils.isNotEmpty(validationData.getErrors())
120 ? Response.status(Response.Status.EXPECTATION_FAILED).entity(
121 new MapCompositionEntityValidationDataToDto().applyMapping(validationData,
122 CompositionEntityValidationDataDto.class)).build() : Response.ok().build();
126 public Response getQuestionnaire(String vspId, String versionId, String componentId,
128 QuestionnaireResponse questionnaireResponse =
129 componentManager.getQuestionnaire(vspId, new Version(versionId), componentId);
131 QuestionnaireResponseDto result = new MapQuestionnaireResponseToQuestionnaireResponseDto()
132 .applyMapping(questionnaireResponse, QuestionnaireResponseDto.class);
133 return Response.ok(result).build();
137 public Response updateQuestionnaire(String questionnaireData, String vspId, String versionId,
138 String componentId, String user) {
140 .updateQuestionnaire(vspId, new Version(versionId), componentId, questionnaireData);
141 return Response.ok().build();