Added oparent to sdc main
[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 / ComponentDependenciesImpl.java
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdcrests.vsp.rest.services;
18
19 import org.openecomp.sdc.vendorsoftwareproduct.ComponentDependencyModelManager;
20 import org.openecomp.sdc.vendorsoftwareproduct.ComponentDependencyModelManagerFactory;
21 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentDependencyModelEntity;
22 import org.openecomp.sdc.versioning.dao.types.Version;
23 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentDependencyCreationDto;
24 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentDependencyModel;
25 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentDependencyResponseDto;
26 import org.openecomp.sdcrests.vsp.rest.ComponentDependencies;
27 import org.openecomp.sdcrests.vsp.rest.mapping.MapComponentDependencyEntityToCreationDto;
28 import org.openecomp.sdcrests.vsp.rest.mapping.MapComponentDependencyEntityToDto;
29 import org.openecomp.sdcrests.vsp.rest.mapping.MapComponentDependencyModelRequestToEntity;
30 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
31 import org.springframework.context.annotation.Scope;
32 import org.springframework.stereotype.Service;
33
34 import javax.inject.Named;
35 import javax.ws.rs.core.Response;
36 import java.util.Collection;
37
38 @Named
39 @Service("componentDependencies")
40 @Scope(value = "prototype")
41 public class ComponentDependenciesImpl implements ComponentDependencies {
42
43   private final ComponentDependencyModelManager componentDependencyModelManager =
44       ComponentDependencyModelManagerFactory.getInstance().createInterface();
45
46   @Override
47   public Response create(ComponentDependencyModel request, String vspId, String versionId,
48                          String user) {
49     final Version version = new Version(versionId);
50
51     ComponentDependencyModelEntity modelEntity =
52         new MapComponentDependencyModelRequestToEntity().applyMapping(request,
53             ComponentDependencyModelEntity.class);
54
55     modelEntity.setVspId(vspId);
56     modelEntity.setVersion(version);
57
58     ComponentDependencyModelEntity componentDependency =
59         componentDependencyModelManager.createComponentDependency(modelEntity, vspId, version);
60
61     MapComponentDependencyEntityToCreationDto mapping =
62         new MapComponentDependencyEntityToCreationDto();
63     ComponentDependencyCreationDto createdComponentDependencyDto = mapping.applyMapping(
64         componentDependency, ComponentDependencyCreationDto.class);
65     return Response.ok(componentDependency != null ? createdComponentDependencyDto : null)
66         .build();
67   }
68
69   @Override
70   public Response list(String vspId, String versionId, String user) {
71
72     Version vspVersion = new Version(versionId);
73
74     Collection<ComponentDependencyModelEntity> componentDependencies =
75         componentDependencyModelManager.list(vspId, vspVersion);
76
77     MapComponentDependencyEntityToDto mapper = new MapComponentDependencyEntityToDto();
78     GenericCollectionWrapper<ComponentDependencyResponseDto> results = new GenericCollectionWrapper
79         <>();
80     for (ComponentDependencyModelEntity entity : componentDependencies) {
81       results.add(mapper.applyMapping(entity, ComponentDependencyResponseDto.class));
82     }
83
84     return Response.ok(results).build();
85   }
86
87   @Override
88   public Response delete(String vspId, String versionId, String dependencyId, String user) {
89
90     Version vspVersion = new Version(versionId);
91     componentDependencyModelManager.delete(vspId, vspVersion, dependencyId);
92     return Response.ok().build();
93   }
94
95   @Override
96   public Response update(ComponentDependencyModel request, String vspId, String versionId, String
97       dependencyId, String user) {
98
99     final Version version = new Version(versionId);
100     ComponentDependencyModelEntity modelEntity =
101         new MapComponentDependencyModelRequestToEntity().applyMapping(request,
102             ComponentDependencyModelEntity.class);
103
104     modelEntity.setId(dependencyId);
105     modelEntity.setVspId(vspId);
106     modelEntity.setVersion(version);
107     componentDependencyModelManager.update(modelEntity);
108     return Response.ok().build();
109   }
110
111   @Override
112   public Response get(String vspId, String version, String dependencyId, String user) {
113
114     ComponentDependencyModelEntity componentDependencyModelEntity = componentDependencyModelManager
115         .get(vspId, new Version(version), dependencyId);
116
117     MapComponentDependencyEntityToDto mapper = new MapComponentDependencyEntityToDto();
118     ComponentDependencyResponseDto componentDependencyResponseDto =
119         mapper.applyMapping(componentDependencyModelEntity, ComponentDependencyResponseDto.class);
120
121     return Response.ok(componentDependencyModelEntity != null ? componentDependencyResponseDto :
122         null).build();
123   }
124
125 }