blob: 337a1f719e4204521bac1a3a6eebe84bccd4d85e [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska7d693f52014-10-21 19:17:57 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
tom13cb4852014-09-11 12:44:17 -070017
tom32085cf2014-10-16 00:04:33 -070018import com.fasterxml.jackson.databind.ObjectMapper;
tom13cb4852014-09-11 12:44:17 -070019import org.apache.karaf.shell.commands.Command;
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070020import org.apache.karaf.shell.commands.Option;
Brian O'Connorabafb502014-12-02 22:26:20 -080021import org.onosproject.cli.AbstractShellCommand;
22import org.onosproject.net.topology.Topology;
23import org.onosproject.net.topology.TopologyProvider;
24import org.onosproject.net.topology.TopologyService;
tom13cb4852014-09-11 12:44:17 -070025
26/**
27 * Lists summary of the current topology.
28 */
29@Command(scope = "onos", name = "topology",
30 description = "Lists summary of the current topology")
31public class TopologyCommand extends AbstractShellCommand {
32
tom13cb4852014-09-11 12:44:17 -070033 private static final String FMT =
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080034 "time=%s, devices=%d, links=%d, clusters=%d";
tom13cb4852014-09-11 12:44:17 -070035
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070036 @Option(name = "-r", aliases = "--recompute", description = "Trigger topology re-computation",
37 required = false, multiValued = false)
38 private boolean recompute = false;
39
tom13cb4852014-09-11 12:44:17 -070040 protected TopologyService service;
41 protected Topology topology;
42
43 /**
44 * Initializes the context for all cluster commands.
45 */
46 protected void init() {
tomcaf3bf72014-09-23 13:20:53 -070047 service = get(TopologyService.class);
tom13cb4852014-09-11 12:44:17 -070048 topology = service.currentTopology();
49 }
50
51 @Override
tom0872a172014-09-23 11:24:26 -070052 protected void execute() {
tom13cb4852014-09-11 12:44:17 -070053 init();
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070054 if (recompute) {
55 get(TopologyProvider.class).triggerRecompute();
56
57 } else if (outputJson()) {
tom32085cf2014-10-16 00:04:33 -070058 print("%s", new ObjectMapper().createObjectNode()
59 .put("time", topology.time())
60 .put("deviceCount", topology.deviceCount())
61 .put("linkCount", topology.linkCount())
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080062 .put("clusterCount", topology.clusterCount()));
tom32085cf2014-10-16 00:04:33 -070063 } else {
64 print(FMT, topology.time(), topology.deviceCount(), topology.linkCount(),
Thomas Vachuskac31d9f12015-01-22 12:33:27 -080065 topology.clusterCount());
tom32085cf2014-10-16 00:04:33 -070066 }
tom13cb4852014-09-11 12:44:17 -070067 }
68
69}