Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Hyunsun Moon | 44aac66 | 2017-02-18 02:07:01 +0900 | [diff] [blame] | 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.core.JsonProcessingException; |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import com.google.common.base.Strings; |
| 23 | import com.google.common.collect.Lists; |
| 24 | import org.apache.karaf.shell.commands.Argument; |
| 25 | import org.apache.karaf.shell.commands.Command; |
| 26 | import org.onosproject.cli.AbstractShellCommand; |
| 27 | import org.onosproject.openstacknetworking.api.OpenstackNetworkService; |
| 28 | import org.openstack4j.core.transport.ObjectMapperSingleton; |
| 29 | import org.openstack4j.model.network.IP; |
| 30 | import org.openstack4j.model.network.Network; |
| 31 | import org.openstack4j.model.network.Port; |
| 32 | import org.openstack4j.openstack.networking.domain.NeutronPort; |
| 33 | |
| 34 | import java.util.Comparator; |
| 35 | import java.util.List; |
| 36 | import java.util.stream.Collectors; |
| 37 | |
| 38 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 39 | |
| 40 | /** |
| 41 | * Lists OpenStack ports. |
| 42 | */ |
| 43 | @Command(scope = "onos", name = "openstack-ports", |
| 44 | description = "Lists all OpenStack ports") |
| 45 | public class OpenstackPortListCommand extends AbstractShellCommand { |
| 46 | |
| 47 | private static final String FORMAT = "%-40s%-20s%-20s%-8s"; |
| 48 | |
| 49 | @Argument(name = "networkId", description = "Network ID") |
| 50 | private String networkId = null; |
| 51 | |
| 52 | @Override |
| 53 | protected void execute() { |
| 54 | OpenstackNetworkService service = AbstractShellCommand.get(OpenstackNetworkService.class); |
| 55 | |
| 56 | List<Port> ports = Lists.newArrayList(service.ports()); |
| 57 | ports.sort(Comparator.comparing(Port::getNetworkId)); |
| 58 | |
| 59 | if (!Strings.isNullOrEmpty(networkId)) { |
| 60 | ports.removeIf(port -> !port.getNetworkId().equals(networkId)); |
| 61 | } |
| 62 | |
| 63 | if (outputJson()) { |
| 64 | try { |
| 65 | print("%s", mapper().writeValueAsString(json(ports))); |
| 66 | } catch (JsonProcessingException e) { |
| 67 | print("Failed to list ports in JSON format"); |
| 68 | } |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | print(FORMAT, "ID", "Network", "MAC", "Fixed IPs"); |
| 73 | for (Port port: ports) { |
| 74 | List<String> fixedIps = port.getFixedIps().stream() |
| 75 | .map(IP::getIpAddress) |
| 76 | .collect(Collectors.toList()); |
| 77 | Network osNet = service.network(port.getNetworkId()); |
| 78 | print(FORMAT, port.getId(), |
| 79 | osNet.getName(), |
| 80 | port.getMacAddress(), |
| 81 | fixedIps.isEmpty() ? "" : fixedIps); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private JsonNode json(List<Port> ports) { |
| 86 | ArrayNode result = mapper().enable(INDENT_OUTPUT).createArrayNode(); |
| 87 | for (Port port: ports) { |
| 88 | result.add(writePort(port)); |
| 89 | } |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | private ObjectNode writePort(Port port) { |
| 94 | try { |
| 95 | String strPort = ObjectMapperSingleton.getContext(NeutronPort.class) |
| 96 | .writerFor(NeutronPort.class) |
| 97 | .writeValueAsString(port); |
| 98 | return (ObjectNode) mapper().readTree(strPort.getBytes()); |
| 99 | } catch (Exception e) { |
| 100 | throw new IllegalStateException(); |
| 101 | } |
| 102 | } |
| 103 | } |