blob: 4a4a3c5a3b730a2da7c33181786c48c0e22521a2 [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;
Ray Milkeydf521292018-10-04 15:13:33 -070021import org.apache.karaf.shell.api.action.Command;
22import org.apache.karaf.shell.api.action.lifecycle.Service;
Daniel Park227d88a2018-10-02 14:34:43 +090023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
Jian Li40f032a2019-10-02 20:36:09 +090025import org.openstack4j.model.network.Network;
Daniel Park227d88a2018-10-02 14:34:43 +090026import org.openstack4j.model.network.Subnet;
27import org.openstack4j.openstack.networking.domain.NeutronSubnet;
28
29import java.util.Comparator;
30import java.util.List;
31
32import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.modelEntityToJson;
33import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson;
34
35/**
36 * Lists OpenStack subnets.
37 */
Ray Milkeydf521292018-10-04 15:13:33 -070038@Service
Daniel Park227d88a2018-10-02 14:34:43 +090039@Command(scope = "onos", name = "openstack-subnets",
40 description = "Lists all OpenStack subnets")
41public class OpenstackSubnetListCommand extends AbstractShellCommand {
42
43 private static final String FORMAT = "%-40s%-20s%-20s%-20s%-40s%-20s%-8s";
44
45 @Override
Ray Milkeydf521292018-10-04 15:13:33 -070046 protected void doExecute() {
Jian Li5ecfd1a2018-12-10 11:41:03 +090047 OpenstackNetworkService service = get(OpenstackNetworkService.class);
Daniel Park227d88a2018-10-02 14:34:43 +090048 List<Subnet> subnets = Lists.newArrayList(service.subnets());
49 subnets.sort(Comparator.comparing(Subnet::getName));
50
51 if (outputJson()) {
52 print("%s", json(subnets));
53 } else {
Jian Li5ecfd1a2018-12-10 11:41:03 +090054 print(FORMAT, "ID", "Name", "CIDR", "GatewayIp", "NetworkId",
55 "NetworkName", "HostRoutes");
Daniel Park227d88a2018-10-02 14:34:43 +090056
57 for (Subnet subnet: subnets) {
Jian Li40f032a2019-10-02 20:36:09 +090058 Network osNet = service.network(subnet.getNetworkId());
59 String netName = osNet == null ? "N/A" : osNet.getName();
Daniel Park227d88a2018-10-02 14:34:43 +090060 print(FORMAT,
61 subnet.getId(),
62 subnet.getName(),
63 subnet.getCidr(),
64 subnet.getGateway(),
65 subnet.getNetworkId(),
Jian Li40f032a2019-10-02 20:36:09 +090066 netName,
Daniel Park227d88a2018-10-02 14:34:43 +090067 subnet.getHostRoutes());
68 }
69 }
70 }
71
72 private String json(List<Subnet> subnets) {
73 ObjectMapper mapper = new ObjectMapper();
74 ArrayNode result = mapper.createArrayNode();
75 for (Subnet net: subnets) {
76 result.add(modelEntityToJson(net, NeutronSubnet.class));
77 }
78 return prettyJson(mapper, result.toString());
79 }
80}