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