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