Hyunsun Moon | a74171d | 2016-02-24 14:41:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 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.onosproject.openstack4j; |
| 17 | |
| 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Service; |
| 22 | import org.openstack4j.api.OSClient; |
| 23 | import org.openstack4j.api.exceptions.AuthenticationException; |
| 24 | import org.openstack4j.model.network.Network; |
| 25 | import org.openstack4j.model.network.Port; |
| 26 | import org.openstack4j.model.network.Subnet; |
| 27 | import org.openstack4j.model.network.options.PortListOptions; |
| 28 | import org.openstack4j.openstack.OSFactory; |
| 29 | import org.slf4j.Logger; |
| 30 | import org.slf4j.LoggerFactory; |
| 31 | |
| 32 | import java.util.List; |
| 33 | import java.util.stream.Collectors; |
| 34 | |
| 35 | import static com.google.common.base.Preconditions.checkNotNull; |
| 36 | |
| 37 | /** |
| 38 | * Wrapper implementation of openstack4j. |
| 39 | */ |
| 40 | @Component(immediate = true) |
| 41 | @Service |
| 42 | public class OpenStack4jManager implements OpenStack4jService { |
| 43 | |
| 44 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 45 | |
| 46 | @Activate |
| 47 | protected void activate() { |
| 48 | log.info("Started"); |
| 49 | } |
| 50 | |
| 51 | @Deactivate |
| 52 | protected void deactivate() { |
| 53 | log.info("Stopped"); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public OSClient getClient(String endpoint, String tenant, String user, String password) { |
| 58 | try { |
| 59 | return OSFactory.builder() |
| 60 | .endpoint(endpoint) |
| 61 | .credentials(user, password) |
| 62 | .tenantName(tenant) |
| 63 | .authenticate(); |
| 64 | } catch (AuthenticationException e) { |
| 65 | log.warn("Failed to authenticate"); |
| 66 | return null; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public List<Network> getNetworks(OSClient client) { |
| 72 | checkNotNull(client, "OSClient is null"); |
| 73 | return client.networking().network().list() |
| 74 | .stream() |
| 75 | .collect(Collectors.toList()); |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public Network getNetwork(OSClient client, String networkId) { |
| 80 | checkNotNull(client, "OSClient is null"); |
| 81 | return client.networking().network().get(networkId); |
| 82 | } |
| 83 | |
| 84 | @Override |
| 85 | public List<Port> getPorts(OSClient client) { |
| 86 | checkNotNull(client, "OSClient is null"); |
| 87 | return client.networking().port().list() |
| 88 | .stream() |
| 89 | .collect(Collectors.toList()); |
| 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public List<Port> getPorts(OSClient client, PortListOptions options) { |
| 94 | checkNotNull(client, "OSClient is null"); |
| 95 | return client.networking().port().list(options) |
| 96 | .stream() |
| 97 | .collect(Collectors.toList()); |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public Port getPort(OSClient client, String portId) { |
| 102 | checkNotNull(client, "OSClient is null"); |
| 103 | return client.networking().port().get(portId); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public List<Subnet> getSubnets(OSClient client) { |
| 108 | checkNotNull(client, "OSClient is null"); |
| 109 | return client.networking().subnet().list() |
| 110 | .stream() |
| 111 | .collect(Collectors.toList()); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public Subnet getSubnet(OSClient client, String subnetId) { |
| 116 | checkNotNull(client, "OSClient is null"); |
| 117 | return client.networking().subnet().get(subnetId); |
| 118 | } |
| 119 | } |