blob: 23804625eb4232ecf412f9924fecaea26ac82bc2 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
tom13cb4852014-09-11 12:44:17 -070017
tom32085cf2014-10-16 00:04:33 -070018import com.fasterxml.jackson.databind.ObjectMapper;
tom13cb4852014-09-11 12:44:17 -070019import com.google.common.collect.Lists;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070020import org.apache.karaf.shell.api.action.Argument;
21import org.apache.karaf.shell.api.action.Command;
Ray Milkey0068fd02018-10-11 15:45:39 -070022import org.apache.karaf.shell.api.action.Completion;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070023import org.apache.karaf.shell.api.action.lifecycle.Service;
Ray Milkeyc7477292016-03-11 10:53:43 -080024import org.onosproject.utils.Comparators;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.DeviceId;
26import org.onosproject.net.topology.TopologyCluster;
tom13cb4852014-09-11 12:44:17 -070027
28import java.util.Collections;
tom13cb4852014-09-11 12:44:17 -070029import java.util.List;
30
Brian O'Connorabafb502014-12-02 22:26:20 -080031import static org.onosproject.cli.MastersListCommand.json;
32import static org.onosproject.net.topology.ClusterId.clusterId;
tom13cb4852014-09-11 12:44:17 -070033
34/**
35 * Lists devices of the specified topology cluster in the current topology.
36 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070037@Service
Charles Chan3da0b1d2018-04-17 20:25:32 -070038@Command(scope = "onos", name = "topo-cluster-devices",
tom13cb4852014-09-11 12:44:17 -070039 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",
Jon Hall8f31b7b2015-02-09 11:17:39 -080043 required = true, multiValued = false)
Ray Milkey0068fd02018-10-11 15:45:39 -070044 @Completion(ClusterIdCompleter.class)
tom13cb4852014-09-11 12:44:17 -070045 String id = null;
46
tom13cb4852014-09-11 12:44:17 -070047 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070048 protected void doExecute() {
tom13cb4852014-09-11 12:44:17 -070049 int cid = Integer.parseInt(id);
50 init();
51 TopologyCluster cluster = service.getCluster(topology, clusterId(cid));
tom9eb57fb2014-09-11 19:42:38 -070052 if (cluster == null) {
53 error("No such cluster %s", cid);
54 } else {
55 List<DeviceId> ids = Lists.newArrayList(service.getClusterDevices(topology, cluster));
tom1380eee2014-09-24 09:22:02 -070056 Collections.sort(ids, Comparators.ELEMENT_ID_COMPARATOR);
tom32085cf2014-10-16 00:04:33 -070057 if (outputJson()) {
58 print("%s", json(new ObjectMapper(), ids));
59 } else {
60 for (DeviceId deviceId : ids) {
61 print("%s", deviceId);
62 }
tom9eb57fb2014-09-11 19:42:38 -070063 }
tom13cb4852014-09-11 12:44:17 -070064 }
tom13cb4852014-09-11 12:44:17 -070065 }
66
tom13cb4852014-09-11 12:44:17 -070067}