blob: ed732c5939970db3a667dc6bdbd88a22fc05de1f [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 */
tom13cb4852014-09-11 12:44:17 -070016package org.onlab.onos.cli.net;
17
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;
tom13cb4852014-09-11 12:44:17 -070021import org.onlab.onos.cli.AbstractShellCommand;
22import org.onlab.onos.net.topology.Topology;
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070023import org.onlab.onos.net.topology.TopologyProvider;
tom13cb4852014-09-11 12:44:17 -070024import org.onlab.onos.net.topology.TopologyService;
25
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
tom613d8142014-09-11 15:09:37 -070033 // TODO: format the time-stamp
tom13cb4852014-09-11 12:44:17 -070034 private static final String FMT =
35 "time=%s, devices=%d, links=%d, clusters=%d, paths=%d";
36
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070037 @Option(name = "-r", aliases = "--recompute", description = "Trigger topology re-computation",
38 required = false, multiValued = false)
39 private boolean recompute = false;
40
tom13cb4852014-09-11 12:44:17 -070041 protected TopologyService service;
42 protected Topology topology;
43
44 /**
45 * Initializes the context for all cluster commands.
46 */
47 protected void init() {
tomcaf3bf72014-09-23 13:20:53 -070048 service = get(TopologyService.class);
tom13cb4852014-09-11 12:44:17 -070049 topology = service.currentTopology();
50 }
51
52 @Override
tom0872a172014-09-23 11:24:26 -070053 protected void execute() {
tom13cb4852014-09-11 12:44:17 -070054 init();
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070055 if (recompute) {
56 get(TopologyProvider.class).triggerRecompute();
57
58 } else if (outputJson()) {
tom32085cf2014-10-16 00:04:33 -070059 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 }
tom13cb4852014-09-11 12:44:17 -070069 }
70
71}