blob: 66cd2ead1a3390b463c1e4edd5f743cf0e322332 [file] [log] [blame]
Hyunsun Moon44aac662017-02-18 02:07:01 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Hyunsun Moon44aac662017-02-18 02:07:01 +09003 *
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 */
16package org.onosproject.openstacknetworking.cli;
17
18import com.fasterxml.jackson.core.JsonProcessingException;
19import com.fasterxml.jackson.databind.JsonNode;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
22import com.google.common.base.Strings;
23import com.google.common.collect.Lists;
24import org.apache.karaf.shell.commands.Command;
25import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.openstacknetworking.api.OpenstackRouterService;
27import org.openstack4j.core.transport.ObjectMapperSingleton;
28import org.openstack4j.model.network.NetFloatingIP;
29import org.openstack4j.openstack.networking.domain.NeutronFloatingIP;
30
31import java.util.Comparator;
32import java.util.List;
33
34import 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")
41public 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}