heatbridge implementation for openstack-adapter
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / heatbridge / openstack / factory / OpenstackClientFactoryImpl.java
1 /*-
2  * Copyright (C) 2018 Bell Canada. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.so.heatbridge.openstack.factory;
17
18 import com.google.common.base.Preconditions;
19 import org.onap.so.heatbridge.openstack.api.OpenstackAccess;
20 import org.onap.so.heatbridge.openstack.api.OpenstackClient;
21 import org.onap.so.heatbridge.openstack.api.OpenstackClientException;
22 import org.onap.so.heatbridge.openstack.api.OpenstackV2ClientImpl;
23 import org.onap.so.heatbridge.openstack.api.OpenstackV3ClientImpl;
24 import org.openstack4j.api.OSClient.OSClientV2;
25 import org.openstack4j.api.OSClient.OSClientV3;
26 import org.openstack4j.api.exceptions.AuthenticationException;
27 import org.openstack4j.openstack.OSFactory;
28
29 public class OpenstackClientFactoryImpl implements OpenstackClientFactory {
30
31     @Override
32     public OpenstackClient createOpenstackV3Client(OpenstackAccess osAccess) throws OpenstackClientException {
33         Preconditions.checkNotNull(osAccess.getUrl(), "Keystone-v3 Auth: endpoint not set.");
34         Preconditions.checkNotNull(osAccess.getUser(), "Keystone-v3 Auth: username not set.");
35         Preconditions.checkNotNull(osAccess.getPassword(), "Keystone-v3 Auth: password not set.");
36         Preconditions.checkNotNull(osAccess.getDomainNameIdentifier(), "Keystone-v3 Auth: domain not set.");
37         Preconditions.checkNotNull(osAccess.getRegion(), "Keystone-v3 Auth: region not set.");
38
39         OSClientV3 client;
40         try {
41             client = OSFactory.builderV3()
42                 .endpoint(osAccess.getUrl())
43                 .credentials(osAccess.getUser(), osAccess.getPassword(), osAccess.getDomainNameIdentifier())
44                 .authenticate()
45                 .useRegion(osAccess.getRegion());
46             return new OpenstackV3ClientImpl(client);
47         } catch (AuthenticationException exception) {
48             throw new OpenstackClientException("Failed to authenticate with Keystone-v3: " + osAccess.getUrl(), exception);
49         }
50     }
51
52     @Override
53     public OpenstackClient createOpenstackV2Client(OpenstackAccess osAccess) throws OpenstackClientException {
54         Preconditions.checkNotNull(osAccess.getUrl(), "Keystone-v2 Auth: endpoint not set.");
55         Preconditions.checkNotNull(osAccess.getUser(), "Keystone-v2 Auth: username not set.");
56         Preconditions.checkNotNull(osAccess.getPassword(), "Keystone-v2 Auth: password not set.");
57         Preconditions.checkNotNull(osAccess.getTenantId(), "Keystone-v2 Auth: domain not set.");
58         Preconditions.checkNotNull(osAccess.getRegion(), "Keystone-v2 Auth: region not set.");
59
60         OSClientV2 client;
61         try {
62             client = OSFactory.builderV2()
63                 .endpoint(osAccess.getUrl())
64                 .credentials(osAccess.getUser(), osAccess.getPassword())
65                 .tenantId(osAccess.getTenantId())
66                 .authenticate()
67                 .useRegion(osAccess.getRegion());
68             return new OpenstackV2ClientImpl(client);
69         } catch (AuthenticationException exception) {
70             throw new OpenstackClientException("Failed to authenticate with Keystone-v2.0: " + osAccess.getUrl(),
71                 exception);
72         }
73     }
74 }