Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
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; |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.api.action.Command; |
| 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
Ray Milkey | c747729 | 2016-03-11 10:53:43 -0800 | [diff] [blame] | 24 | import org.onosproject.utils.Comparators; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.net.topology.TopologyCluster; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 26 | |
| 27 | import java.util.Collections; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 28 | import java.util.List; |
| 29 | |
| 30 | /** |
| 31 | * Lists all clusters in the current topology. |
| 32 | */ |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 33 | @Service |
Charles Chan | 3da0b1d | 2018-04-17 20:25:32 -0700 | [diff] [blame] | 34 | @Command(scope = "onos", name = "topo-clusters", |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 35 | description = "Lists all clusters in the current topology") |
| 36 | public class ClustersListCommand extends TopologyCommand { |
| 37 | |
| 38 | private static final String FMT = |
tom | d0344a8 | 2014-09-12 00:20:30 -0700 | [diff] [blame] | 39 | "id=%d, devices=%d, links=%d"; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 40 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 41 | @Override |
Ray Milkey | d84f89b | 2018-08-17 14:54:17 -0700 | [diff] [blame] | 42 | protected void doExecute() { |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 43 | init(); |
| 44 | List<TopologyCluster> clusters = Lists.newArrayList(service.getClusters(topology)); |
tom | 1380eee | 2014-09-24 09:22:02 -0700 | [diff] [blame] | 45 | Collections.sort(clusters, Comparators.CLUSTER_COMPARATOR); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 46 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 47 | 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 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 53 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 54 | } |
| 55 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 56 | // Produces a JSON result. |
| 57 | private JsonNode json(Iterable<TopologyCluster> clusters) { |
| 58 | ObjectMapper mapper = new ObjectMapper(); |
| 59 | ArrayNode result = mapper.createArrayNode(); |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 60 | |
| 61 | clusters.spliterator() |
| 62 | .forEachRemaining(cluster -> |
| 63 | result.add(jsonForEntity(cluster, TopologyCluster.class))); |
| 64 | |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 65 | return result; |
| 66 | } |
| 67 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 68 | } |