blob: 6fbfd5bb273a951a97773e6436d0d492e942b27f [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
Jian Li5b22f112020-10-05 22:36:41 +090032import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.deriveResourceName;
Daniel Park227d88a2018-10-02 14:34:43 +090033import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.modelEntityToJson;
34import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson;
35
36/**
37 * Lists OpenStack subnets.
38 */
Ray Milkeydf521292018-10-04 15:13:33 -070039@Service
Daniel Park227d88a2018-10-02 14:34:43 +090040@Command(scope = "onos", name = "openstack-subnets",
41 description = "Lists all OpenStack subnets")
42public class OpenstackSubnetListCommand extends AbstractShellCommand {
43
44 private static final String FORMAT = "%-40s%-20s%-20s%-20s%-40s%-20s%-8s";
45
46 @Override
Ray Milkeydf521292018-10-04 15:13:33 -070047 protected void doExecute() {
Jian Li5ecfd1a2018-12-10 11:41:03 +090048 OpenstackNetworkService service = get(OpenstackNetworkService.class);
Daniel Park227d88a2018-10-02 14:34:43 +090049 List<Subnet> subnets = Lists.newArrayList(service.subnets());
Jian Li5b22f112020-10-05 22:36:41 +090050 subnets.sort(Comparator.comparing(Subnet::getId));
Daniel Park227d88a2018-10-02 14:34:43 +090051
52 if (outputJson()) {
53 print("%s", json(subnets));
54 } else {
Jian Li5ecfd1a2018-12-10 11:41:03 +090055 print(FORMAT, "ID", "Name", "CIDR", "GatewayIp", "NetworkId",
56 "NetworkName", "HostRoutes");
Daniel Park227d88a2018-10-02 14:34:43 +090057
58 for (Subnet subnet: subnets) {
Jian Li40f032a2019-10-02 20:36:09 +090059 Network osNet = service.network(subnet.getNetworkId());
Jian Li5b22f112020-10-05 22:36:41 +090060 String netName = osNet == null ? "N/A" : deriveResourceName(osNet);
Daniel Park227d88a2018-10-02 14:34:43 +090061 print(FORMAT,
62 subnet.getId(),
Jian Li5b22f112020-10-05 22:36:41 +090063 deriveResourceName(subnet),
Daniel Park227d88a2018-10-02 14:34:43 +090064 subnet.getCidr(),
65 subnet.getGateway(),
66 subnet.getNetworkId(),
Jian Li40f032a2019-10-02 20:36:09 +090067 netName,
Daniel Park227d88a2018-10-02 14:34:43 +090068 subnet.getHostRoutes());
69 }
70 }
71 }
72
73 private String json(List<Subnet> subnets) {
74 ObjectMapper mapper = new ObjectMapper();
75 ArrayNode result = mapper.createArrayNode();
76 for (Subnet net: subnets) {
77 result.add(modelEntityToJson(net, NeutronSubnet.class));
78 }
79 return prettyJson(mapper, result.toString());
80 }
81}