blob: 0b4b4b3fd0fd91b8d437c21deefcdafd498c74c3 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2014-present Open Networking Foundation
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
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050018import java.text.DateFormat;
19import java.text.SimpleDateFormat;
20import java.util.Calendar;
21import java.util.concurrent.TimeUnit;
22
tom13cb4852014-09-11 12:44:17 -070023import org.apache.karaf.shell.commands.Command;
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070024import org.apache.karaf.shell.commands.Option;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cli.AbstractShellCommand;
26import org.onosproject.net.topology.Topology;
27import org.onosproject.net.topology.TopologyProvider;
28import org.onosproject.net.topology.TopologyService;
tom13cb4852014-09-11 12:44:17 -070029
30/**
31 * Lists summary of the current topology.
32 */
33@Command(scope = "onos", name = "topology",
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050034description = "Lists summary of the current topology")
tom13cb4852014-09-11 12:44:17 -070035public class TopologyCommand extends AbstractShellCommand {
36
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050037 private static final String FMT = "created=%s, uptime=%s, devices=%d, links=%d, clusters=%d";
tom13cb4852014-09-11 12:44:17 -070038
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050039 @Option(name = "-r", aliases = "--recompute",
40 description = "Trigger topology re-computation", required = false,
41 multiValued = false)
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070042 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();
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050058 long topologyUptime =
59 Math.max(0, (System.currentTimeMillis() - topology.creationTime()));
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070060 if (recompute) {
61 get(TopologyProvider.class).triggerRecompute();
62
63 } else if (outputJson()) {
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050064 print("%s",
Ray Milkey3078fc02015-05-06 16:14:14 -070065 jsonForEntity(topology, Topology.class));
tom32085cf2014-10-16 00:04:33 -070066 } else {
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050067 print(FMT, formatCreationTime(topology.creationTime()),
68 formatElapsedTime(topologyUptime),
69 topology.deviceCount(), topology.linkCount(),
70 topology.clusterCount());
tom32085cf2014-10-16 00:04:33 -070071 }
tom13cb4852014-09-11 12:44:17 -070072 }
73
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050074 /**
75 * Converts millis to a formatted elapsed time string.
76 *
77 * @param millis Duration in millis to convert to a string
78 *
79 * @return Formatted string: "D days, H hrs, M mins, S secs".
80 */
81 private static String formatElapsedTime(long millis) {
82 if (millis < 0) {
83 throw new IllegalArgumentException("Interval less than zero. "
84 + "Possible unsynchronized timestamps");
85 }
86
87 final long days = TimeUnit.MILLISECONDS.toDays(millis);
88 millis -= TimeUnit.DAYS.toMillis(days);
89 final long hours = TimeUnit.MILLISECONDS.toHours(millis);
90 millis -= TimeUnit.HOURS.toMillis(hours);
91 final long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
92 millis -= TimeUnit.MINUTES.toMillis(minutes);
93 final long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
94
95 final StringBuilder topologyUptimeString = new StringBuilder(64);
96 topologyUptimeString.append(days);
97 topologyUptimeString.append(" days, ");
98 topologyUptimeString.append(hours);
99 topologyUptimeString.append(" hrs, ");
100 topologyUptimeString.append(minutes);
101 topologyUptimeString.append(" mins, ");
102 topologyUptimeString.append(seconds);
103 topologyUptimeString.append(" secs");
104
105 return (topologyUptimeString.toString());
106 }
107
108 /**
109 * Converts millis to a formatted Date String.
110 *
111 * @param millis Duration in millis to convert to a string
112 *
113 * @return Formatted string: yyyy-MM-dd HH:mm:ss.
114 */
115 private static String formatCreationTime(long millis) {
116 final DateFormat dateFormatter =
117 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
118 Calendar calendar = Calendar.getInstance();
119 calendar.setTimeInMillis(millis);
120 return (dateFormatter.format(calendar.getTime()));
121 }
tom13cb4852014-09-11 12:44:17 -0700122}