Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 19 | package org.onlab.onos.cli.net; |
| 20 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 21 | import com.fasterxml.jackson.databind.JsonNode; |
| 22 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 23 | import com.fasterxml.jackson.databind.node.ArrayNode; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 24 | import com.google.common.collect.Lists; |
| 25 | import org.apache.karaf.shell.commands.Command; |
tom | 91c7bd0 | 2014-09-25 22:50:44 -0700 | [diff] [blame] | 26 | import org.onlab.onos.cli.Comparators; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 27 | import org.onlab.onos.net.topology.TopologyCluster; |
| 28 | |
| 29 | import java.util.Collections; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 30 | import 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") |
| 37 | public class ClustersListCommand extends TopologyCommand { |
| 38 | |
| 39 | private static final String FMT = |
tom | d0344a8 | 2014-09-12 00:20:30 -0700 | [diff] [blame] | 40 | "id=%d, devices=%d, links=%d"; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 41 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 42 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 43 | protected void execute() { |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 44 | init(); |
| 45 | List<TopologyCluster> clusters = Lists.newArrayList(service.getClusters(topology)); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 46 | Collections.sort(clusters, Comparators.CLUSTER_COMPARATOR); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 47 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 48 | 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 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 54 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 55 | } |
| 56 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 57 | // 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 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 70 | } |