blob: ad1db1123d9f2a89220f832423817faa2b690f87 [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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070023import org.apache.karaf.shell.api.action.Command;
24import org.apache.karaf.shell.api.action.lifecycle.Service;
25import org.apache.karaf.shell.api.action.Option;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.cli.AbstractShellCommand;
27import org.onosproject.net.topology.Topology;
28import org.onosproject.net.topology.TopologyProvider;
29import org.onosproject.net.topology.TopologyService;
tom13cb4852014-09-11 12:44:17 -070030
31/**
32 * Lists summary of the current topology.
33 */
Ray Milkeyd84f89b2018-08-17 14:54:17 -070034@Service
tom13cb4852014-09-11 12:44:17 -070035@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
Ray Milkeyd84f89b2018-08-17 14:54:17 -070058 protected void doExecute() {
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",
Ray Milkey3078fc02015-05-06 16:14:14 -070067 jsonForEntity(topology, Topology.class));
tom32085cf2014-10-16 00:04:33 -070068 } else {
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050069 print(FMT, formatCreationTime(topology.creationTime()),
70 formatElapsedTime(topologyUptime),
71 topology.deviceCount(), topology.linkCount(),
72 topology.clusterCount());
tom32085cf2014-10-16 00:04:33 -070073 }
tom13cb4852014-09-11 12:44:17 -070074 }
75
Abhishek Dwaraki1e5873e2015-03-08 00:01:17 -050076 /**
77 * Converts millis to a formatted elapsed time string.
78 *
79 * @param millis Duration in millis to convert to a string
80 *
81 * @return Formatted string: "D days, H hrs, M mins, S secs".
82 */
83 private static String formatElapsedTime(long millis) {
84 if (millis < 0) {
85 throw new IllegalArgumentException("Interval less than zero. "
86 + "Possible unsynchronized timestamps");
87 }
88
89 final long days = TimeUnit.MILLISECONDS.toDays(millis);
90 millis -= TimeUnit.DAYS.toMillis(days);
91 final long hours = TimeUnit.MILLISECONDS.toHours(millis);
92 millis -= TimeUnit.HOURS.toMillis(hours);
93 final long minutes = TimeUnit.MILLISECONDS.toMinutes(millis);
94 millis -= TimeUnit.MINUTES.toMillis(minutes);
95 final long seconds = TimeUnit.MILLISECONDS.toSeconds(millis);
96
97 final StringBuilder topologyUptimeString = new StringBuilder(64);
98 topologyUptimeString.append(days);
99 topologyUptimeString.append(" days, ");
100 topologyUptimeString.append(hours);
101 topologyUptimeString.append(" hrs, ");
102 topologyUptimeString.append(minutes);
103 topologyUptimeString.append(" mins, ");
104 topologyUptimeString.append(seconds);
105 topologyUptimeString.append(" secs");
106
107 return (topologyUptimeString.toString());
108 }
109
110 /**
111 * Converts millis to a formatted Date String.
112 *
113 * @param millis Duration in millis to convert to a string
114 *
115 * @return Formatted string: yyyy-MM-dd HH:mm:ss.
116 */
117 private static String formatCreationTime(long millis) {
118 final DateFormat dateFormatter =
119 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
120 Calendar calendar = Calendar.getInstance();
121 calendar.setTimeInMillis(millis);
122 return (dateFormatter.format(calendar.getTime()));
123 }
tom13cb4852014-09-11 12:44:17 -0700124}