Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present 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.openstacknetworking.cli; |
| 17 | |
| 18 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import com.google.common.base.Strings; |
| 23 | import com.google.common.collect.Lists; |
| 24 | import org.apache.karaf.shell.commands.Command; |
| 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.openstacknetworking.api.OpenstackRouterService; |
| 27 | import org.openstack4j.core.transport.ObjectMapperSingleton; |
| 28 | import org.openstack4j.model.network.NetFloatingIP; |
| 29 | import org.openstack4j.openstack.networking.domain.NeutronFloatingIP; |
| 30 | |
| 31 | import java.util.Comparator; |
| 32 | import java.util.List; |
| 33 | |
| 34 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 35 | |
| 36 | /** |
| 37 | * Lists OpenStack floating IP addresses. |
| 38 | */ |
| 39 | @Command(scope = "onos", name = "openstack-floatingips", |
| 40 | description = "Lists all OpenStack floating IP addresses") |
| 41 | public class OpenstackFloatingIpListCommand extends AbstractShellCommand { |
| 42 | |
| 43 | private static final String FORMAT = "%-40s%-20s%-20s"; |
| 44 | |
| 45 | @Override |
| 46 | protected void execute() { |
| 47 | OpenstackRouterService service = AbstractShellCommand.get(OpenstackRouterService.class); |
| 48 | List<NetFloatingIP> floatingIps = Lists.newArrayList(service.floatingIps()); |
| 49 | floatingIps.sort(Comparator.comparing(NetFloatingIP::getFloatingIpAddress)); |
| 50 | |
| 51 | if (outputJson()) { |
| 52 | try { |
| 53 | print("%s", mapper().writeValueAsString(json(floatingIps))); |
| 54 | } catch (JsonProcessingException e) { |
| 55 | print("Failed to list networks in JSON format"); |
| 56 | } |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | print(FORMAT, "ID", "Floating IP", "Fixed IP"); |
| 61 | for (NetFloatingIP floatingIp: floatingIps) { |
| 62 | print(FORMAT, floatingIp.getId(), |
| 63 | floatingIp.getFloatingIpAddress(), |
| 64 | Strings.isNullOrEmpty(floatingIp.getFixedIpAddress()) ? |
| 65 | "" : floatingIp.getFixedIpAddress()); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | private JsonNode json(List<NetFloatingIP> floatingIps) { |
| 70 | ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode(); |
| 71 | for (NetFloatingIP floatingIp: floatingIps) { |
| 72 | result.add(writeFloatingIp(floatingIp)); |
| 73 | } |
| 74 | return result; |
| 75 | } |
| 76 | |
| 77 | private ObjectNode writeFloatingIp(NetFloatingIP floatingIp) { |
| 78 | try { |
| 79 | String strFloatingIp = ObjectMapperSingleton.getContext(NeutronFloatingIP.class) |
| 80 | .writerFor(NeutronFloatingIP.class) |
| 81 | .writeValueAsString(floatingIp); |
| 82 | log.trace(strFloatingIp); |
| 83 | return (ObjectNode) mapper().readTree(strFloatingIp.getBytes()); |
| 84 | } catch (Exception e) { |
| 85 | throw new IllegalStateException(); |
| 86 | } |
| 87 | } |
| 88 | } |