blob: 63f48fe3f18ada8d0d7e92ea99e0d8eb12ac76db [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tom13cb4852014-09-11 12:44:17 -070019package org.onlab.onos.cli.net;
20
tom32085cf2014-10-16 00:04:33 -070021import com.fasterxml.jackson.databind.ObjectMapper;
tom13cb4852014-09-11 12:44:17 -070022import com.google.common.collect.Lists;
23import org.apache.karaf.shell.commands.Argument;
24import org.apache.karaf.shell.commands.Command;
tom91c7bd02014-09-25 22:50:44 -070025import org.onlab.onos.cli.Comparators;
tom13cb4852014-09-11 12:44:17 -070026import org.onlab.onos.net.DeviceId;
27import org.onlab.onos.net.topology.TopologyCluster;
28
29import java.util.Collections;
tom13cb4852014-09-11 12:44:17 -070030import java.util.List;
31
tom32085cf2014-10-16 00:04:33 -070032import static org.onlab.onos.cli.MastersListCommand.json;
tom13cb4852014-09-11 12:44:17 -070033import static org.onlab.onos.net.topology.ClusterId.clusterId;
34
35/**
36 * Lists devices of the specified topology cluster in the current topology.
37 */
38@Command(scope = "onos", name = "cluster-devices",
39 description = "Lists devices of the specified topology cluster in the current topology")
40public class ClusterDevicesCommand extends ClustersListCommand {
41
42 @Argument(index = 0, name = "id", description = "Cluster ID",
43 required = false, multiValued = false)
44 String id = null;
45
tom13cb4852014-09-11 12:44:17 -070046 @Override
tom0872a172014-09-23 11:24:26 -070047 protected void execute() {
tom13cb4852014-09-11 12:44:17 -070048 int cid = Integer.parseInt(id);
49 init();
50 TopologyCluster cluster = service.getCluster(topology, clusterId(cid));
tom9eb57fb2014-09-11 19:42:38 -070051 if (cluster == null) {
52 error("No such cluster %s", cid);
53 } else {
54 List<DeviceId> ids = Lists.newArrayList(service.getClusterDevices(topology, cluster));
tom1380eee2014-09-24 09:22:02 -070055 Collections.sort(ids, Comparators.ELEMENT_ID_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070056 if (outputJson()) {
57 print("%s", json(new ObjectMapper(), ids));
58 } else {
59 for (DeviceId deviceId : ids) {
60 print("%s", deviceId);
61 }
tom9eb57fb2014-09-11 19:42:38 -070062 }
tom13cb4852014-09-11 12:44:17 -070063 }
tom13cb4852014-09-11 12:44:17 -070064 }
65
tom13cb4852014-09-11 12:44:17 -070066}