store openstack request status in requestdb
[so.git] / adapters / mso-adapter-utils / src / main / java / org / onap / so / openstack / utils / CinderClientImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 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
22 package org.onap.so.openstack.utils;
23
24 import org.onap.so.cloud.authentication.KeystoneAuthHolder;
25 import org.onap.so.openstack.exceptions.MsoCloudSiteNotFound;
26 import org.onap.so.openstack.exceptions.MsoException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.stereotype.Component;
30 import com.woorea.openstack.base.client.OpenStackRequest;
31 import com.woorea.openstack.cinder.Cinder;
32 import com.woorea.openstack.cinder.model.Volume;
33 import com.woorea.openstack.cinder.model.Volumes;
34
35
36
37 @Component
38 public class CinderClientImpl extends MsoCommonUtils {
39
40     private static final Logger logger = LoggerFactory.getLogger(CinderClientImpl.class);
41
42     /**
43      * Gets the Cinder client.
44      *
45      * @param cloudSite the cloud site
46      * @param tenantId the tenant id
47      * @return the glance client
48      * @throws MsoException the mso exception
49      */
50     private Cinder getCinderClient(String cloudSiteId, String tenantId) throws MsoException {
51         KeystoneAuthHolder keystone = getKeystoneAuthHolder(cloudSiteId, tenantId, "volumev2");
52         Cinder cinderClient = new Cinder(keystone.getServiceUrl());
53         cinderClient.token(keystone.getId());
54         return cinderClient;
55     }
56
57
58     /**
59      * Query images
60      *
61      * 
62      * @param cloudSiteId the cloud site id
63      * @param tenantId the tenant id
64      * @param limit limits the number of records returned
65      * @param visibility visibility in the image in openstack
66      * @param marker the last viewed record
67      * @param name the image names
68      * @return the list of images in openstack
69      * @throws MsoCloudSiteNotFound the mso cloud site not found
70      * @throws CinderClientException the glance client exception
71      */
72     public Volumes queryVolumes(String cloudSiteId, String tenantId, int limit, String marker)
73             throws MsoCloudSiteNotFound, CinderClientException {
74         try {
75             Cinder cinderClient = getCinderClient(cloudSiteId, tenantId);
76             // list is set to false, otherwise an invalid URL is appended
77             OpenStackRequest<Volumes> request =
78                     cinderClient.volumes().list(false).queryParam("limit", limit).queryParam("marker", marker);
79             return executeAndRecordOpenstackRequest(request);
80         } catch (MsoException e) {
81             logger.error("Error building Cinder Client", e);
82             throw new CinderClientException("Error building Cinder Client", e);
83         }
84     }
85
86
87     public Volume queryVolume(String cloudSiteId, String tenantId, String volumeId)
88             throws MsoCloudSiteNotFound, CinderClientException {
89         try {
90             Cinder cinderClient = getCinderClient(cloudSiteId, tenantId);
91             // list is set to false, otherwise an invalid URL is appended
92             OpenStackRequest<Volume> request = cinderClient.volumes().show(volumeId);
93             return executeAndRecordOpenstackRequest(request);
94         } catch (MsoException e) {
95             logger.error("Error building Cinder Client", e);
96             throw new CinderClientException("Error building Cinder Client", e);
97         }
98     }
99
100 }