blob: ae6f1894d06418cf9eebbc910a8e1423daa7bcfe [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 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
Thomas Vachuska6ce73042014-10-21 10:01:49 -070018import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ArrayNode;
21import com.fasterxml.jackson.databind.node.ObjectNode;
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;
Brian O'Connorabafb502014-12-02 22:26:20 -080024import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.net.ConnectPoint;
26import org.onosproject.net.Link;
27import org.onosproject.net.NetworkResource;
28import org.onosproject.net.intent.ConnectivityIntent;
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -080029import org.onosproject.net.intent.HostToHostIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080030import org.onosproject.net.intent.Intent;
31import org.onosproject.net.intent.IntentService;
32import org.onosproject.net.intent.IntentState;
33import org.onosproject.net.intent.LinkCollectionIntent;
34import org.onosproject.net.intent.MultiPointToSinglePointIntent;
35import org.onosproject.net.intent.PathIntent;
36import org.onosproject.net.intent.PointToPointIntent;
37import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070038
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070039import java.util.List;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070040import java.util.Set;
tomf5c9d922014-10-03 15:22:03 -070041
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")
47public class IntentsListCommand extends AbstractShellCommand {
48
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080049 @Option(name = "-i", aliases = "--installable",
50 description = "Output Installable Intents",
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070051 required = false, multiValued = false)
52 private boolean showInstallable = false;
53
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080054 @Option(name = "-s", aliases = "--summary",
55 description = "Intents summary",
56 required = false, multiValued = false)
57 private boolean intentsSummary = false;
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070058
Jonathan Hart34f1e382015-02-24 16:52:23 -080059 @Option(name = "-p", aliases = "--pending",
60 description = "Show inforamtion about pending intents",
61 required = false, multiValued = false)
62 private boolean pending = false;
63
tomf5c9d922014-10-03 15:22:03 -070064 @Override
65 protected void execute() {
66 IntentService service = get(IntentService.class);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080067
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 Hart34f1e382015-02-24 16:52:23 -080078 } 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 Radoslavov708e8202014-11-14 17:18:37 -080086 }
87
Thomas Vachuska6ce73042014-10-21 10:01:49 -070088 if (outputJson()) {
89 print("%s", json(service, service.getIntents()));
90 } else {
91 for (Intent intent : service.getIntents()) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -080092 IntentState state = service.getIntentState(intent.key());
Thomas Vachuska3ea690b2014-11-29 12:39:03 -080093 if (state != null) {
Jonathan Hart189f9bf2015-02-11 19:24:28 -080094 print("id=%s, state=%s, key=%s, type=%s, appId=%s",
95 intent.id(), state, intent.key(),
96 intent.getClass().getSimpleName(),
Thomas Vachuska3ea690b2014-11-29 12:39:03 -080097 intent.appId().name());
98 printDetails(service, intent);
99 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700100 }
tomf5c9d922014-10-03 15:22:03 -0700101 }
102 }
103
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800104 /**
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 Radoslavovaab51a32014-12-08 11:07:38 -0800110 private IntentSummary summaryHostToHost;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800111 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 Radoslavovaab51a32014-12-08 11:07:38 -0800124 summaryHostToHost = new IntentSummary("HostToHost");
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800125 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 Milkeyf9af43c2015-02-09 16:45:48 -0800147 IntentState intentState = service.getIntentState(intent.key());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800148 if (intentState == null) {
149 continue;
150 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800151
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 Radoslavovaab51a32014-12-08 11:07:38 -0800160 if (intent instanceof HostToHostIntent) {
161 summaryHostToHost.update(intentState);
162 continue;
163 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800164 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 Milkey9d810f62015-02-13 11:20:58 -0800197 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 Radoslavov708e8202014-11-14 17:18:37 -0800201 summaryMultiPointToSinglePoint.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800202 result.set("singlePointToMultiPoint",
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800203 summarySinglePointToMultiPoint.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800204 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 Radoslavov708e8202014-11-14 17:18:37 -0800208 return result;
209 }
210
211 /**
212 * Prints summary of the intents.
213 */
214 private void printSummary() {
215 summaryConnectivity.printState();
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800216 summaryHostToHost.printState();
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800217 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 Radoslavov67c05142014-12-02 13:04:08 -0800232 private int installReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800233 private int compiling = 0;
234 private int installing = 0;
235 private int installed = 0;
236 private int recompiling = 0;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800237 private int withdrawReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800238 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 Radoslavov67c05142014-12-02 13:04:08 -0800248 "%-23s installReq= %7d compiling= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800249 private static final String FORMAT_SUMMARY_LINE4 =
250 "%-23s installing= %7d recompiling= %7d";
251 private static final String FORMAT_SUMMARY_LINE5 =
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800252 "%-23s withdrawReq= %7d withdrawing= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800253 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'Connor7a71d5d2014-12-02 00:12:27 -0800273 case INSTALL_REQ:
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800274 installReq++;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800275 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 Radoslavov67c05142014-12-02 13:04:08 -0800288 case WITHDRAW_REQ:
289 withdrawReq++;
290 break;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800291 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 Radoslavov67c05142014-12-02 13:04:08 -0800312 print(FORMAT_SUMMARY_LINE3, intentType, installReq, compiling);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800313 print(FORMAT_SUMMARY_LINE4, intentType, installing, recompiling);
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800314 print(FORMAT_SUMMARY_LINE5, intentType, withdrawReq, withdrawing);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800315 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 Radoslavov67c05142014-12-02 13:04:08 -0800330 .put("installReq", installReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800331 .put("compiling", compiling)
332 .put("installing", installing)
333 .put("recompiling", recompiling)
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800334 .put("withdrawReq", withdrawReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800335 .put("withdrawing", withdrawing)
336 .put("withdrawn", withdrawn)
337 .put("unknownState", unknownState);
338
339 return result;
340 }
341 }
342 }
343
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700344 private void printDetails(IntentService service, Intent intent) {
Sho SHIMIZUd7d18002015-01-21 14:37:14 -0800345 if (!intent.resources().isEmpty()) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700346 print(" resources=%s", intent.resources());
347 }
348 if (intent instanceof ConnectivityIntent) {
349 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700350 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 Vachuskaedc944c2014-11-04 15:42:25 -0800356 if (ci.constraints() != null && !ci.constraints().isEmpty()) {
357 print(" constraints=%s", ci.constraints());
358 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700359 }
360
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800361 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 Vachuska6ce73042014-10-21 10:01:49 -0700365 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 Santuari4a338072014-11-05 18:38:55 +0100379 print(" egress=%s", li.egressPoints());
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700380 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700381
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800382 List<Intent> installable = service.getInstallableIntents(intent.key());
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700383 if (showInstallable && installable != null && !installable.isEmpty()) {
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700384 print(" installable=%s", installable);
385 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700386 }
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 Vachuska6ce73042014-10-21 10:01:49 -0700401 .put("type", intent.getClass().getSimpleName())
402 .put("appId", intent.appId().name());
403
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800404 IntentState state = service.getIntentState(intent.key());
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700405 if (state != null) {
406 result.put("state", state.toString());
407 }
408
Sho SHIMIZUd7d18002015-01-21 14:37:14 -0800409 if (!intent.resources().isEmpty()) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700410 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 Radoslavovaab51a32014-12-08 11:07:38 -0800434 } 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 Vachuska6ce73042014-10-21 10:01:49 -0700438 } 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 Vachuska6ce73042014-10-21 10:01:49 -0700442 } 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 Vachuska6ce73042014-10-21 10:01:49 -0700446 } 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 Vachuska6ce73042014-10-21 10:01:49 -0700450 } else if (intent instanceof LinkCollectionIntent) {
451 LinkCollectionIntent li = (LinkCollectionIntent) intent;
452 result.set("links", LinksListCommand.json(li.links()));
453 }
454
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800455 List<Intent> installable = service.getInstallableIntents(intent.key());
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700456 if (installable != null && !installable.isEmpty()) {
457 result.set("installable", json(service, installable));
458 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700459 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 }
tomf5c9d922014-10-03 15:22:03 -0700469}