8829c702cf65b3bfb115e7a2030b5d10dc698474
[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");
25  * you may not use this file except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *      http://www.apache.org/licenses/LICENSE-2.0
29  *
30  * Unless required by applicable law or agreed to in writing, software
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  */
36 package org.onap.so.heatbridge.openstack.factory;
37
38 import com.google.common.base.Preconditions;
39 import org.onap.so.heatbridge.openstack.api.OpenstackAccess;
40 import org.onap.so.heatbridge.openstack.api.OpenstackClient;
41 import org.onap.so.heatbridge.openstack.api.OpenstackClientException;
42 import org.onap.so.heatbridge.openstack.api.OpenstackV2ClientImpl;
43 import org.onap.so.heatbridge.openstack.api.OpenstackV3ClientImpl;
44 import org.openstack4j.api.OSClient.OSClientV2;
45 import org.openstack4j.api.OSClient.OSClientV3;
46 import org.openstack4j.api.exceptions.AuthenticationException;
47 import org.openstack4j.openstack.OSFactory;
48 import org.openstack4j.model.common.Identifier;
49
50 public class OpenstackClientFactoryImpl implements OpenstackClientFactory {
51
52     @Override
53     public OpenstackClient createOpenstackV3Client(OpenstackAccess osAccess) throws OpenstackClientException {
54         Preconditions.checkNotNull(osAccess.getUrl(), "Keystone-v3 Auth: endpoint not set.");
55         Preconditions.checkNotNull(osAccess.getUser(), "Keystone-v3 Auth: username not set.");
56         Preconditions.checkNotNull(osAccess.getPassword(), "Keystone-v3 Auth: password not set.");
57         Preconditions.checkNotNull(osAccess.getDomainNameIdentifier(), "Keystone-v3 Auth: domain not set.");
58         Preconditions.checkNotNull(osAccess.getRegion(), "Keystone-v3 Auth: region not set.");
59         Preconditions.checkNotNull(osAccess.getTenantId(), "Keystone-v3 Auth: tenant-id not set.");
60
61         OSClientV3 client;
62         try {
63             client = OSFactory.builderV3().endpoint(osAccess.getUrl())
64                     .credentials(osAccess.getUser(), osAccess.getPassword(), osAccess.getDomainNameIdentifier())
65                     .scopeToProject(Identifier.byId(osAccess.getTenantId()), osAccess.getProjectNameIdentifier())
66                     .authenticate().useRegion(osAccess.getRegion());
67             return new OpenstackV3ClientImpl(client);
68         } catch (AuthenticationException exception) {
69             throw new OpenstackClientException("Failed to authenticate with Keystone-v3: " + osAccess.getUrl(),
70                     exception);
71         }
72     }
73
74     @Override
75     public OpenstackClient createOpenstackV2Client(OpenstackAccess osAccess) throws OpenstackClientException {
76         Preconditions.checkNotNull(osAccess.getUrl(), "Keystone-v2 Auth: endpoint not set.");
77         Preconditions.checkNotNull(osAccess.getUser(), "Keystone-v2 Auth: username not set.");
78         Preconditions.checkNotNull(osAccess.getPassword(), "Keystone-v2 Auth: password not set.");
79         Preconditions.checkNotNull(osAccess.getTenantId(), "Keystone-v2 Auth: tenant-id not set.");
80         Preconditions.checkNotNull(osAccess.getRegion(), "Keystone-v2 Auth: region not set.");
81
82         OSClientV2 client;
83         try {
84             client = OSFactory.builderV2().endpoint(osAccess.getUrl())
85                     .credentials(osAccess.getUser(), osAccess.getPassword()).tenantId(osAccess.getTenantId())
86                     .authenticate().useRegion(osAccess.getRegion());
87             return new OpenstackV2ClientImpl(client);
88         } catch (AuthenticationException exception) {
89             throw new OpenstackClientException("Failed to authenticate with Keystone-v2.0: " + osAccess.getUrl(),
90                     exception);
91         }
92     }
93 }