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.logging.context.MdcUtil;
25 import org.openecomp.sdc.logging.types.LoggerServiceName;
26 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
27 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManagerFactory;
28 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
30 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
31 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
32 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
33 import org.openecomp.sdc.versioning.dao.types.Version;
34 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentCreationDto;
35 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentDto;
36 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentRequestDto;
37 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityResponseDto;
38 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityValidationDataDto;
39 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireResponseDto;
40 import org.openecomp.sdcrests.vsp.rest.Components;
41 import org.openecomp.sdcrests.vsp.rest.mapping.MapComponentDataToComponentDto;
42 import org.openecomp.sdcrests.vsp.rest.mapping.MapComponentEntityToComponentCreationDto;
43 import org.openecomp.sdcrests.vsp.rest.mapping.MapComponentEntityToComponentDto;
44 import org.openecomp.sdcrests.vsp.rest.mapping.MapComponentRequestDtoToComponentEntity;
45 import org.openecomp.sdcrests.vsp.rest.mapping.MapCompositionEntityResponseToDto;
46 import org.openecomp.sdcrests.vsp.rest.mapping.MapCompositionEntityValidationDataToDto;
47 import org.openecomp.sdcrests.vsp.rest.mapping.MapQuestionnaireResponseToQuestionnaireResponseDto;
48 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
49 import org.springframework.context.annotation.Scope;
50 import org.springframework.stereotype.Service;
52 import javax.inject.Named;
53 import javax.ws.rs.core.Response;
54 import java.util.Collection;
57 @Service("components")
58 @Scope(value = "prototype")
59 public class ComponentsImpl implements Components {
60 private ComponentManager componentManager =
61 ComponentManagerFactory.getInstance().createInterface();
64 public Response list(String vspId, String versionId, String user) {
65 MdcUtil.initMdc(LoggerServiceName.List_Components.toString());
66 Collection<ComponentEntity> components =
67 componentManager.listComponents(vspId, new Version(versionId));
69 MapComponentEntityToComponentDto mapper = new MapComponentEntityToComponentDto();
70 GenericCollectionWrapper<ComponentDto> results = new GenericCollectionWrapper<>();
71 for (ComponentEntity component : components) {
72 results.add(mapper.applyMapping(component, ComponentDto.class));
75 return Response.ok(results).build();
79 public Response deleteList(String vspId, String versionId, String user) {
80 MdcUtil.initMdc(LoggerServiceName.Delete_List_Components.toString());
81 componentManager.deleteComponents(vspId, new Version(versionId));
82 return Response.ok().build();
86 public Response create(ComponentRequestDto request, String vspId, String versionId, String user) {
87 MdcUtil.initMdc(LoggerServiceName.Create_Component.toString());
88 ComponentEntity component =
89 new MapComponentRequestDtoToComponentEntity().applyMapping(request, ComponentEntity.class);
90 component.setVspId(vspId);
91 component.setVersion(new Version(versionId));
93 ComponentEntity createdComponent = componentManager.createComponent(component);
94 MapComponentEntityToComponentCreationDto mapping =
95 new MapComponentEntityToComponentCreationDto();
96 ComponentCreationDto createdComponentDto = mapping.applyMapping(createdComponent,
97 ComponentCreationDto.class);
99 .ok(createdComponent != null ? createdComponentDto : null)
104 public Response get(String vspId, String versionId, String componentId, String user) {
105 MdcUtil.initMdc(LoggerServiceName.Get_Component.toString());
106 CompositionEntityResponse<ComponentData> response =
107 componentManager.getComponent(vspId, new Version(versionId), componentId);
109 CompositionEntityResponseDto<ComponentDto> responseDto = new CompositionEntityResponseDto<>();
110 new MapCompositionEntityResponseToDto<>(new MapComponentDataToComponentDto(),
111 ComponentDto.class).doMapping(response, responseDto);
112 return Response.ok(responseDto).build();
116 public Response delete(String vspId, String versionId, String componentId, String user) {
117 MdcUtil.initMdc(LoggerServiceName.Delete_Component.toString());
118 componentManager.deleteComponent(vspId, new Version(versionId), componentId);
119 return Response.ok().build();
123 public Response update(ComponentRequestDto request, String vspId, String versionId,
126 MdcUtil.initMdc(LoggerServiceName.Update_Component.toString());
127 ComponentEntity componentEntity =
128 new MapComponentRequestDtoToComponentEntity().applyMapping(request, ComponentEntity.class);
129 componentEntity.setVspId(vspId);
130 componentEntity.setVersion(new Version(versionId));
131 componentEntity.setId(componentId);
133 CompositionEntityValidationData validationData =
134 componentManager.updateComponent(componentEntity);
135 return validationData != null && CollectionUtils.isNotEmpty(validationData.getErrors())
136 ? Response.status(Response.Status.EXPECTATION_FAILED).entity(
137 new MapCompositionEntityValidationDataToDto().applyMapping(validationData,
138 CompositionEntityValidationDataDto.class)).build() : Response.ok().build();
142 public Response getQuestionnaire(String vspId, String versionId, String componentId,
144 MdcUtil.initMdc(LoggerServiceName.Get_Questionnaire_Component.toString());
145 QuestionnaireResponse questionnaireResponse =
146 componentManager.getQuestionnaire(vspId, new Version(versionId), componentId);
148 QuestionnaireResponseDto result = new MapQuestionnaireResponseToQuestionnaireResponseDto()
149 .applyMapping(questionnaireResponse, QuestionnaireResponseDto.class);
150 return Response.ok(result).build();
154 public Response updateQuestionnaire(String questionnaireData, String vspId, String versionId,
155 String componentId, String user) {
156 MdcUtil.initMdc(LoggerServiceName.Update_Questionnaire_Component.toString());
158 .updateQuestionnaire(vspId, new Version(versionId), componentId, questionnaireData);
159 return Response.ok().build();