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