e299f6ffb84014740ad7231e73561de9a6f1a562
[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.util.Objects;
36 import javax.annotation.Nonnull;
37 import org.onap.so.heatbridge.HeatBridgeException;
38 import org.onap.so.heatbridge.constants.HeatBridgeConstants;
39 import org.onap.so.heatbridge.openstack.api.OpenstackAccess;
40 import org.onap.so.heatbridge.openstack.api.OpenstackAccess.OpenstackAccessBuilder;
41 import org.onap.so.heatbridge.openstack.api.OpenstackClient;
42 import org.onap.so.heatbridge.openstack.api.OpenstackClientException;
43 import org.onap.so.heatbridge.openstack.factory.OpenstackClientFactory;
44 import org.onap.so.utils.CryptoUtils;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
47
48
49 /**
50  * This class implements {@link MsoCloudClientFactory} It loads the cloud configuration from SO and uses it to
51  * authenticate with keystone. As a result of authentication with keystone, it returns the Openstack client with the
52  * auth token so that subsequent API calls to Openstack can be made.
53  */
54 public class MsoCloudClientFactoryImpl implements MsoCloudClientFactory {
55
56     private static final Logger logger = LoggerFactory.getLogger(MsoCloudClientFactoryImpl.class);
57
58     private OpenstackClientFactory openstackClientFactory;
59
60     public MsoCloudClientFactoryImpl(@Nonnull OpenstackClientFactory openstackClientFactory) {
61         Objects.requireNonNull(openstackClientFactory, "Null OpenstackClientFactory object");
62         this.openstackClientFactory = openstackClientFactory;
63     }
64
65     @Override
66     public OpenstackClient getOpenstackClient(@Nonnull String url, @Nonnull String msoId, @Nonnull String msoPass,
67             @Nonnull String regionId, @Nonnull String tenantId, @Nonnull String keystoneVersion, String userDomainName,
68             String projectDomainName) throws HeatBridgeException {
69         Objects.requireNonNull(url, "Null openstack url!");
70         Objects.requireNonNull(msoId, "Null openstack user id!");
71         Objects.requireNonNull(msoPass, "Null openstack password!");
72         Objects.requireNonNull(regionId, "Null regionId ID!");
73         Objects.requireNonNull(tenantId, "Null tenant ID!");
74         Objects.requireNonNull(keystoneVersion, "Null keystone version");
75         if (userDomainName == null) {
76             userDomainName = HeatBridgeConstants.OS_DEFAULT_DOMAIN_NAME;
77         }
78         if (projectDomainName == null) {
79             projectDomainName = HeatBridgeConstants.OS_DEFAULT_DOMAIN_NAME;
80         }
81         try {
82             final OpenstackAccess osAccess = new OpenstackAccessBuilder().setBaseUrl(url) // keystone URL
83                     .setUser(msoId) // keystone username
84                     .setPassword(CryptoUtils.decryptCloudConfigPassword(msoPass)) // keystone decrypted password
85                     .setRegion(regionId) // openstack region
86                     .setDomainName(userDomainName).setProjectName(projectDomainName).setTenantId(tenantId) // tenantId
87                     .build();
88
89             // Identify the Keystone version
90             if (keystoneVersion.equals(HeatBridgeConstants.OS_KEYSTONE_V2_KEY)) {
91                 return openstackClientFactory.createOpenstackV2Client(osAccess);
92             } else if (keystoneVersion.equals(HeatBridgeConstants.OS_KEYSTONE_V3_KEY)) {
93                 return openstackClientFactory.createOpenstackV3Client(osAccess);
94             }
95             throw new OpenstackClientException("Unsupported keystone version! " + keystoneVersion);
96         } catch (OpenstackClientException osClientEx) {
97             logger.error("Error creating OS Client", osClientEx);
98             throw new HeatBridgeException("Client error when authenticating with the Openstack", osClientEx);
99         }
100     }
101 }