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 | 613d814 | 2014-09-11 15:09:37 -0700 | [diff] [blame] | 33 | // TODO: format the time-stamp |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 34 | private static final String FMT = |
| 35 | "time=%s, devices=%d, links=%d, clusters=%d, paths=%d"; |
| 36 | |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 37 | @Option(name = "-r", aliases = "--recompute", description = "Trigger topology re-computation", |
| 38 | required = false, multiValued = false) |
| 39 | private boolean recompute = false; |
| 40 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 41 | protected TopologyService service; |
| 42 | protected Topology topology; |
| 43 | |
| 44 | /** |
| 45 | * Initializes the context for all cluster commands. |
| 46 | */ |
| 47 | protected void init() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 48 | service = get(TopologyService.class); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 49 | topology = service.currentTopology(); |
| 50 | } |
| 51 | |
| 52 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 53 | protected void execute() { |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 54 | init(); |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 55 | if (recompute) { |
| 56 | get(TopologyProvider.class).triggerRecompute(); |
| 57 | |
| 58 | } else if (outputJson()) { |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 59 | print("%s", new ObjectMapper().createObjectNode() |
| 60 | .put("time", topology.time()) |
| 61 | .put("deviceCount", topology.deviceCount()) |
| 62 | .put("linkCount", topology.linkCount()) |
| 63 | .put("clusterCount", topology.clusterCount()) |
| 64 | .put("pathCount", topology.pathCount())); |
| 65 | } else { |
| 66 | print(FMT, topology.time(), topology.deviceCount(), topology.linkCount(), |
| 67 | topology.clusterCount(), topology.pathCount()); |
| 68 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | } |