11ed508286eb319aa64dda019ab9328bdf47d12f
[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.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 final NicManager nicManager;
50   private final ComponentManager componentManager;
51
52   public NicsImpl() {
53     this.nicManager = NicManagerFactory.getInstance().createInterface();
54     this.componentManager = ComponentManagerFactory.getInstance().createInterface();
55   }
56
57   public NicsImpl(NicManager nicManager, ComponentManager componentManager) {
58     this.nicManager = nicManager;
59     this.componentManager = componentManager;
60   }
61
62   @Override
63   public Response list(String vspId, String versionId, String componentId, String user) {
64     Version vspVersion = new Version(versionId);
65     componentManager.validateComponentExistence(vspId, vspVersion, componentId);
66     Collection<NicEntity> nics = nicManager.listNics(vspId, vspVersion, componentId);
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 versionId, String componentId,
79                          String user) {
80     NicEntity nic = new MapNicRequestDtoToNicEntity().applyMapping(request, NicEntity.class);
81     nic.setVspId(vspId);
82     nic.setVersion(new Version(versionId));
83     nic.setComponentId(componentId);
84     componentManager.validateComponentExistence(vspId, nic.getVersion(), componentId);
85
86     NicEntity createdNic = nicManager.createNic(nic);
87     MapNicEntityToNicCreationResponseDto mapping =
88         new MapNicEntityToNicCreationResponseDto();
89     NicCreationResponseDto createdNicDto = mapping.applyMapping(createdNic,
90         NicCreationResponseDto.class);
91     return Response.ok(createdNic != null ? createdNicDto : null)
92         .build();
93   }
94
95   @Override
96   public Response get(String vspId, String versionId, String componentId, String nicId,
97                       String user) {
98     Version vspVersion = new Version(versionId);
99     componentManager.validateComponentExistence(vspId, vspVersion, componentId);
100     CompositionEntityResponse<Nic> response =
101         nicManager.getNic(vspId, vspVersion, componentId, nicId);
102
103     CompositionEntityResponseDto<NicDto> responseDto = new CompositionEntityResponseDto<>();
104     new MapCompositionEntityResponseToDto<>(new MapNicToNicDto(), NicDto.class)
105         .doMapping(response, responseDto);
106     return Response.ok(responseDto).build();
107   }
108
109   @Override
110   public Response delete(String vspId, String versionId, String componentId, String nicId,
111                          String user) {
112     Version vspVersion = new Version(versionId);
113     componentManager.validateComponentExistence(vspId, vspVersion, componentId);
114     nicManager.deleteNic(vspId, vspVersion, componentId, nicId);
115     return Response.ok().build();
116   }
117
118   @Override
119   public Response update(NicRequestDto request, String vspId, String versionId, String componentId,
120                          String nicId,
121                          String user) {
122     NicEntity nicEntity = new MapNicRequestDtoToNicEntity().applyMapping(request, NicEntity.class);
123     nicEntity.setVspId(vspId);
124     nicEntity.setVersion(new Version(versionId));
125     nicEntity.setComponentId(componentId);
126     nicEntity.setId(nicId);
127
128     componentManager.validateComponentExistence(vspId, nicEntity.getVersion(), componentId);
129     CompositionEntityValidationData validationData =
130         nicManager.updateNic(nicEntity);
131     return validationData != null && CollectionUtils.isNotEmpty(validationData.getErrors())
132         ? Response.status(Response.Status.EXPECTATION_FAILED).entity(
133         new MapCompositionEntityValidationDataToDto()
134             .applyMapping(validationData, CompositionEntityValidationDataDto.class)).build() :
135         Response.ok().build();
136   }
137
138   @Override
139   public Response getQuestionnaire(String vspId, String versionId, String componentId, String nicId,
140                                    String user) {
141     Version vspVersion = new Version(versionId);
142     componentManager.validateComponentExistence(vspId, vspVersion, componentId);
143     QuestionnaireResponse questionnaireResponse =
144         nicManager.getNicQuestionnaire(vspId, vspVersion, componentId, nicId);
145
146     QuestionnaireResponseDto result = new MapQuestionnaireResponseToQuestionnaireResponseDto()
147         .applyMapping(questionnaireResponse, QuestionnaireResponseDto.class);
148     return Response.ok(result).build();
149   }
150
151   @Override
152   public Response updateQuestionnaire(String questionnaireData, String vspId, String versionId,
153                                       String componentId,
154                                       String nicId, String user) {
155     Version vspVersion = new Version(versionId);
156     componentManager.validateComponentExistence(vspId, vspVersion, componentId);
157     nicManager
158         .updateNicQuestionnaire(vspId, vspVersion, componentId, nicId, questionnaireData);
159     return Response.ok().build();
160   }
161 }