blob: 66d10d17b0b08149433f15bc7b00b56a59f8580b [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.JsonNode;
22import com.fasterxml.jackson.databind.ObjectMapper;
23import com.fasterxml.jackson.databind.node.ArrayNode;
tom13cb4852014-09-11 12:44:17 -070024import com.google.common.collect.Lists;
25import org.apache.karaf.shell.commands.Command;
tom91c7bd02014-09-25 22:50:44 -070026import org.onlab.onos.cli.Comparators;
tom13cb4852014-09-11 12:44:17 -070027import org.onlab.onos.net.topology.TopologyCluster;
28
29import java.util.Collections;
tom13cb4852014-09-11 12:44:17 -070030import java.util.List;
31
32/**
33 * Lists all clusters in the current topology.
34 */
35@Command(scope = "onos", name = "clusters",
36 description = "Lists all clusters in the current topology")
37public class ClustersListCommand extends TopologyCommand {
38
39 private static final String FMT =
tomd0344a82014-09-12 00:20:30 -070040 "id=%d, devices=%d, links=%d";
tom13cb4852014-09-11 12:44:17 -070041
tom13cb4852014-09-11 12:44:17 -070042 @Override
tom0872a172014-09-23 11:24:26 -070043 protected void execute() {
tom13cb4852014-09-11 12:44:17 -070044 init();
45 List<TopologyCluster> clusters = Lists.newArrayList(service.getClusters(topology));
tom1380eee2014-09-24 09:22:02 -070046 Collections.sort(clusters, Comparators.CLUSTER_COMPARATOR);
tom13cb4852014-09-11 12:44:17 -070047
tom32085cf2014-10-16 00:04:33 -070048 if (outputJson()) {
49 print("%s", json(clusters));
50 } else {
51 for (TopologyCluster cluster : clusters) {
52 print(FMT, cluster.id().index(), cluster.deviceCount(), cluster.linkCount());
53 }
tom13cb4852014-09-11 12:44:17 -070054 }
tom13cb4852014-09-11 12:44:17 -070055 }
56
tom32085cf2014-10-16 00:04:33 -070057 // Produces a JSON result.
58 private JsonNode json(Iterable<TopologyCluster> clusters) {
59 ObjectMapper mapper = new ObjectMapper();
60 ArrayNode result = mapper.createArrayNode();
61 for (TopologyCluster cluster : clusters) {
62 result.add(mapper.createObjectNode()
63 .put("id", cluster.id().index())
64 .put("deviceCount", cluster.deviceCount())
65 .put("linkCount", cluster.linkCount()));
66 }
67 return result;
68 }
69
tom13cb4852014-09-11 12:44:17 -070070}