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 NicManager nicManager = NicManagerFactory.getInstance().createInterface();
50 private ComponentManager componentManager =
51 ComponentManagerFactory.getInstance().createInterface();
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);
59 MapNicEntityToNicDto mapper = new MapNicEntityToNicDto();
60 GenericCollectionWrapper<NicDto> results = new GenericCollectionWrapper<>();
61 for (NicEntity nic : nics) {
62 results.add(mapper.applyMapping(nic, NicDto.class));
65 return Response.ok(results).build();
69 public Response create(NicRequestDto request, String vspId, String versionId, String componentId,
71 NicEntity nic = new MapNicRequestDtoToNicEntity().applyMapping(request, NicEntity.class);
73 nic.setVersion(new Version(versionId));
74 nic.setComponentId(componentId);
75 componentManager.validateComponentExistence(vspId, nic.getVersion(), componentId);
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)
87 public Response get(String vspId, String versionId, String componentId, String nicId,
89 Version vspVersion = new Version(versionId);
90 componentManager.validateComponentExistence(vspId, vspVersion, componentId);
91 CompositionEntityResponse<Nic> response =
92 nicManager.getNic(vspId, vspVersion, componentId, nicId);
94 CompositionEntityResponseDto<NicDto> responseDto = new CompositionEntityResponseDto<>();
95 new MapCompositionEntityResponseToDto<>(new MapNicToNicDto(), NicDto.class)
96 .doMapping(response, responseDto);
97 return Response.ok(responseDto).build();
101 public Response delete(String vspId, String versionId, String componentId, String nicId,
103 Version vspVersion = new Version(versionId);
104 componentManager.validateComponentExistence(vspId, vspVersion, componentId);
105 nicManager.deleteNic(vspId, vspVersion, componentId, nicId);
106 return Response.ok().build();
110 public Response update(NicRequestDto request, String vspId, String versionId, String componentId,
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);
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();
130 public Response getQuestionnaire(String vspId, String versionId, String componentId, String nicId,
132 Version vspVersion = new Version(versionId);
133 componentManager.validateComponentExistence(vspId, vspVersion, componentId);
134 QuestionnaireResponse questionnaireResponse =
135 nicManager.getNicQuestionnaire(vspId, vspVersion, componentId, nicId);
137 QuestionnaireResponseDto result = new MapQuestionnaireResponseToQuestionnaireResponseDto()
138 .applyMapping(questionnaireResponse, QuestionnaireResponseDto.class);
139 return Response.ok(result).build();
143 public Response updateQuestionnaire(String questionnaireData, String vspId, String versionId,
145 String nicId, String user) {
146 Version vspVersion = new Version(versionId);
147 componentManager.validateComponentExistence(vspId, vspVersion, componentId);
149 .updateNicQuestionnaire(vspId, vspVersion, componentId, nicId, questionnaireData);
150 return Response.ok().build();