fefa52842ed0dace38ec5206dc5ab0c963dfc088
[sdc.git] /
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.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;
38
39 import javax.inject.Named;
40 import javax.ws.rs.core.Response;
41 import java.util.Collection;
42
43 @Named
44 @Service("components")
45 @Scope(value = "prototype")
46 public class ComponentsImpl implements Components {
47   private final ComponentManager componentManager;
48
49   public ComponentsImpl() {
50     this.componentManager = ComponentManagerFactory.getInstance().createInterface();
51   }
52
53   public ComponentsImpl(ComponentManager componentManager) {
54     this.componentManager = componentManager;
55   }
56
57   @Override
58   public Response list(String vspId, String versionId, String user) {
59
60     Collection<ComponentEntity> components =
61         componentManager.listComponents(vspId, new Version(versionId));
62
63     MapComponentEntityToComponentDto mapper = new MapComponentEntityToComponentDto();
64     GenericCollectionWrapper<ComponentDto> results = new GenericCollectionWrapper<>();
65     for (ComponentEntity component : components) {
66       results.add(mapper.applyMapping(component, ComponentDto.class));
67     }
68
69     return Response.ok(results).build();
70   }
71
72   @Override
73   public Response deleteList(String vspId, String versionId, String user) {
74     componentManager.deleteComponents(vspId, new Version(versionId));
75     return Response.ok().build();
76   }
77
78   @Override
79   public Response create(ComponentRequestDto request, String vspId, String versionId, String user) {
80
81     ComponentEntity component =
82         new MapComponentRequestDtoToComponentEntity().applyMapping(request, ComponentEntity.class);
83     component.setVspId(vspId);
84     component.setVersion(new Version(versionId));
85
86     ComponentEntity createdComponent = componentManager.createComponent(component);
87     MapComponentEntityToComponentCreationDto mapping =
88         new MapComponentEntityToComponentCreationDto();
89     ComponentCreationDto createdComponentDto = mapping.applyMapping(createdComponent,
90         ComponentCreationDto.class);
91     return Response
92         .ok(createdComponent != null ? createdComponentDto : null)
93         .build();
94   }
95
96   @Override
97   public Response get(String vspId, String versionId, String componentId, String user) {
98
99     CompositionEntityResponse<ComponentData> response =
100         componentManager.getComponent(vspId, new Version(versionId), componentId);
101
102     CompositionEntityResponseDto<ComponentDto> responseDto = new CompositionEntityResponseDto<>();
103     new MapCompositionEntityResponseToDto<>(new MapComponentDataToComponentDto(),
104         ComponentDto.class).doMapping(response, responseDto);
105     return Response.ok(responseDto).build();
106   }
107
108   @Override
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();
112   }
113
114   @Override
115   public Response update(ComponentRequestDto request, String vspId, String versionId,
116                          String componentId,
117                          String user) {
118     ComponentEntity componentEntity =
119         new MapComponentRequestDtoToComponentEntity().applyMapping(request, ComponentEntity.class);
120     componentEntity.setVspId(vspId);
121     componentEntity.setVersion(new Version(versionId));
122     componentEntity.setId(componentId);
123
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();
130   }
131
132   @Override
133   public Response getQuestionnaire(String vspId, String versionId, String componentId,
134                                    String user) {
135     QuestionnaireResponse questionnaireResponse =
136         componentManager.getQuestionnaire(vspId, new Version(versionId), componentId);
137
138     QuestionnaireResponseDto result = new MapQuestionnaireResponseToQuestionnaireResponseDto()
139         .applyMapping(questionnaireResponse, QuestionnaireResponseDto.class);
140     return Response.ok(result).build();
141   }
142
143   @Override
144   public Response updateQuestionnaire(String questionnaireData, String vspId, String versionId,
145                                       String componentId, String user) {
146     componentManager
147         .updateQuestionnaire(vspId, new Version(versionId), componentId, questionnaireData);
148     return Response.ok().build();
149   }
150 }