f0c5a0b434c7158a5f2e417df1095209bf7f8b79
[so.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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  * Copyright (C) 2018 Bell Canada. All rights reserved.
23  *
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
26  *
27  * http://www.apache.org/licenses/LICENSE-2.0
28  *
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.
32  */
33 package org.onap.so.heatbridge.factory;
34
35 import java.net.MalformedURLException;
36 import java.net.URL;
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;
47
48 /**
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.
52  */
53 public class MsoCloudClientFactoryImpl implements MsoCloudClientFactory {
54
55     private OpenstackClientFactory openstackClientFactory;
56
57     public MsoCloudClientFactoryImpl(@Nonnull OpenstackClientFactory openstackClientFactory) {
58         Objects.requireNonNull(openstackClientFactory, "Null OpenstackClientFactory object");
59         this.openstackClientFactory = openstackClientFactory;
60     }
61
62     @Override
63     public OpenstackClient getOpenstackClient(@Nonnull String url, @Nonnull String msoId, @Nonnull String msoPass,
64             @Nonnull String cloudRegionId, @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(cloudRegionId, "Null cloud-region ID!");
69         Objects.requireNonNull(tenantId, "Null tenant ID!");
70         try {
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(cloudRegionId) // openstack region
75                     .setDomainName(HeatBridgeConstants.OS_DEFAULT_DOMAIN_NAME) // hardcode to "default"
76                     .setTenantId(tenantId) // tenantId
77                     .build();
78
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);
85             }
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);
91         }
92     }
93 }