Jian Li | 2c63bd2 | 2018-07-15 23:35:34 +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.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.google.common.collect.Lists; |
| 22 | import org.apache.karaf.shell.commands.Command; |
| 23 | import org.onosproject.cli.AbstractShellCommand; |
| 24 | import org.onosproject.openstacknetworking.api.InstancePort; |
| 25 | import org.onosproject.openstacknetworking.api.InstancePortService; |
| 26 | |
| 27 | import java.util.Comparator; |
| 28 | import java.util.List; |
| 29 | |
| 30 | import static com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT; |
| 31 | |
| 32 | /** |
| 33 | * Lists OpenStack instance ports. |
| 34 | */ |
| 35 | @Command(scope = "onos", name = "openstack-instance-ports", |
| 36 | description = "Lists all OpenStack instance ports") |
| 37 | public class InstancePortListCommand extends AbstractShellCommand { |
| 38 | |
| 39 | private static final String FORMAT = "%-40s%-10s%-25s%-15s%-20s"; |
| 40 | |
| 41 | @Override |
| 42 | protected void execute() { |
| 43 | InstancePortService service = get(InstancePortService.class); |
| 44 | List<InstancePort> instancePorts = Lists.newArrayList(service.instancePorts()); |
| 45 | instancePorts.sort(Comparator.comparing(InstancePort::portId)); |
| 46 | |
| 47 | if (outputJson()) { |
| 48 | print("%s", json(this, instancePorts)); |
| 49 | } else { |
| 50 | print(FORMAT, "ID", "State", "Device ID", "Port Number", "Fixed IP"); |
| 51 | for (InstancePort port : instancePorts) { |
| 52 | print(FORMAT, port.portId(), port.state(), port.deviceId().toString(), |
| 53 | port.portNumber().toLong(), port.ipAddress().toString()); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private JsonNode json(AbstractShellCommand context, List<InstancePort> ports) { |
| 59 | ObjectMapper mapper = new ObjectMapper(); |
| 60 | ArrayNode result = mapper.enable(INDENT_OUTPUT).createArrayNode(); |
| 61 | ports.forEach(p -> result.add(context.jsonForEntity(p, InstancePort.class))); |
| 62 | |
| 63 | return result; |
| 64 | } |
| 65 | } |