blob: 0c2d2f44144c303a858bc9efb31e1068ac4e6ced [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 */
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.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
tom13cb4852014-09-11 12:44:17 -070021import com.google.common.collect.Lists;
22import org.apache.karaf.shell.commands.Command;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.cli.Comparators;
24import org.onosproject.net.topology.TopologyCluster;
tom13cb4852014-09-11 12:44:17 -070025
26import java.util.Collections;
tom13cb4852014-09-11 12:44:17 -070027import java.util.List;
28
29/**
30 * Lists all clusters in the current topology.
31 */
32@Command(scope = "onos", name = "clusters",
33 description = "Lists all clusters in the current topology")
34public class ClustersListCommand extends TopologyCommand {
35
36 private static final String FMT =
tomd0344a82014-09-12 00:20:30 -070037 "id=%d, devices=%d, links=%d";
tom13cb4852014-09-11 12:44:17 -070038
tom13cb4852014-09-11 12:44:17 -070039 @Override
tom0872a172014-09-23 11:24:26 -070040 protected void execute() {
tom13cb4852014-09-11 12:44:17 -070041 init();
42 List<TopologyCluster> clusters = Lists.newArrayList(service.getClusters(topology));
tom1380eee2014-09-24 09:22:02 -070043 Collections.sort(clusters, Comparators.CLUSTER_COMPARATOR);
tom13cb4852014-09-11 12:44:17 -070044
tom32085cf2014-10-16 00:04:33 -070045 if (outputJson()) {
46 print("%s", json(clusters));
47 } else {
48 for (TopologyCluster cluster : clusters) {
49 print(FMT, cluster.id().index(), cluster.deviceCount(), cluster.linkCount());
50 }
tom13cb4852014-09-11 12:44:17 -070051 }
tom13cb4852014-09-11 12:44:17 -070052 }
53
tom32085cf2014-10-16 00:04:33 -070054 // Produces a JSON result.
55 private JsonNode json(Iterable<TopologyCluster> clusters) {
56 ObjectMapper mapper = new ObjectMapper();
57 ArrayNode result = mapper.createArrayNode();
58 for (TopologyCluster cluster : clusters) {
59 result.add(mapper.createObjectNode()
60 .put("id", cluster.id().index())
61 .put("deviceCount", cluster.deviceCount())
62 .put("linkCount", cluster.linkCount()));
63 }
64 return result;
65 }
66
tom13cb4852014-09-11 12:44:17 -070067}