blob: b4b5540cbcb434c7405d40b5fa646f19419f8ab5 [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.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;
Ray Milkeyd84f89b2018-08-17 14:54:17 -070022import org.apache.karaf.shell.api.action.Command;
23import 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.topology.TopologyCluster;
tom13cb4852014-09-11 12:44:17 -070026
27import java.util.Collections;
tom13cb4852014-09-11 12:44:17 -070028import java.util.List;
29
30/**
31 * Lists all clusters in the current topology.
32 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070033@Service
Charles Chan3da0b1d2018-04-17 20:25:32 -070034@Command(scope = "onos", name = "topo-clusters",
tom13cb4852014-09-11 12:44:17 -070035 description = "Lists all clusters in the current topology")
36public class ClustersListCommand extends TopologyCommand {
37
38 private static final String FMT =
tomd0344a82014-09-12 00:20:30 -070039 "id=%d, devices=%d, links=%d";
tom13cb4852014-09-11 12:44:17 -070040
tom13cb4852014-09-11 12:44:17 -070041 @Override
Ray Milkeyd84f89b2018-08-17 14:54:17 -070042 protected void doExecute() {
tom13cb4852014-09-11 12:44:17 -070043 init();
44 List<TopologyCluster> clusters = Lists.newArrayList(service.getClusters(topology));
tom1380eee2014-09-24 09:22:02 -070045 Collections.sort(clusters, Comparators.CLUSTER_COMPARATOR);
tom13cb4852014-09-11 12:44:17 -070046
tom32085cf2014-10-16 00:04:33 -070047 if (outputJson()) {
48 print("%s", json(clusters));
49 } else {
50 for (TopologyCluster cluster : clusters) {
51 print(FMT, cluster.id().index(), cluster.deviceCount(), cluster.linkCount());
52 }
tom13cb4852014-09-11 12:44:17 -070053 }
tom13cb4852014-09-11 12:44:17 -070054 }
55
tom32085cf2014-10-16 00:04:33 -070056 // Produces a JSON result.
57 private JsonNode json(Iterable<TopologyCluster> clusters) {
58 ObjectMapper mapper = new ObjectMapper();
59 ArrayNode result = mapper.createArrayNode();
Ray Milkey3078fc02015-05-06 16:14:14 -070060
61 clusters.spliterator()
62 .forEachRemaining(cluster ->
63 result.add(jsonForEntity(cluster, TopologyCluster.class)));
64
tom32085cf2014-10-16 00:04:33 -070065 return result;
66 }
67
tom13cb4852014-09-11 12:44:17 -070068}