push addional 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 / NetworksImpl.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.VendorSoftwareProductManager;
25 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
26 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
27 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityValidationData;
28 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
29 import org.openecomp.sdc.versioning.dao.types.Version;
30 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityResponseDto;
31 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityValidationDataDto;
32 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NetworkDto;
33 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NetworkRequestDto;
34 import org.openecomp.sdcrests.vsp.rest.Networks;
35 import org.openecomp.sdcrests.vsp.rest.mapping.MapCompositionEntityResponseToDto;
36 import org.openecomp.sdcrests.vsp.rest.mapping.MapCompositionEntityValidationDataToDto;
37 import org.openecomp.sdcrests.vsp.rest.mapping.MapNetworkEntityToNetworkDto;
38 import org.openecomp.sdcrests.vsp.rest.mapping.MapNetworkRequestDtoToNetworkEntity;
39 import org.openecomp.sdcrests.vsp.rest.mapping.MapNetworkToNetworkDto;
40 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
41 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.context.annotation.Scope;
44 import org.springframework.stereotype.Service;
45
46 import java.util.Collection;
47 import javax.inject.Named;
48 import javax.ws.rs.core.Response;
49
50
51 @Named
52 @Service("networks")
53 @Scope(value = "prototype")
54 public class NetworksImpl implements Networks {
55   @Autowired
56   private VendorSoftwareProductManager vendorSoftwareProductManager;
57
58   @Override
59   public Response list(String vspId, String version, String user) {
60     Collection<NetworkEntity> networks =
61         vendorSoftwareProductManager.listNetworks(vspId, Version.valueOf(version), user);
62
63     MapNetworkEntityToNetworkDto mapper = new MapNetworkEntityToNetworkDto();
64     GenericCollectionWrapper<NetworkDto> results = new GenericCollectionWrapper<>();
65     for (NetworkEntity network : networks) {
66       results.add(mapper.applyMapping(network, NetworkDto.class));
67     }
68
69     return Response.ok(results).build();
70   }
71
72   @Override
73   public Response create(NetworkRequestDto request, String vspId, String user) {
74     NetworkEntity network =
75         new MapNetworkRequestDtoToNetworkEntity().applyMapping(request, NetworkEntity.class);
76     network.setVspId(vspId);
77     NetworkEntity createdNetwork = vendorSoftwareProductManager.createNetwork(network, user);
78     return Response
79         .ok(createdNetwork != null ? new StringWrapperResponse(createdNetwork.getId()) : null)
80         .build();
81   }
82
83   @Override
84   public Response get(String vspId, String networkId, String version, String user) {
85     CompositionEntityResponse<Network> response =
86         vendorSoftwareProductManager.getNetwork(vspId, Version.valueOf(version), networkId, user);
87
88     CompositionEntityResponseDto<NetworkDto> responseDto = new CompositionEntityResponseDto<>();
89     new MapCompositionEntityResponseToDto<>(new MapNetworkToNetworkDto(), NetworkDto.class)
90         .doMapping(response, responseDto);
91     return Response.ok(responseDto).build();
92   }
93
94   @Override
95   public Response delete(String vspId, String networkId, String user) {
96     vendorSoftwareProductManager.deleteNetwork(vspId, networkId, user);
97     return Response.ok().build();
98   }
99
100   @Override
101   public Response update(NetworkRequestDto request, String vspId, String networkId, String user) {
102     NetworkEntity networkEntity =
103         new MapNetworkRequestDtoToNetworkEntity().applyMapping(request, NetworkEntity.class);
104     networkEntity.setVspId(vspId);
105     networkEntity.setId(networkId);
106
107     CompositionEntityValidationData validationData =
108         vendorSoftwareProductManager.updateNetwork(networkEntity, user);
109     return validationData != null && CollectionUtils.isNotEmpty(validationData.getErrors())
110         ? Response.status(Response.Status.EXPECTATION_FAILED).entity(
111             new MapCompositionEntityValidationDataToDto()
112                 .applyMapping(validationData, CompositionEntityValidationDataDto.class)).build() :
113         Response.ok().build();
114   }
115 }