blob: 8b0d1113cb1d095a996f2de2f70f2b9b97f10120 [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;
21import org.apache.karaf.shell.commands.Command;
22import org.onosproject.cli.AbstractShellCommand;
23import org.onosproject.openstacknetworking.api.InstancePort;
24import org.onosproject.openstacknetworking.api.InstancePortService;
25
26import java.util.Comparator;
27import java.util.List;
28
Jian Lif1efbe52018-07-17 23:20:16 +090029import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson;
Jian Li2c63bd22018-07-15 23:35:34 +090030
31/**
32 * Lists OpenStack instance ports.
33 */
34@Command(scope = "onos", name = "openstack-instance-ports",
35 description = "Lists all OpenStack instance ports")
36public class InstancePortListCommand extends AbstractShellCommand {
37
38 private static final String FORMAT = "%-40s%-10s%-25s%-15s%-20s";
39
40 @Override
41 protected void execute() {
42 InstancePortService service = get(InstancePortService.class);
43 List<InstancePort> instancePorts = Lists.newArrayList(service.instancePorts());
44 instancePorts.sort(Comparator.comparing(InstancePort::portId));
45
46 if (outputJson()) {
47 print("%s", json(this, instancePorts));
48 } else {
49 print(FORMAT, "ID", "State", "Device ID", "Port Number", "Fixed IP");
50 for (InstancePort port : instancePorts) {
51 print(FORMAT, port.portId(), port.state(), port.deviceId().toString(),
52 port.portNumber().toLong(), port.ipAddress().toString());
53 }
54 }
55 }
56
Jian Lif1efbe52018-07-17 23:20:16 +090057 private String json(AbstractShellCommand context, List<InstancePort> ports) {
Jian Li2c63bd22018-07-15 23:35:34 +090058 ObjectMapper mapper = new ObjectMapper();
Jian Lif1efbe52018-07-17 23:20:16 +090059 ArrayNode result = mapper.createArrayNode();
Jian Li2c63bd22018-07-15 23:35:34 +090060 ports.forEach(p -> result.add(context.jsonForEntity(p, InstancePort.class)));
61
Jian Lif1efbe52018-07-17 23:20:16 +090062 return prettyJson(mapper, result.toString());
Jian Li2c63bd22018-07-15 23:35:34 +090063 }
64}