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