Containerization feature of SO
[so.git] / bpmn / so-bpmn-tasks / src / main / java / org / onap / so / client / adapter / vnf / VnfVolumeAdapterClientImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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.onap.so.client.adapter.vnf;
22
23 import javax.ws.rs.InternalServerErrorException;
24 import javax.ws.rs.core.MediaType;
25 import javax.ws.rs.core.UriBuilder;
26
27 import org.onap.so.adapters.vnfrest.CreateVolumeGroupRequest;
28 import org.onap.so.adapters.vnfrest.CreateVolumeGroupResponse;
29 import org.onap.so.adapters.vnfrest.DeleteVolumeGroupRequest;
30 import org.onap.so.adapters.vnfrest.DeleteVolumeGroupResponse;
31 import org.onap.so.adapters.vnfrest.QueryVolumeGroupResponse;
32 import org.onap.so.adapters.vnfrest.RollbackVolumeGroupRequest;
33 import org.onap.so.adapters.vnfrest.RollbackVolumeGroupResponse;
34 import org.onap.so.adapters.vnfrest.UpdateVolumeGroupRequest;
35 import org.onap.so.adapters.vnfrest.UpdateVolumeGroupResponse;
36 import org.onap.so.client.RestClient;
37 import org.onap.so.client.adapter.rest.AdapterRestClient;
38 import org.springframework.stereotype.Component;
39
40 @Component
41 public class VnfVolumeAdapterClientImpl implements VnfVolumeAdapterClient {
42
43         private final VnfVolumeAdapterRestProperties props;
44
45         public VnfVolumeAdapterClientImpl() {
46                 this.props = new VnfVolumeAdapterRestProperties();
47         }
48
49         @Override
50         public CreateVolumeGroupResponse createVNFVolumes(CreateVolumeGroupRequest req) throws VnfAdapterClientException {
51                 try {
52                         return this.getAdapterRestClient("").post(req, CreateVolumeGroupResponse.class);
53                 } catch (InternalServerErrorException e) {
54                         throw new VnfAdapterClientException(e.getMessage());
55                 }
56         }
57
58         @Override
59         public DeleteVolumeGroupResponse deleteVNFVolumes(String aaiVolumeGroupId, DeleteVolumeGroupRequest req)
60                         throws VnfAdapterClientException {
61                 try {
62                         return this.getAdapterRestClient("/" + aaiVolumeGroupId).delete(req, DeleteVolumeGroupResponse.class);
63                 } catch (InternalServerErrorException e) {
64                         throw new VnfAdapterClientException(e.getMessage());
65                 }
66         }
67
68         @Override
69         public RollbackVolumeGroupResponse rollbackVNFVolumes(String aaiVolumeGroupId, RollbackVolumeGroupRequest req)
70                         throws VnfAdapterClientException {
71                 try {
72                         return this.getAdapterRestClient("/" + aaiVolumeGroupId + "/rollback").delete(req,
73                                         RollbackVolumeGroupResponse.class);
74                 } catch (InternalServerErrorException e) {
75                         throw new VnfAdapterClientException(e.getMessage());
76                 }
77         }
78
79         @Override
80         public UpdateVolumeGroupResponse updateVNFVolumes(String aaiVolumeGroupId, UpdateVolumeGroupRequest req)
81                         throws VnfAdapterClientException {
82                 try {
83                         return this.getAdapterRestClient("/" + aaiVolumeGroupId).put(req, UpdateVolumeGroupResponse.class);
84                 } catch (InternalServerErrorException e) {
85                         throw new VnfAdapterClientException(e.getMessage());
86                 }
87         }
88
89         @Override
90         public QueryVolumeGroupResponse queryVNFVolumes(String aaiVolumeGroupId, String cloudSiteId, String tenantId,
91                         String volumeGroupStackId, Boolean skipAAI, String requestId, String serviceInstanceId)
92                         throws VnfAdapterClientException {
93                 try {
94                         String path = buildQueryPath(aaiVolumeGroupId, cloudSiteId, tenantId, volumeGroupStackId, skipAAI,
95                                         requestId, serviceInstanceId);
96                         return this.getAdapterRestClient(path).get(QueryVolumeGroupResponse.class).get();
97                 } catch (InternalServerErrorException e) {
98                         throw new VnfAdapterClientException(e.getMessage());
99                 }
100         }
101
102         protected String buildQueryPath(String aaiVolumeGroupId, String cloudSiteId, String tenantId,
103                         String volumeGroupStackId, Boolean skipAAI, String requestId, String serviceInstanceId) {
104                 javax.ws.rs.core.UriBuilder builder = this.getUri("/" + aaiVolumeGroupId);
105                 builder.queryParam("cloudSiteId", cloudSiteId).queryParam("tenantId", tenantId)
106                                 .queryParam("volumeGroupStackId", volumeGroupStackId).queryParam("skipAAI", skipAAI)
107                                 .queryParam("msoRequest.requestId", requestId)
108                                 .queryParam("msoRequest.serviceInstanceId", serviceInstanceId);
109                 return builder.build().toString();
110         }
111
112         protected UriBuilder getUri(String path) {
113                 return UriBuilder.fromPath(path);
114         }
115
116         protected RestClient getAdapterRestClient(String path) {
117                 return new AdapterRestClient(props, this.getUri(path).build(), MediaType.APPLICATION_JSON,
118                                 MediaType.APPLICATION_JSON);
119         }
120 }