Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 17 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Lists; |
| 22 | import org.apache.karaf.shell.commands.Command; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 23 | import org.onosproject.utils.Comparators; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 24 | import org.onosproject.net.topology.TopologyCluster; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 25 | |
| 26 | import java.util.Collections; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 27 | import 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") |
| 34 | public class ClustersListCommand extends TopologyCommand { |
| 35 | |
| 36 | private static final String FMT = |
tom | d0344a8 | 2014-09-12 00:20:30 -0700 | [diff] [blame] | 37 | "id=%d, devices=%d, links=%d"; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 38 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 39 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 40 | protected void execute() { |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 41 | init(); |
| 42 | List<TopologyCluster> clusters = Lists.newArrayList(service.getClusters(topology)); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 43 | Collections.sort(clusters, Comparators.CLUSTER_COMPARATOR); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 44 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 45 | 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 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 51 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 52 | } |
| 53 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 54 | // Produces a JSON result. |
| 55 | private JsonNode json(Iterable<TopologyCluster> clusters) { |
| 56 | ObjectMapper mapper = new ObjectMapper(); |
| 57 | ArrayNode result = mapper.createArrayNode(); |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 58 | |
| 59 | clusters.spliterator() |
| 60 | .forEachRemaining(cluster -> |
| 61 | result.add(jsonForEntity(cluster, TopologyCluster.class))); |
| 62 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 63 | return result; |
| 64 | } |
| 65 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 66 | } |