blob: 8b489c961f66d69826b7b10c7f7da1fcc4ad39e4 [file] [log] [blame]
Hyunsun Moon44aac662017-02-18 02:07:01 +09001/*
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 */
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.openstack4j.core.transport.ObjectMapperSingleton;
27import org.openstack4j.model.network.Network;
28import org.openstack4j.model.network.Subnet;
29import org.openstack4j.openstack.networking.domain.NeutronNetwork;
30
31import java.util.Comparator;
32import java.util.List;
33import java.util.stream.Collectors;
34
35import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT;
36
37/**
38 * Lists OpenStack networks.
39 */
40@Command(scope = "onos", name = "openstack-networks",
41 description = "Lists all OpenStack networks")
42public class OpenstackNetworkListCommand extends AbstractShellCommand {
43
44 private static final String FORMAT = "%-40s%-20s%-20s%-8s";
45
46 @Override
47 protected void execute() {
48 OpenstackNetworkService service = AbstractShellCommand.get(OpenstackNetworkService.class);
49 List<Network> networks = Lists.newArrayList(service.networks());
50 networks.sort(Comparator.comparing(Network::getName));
51
52 if (outputJson()) {
53 try {
54 print("%s", mapper().writeValueAsString(json(networks)));
55 } catch (JsonProcessingException e) {
56 print("Failed to list networks in JSON format");
57 }
58 return;
59 }
60
61 print(FORMAT, "ID", "Name", "VNI", "Subnets");
62 for (Network net: networks) {
63 List<String> subnets = service.subnets().stream()
64 .filter(subnet -> subnet.getNetworkId().equals(net.getId()))
65 .map(Subnet::getCidr)
66 .collect(Collectors.toList());
67 print(FORMAT, net.getId(),
68 net.getName(),
69 net.getProviderSegID(),
70 subnets.isEmpty() ? "" : subnets);
71 }
72 }
73
74 private JsonNode json(List<Network> networks) {
75 ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode();
76 for (Network net: networks) {
77 result.add(writeNetwork(net));
78 }
79 return result;
80 }
81
82 private ObjectNode writeNetwork(Network net) {
83 try {
84 String strNet = ObjectMapperSingleton.getContext(NeutronNetwork.class)
85 .writerFor(NeutronNetwork.class)
86 .writeValueAsString(net);
87 return (ObjectNode) mapper().readTree(strNet.getBytes());
88 } catch (Exception e) {
89 throw new IllegalStateException();
90 }
91 }
92}