Daniel Park | 227d88a | 2018-10-02 14:34:43 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.openstacknetworking.cli; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 20 | import com.google.common.collect.Lists; |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 21 | import org.apache.karaf.shell.api.action.Command; |
| 22 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Daniel Park | 227d88a | 2018-10-02 14:34:43 +0900 | [diff] [blame] | 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
Jian Li | 40f032a | 2019-10-02 20:36:09 +0900 | [diff] [blame] | 25 | import org.openstack4j.model.network.Network; |
Daniel Park | 227d88a | 2018-10-02 14:34:43 +0900 | [diff] [blame] | 26 | import org.openstack4j.model.network.Subnet; |
| 27 | import org.openstack4j.openstack.networking.domain.NeutronSubnet; |
| 28 | |
| 29 | import java.util.Comparator; |
| 30 | import java.util.List; |
| 31 | |
| 32 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.modelEntityToJson; |
| 33 | import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson; |
| 34 | |
| 35 | /** |
| 36 | * Lists OpenStack subnets. |
| 37 | */ |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 38 | @Service |
Daniel Park | 227d88a | 2018-10-02 14:34:43 +0900 | [diff] [blame] | 39 | @Command(scope = "onos", name = "openstack-subnets", |
| 40 | description = "Lists all OpenStack subnets") |
| 41 | public class OpenstackSubnetListCommand extends AbstractShellCommand { |
| 42 | |
| 43 | private static final String FORMAT = "%-40s%-20s%-20s%-20s%-40s%-20s%-8s"; |
| 44 | |
| 45 | @Override |
Ray Milkey | df52129 | 2018-10-04 15:13:33 -0700 | [diff] [blame] | 46 | protected void doExecute() { |
Jian Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 47 | OpenstackNetworkService service = get(OpenstackNetworkService.class); |
Daniel Park | 227d88a | 2018-10-02 14:34:43 +0900 | [diff] [blame] | 48 | 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 Li | 5ecfd1a | 2018-12-10 11:41:03 +0900 | [diff] [blame] | 54 | print(FORMAT, "ID", "Name", "CIDR", "GatewayIp", "NetworkId", |
| 55 | "NetworkName", "HostRoutes"); |
Daniel Park | 227d88a | 2018-10-02 14:34:43 +0900 | [diff] [blame] | 56 | |
| 57 | for (Subnet subnet: subnets) { |
Jian Li | 40f032a | 2019-10-02 20:36:09 +0900 | [diff] [blame] | 58 | Network osNet = service.network(subnet.getNetworkId()); |
| 59 | String netName = osNet == null ? "N/A" : osNet.getName(); |
Daniel Park | 227d88a | 2018-10-02 14:34:43 +0900 | [diff] [blame] | 60 | print(FORMAT, |
| 61 | subnet.getId(), |
| 62 | subnet.getName(), |
| 63 | subnet.getCidr(), |
| 64 | subnet.getGateway(), |
| 65 | subnet.getNetworkId(), |
Jian Li | 40f032a | 2019-10-02 20:36:09 +0900 | [diff] [blame] | 66 | netName, |
Daniel Park | 227d88a | 2018-10-02 14:34:43 +0900 | [diff] [blame] | 67 | 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 | } |