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