blob: da378b098ffbea13a056a80e37ee5def7336b32b [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;
25import org.openstack4j.model.network.Subnet;
26import org.openstack4j.openstack.networking.domain.NeutronSubnet;
27
28import java.util.Comparator;
29import java.util.List;
30
Jian Li5ecfd1a2018-12-10 11:41:03 +090031import static org.onosproject.cli.AbstractShellCommand.get;
Daniel Park227d88a2018-10-02 14:34:43 +090032import 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) {
58 print(FORMAT,
59 subnet.getId(),
60 subnet.getName(),
61 subnet.getCidr(),
62 subnet.getGateway(),
63 subnet.getNetworkId(),
64 service.network(subnet.getNetworkId()).getName(),
65 subnet.getHostRoutes());
66 }
67 }
68 }
69
70 private String json(List<Subnet> subnets) {
71 ObjectMapper mapper = new ObjectMapper();
72 ArrayNode result = mapper.createArrayNode();
73 for (Subnet net: subnets) {
74 result.add(modelEntityToJson(net, NeutronSubnet.class));
75 }
76 return prettyJson(mapper, result.toString());
77 }
78}