blob: 69573e2326e54fc3fb36cb2ddb127cf9f34c7092 [file] [log] [blame]
Daniel Park227d88a2018-10-02 14:34:43 +09001/*
2 * Copyright 2018-present Open Networking Foundation
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.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.google.common.collect.Lists;
21import org.apache.karaf.shell.commands.Command;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
24import org.openstack4j.model.network.Subnet;
25import org.openstack4j.openstack.networking.domain.NeutronSubnet;
26
27import java.util.Comparator;
28import java.util.List;
29
30import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.modelEntityToJson;
31import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson;
32
33/**
34 * Lists OpenStack subnets.
35 */
36@Command(scope = "onos", name = "openstack-subnets",
37 description = "Lists all OpenStack subnets")
38public class OpenstackSubnetListCommand extends AbstractShellCommand {
39
40 private static final String FORMAT = "%-40s%-20s%-20s%-20s%-40s%-20s%-8s";
41
42 @Override
43 protected void execute() {
44 OpenstackNetworkService service = AbstractShellCommand.get(OpenstackNetworkService.class);
45 List<Subnet> subnets = Lists.newArrayList(service.subnets());
46 subnets.sort(Comparator.comparing(Subnet::getName));
47
48 if (outputJson()) {
49 print("%s", json(subnets));
50 } else {
51 print(FORMAT, "ID", "Name", "CIDR", "GatewayIp", "NetworkId", "NetworkName", "HostRoutes");
52
53 for (Subnet subnet: subnets) {
54 print(FORMAT,
55 subnet.getId(),
56 subnet.getName(),
57 subnet.getCidr(),
58 subnet.getGateway(),
59 subnet.getNetworkId(),
60 service.network(subnet.getNetworkId()).getName(),
61 subnet.getHostRoutes());
62 }
63 }
64 }
65
66 private String json(List<Subnet> subnets) {
67 ObjectMapper mapper = new ObjectMapper();
68 ArrayNode result = mapper.createArrayNode();
69 for (Subnet net: subnets) {
70 result.add(modelEntityToJson(net, NeutronSubnet.class));
71 }
72 return prettyJson(mapper, result.toString());
73 }
74}