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 / NicsImpl.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.NicEntity;
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.Nic;
30 import org.openecomp.sdc.versioning.dao.types.Version;
31
32 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityResponseDto;
33 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityValidationDataDto;
34 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicDto;
35 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicRequestDto;
36 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireResponseDto;
37 import org.openecomp.sdcrests.vsp.rest.Nics;
38 import org.openecomp.sdcrests.vsp.rest.mapping.MapCompositionEntityResponseToDto;
39 import org.openecomp.sdcrests.vsp.rest.mapping.MapCompositionEntityValidationDataToDto;
40 import org.openecomp.sdcrests.vsp.rest.mapping.MapNicEntityToNicDto;
41 import org.openecomp.sdcrests.vsp.rest.mapping.MapNicRequestDtoToNicEntity;
42 import org.openecomp.sdcrests.vsp.rest.mapping.MapNicToNicDto;
43 import org.openecomp.sdcrests.vsp.rest.mapping.MapQuestionnaireResponseToQuestionnaireResponseDto;
44 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
45 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
46
47 import org.springframework.beans.factory.annotation.Autowired;
48 import org.springframework.context.annotation.Scope;
49 import org.springframework.stereotype.Service;
50
51 import java.util.Collection;
52 import javax.inject.Named;
53 import javax.ws.rs.core.Response;
54
55
56 @Named
57 @Service("nics")
58 @Scope(value = "prototype")
59 public class NicsImpl implements Nics {
60   @Autowired
61   private VendorSoftwareProductManager vendorSoftwareProductManager;
62
63   @Override
64   public Response list(String vspId, String componentId, String version, String user) {
65     Collection<NicEntity> nics =
66         vendorSoftwareProductManager.listNics(vspId, Version.valueOf(version), componentId, user);
67
68     MapNicEntityToNicDto mapper = new MapNicEntityToNicDto();
69     GenericCollectionWrapper<NicDto> results = new GenericCollectionWrapper<>();
70     for (NicEntity nic : nics) {
71       results.add(mapper.applyMapping(nic, NicDto.class));
72     }
73
74     return Response.ok(results).build();
75   }
76
77   @Override
78   public Response create(NicRequestDto request, String vspId, String componentId, String user) {
79     NicEntity nic = new MapNicRequestDtoToNicEntity().applyMapping(request, NicEntity.class);
80     nic.setVspId(vspId);
81     nic.setComponentId(componentId);
82
83     NicEntity createdNic = vendorSoftwareProductManager.createNic(nic, user);
84     return Response.ok(createdNic != null ? new StringWrapperResponse(createdNic.getId()) : null)
85         .build();
86   }
87
88   @Override
89   public Response get(String vspId, String componentId, String nicId, String version, String user) {
90     CompositionEntityResponse<Nic> response = vendorSoftwareProductManager
91         .getNic(vspId, Version.valueOf(version), componentId, nicId, user);
92
93     CompositionEntityResponseDto<NicDto> responseDto = new CompositionEntityResponseDto<>();
94     new MapCompositionEntityResponseToDto<>(new MapNicToNicDto(), NicDto.class)
95         .doMapping(response, responseDto);
96     return Response.ok(responseDto).build();
97   }
98
99   @Override
100   public Response delete(String vspId, String componentId, String nicId, String user) {
101     vendorSoftwareProductManager.deleteNic(vspId, componentId, nicId, user);
102     return Response.ok().build();
103   }
104
105   @Override
106   public Response update(NicRequestDto request, String vspId, String componentId, String nicId,
107                          String user) {
108     NicEntity nicEntity = new MapNicRequestDtoToNicEntity().applyMapping(request, NicEntity.class);
109     nicEntity.setVspId(vspId);
110     nicEntity.setComponentId(componentId);
111     nicEntity.setId(nicId);
112
113     CompositionEntityValidationData validationData =
114         vendorSoftwareProductManager.updateNic(nicEntity, user);
115     return validationData != null && CollectionUtils.isNotEmpty(validationData.getErrors())
116         ? Response.status(Response.Status.EXPECTATION_FAILED).entity(
117             new MapCompositionEntityValidationDataToDto()
118                 .applyMapping(validationData, CompositionEntityValidationDataDto.class)).build() :
119         Response.ok().build();
120   }
121
122   @Override
123   public Response getQuestionnaire(String vspId, String componentId, String nicId, String version,
124                                    String user) {
125     QuestionnaireResponse questionnaireResponse = vendorSoftwareProductManager
126         .getNicQuestionnaire(vspId, Version.valueOf(version), componentId, nicId, user);
127
128     QuestionnaireResponseDto result = new MapQuestionnaireResponseToQuestionnaireResponseDto()
129         .applyMapping(questionnaireResponse, QuestionnaireResponseDto.class);
130     return Response.ok(result).build();
131   }
132
133   @Override
134   public Response updateQuestionnaire(String questionnaireData, String vspId, String componentId,
135                                       String nicId, String user) {
136     vendorSoftwareProductManager
137         .updateNicQuestionnaire(vspId, componentId, nicId, questionnaireData, user);
138     return Response.ok().build();
139   }
140 }