blob: da7dc3ced393ec6d6e507075a1845e45d732ba19 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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;
Ray Milkeyc7477292016-03-11 10:53:43 -080023import org.onosproject.utils.Comparators;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import 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();
Ray Milkey3078fc02015-05-06 16:14:14 -070058
59 clusters.spliterator()
60 .forEachRemaining(cluster ->
61 result.add(jsonForEntity(cluster, TopologyCluster.class)));
62
tom32085cf2014-10-16 00:04:33 -070063 return result;
64 }
65
tom13cb4852014-09-11 12:44:17 -070066}