Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * 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 Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 17 | |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 18 | import java.text.DateFormat; |
| 19 | import java.text.SimpleDateFormat; |
| 20 | import java.util.Calendar; |
| 21 | import java.util.concurrent.TimeUnit; |
| 22 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Command; |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 24 | import org.apache.karaf.shell.commands.Option; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.cli.AbstractShellCommand; |
| 26 | import org.onosproject.net.topology.Topology; |
| 27 | import org.onosproject.net.topology.TopologyProvider; |
| 28 | import org.onosproject.net.topology.TopologyService; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Lists summary of the current topology. |
| 32 | */ |
| 33 | @Command(scope = "onos", name = "topology", |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 34 | description = "Lists summary of the current topology") |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 35 | public class TopologyCommand extends AbstractShellCommand { |
| 36 | |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 37 | private static final String FMT = "created=%s, uptime=%s, devices=%d, links=%d, clusters=%d"; |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 38 | |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 39 | @Option(name = "-r", aliases = "--recompute", |
| 40 | description = "Trigger topology re-computation", required = false, |
| 41 | multiValued = false) |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 42 | private boolean recompute = false; |
| 43 | |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 44 | protected TopologyService service; |
| 45 | protected Topology topology; |
| 46 | |
| 47 | /** |
| 48 | * Initializes the context for all cluster commands. |
| 49 | */ |
| 50 | protected void init() { |
tom | caf3bf7 | 2014-09-23 13:20:53 -0700 | [diff] [blame] | 51 | service = get(TopologyService.class); |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 52 | topology = service.currentTopology(); |
| 53 | } |
| 54 | |
| 55 | @Override |
tom | 0872a17 | 2014-09-23 11:24:26 -0700 | [diff] [blame] | 56 | protected void execute() { |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 57 | init(); |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 58 | long topologyUptime = |
| 59 | Math.max(0, (System.currentTimeMillis() - topology.creationTime())); |
Thomas Vachuska | 0e752bd | 2014-10-22 22:33:41 -0700 | [diff] [blame] | 60 | if (recompute) { |
| 61 | get(TopologyProvider.class).triggerRecompute(); |
| 62 | |
| 63 | } else if (outputJson()) { |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 64 | print("%s", |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 65 | jsonForEntity(topology, Topology.class)); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 66 | } else { |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 67 | print(FMT, formatCreationTime(topology.creationTime()), |
| 68 | formatElapsedTime(topologyUptime), |
| 69 | topology.deviceCount(), topology.linkCount(), |
| 70 | topology.clusterCount()); |
tom | 32085cf | 2014-10-16 00:04:33 -0700 | [diff] [blame] | 71 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Abhishek Dwaraki | 1e5873e | 2015-03-08 00:01:17 -0500 | [diff] [blame] | 74 | /** |
| 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 | } |
tom | 13cb485 | 2014-09-11 12:44:17 -0700 | [diff] [blame] | 122 | } |