blob: 55fac6f63aeb6075314c25be32cde61f47b6277b [file] [log] [blame]
Jian Li2c63bd22018-07-15 23:35:34 +09001/*
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 */
16package org.onosproject.openstacknetworking.cli;
17
Jian Li2c63bd22018-07-15 23:35:34 +090018import com.fasterxml.jackson.databind.ObjectMapper;
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.google.common.collect.Lists;
Ray Milkey86ad7bb2018-09-27 12:32:28 -070021import org.apache.karaf.shell.api.action.Command;
Ray Milkey7a2dee52018-09-28 10:58:28 -070022import org.apache.karaf.shell.api.action.lifecycle.Service;
Jian Li2c63bd22018-07-15 23:35:34 +090023import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.openstacknetworking.api.InstancePort;
25import org.onosproject.openstacknetworking.api.InstancePortService;
Daniel Parkc7102f32019-05-17 16:35:04 +090026import org.onosproject.openstacknetworking.api.OpenstackNetworkService;
27import org.openstack4j.model.network.Port;
Jian Li2c63bd22018-07-15 23:35:34 +090028
29import java.util.Comparator;
30import java.util.List;
31
Jian Lif1efbe52018-07-17 23:20:16 +090032import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson;
Jian Li2c63bd22018-07-15 23:35:34 +090033
34/**
35 * Lists OpenStack instance ports.
36 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070037@Service
Jian Li2c63bd22018-07-15 23:35:34 +090038@Command(scope = "onos", name = "openstack-instance-ports",
39 description = "Lists all OpenStack instance ports")
40public class InstancePortListCommand extends AbstractShellCommand {
41
Daniel Parkc7102f32019-05-17 16:35:04 +090042 private static final String FORMAT = "%-40s%-40s%-10s%-25s%-10s%-15s";
Jian Li2c63bd22018-07-15 23:35:34 +090043
44 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070045 protected void doExecute() {
Jian Li2c63bd22018-07-15 23:35:34 +090046 InstancePortService service = get(InstancePortService.class);
Daniel Parkc7102f32019-05-17 16:35:04 +090047 OpenstackNetworkService osNetService = get(OpenstackNetworkService.class);
Jian Li2c63bd22018-07-15 23:35:34 +090048 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 Parkc7102f32019-05-17 16:35:04 +090054 print(FORMAT, "Port ID", "VM Device ID", "State", "Device ID", "Port Number", "Fixed IP");
Jian Li2c63bd22018-07-15 23:35:34 +090055 for (InstancePort port : instancePorts) {
Daniel Parkc7102f32019-05-17 16:35:04 +090056 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 Li2c63bd22018-07-15 23:35:34 +090061 }
62 }
63 }
64
Jian Lif1efbe52018-07-17 23:20:16 +090065 private String json(AbstractShellCommand context, List<InstancePort> ports) {
Jian Li2c63bd22018-07-15 23:35:34 +090066 ObjectMapper mapper = new ObjectMapper();
Jian Lif1efbe52018-07-17 23:20:16 +090067 ArrayNode result = mapper.createArrayNode();
Jian Li2c63bd22018-07-15 23:35:34 +090068 ports.forEach(p -> result.add(context.jsonForEntity(p, InstancePort.class)));
69
Jian Lif1efbe52018-07-17 23:20:16 +090070 return prettyJson(mapper, result.toString());
Jian Li2c63bd22018-07-15 23:35:34 +090071 }
72}