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