2 * Copyright (C) 2018 Bell Canada. All rights reserved.
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
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
13 package org.onap.so.heatbridge.factory;
15 import java.net.MalformedURLException;
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;
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.
33 public class MsoCloudClientFactoryImpl implements MsoCloudClientFactory {
35 private OpenstackClientFactory openstackClientFactory;
37 public MsoCloudClientFactoryImpl(@Nonnull OpenstackClientFactory openstackClientFactory) {
38 Objects.requireNonNull(openstackClientFactory, "Null OpenstackClientFactory object");
39 this.openstackClientFactory = openstackClientFactory;
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!");
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
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);
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);