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.NetworkManager;
25 import org.openecomp.sdc.vendorsoftwareproduct.NetworkManagerFactory;
26 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
27 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
28 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
30 import org.openecomp.sdc.versioning.dao.types.Version;
31 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityResponseDto;
32 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityValidationDataDto;
33 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NetworkDto;
34 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NetworkRequestDto;
35 import org.openecomp.sdcrests.vsp.rest.Networks;
36 import org.openecomp.sdcrests.vsp.rest.mapping.MapCompositionEntityResponseToDto;
37 import org.openecomp.sdcrests.vsp.rest.mapping.MapCompositionEntityValidationDataToDto;
38 import org.openecomp.sdcrests.vsp.rest.mapping.MapNetworkEntityToNetworkDto;
39 import org.openecomp.sdcrests.vsp.rest.mapping.MapNetworkRequestDtoToNetworkEntity;
40 import org.openecomp.sdcrests.vsp.rest.mapping.MapNetworkToNetworkDto;
41 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
42 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
43 import org.springframework.context.annotation.Scope;
44 import org.springframework.stereotype.Service;
46 import javax.inject.Named;
47 import javax.ws.rs.core.Response;
48 import java.util.Collection;
52 @Scope(value = "prototype")
53 public class NetworksImpl implements Networks {
55 private NetworkManager networkManager =
56 NetworkManagerFactory.getInstance().createInterface();
59 public Response list(String vspId, String versionId, String user) {
60 Collection<NetworkEntity> networks =
61 networkManager.listNetworks(vspId, new Version(versionId));
63 MapNetworkEntityToNetworkDto mapper = new MapNetworkEntityToNetworkDto();
64 GenericCollectionWrapper<NetworkDto> results = new GenericCollectionWrapper<>();
65 for (NetworkEntity network : networks) {
66 results.add(mapper.applyMapping(network, NetworkDto.class));
69 return Response.ok(results).build();
73 public Response create(NetworkRequestDto request, String vspId, String versionId, String user) {
74 NetworkEntity network =
75 new MapNetworkRequestDtoToNetworkEntity().applyMapping(request, NetworkEntity.class);
76 network.setVspId(vspId);
77 network.setVersion(new Version(versionId));
78 NetworkEntity createdNetwork = networkManager.createNetwork(network);
80 .ok(createdNetwork != null ? new StringWrapperResponse(createdNetwork.getId()) : null)
85 public Response get(String vspId, String versionId, String networkId, String user) {
86 CompositionEntityResponse<Network> response =
87 networkManager.getNetwork(vspId,
88 new Version(versionId), networkId
91 CompositionEntityResponseDto<NetworkDto> responseDto = new CompositionEntityResponseDto<>();
92 new MapCompositionEntityResponseToDto<>(new MapNetworkToNetworkDto(), NetworkDto.class)
93 .doMapping(response, responseDto);
94 return Response.ok(responseDto).build();
98 public Response delete(String vspId, String versionId, String networkId, String user) {
100 .deleteNetwork(vspId, new Version(versionId),
102 return Response.ok().build();
106 public Response update(NetworkRequestDto request, String vspId, String versionId,
107 String networkId, String user) {
108 NetworkEntity networkEntity =
109 new MapNetworkRequestDtoToNetworkEntity().applyMapping(request, NetworkEntity.class);
110 networkEntity.setVspId(vspId);
111 networkEntity.setVersion(new Version(versionId));
112 networkEntity.setId(networkId);
114 CompositionEntityValidationData validationData =
115 networkManager.updateNetwork(networkEntity);
116 return validationData != null && CollectionUtils.isNotEmpty(validationData.getErrors())
117 ? Response.status(Response.Status.EXPECTATION_FAILED).entity(
118 new MapCompositionEntityValidationDataToDto()
119 .applyMapping(validationData, CompositionEntityValidationDataDto.class)).build() :
120 Response.ok().build();