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