2 * Copyright (C) 2018 Bell Canada. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onap.so.heatbridge.openstack.factory;
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;
29 public class OpenstackClientFactoryImpl implements OpenstackClientFactory {
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.");
41 client = OSFactory.builderV3()
42 .endpoint(osAccess.getUrl())
43 .credentials(osAccess.getUser(), osAccess.getPassword(), osAccess.getDomainNameIdentifier())
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);
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.");
62 client = OSFactory.builderV2()
63 .endpoint(osAccess.getUrl())
64 .credentials(osAccess.getUser(), osAccess.getPassword())
65 .tenantId(osAccess.getTenantId())
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(),