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