Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 19 | package org.onlab.onos.cli.net; |
| 20 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 22 | import com.google.common.collect.Lists; |
| 23 | import org.apache.karaf.shell.commands.Argument; |
| 24 | import org.apache.karaf.shell.commands.Command; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 25 | import org.onlab.onos.cli.Comparators; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 26 | import org.onlab.onos.net.DeviceId; |
| 27 | import org.onlab.onos.net.topology.TopologyCluster; |
| 28 | |
| 29 | import java.util.Collections; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 30 | import java.util.List; |
| 31 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 32 | import static org.onlab.onos.cli.MastersListCommand.json; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 33 | import 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") |
| 40 | public class ClusterDevicesCommand extends ClustersListCommand { |
| 41 | |
| 42 | @Argument(index = 0, name = "id", description = "Cluster ID", |
| 43 | required = false, multiValued = false) |
| 44 | String id = null; |
| 45 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 46 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 47 | protected void execute() { |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 48 | int cid = Integer.parseInt(id); |
| 49 | init(); |
| 50 | TopologyCluster cluster = service.getCluster(topology, clusterId(cid)); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 51 | if (cluster == null) { |
| 52 | error("No such cluster %s", cid); |
| 53 | } else { |
| 54 | List<DeviceId> ids = Lists.newArrayList(service.getClusterDevices(topology, cluster)); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 55 | Collections.sort(ids, Comparators.ELEMENT_ID_COMPARATOR); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 56 | if (outputJson()) { |
| 57 | print("%s", json(new ObjectMapper(), ids)); |
| 58 | } else { |
| 59 | for (DeviceId deviceId : ids) { |
| 60 | print("%s", deviceId); |
| 61 | } |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 62 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 63 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 64 | } |
| 65 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 66 | } |