blob: 3d5bbb8656fef75d9eb7e14900dacc4f658cc855 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska7d693f52014-10-21 19:17:57 -070015 */
tom13cb4852014-09-11 12:44:17 -070016package org.onlab.onos.cli.net;
17
tom32085cf2014-10-16 00:04:33 -070018import com.fasterxml.jackson.databind.ObjectMapper;
tom13cb4852014-09-11 12:44:17 -070019import com.google.common.collect.Lists;
20import org.apache.karaf.shell.commands.Argument;
21import org.apache.karaf.shell.commands.Command;
tom91c7bd02014-09-25 22:50:44 -070022import org.onlab.onos.cli.Comparators;
tom13cb4852014-09-11 12:44:17 -070023import org.onlab.onos.net.DeviceId;
24import org.onlab.onos.net.topology.TopologyCluster;
25
26import java.util.Collections;
tom13cb4852014-09-11 12:44:17 -070027import java.util.List;
28
tom32085cf2014-10-16 00:04:33 -070029import static org.onlab.onos.cli.MastersListCommand.json;
tom13cb4852014-09-11 12:44:17 -070030import static org.onlab.onos.net.topology.ClusterId.clusterId;
31
32/**
33 * Lists devices of the specified topology cluster in the current topology.
34 */
35@Command(scope = "onos", name = "cluster-devices",
36 description = "Lists devices of the specified topology cluster in the current topology")
37public class ClusterDevicesCommand extends ClustersListCommand {
38
39 @Argument(index = 0, name = "id", description = "Cluster ID",
40 required = false, multiValued = false)
41 String id = null;
42
tom13cb4852014-09-11 12:44:17 -070043 @Override
tom0872a172014-09-23 11:24:26 -070044 protected void execute() {
tom13cb4852014-09-11 12:44:17 -070045 int cid = Integer.parseInt(id);
46 init();
47 TopologyCluster cluster = service.getCluster(topology, clusterId(cid));
tom9eb57fb2014-09-11 19:42:38 -070048 if (cluster == null) {
49 error("No such cluster %s", cid);
50 } else {
51 List<DeviceId> ids = Lists.newArrayList(service.getClusterDevices(topology, cluster));
tom1380eee2014-09-24 09:22:02 -070052 Collections.sort(ids, Comparators.ELEMENT_ID_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070053 if (outputJson()) {
54 print("%s", json(new ObjectMapper(), ids));
55 } else {
56 for (DeviceId deviceId : ids) {
57 print("%s", deviceId);
58 }
tom9eb57fb2014-09-11 19:42:38 -070059 }
tom13cb4852014-09-11 12:44:17 -070060 }
tom13cb4852014-09-11 12:44:17 -070061 }
62
tom13cb4852014-09-11 12:44:17 -070063}