2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdcrests.vsp.rest.services;
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;
41 import javax.inject.Named;
42 import javax.ws.rs.core.Response;
43 import java.util.Collection;
47 @Scope(value = "prototype")
48 public class NicsImpl implements Nics {
49 private final NicManager nicManager;
50 private final ComponentManager componentManager;
53 this.nicManager = NicManagerFactory.getInstance().createInterface();
54 this.componentManager = ComponentManagerFactory.getInstance().createInterface();
57 public NicsImpl(NicManager nicManager, ComponentManager componentManager) {
58 this.nicManager = nicManager;
59 this.componentManager = componentManager;
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);
68 MapNicEntityToNicDto mapper = new MapNicEntityToNicDto();
69 GenericCollectionWrapper<NicDto> results = new GenericCollectionWrapper<>();
70 for (NicEntity nic : nics) {
71 results.add(mapper.applyMapping(nic, NicDto.class));
74 return Response.ok(results).build();
78 public Response create(NicRequestDto request, String vspId, String versionId, String componentId,
80 NicEntity nic = new MapNicRequestDtoToNicEntity().applyMapping(request, NicEntity.class);
82 nic.setVersion(new Version(versionId));
83 nic.setComponentId(componentId);
84 componentManager.validateComponentExistence(vspId, nic.getVersion(), componentId);
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)
96 public Response get(String vspId, String versionId, String componentId, String nicId,
98 Version vspVersion = new Version(versionId);
99 componentManager.validateComponentExistence(vspId, vspVersion, componentId);
100 CompositionEntityResponse<Nic> response =
101 nicManager.getNic(vspId, vspVersion, componentId, nicId);
103 CompositionEntityResponseDto<NicDto> responseDto = new CompositionEntityResponseDto<>();
104 new MapCompositionEntityResponseToDto<>(new MapNicToNicDto(), NicDto.class)
105 .doMapping(response, responseDto);
106 return Response.ok(responseDto).build();
110 public Response delete(String vspId, String versionId, String componentId, String nicId,
112 Version vspVersion = new Version(versionId);
113 componentManager.validateComponentExistence(vspId, vspVersion, componentId);
114 nicManager.deleteNic(vspId, vspVersion, componentId, nicId);
115 return Response.ok().build();
119 public Response update(NicRequestDto request, String vspId, String versionId, String componentId,
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);
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();
139 public Response getQuestionnaire(String vspId, String versionId, String componentId, String nicId,
141 Version vspVersion = new Version(versionId);
142 componentManager.validateComponentExistence(vspId, vspVersion, componentId);
143 QuestionnaireResponse questionnaireResponse =
144 nicManager.getNicQuestionnaire(vspId, vspVersion, componentId, nicId);
146 QuestionnaireResponseDto result = new MapQuestionnaireResponseToQuestionnaireResponseDto()
147 .applyMapping(questionnaireResponse, QuestionnaireResponseDto.class);
148 return Response.ok(result).build();
152 public Response updateQuestionnaire(String questionnaireData, String vspId, String versionId,
154 String nicId, String user) {
155 Version vspVersion = new Version(versionId);
156 componentManager.validateComponentExistence(vspId, vspVersion, componentId);
158 .updateNicQuestionnaire(vspId, vspVersion, componentId, nicId, questionnaireData);
159 return Response.ok().build();