81f09b8a3e28876fb3835973024369263b82377d
[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
37 package org.onap.so.heatbridge.openstack.api;
38
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Objects;
42 import java.util.stream.Collectors;
43 import org.openstack4j.api.OSClient;
44 import org.openstack4j.model.compute.Server;
45 import org.openstack4j.model.heat.Resource;
46 import org.openstack4j.model.network.Network;
47 import org.openstack4j.model.network.Port;
48 import org.openstack4j.model.network.Subnet;
49 import org.openstack4j.model.storage.block.Volume;
50 import org.openstack4j.model.storage.block.VolumeBackup;
51
52 abstract class OpenstackClientImpl implements OpenstackClient {
53     @Override
54     public Server getServerById(String serverId) {
55         return getClient().compute().servers().get(serverId);
56     }
57
58     @Override
59     public Port getPortById(String portId) {
60         return getClient().networking().port().get(portId);
61     }
62
63     @Override
64     public List<Port> getAllPorts() {
65         return (List<Port>) getClient().networking().port().list();
66     }
67
68     @Override
69     public List<Resource> getStackBasedResources(String stackId, int nestingDepth) {
70         return getClient().heat().resources().list(stackId, nestingDepth).stream().filter(Objects::nonNull)
71                 .collect(Collectors.toList());
72     }
73
74     @Override
75     public Network getNetworkById(String networkId) {
76         return getClient().networking().network().get(networkId);
77     }
78
79     @Override
80     public List<Network> listNetworksByFilter(Map<String, String> filterParams) {
81         return (List<Network>) getClient().networking().network().list(filterParams);
82     }
83
84     @Override
85     public Subnet getSubnetById(String subnetId) {
86         return getClient().networking().subnet().get(subnetId);
87     }
88
89     @Override
90     public Volume getVolumeById(String id) {
91         return getClient().blockStorage().volumes().get(id);
92     }
93
94     /**
95      * Retrieves the specific client to utilize.
96      * 
97      * @return The specific client to utilize
98      */
99     protected abstract OSClient getClient();
100
101 }