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 | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 17 | |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 18 | import java.util.ArrayList; |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 19 | import java.util.List; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 20 | import java.util.stream.StreamSupport; |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 21 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 22 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 23 | import org.apache.karaf.shell.commands.Option; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 24 | import org.onlab.util.StringFilter; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 25 | import org.onosproject.cli.AbstractShellCommand; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 26 | import org.onosproject.net.intent.ConnectivityIntent; |
Pavlin Radoslavov | aab51a3 | 2014-12-08 11:07:38 -0800 | [diff] [blame] | 27 | import org.onosproject.net.intent.HostToHostIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 28 | import org.onosproject.net.intent.Intent; |
| 29 | import org.onosproject.net.intent.IntentService; |
| 30 | import org.onosproject.net.intent.IntentState; |
| 31 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 32 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 33 | import org.onosproject.net.intent.OpticalCircuitIntent; |
| 34 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 35 | import org.onosproject.net.intent.OpticalOduIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.PathIntent; |
| 37 | import org.onosproject.net.intent.PointToPointIntent; |
| 38 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 39 | |
Ray Milkey | 3078fc0 | 2015-05-06 16:14:14 -0700 | [diff] [blame] | 40 | import com.fasterxml.jackson.databind.JsonNode; |
| 41 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 42 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 43 | import com.fasterxml.jackson.databind.node.ObjectNode; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Lists the inventory of intents and their states. |
| 47 | */ |
| 48 | @Command(scope = "onos", name = "intents", |
| 49 | description = "Lists the inventory of intents and their states") |
| 50 | public class IntentsListCommand extends AbstractShellCommand { |
| 51 | |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 52 | @Option(name = "-i", aliases = "--installable", |
| 53 | description = "Output Installable Intents", |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 54 | required = false, multiValued = false) |
| 55 | private boolean showInstallable = false; |
| 56 | |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 57 | @Option(name = "-s", aliases = "--summary", |
| 58 | description = "Intents summary", |
| 59 | required = false, multiValued = false) |
| 60 | private boolean intentsSummary = false; |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 61 | |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 62 | @Option(name = "-p", aliases = "--pending", |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 63 | description = "Show information about pending intents", |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 64 | required = false, multiValued = false) |
| 65 | private boolean pending = false; |
| 66 | |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 67 | @Option(name = "-f", aliases = "--filter", |
| 68 | description = "Filter intents by specific key", |
| 69 | required = false, multiValued = true) |
| 70 | private List<String> filter = new ArrayList<>(); |
| 71 | |
| 72 | private StringFilter contentFilter; |
| 73 | |
| 74 | private String sep = System.lineSeparator(); |
| 75 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 76 | @Override |
| 77 | protected void execute() { |
| 78 | IntentService service = get(IntentService.class); |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 79 | contentFilter = new StringFilter(filter, StringFilter.Strategy.AND); |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 80 | |
| 81 | if (intentsSummary) { |
| 82 | IntentSummaries intentSummaries = new IntentSummaries(); |
| 83 | intentSummaries.collectIntentSummary(service, |
| 84 | service.getIntents()); |
| 85 | if (outputJson()) { |
| 86 | print("%s", intentSummaries.json()); |
| 87 | } else { |
| 88 | intentSummaries.printSummary(); |
| 89 | } |
| 90 | return; |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 91 | } else if (pending) { |
Ray Milkey | 740c8a3 | 2015-03-17 13:41:03 -0700 | [diff] [blame] | 92 | if (outputJson()) { |
| 93 | print("%s", json(service, service.getPending())); |
| 94 | } else { |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 95 | StreamSupport.stream(service.getPending().spliterator(), false) |
| 96 | .filter(intent -> contentFilter.filter(intent)) |
| 97 | .forEach(intent -> print(fullFormat(intent))); |
Ray Milkey | 740c8a3 | 2015-03-17 13:41:03 -0700 | [diff] [blame] | 98 | } |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 99 | return; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 100 | } |
| 101 | |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 102 | if (outputJson()) { |
| 103 | print("%s", json(service, service.getIntents())); |
| 104 | } else { |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 105 | printIntents(service); |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 109 | /** |
| 110 | * Internal local class to keep track of all intent summaries. |
| 111 | */ |
| 112 | private class IntentSummaries { |
| 113 | private IntentSummary summaryAll; |
| 114 | private IntentSummary summaryConnectivity; |
Pavlin Radoslavov | aab51a3 | 2014-12-08 11:07:38 -0800 | [diff] [blame] | 115 | private IntentSummary summaryHostToHost; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 116 | private IntentSummary summaryPointToPoint; |
| 117 | private IntentSummary summaryMultiPointToSinglePoint; |
| 118 | private IntentSummary summarySinglePointToMultiPoint; |
| 119 | private IntentSummary summaryPath; |
| 120 | private IntentSummary summaryLinkCollection; |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 121 | private IntentSummary summaryOpticalCircuit; |
| 122 | private IntentSummary summaryOpticalConnectivity; |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 123 | private IntentSummary summaryOpticalOdu; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 124 | private IntentSummary summaryUnknownType; |
| 125 | |
| 126 | /** |
| 127 | * Initializes the internal state. |
| 128 | */ |
| 129 | private void init() { |
| 130 | summaryAll = new IntentSummary("All"); |
| 131 | summaryConnectivity = new IntentSummary("Connectivity"); |
Pavlin Radoslavov | aab51a3 | 2014-12-08 11:07:38 -0800 | [diff] [blame] | 132 | summaryHostToHost = new IntentSummary("HostToHost"); |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 133 | summaryPointToPoint = new IntentSummary("PointToPoint"); |
| 134 | summaryMultiPointToSinglePoint = |
| 135 | new IntentSummary("MultiPointToSinglePoint"); |
| 136 | summarySinglePointToMultiPoint = |
| 137 | new IntentSummary("SinglePointToMultiPoint"); |
| 138 | summaryPath = new IntentSummary("Path"); |
| 139 | summaryLinkCollection = new IntentSummary("LinkCollection"); |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 140 | summaryOpticalCircuit = new IntentSummary("OpticalCircuit"); |
| 141 | summaryOpticalConnectivity = new IntentSummary("OpticalConnectivity"); |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 142 | summaryOpticalOdu = new IntentSummary("OpticalOdu"); |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 143 | summaryUnknownType = new IntentSummary("UnknownType"); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Collects summary of all intents. |
| 148 | * |
| 149 | * @param service the Intent Service to use |
| 150 | * @param intents the intents |
| 151 | */ |
| 152 | private void collectIntentSummary(IntentService service, |
| 153 | Iterable<Intent> intents) { |
| 154 | init(); |
| 155 | |
| 156 | // Collect the summary for each intent type intents |
| 157 | for (Intent intent : intents) { |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 158 | IntentState intentState = service.getIntentState(intent.key()); |
Pavlin Radoslavov | deb8a10 | 2014-11-26 13:31:36 -0800 | [diff] [blame] | 159 | if (intentState == null) { |
| 160 | continue; |
| 161 | } |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 162 | if (!contentFilter.filter(intent)) { |
| 163 | break; |
| 164 | } |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 165 | |
| 166 | // Update the summary for all Intents |
| 167 | summaryAll.update(intentState); |
| 168 | |
| 169 | if (intent instanceof ConnectivityIntent) { |
| 170 | summaryConnectivity.update(intentState); |
| 171 | // NOTE: ConnectivityIntent is a base type Intent |
| 172 | // continue; |
| 173 | } |
Pavlin Radoslavov | aab51a3 | 2014-12-08 11:07:38 -0800 | [diff] [blame] | 174 | if (intent instanceof HostToHostIntent) { |
| 175 | summaryHostToHost.update(intentState); |
| 176 | continue; |
| 177 | } |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 178 | if (intent instanceof PointToPointIntent) { |
| 179 | summaryPointToPoint.update(intentState); |
| 180 | continue; |
| 181 | } |
| 182 | if (intent instanceof MultiPointToSinglePointIntent) { |
| 183 | summaryMultiPointToSinglePoint.update(intentState); |
| 184 | continue; |
| 185 | } |
| 186 | if (intent instanceof SinglePointToMultiPointIntent) { |
| 187 | summarySinglePointToMultiPoint.update(intentState); |
| 188 | continue; |
| 189 | } |
| 190 | if (intent instanceof PathIntent) { |
| 191 | summaryPath.update(intentState); |
| 192 | continue; |
| 193 | } |
| 194 | if (intent instanceof LinkCollectionIntent) { |
| 195 | summaryLinkCollection.update(intentState); |
| 196 | continue; |
| 197 | } |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 198 | if (intent instanceof OpticalCircuitIntent) { |
| 199 | summaryOpticalCircuit.update(intentState); |
| 200 | continue; |
| 201 | } |
| 202 | if (intent instanceof OpticalConnectivityIntent) { |
| 203 | summaryOpticalConnectivity.update(intentState); |
| 204 | continue; |
| 205 | } |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 206 | if (intent instanceof OpticalOduIntent) { |
| 207 | summaryOpticalOdu.update(intentState); |
| 208 | continue; |
| 209 | } |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 210 | summaryUnknownType.update(intentState); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Gets JSON representation of all Intents summary. |
| 216 | * |
| 217 | * @return JSON representation of all Intents summary |
| 218 | */ |
| 219 | ObjectNode json() { |
| 220 | ObjectMapper mapper = new ObjectMapper(); |
| 221 | ObjectNode result = mapper.createObjectNode(); |
Ray Milkey | 9d810f6 | 2015-02-13 11:20:58 -0800 | [diff] [blame] | 222 | result.set("connectivity", summaryConnectivity.json(mapper)); |
| 223 | result.set("hostToHost", summaryHostToHost.json(mapper)); |
| 224 | result.set("pointToPoint", summaryPointToPoint.json(mapper)); |
| 225 | result.set("multiPointToSinglePoint", |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 226 | summaryMultiPointToSinglePoint.json(mapper)); |
Ray Milkey | 9d810f6 | 2015-02-13 11:20:58 -0800 | [diff] [blame] | 227 | result.set("singlePointToMultiPoint", |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 228 | summarySinglePointToMultiPoint.json(mapper)); |
Ray Milkey | 9d810f6 | 2015-02-13 11:20:58 -0800 | [diff] [blame] | 229 | result.set("path", summaryPath.json(mapper)); |
| 230 | result.set("linkCollection", summaryLinkCollection.json(mapper)); |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 231 | result.set("opticalCircuit", summaryOpticalCircuit.json(mapper)); |
| 232 | result.set("opticalConnectivity", summaryOpticalConnectivity.json(mapper)); |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 233 | result.set("opticalOdu", summaryOpticalOdu.json(mapper)); |
Ray Milkey | 9d810f6 | 2015-02-13 11:20:58 -0800 | [diff] [blame] | 234 | result.set("unknownType", summaryUnknownType.json(mapper)); |
| 235 | result.set("all", summaryAll.json(mapper)); |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 236 | return result; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Prints summary of the intents. |
| 241 | */ |
| 242 | private void printSummary() { |
| 243 | summaryConnectivity.printState(); |
Pavlin Radoslavov | aab51a3 | 2014-12-08 11:07:38 -0800 | [diff] [blame] | 244 | summaryHostToHost.printState(); |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 245 | summaryPointToPoint.printState(); |
| 246 | summaryMultiPointToSinglePoint.printState(); |
| 247 | summarySinglePointToMultiPoint.printState(); |
| 248 | summaryPath.printState(); |
| 249 | summaryLinkCollection.printState(); |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 250 | summaryOpticalCircuit.printState(); |
| 251 | summaryOpticalConnectivity.printState(); |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 252 | summaryOpticalOdu.printState(); |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 253 | summaryUnknownType.printState(); |
| 254 | summaryAll.printState(); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Internal local class to keep track of a single type Intent summary. |
| 259 | */ |
| 260 | private class IntentSummary { |
| 261 | private final String intentType; |
| 262 | private int total = 0; |
Pavlin Radoslavov | 67c0514 | 2014-12-02 13:04:08 -0800 | [diff] [blame] | 263 | private int installReq = 0; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 264 | private int compiling = 0; |
| 265 | private int installing = 0; |
| 266 | private int installed = 0; |
| 267 | private int recompiling = 0; |
Pavlin Radoslavov | 67c0514 | 2014-12-02 13:04:08 -0800 | [diff] [blame] | 268 | private int withdrawReq = 0; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 269 | private int withdrawing = 0; |
| 270 | private int withdrawn = 0; |
| 271 | private int failed = 0; |
| 272 | private int unknownState = 0; |
| 273 | |
| 274 | private static final String FORMAT_SUMMARY_LINE1 = |
| 275 | "%-23s total= %7d installed= %7d"; |
| 276 | private static final String FORMAT_SUMMARY_LINE2 = |
| 277 | "%-23s withdrawn= %7d failed= %7d"; |
| 278 | private static final String FORMAT_SUMMARY_LINE3 = |
Pavlin Radoslavov | 67c0514 | 2014-12-02 13:04:08 -0800 | [diff] [blame] | 279 | "%-23s installReq= %7d compiling= %7d"; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 280 | private static final String FORMAT_SUMMARY_LINE4 = |
| 281 | "%-23s installing= %7d recompiling= %7d"; |
| 282 | private static final String FORMAT_SUMMARY_LINE5 = |
Pavlin Radoslavov | 67c0514 | 2014-12-02 13:04:08 -0800 | [diff] [blame] | 283 | "%-23s withdrawReq= %7d withdrawing= %7d"; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 284 | private static final String FORMAT_SUMMARY_LINE6 = |
| 285 | "%-23s unknownState= %7d"; |
| 286 | |
| 287 | /** |
| 288 | * Constructor. |
| 289 | * |
| 290 | * @param intentType the scring describing the Intent type |
| 291 | */ |
| 292 | IntentSummary(String intentType) { |
| 293 | this.intentType = intentType; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Updates the Intent Summary. |
| 298 | * |
| 299 | * @param intentState the state of the Intent |
| 300 | */ |
| 301 | void update(IntentState intentState) { |
| 302 | total++; |
| 303 | switch (intentState) { |
Brian O'Connor | 7a71d5d | 2014-12-02 00:12:27 -0800 | [diff] [blame] | 304 | case INSTALL_REQ: |
Pavlin Radoslavov | 67c0514 | 2014-12-02 13:04:08 -0800 | [diff] [blame] | 305 | installReq++; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 306 | break; |
| 307 | case COMPILING: |
| 308 | compiling++; |
| 309 | break; |
| 310 | case INSTALLING: |
| 311 | installing++; |
| 312 | break; |
| 313 | case INSTALLED: |
| 314 | installed++; |
| 315 | break; |
| 316 | case RECOMPILING: |
| 317 | recompiling++; |
| 318 | break; |
Pavlin Radoslavov | 67c0514 | 2014-12-02 13:04:08 -0800 | [diff] [blame] | 319 | case WITHDRAW_REQ: |
| 320 | withdrawReq++; |
| 321 | break; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 322 | case WITHDRAWING: |
| 323 | withdrawing++; |
| 324 | break; |
| 325 | case WITHDRAWN: |
| 326 | withdrawn++; |
| 327 | break; |
| 328 | case FAILED: |
| 329 | failed++; |
| 330 | break; |
| 331 | default: |
| 332 | unknownState++; |
| 333 | break; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Prints the Intent Summary. |
| 339 | */ |
| 340 | void printState() { |
| 341 | print(FORMAT_SUMMARY_LINE1, intentType, total, installed); |
| 342 | print(FORMAT_SUMMARY_LINE2, intentType, withdrawn, failed); |
Pavlin Radoslavov | 67c0514 | 2014-12-02 13:04:08 -0800 | [diff] [blame] | 343 | print(FORMAT_SUMMARY_LINE3, intentType, installReq, compiling); |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 344 | print(FORMAT_SUMMARY_LINE4, intentType, installing, recompiling); |
Pavlin Radoslavov | 67c0514 | 2014-12-02 13:04:08 -0800 | [diff] [blame] | 345 | print(FORMAT_SUMMARY_LINE5, intentType, withdrawReq, withdrawing); |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 346 | if (unknownState != 0) { |
| 347 | print(FORMAT_SUMMARY_LINE6, intentType, unknownState); |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * Gets the JSON representation of the Intent Summary. |
| 353 | * |
| 354 | * @return the JSON representation of the Intent Summary |
| 355 | */ |
| 356 | JsonNode json(ObjectMapper mapper) { |
| 357 | ObjectNode result = mapper.createObjectNode() |
| 358 | .put("total", total) |
| 359 | .put("installed", installed) |
| 360 | .put("failed", failed) |
Pavlin Radoslavov | 67c0514 | 2014-12-02 13:04:08 -0800 | [diff] [blame] | 361 | .put("installReq", installReq) |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 362 | .put("compiling", compiling) |
| 363 | .put("installing", installing) |
| 364 | .put("recompiling", recompiling) |
Pavlin Radoslavov | 67c0514 | 2014-12-02 13:04:08 -0800 | [diff] [blame] | 365 | .put("withdrawReq", withdrawReq) |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 366 | .put("withdrawing", withdrawing) |
| 367 | .put("withdrawn", withdrawn) |
| 368 | .put("unknownState", unknownState); |
| 369 | |
| 370 | return result; |
| 371 | } |
| 372 | } |
| 373 | } |
| 374 | |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 375 | private String detailsFormat(IntentService service, Intent intent) { |
| 376 | StringBuilder builder = new StringBuilder(); |
Sho SHIMIZU | d7d1800 | 2015-01-21 14:37:14 -0800 | [diff] [blame] | 377 | if (!intent.resources().isEmpty()) { |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 378 | builder.append(String.format(" resources=%s%s", intent.resources(), sep)); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 379 | } |
| 380 | if (intent instanceof ConnectivityIntent) { |
| 381 | ConnectivityIntent ci = (ConnectivityIntent) intent; |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 382 | if (!ci.selector().criteria().isEmpty()) { |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 383 | builder.append(String.format(" selector=%s%s", ci.selector().criteria(), sep)); |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 384 | } |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 385 | if (!ci.treatment().allInstructions().isEmpty()) { |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 386 | builder.append(String.format(" treatment=%s%s", ci.treatment().allInstructions(), sep)); |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 387 | } |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 388 | if (ci.constraints() != null && !ci.constraints().isEmpty()) { |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 389 | builder.append(String.format(" constraints=%s%s", ci.constraints(), sep)); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 390 | } |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 391 | } |
| 392 | |
Pavlin Radoslavov | aab51a3 | 2014-12-08 11:07:38 -0800 | [diff] [blame] | 393 | if (intent instanceof HostToHostIntent) { |
| 394 | HostToHostIntent pi = (HostToHostIntent) intent; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 395 | builder.append(String.format(" host1=%s, host2=%s", pi.one(), pi.two())); |
Pavlin Radoslavov | aab51a3 | 2014-12-08 11:07:38 -0800 | [diff] [blame] | 396 | } else if (intent instanceof PointToPointIntent) { |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 397 | PointToPointIntent pi = (PointToPointIntent) intent; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 398 | builder.append(String.format(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint())); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 399 | } else if (intent instanceof MultiPointToSinglePointIntent) { |
| 400 | MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 401 | builder.append(String.format(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint())); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 402 | } else if (intent instanceof SinglePointToMultiPointIntent) { |
| 403 | SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 404 | builder.append(String.format(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints())); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 405 | } else if (intent instanceof PathIntent) { |
| 406 | PathIntent pi = (PathIntent) intent; |
Yuta HIGUCHI | bacea91 | 2016-12-23 13:20:09 -0800 | [diff] [blame] | 407 | builder.append(String.format(" path=%s, cost=%f", pi.path().links(), pi.path().cost())); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 408 | } else if (intent instanceof LinkCollectionIntent) { |
| 409 | LinkCollectionIntent li = (LinkCollectionIntent) intent; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 410 | builder.append(String.format(" links=%s", li.links())); |
| 411 | builder.append(String.format(" egress=%s", li.egressPoints())); |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 412 | } else if (intent instanceof OpticalCircuitIntent) { |
| 413 | OpticalCircuitIntent ci = (OpticalCircuitIntent) intent; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 414 | builder.append(String.format(" src=%s, dst=%s", ci.getSrc(), ci.getDst())); |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 415 | } else if (intent instanceof OpticalConnectivityIntent) { |
| 416 | OpticalConnectivityIntent ci = (OpticalConnectivityIntent) intent; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 417 | builder.append(String.format(" src=%s, dst=%s", ci.getSrc(), ci.getDst())); |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 418 | } else if (intent instanceof OpticalOduIntent) { |
| 419 | OpticalOduIntent ci = (OpticalOduIntent) intent; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 420 | builder.append(String.format(" src=%s, dst=%s", ci.getSrc(), ci.getDst())); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 421 | } |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 422 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 423 | List<Intent> installable = service.getInstallableIntents(intent.key()); |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 424 | installable.stream().filter(i -> contentFilter.filter(i)); |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 425 | if (showInstallable && installable != null && !installable.isEmpty()) { |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 426 | builder.append(String.format("%s installable=%s", sep, installable)); |
| 427 | } |
| 428 | return builder.toString(); |
| 429 | } |
| 430 | |
| 431 | private String fullFormat(Intent intent) { |
| 432 | return fullFormat(intent, null); |
| 433 | } |
| 434 | |
| 435 | private String fullFormat(Intent intent, String state) { |
| 436 | StringBuilder builder = new StringBuilder(); |
| 437 | builder.append(String.format("id=%s, ", intent.id())); |
| 438 | if (state != null) { |
| 439 | builder.append(String.format("state=%s, ", state)); |
| 440 | } |
| 441 | builder.append(String.format("key=%s, type=%s, appId=%s", |
| 442 | intent.key(), |
| 443 | intent.getClass().getSimpleName(), |
| 444 | intent.appId().name())); |
| 445 | return builder.toString(); |
| 446 | } |
| 447 | |
| 448 | private void printIntents(IntentService service) { |
| 449 | for (Intent intent : service.getIntents()) { |
| 450 | IntentState state = service.getIntentState(intent.key()); |
| 451 | String intentFormat = fullFormat(intent, state.toString()); |
| 452 | String detailsIntentFormat = detailsFormat(service, intent); |
| 453 | if (state != null && (contentFilter.filter( |
| 454 | intentFormat + detailsIntentFormat))) { |
| 455 | print(intentFormat); |
| 456 | print(detailsIntentFormat); |
| 457 | } |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 458 | } |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | // Produces JSON array of the specified intents. |
| 462 | private JsonNode json(IntentService service, Iterable<Intent> intents) { |
| 463 | ObjectMapper mapper = new ObjectMapper(); |
| 464 | ArrayNode result = mapper.createArrayNode(); |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 465 | StreamSupport.stream(intents.spliterator(), false) |
| 466 | .filter(intent -> contentFilter.filter(jsonForEntity(intent, Intent.class).toString())) |
| 467 | .forEach(intent -> result.add(jsonForEntity(intent, Intent.class))); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 468 | return result; |
| 469 | } |
| 470 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 471 | } |