blob: 369b27a72076ab41bedaf93719b0a042a5c32e41 [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
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.google.common.collect.Lists;
22import org.apache.karaf.shell.commands.Command;
23import org.onosproject.cli.AbstractShellCommand;
24import org.onosproject.openstacknetworking.api.InstancePort;
25import org.onosproject.openstacknetworking.api.InstancePortService;
26
27import java.util.Comparator;
28import java.util.List;
29
30import 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")
37public 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}