Implement improved MinIo client
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-software-products-rest / vendor-software-products-rest-services / src / main / java / org / openecomp / sdcrests / vsp / rest / services / ComponentsImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20 package org.openecomp.sdcrests.vsp.rest.services;
21
22 import java.util.Collection;
23 import javax.inject.Named;
24 import javax.ws.rs.core.Response;
25 import org.apache.commons.collections4.CollectionUtils;
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;
51
52 @Named
53 @Service("components")
54 @Scope(value = "prototype")
55 public class ComponentsImpl implements Components {
56
57     private final ComponentManager componentManager;
58
59     public ComponentsImpl() {
60         this.componentManager = ComponentManagerFactory.getInstance().createInterface();
61     }
62
63     public ComponentsImpl(ComponentManager componentManager) {
64         this.componentManager = componentManager;
65     }
66
67     @Override
68     public Response list(String vspId, String versionId, String user) {
69         Collection<ComponentEntity> components = componentManager.listComponents(vspId, new Version(versionId));
70         MapComponentEntityToComponentDto mapper = new MapComponentEntityToComponentDto();
71         GenericCollectionWrapper<ComponentDto> results = new GenericCollectionWrapper<>();
72         for (ComponentEntity component : components) {
73             results.add(mapper.applyMapping(component, ComponentDto.class));
74         }
75         return Response.ok(results).build();
76     }
77
78     @Override
79     public Response deleteList(String vspId, String versionId, String user) {
80         componentManager.deleteComponents(vspId, new Version(versionId));
81         return Response.ok().build();
82     }
83
84     @Override
85     public Response create(ComponentRequestDto request, String vspId, String versionId, String user) {
86         ComponentEntity component = new MapComponentRequestDtoToComponentEntity().applyMapping(request, ComponentEntity.class);
87         component.setVspId(vspId);
88         component.setVersion(new Version(versionId));
89         ComponentEntity createdComponent = componentManager.createComponent(component);
90         MapComponentEntityToComponentCreationDto mapping = new MapComponentEntityToComponentCreationDto();
91         ComponentCreationDto createdComponentDto = mapping.applyMapping(createdComponent, ComponentCreationDto.class);
92         return Response.ok(createdComponent != null ? createdComponentDto : null).build();
93     }
94
95     @Override
96     public Response get(String vspId, String versionId, String componentId, String user) {
97         CompositionEntityResponse<ComponentData> response = componentManager.getComponent(vspId, new Version(versionId), componentId);
98         CompositionEntityResponseDto<ComponentDto> responseDto = new CompositionEntityResponseDto<>();
99         new MapCompositionEntityResponseToDto<>(new MapComponentDataToComponentDto(), ComponentDto.class).doMapping(response, responseDto);
100         return Response.ok(responseDto).build();
101     }
102
103     @Override
104     public Response delete(String vspId, String versionId, String componentId, String user) {
105         componentManager.deleteComponent(vspId, new Version(versionId), componentId);
106         return Response.ok().build();
107     }
108
109     @Override
110     public Response update(ComponentRequestDto request, String vspId, String versionId, String componentId, String user) {
111         ComponentEntity componentEntity = new MapComponentRequestDtoToComponentEntity().applyMapping(request, ComponentEntity.class);
112         componentEntity.setVspId(vspId);
113         componentEntity.setVersion(new Version(versionId));
114         componentEntity.setId(componentId);
115         CompositionEntityValidationData validationData = componentManager.updateComponent(componentEntity);
116         return validationData != null && CollectionUtils.isNotEmpty(validationData.getErrors()) ? Response.status(Response.Status.EXPECTATION_FAILED)
117             .entity(new MapCompositionEntityValidationDataToDto().applyMapping(validationData, CompositionEntityValidationDataDto.class)).build()
118             : Response.ok().build();
119     }
120
121     @Override
122     public Response getQuestionnaire(String vspId, String versionId, String componentId, String user) {
123         QuestionnaireResponse questionnaireResponse = componentManager.getQuestionnaire(vspId, new Version(versionId), componentId);
124         QuestionnaireResponseDto result = new MapQuestionnaireResponseToQuestionnaireResponseDto()
125             .applyMapping(questionnaireResponse, QuestionnaireResponseDto.class);
126         return Response.ok(result).build();
127     }
128
129     @Override
130     public Response updateQuestionnaire(String questionnaireData, String vspId, String versionId, String componentId, String user) {
131         componentManager.updateQuestionnaire(vspId, new Version(versionId), componentId, questionnaireData);
132         return Response.ok().build();
133     }
134 }