Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 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.ObjectMapper; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 19 | import org.apache.karaf.shell.commands.Command; |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 20 | import org.apache.karaf.shell.commands.Option; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 21 | import org.onosproject.cli.AbstractShellCommand; |
| 22 | import org.onosproject.net.topology.Topology; |
| 23 | import org.onosproject.net.topology.TopologyProvider; |
| 24 | import org.onosproject.net.topology.TopologyService; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 25 | |
| 26 | /** |
| 27 | * Lists summary of the current topology. |
| 28 | */ |
| 29 | @Command(scope = "onos", name = "topology", |
| 30 | description = "Lists summary of the current topology") |
| 31 | public class TopologyCommand extends AbstractShellCommand { |
| 32 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 33 | private static final String FMT = |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame^] | 34 | "time=%s, devices=%d, links=%d, clusters=%d"; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 35 | |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 36 | @Option(name = "-r", aliases = "--recompute", description = "Trigger topology re-computation", |
| 37 | required = false, multiValued = false) |
| 38 | private boolean recompute = false; |
| 39 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 40 | protected TopologyService service; |
| 41 | protected Topology topology; |
| 42 | |
| 43 | /** |
| 44 | * Initializes the context for all cluster commands. |
| 45 | */ |
| 46 | protected void init() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 47 | service = get(TopologyService.class); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 48 | topology = service.currentTopology(); |
| 49 | } |
| 50 | |
| 51 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 52 | protected void execute() { |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 53 | init(); |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 54 | if (recompute) { |
| 55 | get(TopologyProvider.class).triggerRecompute(); |
| 56 | |
| 57 | } else if (outputJson()) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 58 | print("%s", new ObjectMapper().createObjectNode() |
| 59 | .put("time", topology.time()) |
| 60 | .put("deviceCount", topology.deviceCount()) |
| 61 | .put("linkCount", topology.linkCount()) |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame^] | 62 | .put("clusterCount", topology.clusterCount())); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 63 | } else { |
| 64 | print(FMT, topology.time(), topology.deviceCount(), topology.linkCount(), |
Thomas Vachuska | c31d9f1 | 2015-01-22 12:33:27 -0800 | [diff] [blame^] | 65 | topology.clusterCount()); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 66 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | } |