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