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 final ComponentManager componentManager;
49 public ComponentsImpl() {
50 this.componentManager = ComponentManagerFactory.getInstance().createInterface();
53 public ComponentsImpl(ComponentManager componentManager) {
54 this.componentManager = componentManager;
58 public Response list(String vspId, String versionId, String user) {
60 Collection<ComponentEntity> components =
61 componentManager.listComponents(vspId, new Version(versionId));
63 MapComponentEntityToComponentDto mapper = new MapComponentEntityToComponentDto();
64 GenericCollectionWrapper<ComponentDto> results = new GenericCollectionWrapper<>();
65 for (ComponentEntity component : components) {
66 results.add(mapper.applyMapping(component, ComponentDto.class));
69 return Response.ok(results).build();
73 public Response deleteList(String vspId, String versionId, String user) {
74 componentManager.deleteComponents(vspId, new Version(versionId));
75 return Response.ok().build();
79 public Response create(ComponentRequestDto request, String vspId, String versionId, String user) {
81 ComponentEntity component =
82 new MapComponentRequestDtoToComponentEntity().applyMapping(request, ComponentEntity.class);
83 component.setVspId(vspId);
84 component.setVersion(new Version(versionId));
86 ComponentEntity createdComponent = componentManager.createComponent(component);
87 MapComponentEntityToComponentCreationDto mapping =
88 new MapComponentEntityToComponentCreationDto();
89 ComponentCreationDto createdComponentDto = mapping.applyMapping(createdComponent,
90 ComponentCreationDto.class);
92 .ok(createdComponent != null ? createdComponentDto : null)
97 public Response get(String vspId, String versionId, String componentId, String user) {
99 CompositionEntityResponse<ComponentData> response =
100 componentManager.getComponent(vspId, new Version(versionId), componentId);
102 CompositionEntityResponseDto<ComponentDto> responseDto = new CompositionEntityResponseDto<>();
103 new MapCompositionEntityResponseToDto<>(new MapComponentDataToComponentDto(),
104 ComponentDto.class).doMapping(response, responseDto);
105 return Response.ok(responseDto).build();
109 public Response delete(String vspId, String versionId, String componentId, String user) {
110 componentManager.deleteComponent(vspId, new Version(versionId), componentId);
111 return Response.ok().build();
115 public Response update(ComponentRequestDto request, String vspId, String versionId,
118 ComponentEntity componentEntity =
119 new MapComponentRequestDtoToComponentEntity().applyMapping(request, ComponentEntity.class);
120 componentEntity.setVspId(vspId);
121 componentEntity.setVersion(new Version(versionId));
122 componentEntity.setId(componentId);
124 CompositionEntityValidationData validationData =
125 componentManager.updateComponent(componentEntity);
126 return validationData != null && CollectionUtils.isNotEmpty(validationData.getErrors())
127 ? Response.status(Response.Status.EXPECTATION_FAILED).entity(
128 new MapCompositionEntityValidationDataToDto().applyMapping(validationData,
129 CompositionEntityValidationDataDto.class)).build() : Response.ok().build();
133 public Response getQuestionnaire(String vspId, String versionId, String componentId,
135 QuestionnaireResponse questionnaireResponse =
136 componentManager.getQuestionnaire(vspId, new Version(versionId), componentId);
138 QuestionnaireResponseDto result = new MapQuestionnaireResponseToQuestionnaireResponseDto()
139 .applyMapping(questionnaireResponse, QuestionnaireResponseDto.class);
140 return Response.ok(result).build();
144 public Response updateQuestionnaire(String questionnaireData, String vspId, String versionId,
145 String componentId, String user) {
147 .updateQuestionnaire(vspId, new Version(versionId), componentId, questionnaireData);
148 return Response.ok().build();