blob: 75bb1d5413294b98327eeb4cc12376df0ce5aee7 [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
31import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.modelEntityToJson;
32import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson;
33
34/**
35 * Lists OpenStack subnets.
36 */
Ray Milkeydf521292018-10-04 15:13:33 -070037@Service
Daniel Park227d88a2018-10-02 14:34:43 +090038@Command(scope = "onos", name = "openstack-subnets",
39 description = "Lists all OpenStack subnets")
40public class OpenstackSubnetListCommand extends AbstractShellCommand {
41
42 private static final String FORMAT = "%-40s%-20s%-20s%-20s%-40s%-20s%-8s";
43
44 @Override
Ray Milkeydf521292018-10-04 15:13:33 -070045 protected void doExecute() {
Daniel Park227d88a2018-10-02 14:34:43 +090046 OpenstackNetworkService service = AbstractShellCommand.get(OpenstackNetworkService.class);
47 List<Subnet> subnets = Lists.newArrayList(service.subnets());
48 subnets.sort(Comparator.comparing(Subnet::getName));
49
50 if (outputJson()) {
51 print("%s", json(subnets));
52 } else {
53 print(FORMAT, "ID", "Name", "CIDR", "GatewayIp", "NetworkId", "NetworkName", "HostRoutes");
54
55 for (Subnet subnet: subnets) {
56 print(FORMAT,
57 subnet.getId(),
58 subnet.getName(),
59 subnet.getCidr(),
60 subnet.getGateway(),
61 subnet.getNetworkId(),
62 service.network(subnet.getNetworkId()).getName(),
63 subnet.getHostRoutes());
64 }
65 }
66 }
67
68 private String json(List<Subnet> subnets) {
69 ObjectMapper mapper = new ObjectMapper();
70 ArrayNode result = mapper.createArrayNode();
71 for (Subnet net: subnets) {
72 result.add(modelEntityToJson(net, NeutronSubnet.class));
73 }
74 return prettyJson(mapper, result.toString());
75 }
76}