push addional code
[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
21 package org.openecomp.sdcrests.vsp.rest.services;
22
23 import org.apache.commons.collections4.CollectionUtils;
24 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
25 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
26 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
27 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityValidationData;
28 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
30 import org.openecomp.sdc.versioning.dao.types.Version;
31 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentDto;
32 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentRequestDto;
33 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityResponseDto;
34 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityValidationDataDto;
35 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireResponseDto;
36 import org.openecomp.sdcrests.vsp.rest.Components;
37 import org.openecomp.sdcrests.vsp.rest.mapping.MapComponentDataToComponentDto;
38 import org.openecomp.sdcrests.vsp.rest.mapping.MapComponentEntityToComponentDto;
39 import org.openecomp.sdcrests.vsp.rest.mapping.MapComponentRequestDtoToComponentEntity;
40 import org.openecomp.sdcrests.vsp.rest.mapping.MapCompositionEntityResponseToDto;
41 import org.openecomp.sdcrests.vsp.rest.mapping.MapCompositionEntityValidationDataToDto;
42 import org.openecomp.sdcrests.vsp.rest.mapping.MapQuestionnaireResponseToQuestionnaireResponseDto;
43 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
44 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
45
46 import org.springframework.beans.factory.annotation.Autowired;
47 import org.springframework.context.annotation.Scope;
48 import org.springframework.stereotype.Service;
49
50 import java.util.Collection;
51 import javax.inject.Named;
52 import javax.ws.rs.core.Response;
53
54
55 @Named
56 @Service("components")
57 @Scope(value = "prototype")
58 public class ComponentsImpl implements Components {
59   @Autowired
60   private VendorSoftwareProductManager vendorSoftwareProductManager;
61
62   @Override
63   public Response list(String vspId, String version, String user) {
64     Collection<ComponentEntity> components =
65         vendorSoftwareProductManager.listComponents(vspId, Version.valueOf(version), user);
66
67     MapComponentEntityToComponentDto mapper = new MapComponentEntityToComponentDto();
68     GenericCollectionWrapper<ComponentDto> results = new GenericCollectionWrapper<>();
69     for (ComponentEntity component : components) {
70       results.add(mapper.applyMapping(component, ComponentDto.class));
71     }
72
73     return Response.ok(results).build();
74   }
75
76   @Override
77   public Response deleteList(String vspId, String user) {
78     vendorSoftwareProductManager.deleteComponents(vspId, user);
79     return Response.ok().build();
80   }
81
82   @Override
83   public Response create(ComponentRequestDto request, String vspId, String user) {
84     ComponentEntity component =
85         new MapComponentRequestDtoToComponentEntity().applyMapping(request, ComponentEntity.class);
86     component.setVspId(vspId);
87     ComponentEntity createdComponent =
88         vendorSoftwareProductManager.createComponent(component, user);
89     return Response
90         .ok(createdComponent != null ? new StringWrapperResponse(createdComponent.getId()) : null)
91         .build();
92   }
93
94   @Override
95   public Response get(String vspId, String componentId, String version, String user) {
96     CompositionEntityResponse<ComponentData> response = vendorSoftwareProductManager
97         .getComponent(vspId, Version.valueOf(version), componentId, user);
98
99     CompositionEntityResponseDto<ComponentDto> responseDto = new CompositionEntityResponseDto<>();
100     new MapCompositionEntityResponseToDto<>(new MapComponentDataToComponentDto(),
101         ComponentDto.class).doMapping(response, responseDto);
102     return Response.ok(responseDto).build();
103   }
104
105   @Override
106   public Response delete(String vspId, String componentId, String user) {
107     vendorSoftwareProductManager.deleteComponent(vspId, componentId, user);
108     return Response.ok().build();
109   }
110
111   @Override
112   public Response update(ComponentRequestDto request, String vspId, String componentId,
113                          String user) {
114     ComponentEntity componentEntity =
115         new MapComponentRequestDtoToComponentEntity().applyMapping(request, ComponentEntity.class);
116     componentEntity.setVspId(vspId);
117     componentEntity.setId(componentId);
118
119     CompositionEntityValidationData validationData =
120         vendorSoftwareProductManager.updateComponent(componentEntity, user);
121     return validationData != null && CollectionUtils.isNotEmpty(validationData.getErrors())
122         ? Response.status(Response.Status.EXPECTATION_FAILED).entity(
123             new MapCompositionEntityValidationDataToDto()
124                 .applyMapping(validationData, CompositionEntityValidationDataDto.class)).build() :
125         Response.ok().build();
126   }
127
128   @Override
129   public Response getQuestionnaire(String vspId, String componentId, String version, String user) {
130     QuestionnaireResponse questionnaireResponse = vendorSoftwareProductManager
131         .getComponentQuestionnaire(vspId, Version.valueOf(version), componentId, user);
132
133     QuestionnaireResponseDto result = new MapQuestionnaireResponseToQuestionnaireResponseDto()
134         .applyMapping(questionnaireResponse, QuestionnaireResponseDto.class);
135     return Response.ok(result).build();
136   }
137
138   @Override
139   public Response updateQuestionnaire(String questionnaireData, String vspId, String componentId,
140                                       String user) {
141     vendorSoftwareProductManager
142         .updateComponentQuestionnaire(vspId, componentId, questionnaireData, user);
143     return Response.ok().build();
144   }
145 }