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