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;
28 import org.openstack4j.model.common.Identifier;
30 public class OpenstackClientFactoryImpl implements OpenstackClientFactory {
33 public OpenstackClient createOpenstackV3Client(OpenstackAccess osAccess) throws OpenstackClientException {
34 Preconditions.checkNotNull(osAccess.getUrl(), "Keystone-v3 Auth: endpoint not set.");
35 Preconditions.checkNotNull(osAccess.getUser(), "Keystone-v3 Auth: username not set.");
36 Preconditions.checkNotNull(osAccess.getPassword(), "Keystone-v3 Auth: password not set.");
37 Preconditions.checkNotNull(osAccess.getDomainNameIdentifier(), "Keystone-v3 Auth: domain not set.");
38 Preconditions.checkNotNull(osAccess.getRegion(), "Keystone-v3 Auth: region not set.");
39 Preconditions.checkNotNull(osAccess.getTenantId(), "Keystone-v3 Auth: tenant-id not set.");
43 client = OSFactory.builderV3().endpoint(osAccess.getUrl())
44 .credentials(osAccess.getUser(), osAccess.getPassword(), osAccess.getDomainNameIdentifier())
45 .scopeToProject(Identifier.byId(osAccess.getTenantId())).authenticate()
46 .useRegion(osAccess.getRegion());
47 return new OpenstackV3ClientImpl(client);
48 } catch (AuthenticationException exception) {
49 throw new OpenstackClientException("Failed to authenticate with Keystone-v3: " + osAccess.getUrl(),
55 public OpenstackClient createOpenstackV2Client(OpenstackAccess osAccess) throws OpenstackClientException {
56 Preconditions.checkNotNull(osAccess.getUrl(), "Keystone-v2 Auth: endpoint not set.");
57 Preconditions.checkNotNull(osAccess.getUser(), "Keystone-v2 Auth: username not set.");
58 Preconditions.checkNotNull(osAccess.getPassword(), "Keystone-v2 Auth: password not set.");
59 Preconditions.checkNotNull(osAccess.getTenantId(), "Keystone-v2 Auth: tenant-id not set.");
60 Preconditions.checkNotNull(osAccess.getRegion(), "Keystone-v2 Auth: region not set.");
64 client = OSFactory.builderV2().endpoint(osAccess.getUrl())
65 .credentials(osAccess.getUser(), osAccess.getPassword()).tenantId(osAccess.getTenantId())
66 .authenticate().useRegion(osAccess.getRegion());
67 return new OpenstackV2ClientImpl(client);
68 } catch (AuthenticationException exception) {
69 throw new OpenstackClientException("Failed to authenticate with Keystone-v2.0: " + osAccess.getUrl(),