bd74bf6aa06de1d7d944b762b41b8fc581f0e5f5
[so.git] /
1 /*
2  * Copyright (C) 2018 Bell Canada. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11  * specific language governing permissions and limitations under the License.
12  */
13 package org.onap.so.heatbridge.factory;
14
15 import java.net.MalformedURLException;
16 import java.net.URL;
17 import java.util.Objects;
18 import javax.annotation.Nonnull;
19 import org.onap.so.heatbridge.HeatBridgeException;
20 import org.onap.so.heatbridge.constants.HeatBridgeConstants;
21 import org.onap.so.heatbridge.openstack.api.OpenstackAccess;
22 import org.onap.so.heatbridge.openstack.api.OpenstackAccess.OpenstackAccessBuilder;
23 import org.onap.so.heatbridge.openstack.api.OpenstackClient;
24 import org.onap.so.heatbridge.openstack.api.OpenstackClientException;
25 import org.onap.so.heatbridge.openstack.factory.OpenstackClientFactory;
26 import org.onap.so.utils.CryptoUtils;
27
28 /**
29  * This class implements {@link MsoCloudClientFactory} It loads the cloud configuration from SO and uses it to
30  * authenticate with keystone. As a result of authentication with keystone, it returns the Openstack client with the
31  * auth token so that subsequent API calls to Openstack can be made.
32  */
33 public class MsoCloudClientFactoryImpl implements MsoCloudClientFactory {
34
35     private OpenstackClientFactory openstackClientFactory;
36
37     public MsoCloudClientFactoryImpl(@Nonnull OpenstackClientFactory openstackClientFactory) {
38         Objects.requireNonNull(openstackClientFactory, "Null OpenstackClientFactory object");
39         this.openstackClientFactory = openstackClientFactory;
40     }
41
42     @Override
43     public OpenstackClient getOpenstackClient(@Nonnull String url, @Nonnull String msoId, @Nonnull String msoPass,
44             @Nonnull String cloudRegionId, @Nonnull String tenantId) throws HeatBridgeException {
45         Objects.requireNonNull(url, "Null openstack url!");
46         Objects.requireNonNull(msoId, "Null openstack user id!");
47         Objects.requireNonNull(msoPass, "Null openstack password!");
48         Objects.requireNonNull(cloudRegionId, "Null cloud-region ID!");
49         Objects.requireNonNull(tenantId, "Null tenant ID!");
50         try {
51             final OpenstackAccess osAccess = new OpenstackAccessBuilder().setBaseUrl(url) // keystone URL
52                     .setUser(msoId) // keystone username
53                     .setPassword(CryptoUtils.decryptCloudConfigPassword(msoPass)) // keystone decrypted password
54                     .setRegion(cloudRegionId) // openstack region
55                     .setDomainName(HeatBridgeConstants.OS_DEFAULT_DOMAIN_NAME) // hardcode to "default"
56                     .setTenantId(tenantId) // tenantId
57                     .build();
58
59             // Identify the Keystone version
60             String version = new URL(url).getPath().replace("/", "");
61             if (version.equals(HeatBridgeConstants.OS_KEYSTONE_V2_KEY)) {
62                 return openstackClientFactory.createOpenstackV2Client(osAccess);
63             } else if (version.equals(HeatBridgeConstants.OS_KEYSTONE_V3_KEY)) {
64                 return openstackClientFactory.createOpenstackV3Client(osAccess);
65             }
66             throw new OpenstackClientException("Unsupported keystone version!");
67         } catch (MalformedURLException e) {
68             throw new HeatBridgeException("Malformed Keystone Endpoint in SO configuration.", e);
69         } catch (OpenstackClientException osClientEx) {
70             throw new HeatBridgeException("Client error when authenticating with the Openstack V3.", osClientEx);
71         }
72     }
73 }