blob: e8c901c32a8c2898b2c17ea8d80a2a3205280131 [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;
26
27import java.util.Comparator;
28import java.util.List;
29
Jian Lif1efbe52018-07-17 23:20:16 +090030import static org.onosproject.openstacknetworking.util.OpenstackNetworkingUtil.prettyJson;
Jian Li2c63bd22018-07-15 23:35:34 +090031
32/**
33 * Lists OpenStack instance ports.
34 */
Ray Milkey7a2dee52018-09-28 10:58:28 -070035@Service
Jian Li2c63bd22018-07-15 23:35:34 +090036@Command(scope = "onos", name = "openstack-instance-ports",
37 description = "Lists all OpenStack instance ports")
38public class InstancePortListCommand extends AbstractShellCommand {
39
40 private static final String FORMAT = "%-40s%-10s%-25s%-15s%-20s";
41
42 @Override
Ray Milkey86ad7bb2018-09-27 12:32:28 -070043 protected void doExecute() {
Jian Li2c63bd22018-07-15 23:35:34 +090044 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 Lif1efbe52018-07-17 23:20:16 +090059 private String json(AbstractShellCommand context, List<InstancePort> ports) {
Jian Li2c63bd22018-07-15 23:35:34 +090060 ObjectMapper mapper = new ObjectMapper();
Jian Lif1efbe52018-07-17 23:20:16 +090061 ArrayNode result = mapper.createArrayNode();
Jian Li2c63bd22018-07-15 23:35:34 +090062 ports.forEach(p -> result.add(context.jsonForEntity(p, InstancePort.class)));
63
Jian Lif1efbe52018-07-17 23:20:16 +090064 return prettyJson(mapper, result.toString());
Jian Li2c63bd22018-07-15 23:35:34 +090065 }
66}