heatbridge implementation for openstack-adapter
[so.git] / adapters / mso-openstack-adapters / src / main / java / org / onap / so / heatbridge / openstack / api / OpenstackClientImpl.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
17 package org.onap.so.heatbridge.openstack.api;
18
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Objects;
22 import java.util.stream.Collectors;
23 import org.openstack4j.api.OSClient;
24 import org.openstack4j.model.compute.Server;
25 import org.openstack4j.model.heat.Resource;
26 import org.openstack4j.model.network.Network;
27 import org.openstack4j.model.network.Port;
28
29 abstract class OpenstackClientImpl implements OpenstackClient {
30     @Override
31     public Server getServerById(String serverId) {
32         return getClient().compute().servers().get(serverId);
33     }
34
35     @Override
36     public Port getPortById(String portId) {
37         return getClient().networking().port().get(portId);
38     }
39
40     @Override
41     public List<Port> getAllPorts() { return (List<Port>)getClient().networking().port().list(); }
42
43     @Override
44     public List<Resource> getStackBasedResources(String stackId, int nestingDepth) {
45         return getClient().heat()
46                 .resources()
47                 .list(stackId, nestingDepth)
48                 .stream()
49                 .filter(Objects::nonNull)
50                 .collect(Collectors.toList());
51     }
52
53     @Override
54     public Network getNetworkById(String networkId) {
55         return getClient().networking().network().get(networkId);
56     }
57
58     @Override
59     public List<Network> listNetworksByFilter(Map<String, String> filterParams) {
60         return (List<Network>) getClient().networking().network().list(filterParams);
61     }
62
63     /**
64      * Retrieves the specific client to utilize.
65      * @return The specific client to utilize
66      */
67     protected abstract OSClient getClient();
68
69 }