blob: 772fa9baf7342ac81bed0bfffa25a6178e526c4e [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.collect.Lists;
22import org.apache.karaf.shell.commands.Command;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
25import org.onosproject.openstacknetworking.api.OpenstackRouterService;
Hyunsun Moon44aac662017-02-18 02:07:01 +090026import org.openstack4j.model.network.IP;
27import org.openstack4j.model.network.Port;
28import org.openstack4j.model.network.Router;
29import org.openstack4j.model.network.RouterInterface;
30import org.openstack4j.openstack.networking.domain.NeutronRouter;
31
32import java.util.Comparator;
33import java.util.List;
34import java.util.Objects;
35import java.util.stream.Collectors;
36
37import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
Jian Lieb9f77d2018-02-20 11:25:45 +090038import static org.onosproject.openstacknetworking.util.OpenstackUtil.modelEntityToJson;
Hyunsun Moon44aac662017-02-18 02:07:01 +090039
40/**
41 * Lists OpenStack routers.
42 */
43@Command(scope = "onos", name = "openstack-routers",
44 description = "Lists all OpenStack routers")
45public class OpenstackRouterListCommand extends AbstractShellCommand {
46
47 private static final String FORMAT = "%-40s%-20s%-20s%-8s";
48
49 private final OpenstackRouterService routerService =
50 AbstractShellCommand.get(OpenstackRouterService.class);
51 private final OpenstackNetworkService netService =
52 AbstractShellCommand.get(OpenstackNetworkService.class);
53
54 @Override
55 protected void execute() {
56 List<Router> routers = Lists.newArrayList(routerService.routers());
57 routers.sort(Comparator.comparing(Router::getName));
58
59 if (outputJson()) {
60 try {
61 print("%s", mapper().writeValueAsString(json(routers)));
62 } catch (JsonProcessingException e) {
63 print("Failed to list routers in JSON format");
64 }
65 return;
66 }
67 print(FORMAT, "ID", "Name", "External", "Internal");
68 for (Router router: routers) {
69 String exNetId = router.getExternalGatewayInfo() != null ?
70 router.getExternalGatewayInfo().getNetworkId() : null;
71
72 List<String> externals = Lists.newArrayList();
73 if (exNetId != null) {
74 // FIXME fix openstack4j to provide external gateway ip info
75 externals = netService.ports(exNetId).stream()
76 .filter(port -> Objects.equals(port.getDeviceId(),
77 router.getId()))
78 .flatMap(port -> port.getFixedIps().stream())
79 .map(IP::getIpAddress)
80 .collect(Collectors.toList());
81 }
82
83 List<String> internals = Lists.newArrayList();
84 routerService.routerInterfaces(router.getId()).forEach(iface -> {
85 internals.add(getRouterIfaceIp(iface));
86 });
87 print(FORMAT, router.getId(), router.getName(), externals, internals);
88 }
89 }
90
91 private String getRouterIfaceIp(RouterInterface iface) {
92 Port osPort = netService.port(iface.getPortId());
93 IP ipAddr = osPort.getFixedIps().stream()
94 .filter(ip -> ip.getSubnetId().equals(iface.getSubnetId()))
95 .findAny().orElse(null);
96 return ipAddr == null ? "" : ipAddr.getIpAddress();
97 }
98
99 private JsonNode json(List<Router> routers) {
100 ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode();
101 for (Router router: routers) {
Jian Lieb9f77d2018-02-20 11:25:45 +0900102 result.add(modelEntityToJson(router, NeutronRouter.class));
Hyunsun Moon44aac662017-02-18 02:07:01 +0900103 }
104 return result;
105 }
Hyunsun Moon44aac662017-02-18 02:07:01 +0900106}