Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 17 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 19 | import com.google.common.collect.Lists; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.api.action.Argument; |
| 21 | import org.apache.karaf.shell.api.action.Command; |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Completion; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 24 | import org.onosproject.utils.Comparators; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.DeviceId; |
| 26 | import org.onosproject.net.topology.TopologyCluster; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 27 | |
| 28 | import java.util.Collections; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 29 | import java.util.List; |
| 30 | |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 31 | import static org.onosproject.cli.MastersListCommand.json; |
| 32 | import static org.onosproject.net.topology.ClusterId.clusterId; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Lists devices of the specified topology cluster in the current topology. |
| 36 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 37 | @Service |
Charles Chan | 3da0b1d | 2018-04-17 20:25:32 -0700 | [diff] [blame] | 38 | @Command(scope = "onos", name = "topo-cluster-devices", |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 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", |
Jon Hall | 8f31b7b | 2015-02-09 11:17:39 -0800 | [diff] [blame] | 43 | required = true, multiValued = false) |
Ray Milkey | 0068fd0 | 2018-10-11 15:45:39 -0700 | [diff] [blame] | 44 | @Completion(ClusterIdCompleter.class) |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 45 | String id = null; |
| 46 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 47 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 48 | protected void doExecute() { |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 49 | int cid = Integer.parseInt(id); |
| 50 | init(); |
| 51 | TopologyCluster cluster = service.getCluster(topology, clusterId(cid)); |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 52 | if (cluster == null) { |
| 53 | error("No such cluster %s", cid); |
| 54 | } else { |
| 55 | List<DeviceId> ids = Lists.newArrayList(service.getClusterDevices(topology, cluster)); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 56 | Collections.sort(ids, Comparators.ELEMENT_ID_COMPARATOR); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 57 | if (outputJson()) { |
| 58 | print("%s", json(new ObjectMapper(), ids)); |
| 59 | } else { |
| 60 | for (DeviceId deviceId : ids) { |
| 61 | print("%s", deviceId); |
| 62 | } |
tom | 9eb57fb | 2014-09-11 19:42:38 -0700 | [diff] [blame] | 63 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 64 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 65 | } |
| 66 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 67 | } |