blob: a2ef4917fade4a65f05e194798d367af0f12a312 [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;
Hyunsun Moon44aac662017-02-18 02:07:01 +090021import com.google.common.base.Strings;
22import com.google.common.collect.Lists;
23import org.apache.karaf.shell.commands.Command;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.openstacknetworking.api.OpenstackRouterService;
Hyunsun Moon44aac662017-02-18 02:07:01 +090026import org.openstack4j.model.network.NetFloatingIP;
27import org.openstack4j.openstack.networking.domain.NeutronFloatingIP;
28
29import java.util.Comparator;
30import java.util.List;
31
32import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
Jian Lieb9f77d2018-02-20 11:25:45 +090033import static org.onosproject.openstacknetworking.util.OpenstackUtil.modelEntityToJson;
Hyunsun Moon44aac662017-02-18 02:07:01 +090034
35/**
36 * Lists OpenStack floating IP addresses.
37 */
38@Command(scope = "onos", name = "openstack-floatingips",
39 description = "Lists all OpenStack floating IP addresses")
40public class OpenstackFloatingIpListCommand extends AbstractShellCommand {
41
42 private static final String FORMAT = "%-40s%-20s%-20s";
43
44 @Override
45 protected void execute() {
46 OpenstackRouterService service = AbstractShellCommand.get(OpenstackRouterService.class);
47 List<NetFloatingIP> floatingIps = Lists.newArrayList(service.floatingIps());
48 floatingIps.sort(Comparator.comparing(NetFloatingIP::getFloatingIpAddress));
49
50 if (outputJson()) {
51 try {
52 print("%s", mapper().writeValueAsString(json(floatingIps)));
53 } catch (JsonProcessingException e) {
54 print("Failed to list networks in JSON format");
55 }
56 return;
57 }
58
59 print(FORMAT, "ID", "Floating IP", "Fixed IP");
60 for (NetFloatingIP floatingIp: floatingIps) {
61 print(FORMAT, floatingIp.getId(),
62 floatingIp.getFloatingIpAddress(),
63 Strings.isNullOrEmpty(floatingIp.getFixedIpAddress()) ?
64 "" : floatingIp.getFixedIpAddress());
65 }
66 }
67
68 private JsonNode json(List<NetFloatingIP> floatingIps) {
69 ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode();
70 for (NetFloatingIP floatingIp: floatingIps) {
Jian Lieb9f77d2018-02-20 11:25:45 +090071 result.add(modelEntityToJson(floatingIp, NeutronFloatingIP.class));
Hyunsun Moon44aac662017-02-18 02:07:01 +090072 }
73 return result;
74 }
Hyunsun Moon44aac662017-02-18 02:07:01 +090075}