blob: 903b352d7d9bc1827001b92ba95cf13622a37281 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
tom13cb4852014-09-11 12:44:17 -070019package org.onlab.onos.cli.net;
20
tom32085cf2014-10-16 00:04:33 -070021import com.fasterxml.jackson.databind.ObjectMapper;
tom13cb4852014-09-11 12:44:17 -070022import org.apache.karaf.shell.commands.Command;
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070023import org.apache.karaf.shell.commands.Option;
tom13cb4852014-09-11 12:44:17 -070024import org.onlab.onos.cli.AbstractShellCommand;
25import org.onlab.onos.net.topology.Topology;
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070026import org.onlab.onos.net.topology.TopologyProvider;
tom13cb4852014-09-11 12:44:17 -070027import org.onlab.onos.net.topology.TopologyService;
28
29/**
30 * Lists summary of the current topology.
31 */
32@Command(scope = "onos", name = "topology",
33 description = "Lists summary of the current topology")
34public class TopologyCommand extends AbstractShellCommand {
35
tom613d8142014-09-11 15:09:37 -070036 // TODO: format the time-stamp
tom13cb4852014-09-11 12:44:17 -070037 private static final String FMT =
38 "time=%s, devices=%d, links=%d, clusters=%d, paths=%d";
39
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070040 @Option(name = "-r", aliases = "--recompute", description = "Trigger topology re-computation",
41 required = false, multiValued = false)
42 private boolean recompute = false;
43
tom13cb4852014-09-11 12:44:17 -070044 protected TopologyService service;
45 protected Topology topology;
46
47 /**
48 * Initializes the context for all cluster commands.
49 */
50 protected void init() {
tomcaf3bf72014-09-23 13:20:53 -070051 service = get(TopologyService.class);
tom13cb4852014-09-11 12:44:17 -070052 topology = service.currentTopology();
53 }
54
55 @Override
tom0872a172014-09-23 11:24:26 -070056 protected void execute() {
tom13cb4852014-09-11 12:44:17 -070057 init();
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070058 if (recompute) {
59 get(TopologyProvider.class).triggerRecompute();
60
61 } else if (outputJson()) {
tom32085cf2014-10-16 00:04:33 -070062 print("%s", new ObjectMapper().createObjectNode()
63 .put("time", topology.time())
64 .put("deviceCount", topology.deviceCount())
65 .put("linkCount", topology.linkCount())
66 .put("clusterCount", topology.clusterCount())
67 .put("pathCount", topology.pathCount()));
68 } else {
69 print(FMT, topology.time(), topology.deviceCount(), topology.linkCount(),
70 topology.clusterCount(), topology.pathCount());
71 }
tom13cb4852014-09-11 12:44:17 -070072 }
73
74}