blob: 59b3e47761102bac96200e82943ba25692ab47de [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska7d693f52014-10-21 19:17:57 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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 Vachuska7d693f52014-10-21 19:17:57 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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 Vachuska7d693f52014-10-21 19:17:57 -070015 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.cli.net;
tomf5c9d922014-10-03 15:22:03 -070017
Carolina Fernandezfa56d142016-11-14 01:13:26 +010018import java.util.ArrayList;
Ray Milkey3078fc02015-05-06 16:14:14 -070019import java.util.List;
Carolina Fernandezfa56d142016-11-14 01:13:26 +010020import java.util.stream.StreamSupport;
Ray Milkey3078fc02015-05-06 16:14:14 -070021
tomf5c9d922014-10-03 15:22:03 -070022import org.apache.karaf.shell.commands.Command;
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070023import org.apache.karaf.shell.commands.Option;
Carolina Fernandezfa56d142016-11-14 01:13:26 +010024import org.onlab.util.StringFilter;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.cli.AbstractShellCommand;
Brian O'Connorabafb502014-12-02 22:26:20 -080026import org.onosproject.net.intent.ConnectivityIntent;
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -080027import org.onosproject.net.intent.HostToHostIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080028import org.onosproject.net.intent.Intent;
29import org.onosproject.net.intent.IntentService;
30import org.onosproject.net.intent.IntentState;
31import org.onosproject.net.intent.LinkCollectionIntent;
32import org.onosproject.net.intent.MultiPointToSinglePointIntent;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020033import org.onosproject.net.intent.OpticalCircuitIntent;
34import org.onosproject.net.intent.OpticalConnectivityIntent;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +020035import org.onosproject.net.intent.OpticalOduIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080036import org.onosproject.net.intent.PathIntent;
37import org.onosproject.net.intent.PointToPointIntent;
38import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070039
Ray Milkey3078fc02015-05-06 16:14:14 -070040import com.fasterxml.jackson.databind.JsonNode;
41import com.fasterxml.jackson.databind.ObjectMapper;
42import com.fasterxml.jackson.databind.node.ArrayNode;
43import com.fasterxml.jackson.databind.node.ObjectNode;
tomf5c9d922014-10-03 15:22:03 -070044
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")
50public class IntentsListCommand extends AbstractShellCommand {
51
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080052 @Option(name = "-i", aliases = "--installable",
53 description = "Output Installable Intents",
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070054 required = false, multiValued = false)
55 private boolean showInstallable = false;
56
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080057 @Option(name = "-s", aliases = "--summary",
58 description = "Intents summary",
59 required = false, multiValued = false)
60 private boolean intentsSummary = false;
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070061
Jonathan Hart34f1e382015-02-24 16:52:23 -080062 @Option(name = "-p", aliases = "--pending",
Carolina Fernandezfa56d142016-11-14 01:13:26 +010063 description = "Show information about pending intents",
Jonathan Hart34f1e382015-02-24 16:52:23 -080064 required = false, multiValued = false)
65 private boolean pending = false;
66
Carolina Fernandezfa56d142016-11-14 01:13:26 +010067 @Option(name = "-f", aliases = "--filter",
Yuta HIGUCHIfadb9a32017-01-13 09:33:10 -080068 description = "Filter intents by specific keyword",
Carolina Fernandezfa56d142016-11-14 01:13:26 +010069 required = false, multiValued = true)
70 private List<String> filter = new ArrayList<>();
71
72 private StringFilter contentFilter;
73
74 private String sep = System.lineSeparator();
75
tomf5c9d922014-10-03 15:22:03 -070076 @Override
77 protected void execute() {
78 IntentService service = get(IntentService.class);
Carolina Fernandezfa56d142016-11-14 01:13:26 +010079 contentFilter = new StringFilter(filter, StringFilter.Strategy.AND);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080080
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 Hart34f1e382015-02-24 16:52:23 -080091 } else if (pending) {
Ray Milkey740c8a32015-03-17 13:41:03 -070092 if (outputJson()) {
93 print("%s", json(service, service.getPending()));
94 } else {
Carolina Fernandezfa56d142016-11-14 01:13:26 +010095 StreamSupport.stream(service.getPending().spliterator(), false)
96 .filter(intent -> contentFilter.filter(intent))
97 .forEach(intent -> print(fullFormat(intent)));
Ray Milkey740c8a32015-03-17 13:41:03 -070098 }
Jonathan Hart34f1e382015-02-24 16:52:23 -080099 return;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800100 }
101
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700102 if (outputJson()) {
103 print("%s", json(service, service.getIntents()));
104 } else {
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100105 printIntents(service);
tomf5c9d922014-10-03 15:22:03 -0700106 }
107 }
108
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800109 /**
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 Radoslavovaab51a32014-12-08 11:07:38 -0800115 private IntentSummary summaryHostToHost;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800116 private IntentSummary summaryPointToPoint;
117 private IntentSummary summaryMultiPointToSinglePoint;
118 private IntentSummary summarySinglePointToMultiPoint;
119 private IntentSummary summaryPath;
120 private IntentSummary summaryLinkCollection;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200121 private IntentSummary summaryOpticalCircuit;
122 private IntentSummary summaryOpticalConnectivity;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200123 private IntentSummary summaryOpticalOdu;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800124 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 Radoslavovaab51a32014-12-08 11:07:38 -0800132 summaryHostToHost = new IntentSummary("HostToHost");
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800133 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 Ashkenazyf0699702016-01-17 19:28:49 +0200140 summaryOpticalCircuit = new IntentSummary("OpticalCircuit");
141 summaryOpticalConnectivity = new IntentSummary("OpticalConnectivity");
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200142 summaryOpticalOdu = new IntentSummary("OpticalOdu");
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800143 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 Milkeyf9af43c2015-02-09 16:45:48 -0800158 IntentState intentState = service.getIntentState(intent.key());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800159 if (intentState == null) {
160 continue;
161 }
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100162 if (!contentFilter.filter(intent)) {
163 break;
164 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800165
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 Radoslavovaab51a32014-12-08 11:07:38 -0800174 if (intent instanceof HostToHostIntent) {
175 summaryHostToHost.update(intentState);
176 continue;
177 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800178 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 Ashkenazyf0699702016-01-17 19:28:49 +0200198 if (intent instanceof OpticalCircuitIntent) {
199 summaryOpticalCircuit.update(intentState);
200 continue;
201 }
202 if (intent instanceof OpticalConnectivityIntent) {
203 summaryOpticalConnectivity.update(intentState);
204 continue;
205 }
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200206 if (intent instanceof OpticalOduIntent) {
207 summaryOpticalOdu.update(intentState);
208 continue;
209 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800210 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 Milkey9d810f62015-02-13 11:20:58 -0800222 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 Radoslavov708e8202014-11-14 17:18:37 -0800226 summaryMultiPointToSinglePoint.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800227 result.set("singlePointToMultiPoint",
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800228 summarySinglePointToMultiPoint.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800229 result.set("path", summaryPath.json(mapper));
230 result.set("linkCollection", summaryLinkCollection.json(mapper));
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200231 result.set("opticalCircuit", summaryOpticalCircuit.json(mapper));
232 result.set("opticalConnectivity", summaryOpticalConnectivity.json(mapper));
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200233 result.set("opticalOdu", summaryOpticalOdu.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800234 result.set("unknownType", summaryUnknownType.json(mapper));
235 result.set("all", summaryAll.json(mapper));
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800236 return result;
237 }
238
239 /**
240 * Prints summary of the intents.
241 */
242 private void printSummary() {
243 summaryConnectivity.printState();
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800244 summaryHostToHost.printState();
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800245 summaryPointToPoint.printState();
246 summaryMultiPointToSinglePoint.printState();
247 summarySinglePointToMultiPoint.printState();
248 summaryPath.printState();
249 summaryLinkCollection.printState();
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200250 summaryOpticalCircuit.printState();
251 summaryOpticalConnectivity.printState();
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200252 summaryOpticalOdu.printState();
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800253 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 Radoslavov67c05142014-12-02 13:04:08 -0800263 private int installReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800264 private int compiling = 0;
265 private int installing = 0;
266 private int installed = 0;
267 private int recompiling = 0;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800268 private int withdrawReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800269 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 Radoslavov67c05142014-12-02 13:04:08 -0800279 "%-23s installReq= %7d compiling= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800280 private static final String FORMAT_SUMMARY_LINE4 =
281 "%-23s installing= %7d recompiling= %7d";
282 private static final String FORMAT_SUMMARY_LINE5 =
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800283 "%-23s withdrawReq= %7d withdrawing= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800284 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'Connor7a71d5d2014-12-02 00:12:27 -0800304 case INSTALL_REQ:
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800305 installReq++;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800306 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 Radoslavov67c05142014-12-02 13:04:08 -0800319 case WITHDRAW_REQ:
320 withdrawReq++;
321 break;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800322 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 Radoslavov67c05142014-12-02 13:04:08 -0800343 print(FORMAT_SUMMARY_LINE3, intentType, installReq, compiling);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800344 print(FORMAT_SUMMARY_LINE4, intentType, installing, recompiling);
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800345 print(FORMAT_SUMMARY_LINE5, intentType, withdrawReq, withdrawing);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800346 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 Radoslavov67c05142014-12-02 13:04:08 -0800361 .put("installReq", installReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800362 .put("compiling", compiling)
363 .put("installing", installing)
364 .put("recompiling", recompiling)
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800365 .put("withdrawReq", withdrawReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800366 .put("withdrawing", withdrawing)
367 .put("withdrawn", withdrawn)
368 .put("unknownState", unknownState);
369
370 return result;
371 }
372 }
373 }
374
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100375 private String detailsFormat(IntentService service, Intent intent) {
376 StringBuilder builder = new StringBuilder();
Sho SHIMIZUd7d18002015-01-21 14:37:14 -0800377 if (!intent.resources().isEmpty()) {
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100378 builder.append(String.format(" resources=%s%s", intent.resources(), sep));
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700379 }
380 if (intent instanceof ConnectivityIntent) {
381 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700382 if (!ci.selector().criteria().isEmpty()) {
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100383 builder.append(String.format(" selector=%s%s", ci.selector().criteria(), sep));
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700384 }
Ray Milkey42507352015-03-20 15:16:10 -0700385 if (!ci.treatment().allInstructions().isEmpty()) {
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100386 builder.append(String.format(" treatment=%s%s", ci.treatment().allInstructions(), sep));
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700387 }
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800388 if (ci.constraints() != null && !ci.constraints().isEmpty()) {
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100389 builder.append(String.format(" constraints=%s%s", ci.constraints(), sep));
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800390 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700391 }
392
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800393 if (intent instanceof HostToHostIntent) {
394 HostToHostIntent pi = (HostToHostIntent) intent;
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100395 builder.append(String.format(" host1=%s, host2=%s", pi.one(), pi.two()));
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800396 } else if (intent instanceof PointToPointIntent) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700397 PointToPointIntent pi = (PointToPointIntent) intent;
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100398 builder.append(String.format(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint()));
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700399 } else if (intent instanceof MultiPointToSinglePointIntent) {
400 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100401 builder.append(String.format(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint()));
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700402 } else if (intent instanceof SinglePointToMultiPointIntent) {
403 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100404 builder.append(String.format(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints()));
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700405 } else if (intent instanceof PathIntent) {
406 PathIntent pi = (PathIntent) intent;
Yuta HIGUCHIbacea912016-12-23 13:20:09 -0800407 builder.append(String.format(" path=%s, cost=%f", pi.path().links(), pi.path().cost()));
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700408 } else if (intent instanceof LinkCollectionIntent) {
409 LinkCollectionIntent li = (LinkCollectionIntent) intent;
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100410 builder.append(String.format(" links=%s", li.links()));
411 builder.append(String.format(" egress=%s", li.egressPoints()));
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200412 } else if (intent instanceof OpticalCircuitIntent) {
413 OpticalCircuitIntent ci = (OpticalCircuitIntent) intent;
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100414 builder.append(String.format(" src=%s, dst=%s", ci.getSrc(), ci.getDst()));
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200415 } else if (intent instanceof OpticalConnectivityIntent) {
416 OpticalConnectivityIntent ci = (OpticalConnectivityIntent) intent;
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100417 builder.append(String.format(" src=%s, dst=%s", ci.getSrc(), ci.getDst()));
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200418 } else if (intent instanceof OpticalOduIntent) {
419 OpticalOduIntent ci = (OpticalOduIntent) intent;
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100420 builder.append(String.format(" src=%s, dst=%s", ci.getSrc(), ci.getDst()));
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700421 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700422
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800423 List<Intent> installable = service.getInstallableIntents(intent.key());
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100424 installable.stream().filter(i -> contentFilter.filter(i));
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700425 if (showInstallable && installable != null && !installable.isEmpty()) {
Carolina Fernandezfa56d142016-11-14 01:13:26 +0100426 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 Vachuska10d4abc2014-10-21 12:47:26 -0700458 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700459 }
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 Fernandezfa56d142016-11-14 01:13:26 +0100465 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 Vachuska6ce73042014-10-21 10:01:49 -0700468 return result;
469 }
470
tomf5c9d922014-10-03 15:22:03 -0700471}