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