Removed reference to MDC for logging
[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.ComponentManager;
25 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManagerFactory;
26 import org.openecomp.sdc.vendorsoftwareproduct.NicManager;
27 import org.openecomp.sdc.vendorsoftwareproduct.NicManagerFactory;
28 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
30 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
31 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
32 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
33 import org.openecomp.sdc.versioning.dao.types.Version;
34 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityResponseDto;
35 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityValidationDataDto;
36 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicCreationResponseDto;
37 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicDto;
38 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicRequestDto;
39 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireResponseDto;
40 import org.openecomp.sdcrests.vsp.rest.Nics;
41 import org.openecomp.sdcrests.vsp.rest.mapping.MapCompositionEntityResponseToDto;
42 import org.openecomp.sdcrests.vsp.rest.mapping.MapCompositionEntityValidationDataToDto;
43 import org.openecomp.sdcrests.vsp.rest.mapping.MapNicEntityToNicCreationResponseDto;
44 import org.openecomp.sdcrests.vsp.rest.mapping.MapNicEntityToNicDto;
45 import org.openecomp.sdcrests.vsp.rest.mapping.MapNicRequestDtoToNicEntity;
46 import org.openecomp.sdcrests.vsp.rest.mapping.MapNicToNicDto;
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 import javax.inject.Named;
53 import javax.ws.rs.core.Response;
54 import java.util.Collection;
55
56 @Named
57 @Service("nics")
58 @Scope(value = "prototype")
59 public class NicsImpl implements Nics {
60   private NicManager nicManager = NicManagerFactory.getInstance().createInterface();
61   private ComponentManager componentManager =
62       ComponentManagerFactory.getInstance().createInterface();
63
64   @Override
65   public Response list(String vspId, String versionId, String componentId, String user) {
66     Version vspVersion = new Version(versionId);
67     componentManager.validateComponentExistence(vspId, vspVersion, componentId);
68     Collection<NicEntity> nics = nicManager.listNics(vspId, vspVersion, componentId);
69
70     MapNicEntityToNicDto mapper = new MapNicEntityToNicDto();
71     GenericCollectionWrapper<NicDto> results = new GenericCollectionWrapper<>();
72     for (NicEntity nic : nics) {
73       results.add(mapper.applyMapping(nic, NicDto.class));
74     }
75
76     return Response.ok(results).build();
77   }
78
79   @Override
80   public Response create(NicRequestDto request, String vspId, String versionId, String componentId,
81                          String user) {
82     NicEntity nic = new MapNicRequestDtoToNicEntity().applyMapping(request, NicEntity.class);
83     nic.setVspId(vspId);
84     nic.setVersion(new Version(versionId));
85     nic.setComponentId(componentId);
86     componentManager.validateComponentExistence(vspId, nic.getVersion(), componentId);
87
88     NicEntity createdNic = nicManager.createNic(nic);
89     MapNicEntityToNicCreationResponseDto mapping =
90         new MapNicEntityToNicCreationResponseDto();
91     NicCreationResponseDto createdNicDto = mapping.applyMapping(createdNic,
92         NicCreationResponseDto.class);
93     return Response.ok(createdNic != null ? createdNicDto : null)
94         .build();
95   }
96
97   @Override
98   public Response get(String vspId, String versionId, String componentId, String nicId,
99                       String user) {
100     Version vspVersion = new Version(versionId);
101     componentManager.validateComponentExistence(vspId, vspVersion, componentId);
102     CompositionEntityResponse<Nic> response =
103         nicManager.getNic(vspId, vspVersion, componentId, nicId);
104
105     CompositionEntityResponseDto<NicDto> responseDto = new CompositionEntityResponseDto<>();
106     new MapCompositionEntityResponseToDto<>(new MapNicToNicDto(), NicDto.class)
107         .doMapping(response, responseDto);
108     return Response.ok(responseDto).build();
109   }
110
111   @Override
112   public Response delete(String vspId, String versionId, String componentId, String nicId,
113                          String user) {
114     Version vspVersion = new Version(versionId);
115     componentManager.validateComponentExistence(vspId, vspVersion, componentId);
116     nicManager.deleteNic(vspId, vspVersion, componentId, nicId);
117     return Response.ok().build();
118   }
119
120   @Override
121   public Response update(NicRequestDto request, String vspId, String versionId, String componentId,
122                          String nicId,
123                          String user) {
124     NicEntity nicEntity = new MapNicRequestDtoToNicEntity().applyMapping(request, NicEntity.class);
125     nicEntity.setVspId(vspId);
126     nicEntity.setVersion(new Version(versionId));
127     nicEntity.setComponentId(componentId);
128     nicEntity.setId(nicId);
129
130     componentManager.validateComponentExistence(vspId, nicEntity.getVersion(), componentId);
131     CompositionEntityValidationData validationData =
132         nicManager.updateNic(nicEntity);
133     return validationData != null && CollectionUtils.isNotEmpty(validationData.getErrors())
134         ? Response.status(Response.Status.EXPECTATION_FAILED).entity(
135         new MapCompositionEntityValidationDataToDto()
136             .applyMapping(validationData, CompositionEntityValidationDataDto.class)).build() :
137         Response.ok().build();
138   }
139
140   @Override
141   public Response getQuestionnaire(String vspId, String versionId, String componentId, String nicId,
142                                    String user) {
143     Version vspVersion = new Version(versionId);
144     componentManager.validateComponentExistence(vspId, vspVersion, componentId);
145     QuestionnaireResponse questionnaireResponse =
146         nicManager.getNicQuestionnaire(vspId, vspVersion, componentId, nicId);
147
148     QuestionnaireResponseDto result = new MapQuestionnaireResponseToQuestionnaireResponseDto()
149         .applyMapping(questionnaireResponse, QuestionnaireResponseDto.class);
150     return Response.ok(result).build();
151   }
152
153   @Override
154   public Response updateQuestionnaire(String questionnaireData, String vspId, String versionId,
155                                       String componentId,
156                                       String nicId, String user) {
157     Version vspVersion = new Version(versionId);
158     componentManager.validateComponentExistence(vspId, vspVersion, componentId);
159     nicManager
160         .updateNicQuestionnaire(vspId, vspVersion, componentId, nicId, questionnaireData);
161     return Response.ok().build();
162   }
163 }