blob: 09682a249d9c955c8fb0d56d93d54df350fad6b8 [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
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
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050030import com.fasterxml.jackson.databind.ObjectMapper;
31
tom13cb4852014-09-11 12:44:17 -070032/**
33 * Lists summary of the current topology.
34 */
35@Command(scope = "onos", name = "topology",
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050036description = "Lists summary of the current topology")
tom13cb4852014-09-11 12:44:17 -070037public class TopologyCommand extends AbstractShellCommand {
38
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050039 private static final String FMT = "created=%s, uptime=%s, devices=%d, links=%d, clusters=%d";
tom13cb4852014-09-11 12:44:17 -070040
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050041 @Option(name = "-r", aliases = "--recompute",
42 description = "Trigger topology re-computation", required = false,
43 multiValued = false)
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070044 private boolean recompute = false;
45
tom13cb4852014-09-11 12:44:17 -070046 protected TopologyService service;
47 protected Topology topology;
48
49 /**
50 * Initializes the context for all cluster commands.
51 */
52 protected void init() {
tomcaf3bf72014-09-23 13:20:53 -070053 service = get(TopologyService.class);
tom13cb4852014-09-11 12:44:17 -070054 topology = service.currentTopology();
55 }
56
57 @Override
tom0872a172014-09-23 11:24:26 -070058 protected void execute() {
tom13cb4852014-09-11 12:44:17 -070059 init();
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050060 long topologyUptime =
61 Math.max(0, (System.currentTimeMillis() - topology.creationTime()));
Thomas Vachuska0e752bd2014-10-22 22:33:41 -070062 if (recompute) {
63 get(TopologyProvider.class).triggerRecompute();
64
65 } else if (outputJson()) {
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050066 print("%s",
67 new ObjectMapper()
68 .createObjectNode()
69 .put("time", topology.time())
70 .put("created", formatCreationTime(topology.creationTime()))
71 .put("uptime", formatElapsedTime(topologyUptime))
72 .put("deviceCount", topology.deviceCount())
73 .put("linkCount", topology.linkCount())
74 .put("clusterCount", topology.clusterCount()));
tom32085cf2014-10-16 00:04:33 -070075 } else {
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050076 print(FMT, formatCreationTime(topology.creationTime()),
77 formatElapsedTime(topologyUptime),
78 topology.deviceCount(), topology.linkCount(),
79 topology.clusterCount());
tom32085cf2014-10-16 00:04:33 -070080 }
tom13cb4852014-09-11 12:44:17 -070081 }
82
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050083 /**
84 * Converts millis to a formatted elapsed time string.
85 *
86 * @param millis Duration in millis to convert to a string
87 *
88 * @return Formatted string: "D days, H hrs, M mins, S secs".
89 */
90 private static String formatElapsedTime(long millis) {
91 if (millis < 0) {
92 throw new IllegalArgumentException("Interval less than zero. "
93 + "Possible unsynchronized timestamps");
94 }
95
96 final long days = TimeUnit.MILLISECONDS.toDays(millis);
97 millis -= TimeUnit.DAYS.toMillis(days);
98 final long hours = TimeUnit.MILLISECONDS.toHours(millis);
99 millis -= TimeUnit.HOURS.toMillis(hours);
100 final long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
101 millis -= TimeUnit.MINUTES.toMillis(minutes);
102 final long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
103
104 final StringBuilder topologyUptimeString = new StringBuilder(64);
105 topologyUptimeString.append(days);
106 topologyUptimeString.append(" days, ");
107 topologyUptimeString.append(hours);
108 topologyUptimeString.append(" hrs, ");
109 topologyUptimeString.append(minutes);
110 topologyUptimeString.append(" mins, ");
111 topologyUptimeString.append(seconds);
112 topologyUptimeString.append(" secs");
113
114 return (topologyUptimeString.toString());
115 }
116
117 /**
118 * Converts millis to a formatted Date String.
119 *
120 * @param millis Duration in millis to convert to a string
121 *
122 * @return Formatted string: yyyy-MM-dd HH:mm:ss.
123 */
124 private static String formatCreationTime(long millis) {
125 final DateFormat dateFormatter =
126 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
127 Calendar calendar = Calendar.getInstance();
128 calendar.setTimeInMillis(millis);
129 return (dateFormatter.format(calendar.getTime()));
130 }
tom13cb4852014-09-11 12:44:17 -0700131}