blob: 992c5285374e87247467040fb6a3303e2d1a5c8c [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
tomf5c9d922014-10-03 15:22:03 -070059 @Override
60 protected void execute() {
61 IntentService service = get(IntentService.class);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080062
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 Vachuska6ce73042014-10-21 10:01:49 -070075 if (outputJson()) {
76 print("%s", json(service, service.getIntents()));
77 } else {
78 for (Intent intent : service.getIntents()) {
79 IntentState state = service.getIntentState(intent.id());
Thomas Vachuska3ea690b2014-11-29 12:39:03 -080080 if (state != null) {
81 print("id=%s, state=%s, type=%s, appId=%s",
82 intent.id(), state, intent.getClass().getSimpleName(),
83 intent.appId().name());
84 printDetails(service, intent);
85 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -070086 }
tomf5c9d922014-10-03 15:22:03 -070087 }
88 }
89
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080090 /**
91 * Internal local class to keep track of all intent summaries.
92 */
93 private class IntentSummaries {
94 private IntentSummary summaryAll;
95 private IntentSummary summaryConnectivity;
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -080096 private IntentSummary summaryHostToHost;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080097 private IntentSummary summaryPointToPoint;
98 private IntentSummary summaryMultiPointToSinglePoint;
99 private IntentSummary summarySinglePointToMultiPoint;
100 private IntentSummary summaryPath;
101 private IntentSummary summaryLinkCollection;
102 private IntentSummary summaryUnknownType;
103
104 /**
105 * Initializes the internal state.
106 */
107 private void init() {
108 summaryAll = new IntentSummary("All");
109 summaryConnectivity = new IntentSummary("Connectivity");
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800110 summaryHostToHost = new IntentSummary("HostToHost");
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800111 summaryPointToPoint = new IntentSummary("PointToPoint");
112 summaryMultiPointToSinglePoint =
113 new IntentSummary("MultiPointToSinglePoint");
114 summarySinglePointToMultiPoint =
115 new IntentSummary("SinglePointToMultiPoint");
116 summaryPath = new IntentSummary("Path");
117 summaryLinkCollection = new IntentSummary("LinkCollection");
118 summaryUnknownType = new IntentSummary("UnknownType");
119 }
120
121 /**
122 * Collects summary of all intents.
123 *
124 * @param service the Intent Service to use
125 * @param intents the intents
126 */
127 private void collectIntentSummary(IntentService service,
128 Iterable<Intent> intents) {
129 init();
130
131 // Collect the summary for each intent type intents
132 for (Intent intent : intents) {
133 IntentState intentState = service.getIntentState(intent.id());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800134 if (intentState == null) {
135 continue;
136 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800137
138 // Update the summary for all Intents
139 summaryAll.update(intentState);
140
141 if (intent instanceof ConnectivityIntent) {
142 summaryConnectivity.update(intentState);
143 // NOTE: ConnectivityIntent is a base type Intent
144 // continue;
145 }
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800146 if (intent instanceof HostToHostIntent) {
147 summaryHostToHost.update(intentState);
148 continue;
149 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800150 if (intent instanceof PointToPointIntent) {
151 summaryPointToPoint.update(intentState);
152 continue;
153 }
154 if (intent instanceof MultiPointToSinglePointIntent) {
155 summaryMultiPointToSinglePoint.update(intentState);
156 continue;
157 }
158 if (intent instanceof SinglePointToMultiPointIntent) {
159 summarySinglePointToMultiPoint.update(intentState);
160 continue;
161 }
162 if (intent instanceof PathIntent) {
163 summaryPath.update(intentState);
164 continue;
165 }
166 if (intent instanceof LinkCollectionIntent) {
167 summaryLinkCollection.update(intentState);
168 continue;
169 }
170
171 summaryUnknownType.update(intentState);
172 }
173 }
174
175 /**
176 * Gets JSON representation of all Intents summary.
177 *
178 * @return JSON representation of all Intents summary
179 */
180 ObjectNode json() {
181 ObjectMapper mapper = new ObjectMapper();
182 ObjectNode result = mapper.createObjectNode();
Ray Milkey9d810f62015-02-13 11:20:58 -0800183 result.set("connectivity", summaryConnectivity.json(mapper));
184 result.set("hostToHost", summaryHostToHost.json(mapper));
185 result.set("pointToPoint", summaryPointToPoint.json(mapper));
186 result.set("multiPointToSinglePoint",
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800187 summaryMultiPointToSinglePoint.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800188 result.set("singlePointToMultiPoint",
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800189 summarySinglePointToMultiPoint.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800190 result.set("path", summaryPath.json(mapper));
191 result.set("linkCollection", summaryLinkCollection.json(mapper));
192 result.set("unknownType", summaryUnknownType.json(mapper));
193 result.set("all", summaryAll.json(mapper));
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800194 return result;
195 }
196
197 /**
198 * Prints summary of the intents.
199 */
200 private void printSummary() {
201 summaryConnectivity.printState();
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800202 summaryHostToHost.printState();
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800203 summaryPointToPoint.printState();
204 summaryMultiPointToSinglePoint.printState();
205 summarySinglePointToMultiPoint.printState();
206 summaryPath.printState();
207 summaryLinkCollection.printState();
208 summaryUnknownType.printState();
209 summaryAll.printState();
210 }
211
212 /**
213 * Internal local class to keep track of a single type Intent summary.
214 */
215 private class IntentSummary {
216 private final String intentType;
217 private int total = 0;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800218 private int installReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800219 private int compiling = 0;
220 private int installing = 0;
221 private int installed = 0;
222 private int recompiling = 0;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800223 private int withdrawReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800224 private int withdrawing = 0;
225 private int withdrawn = 0;
226 private int failed = 0;
227 private int unknownState = 0;
228
229 private static final String FORMAT_SUMMARY_LINE1 =
230 "%-23s total= %7d installed= %7d";
231 private static final String FORMAT_SUMMARY_LINE2 =
232 "%-23s withdrawn= %7d failed= %7d";
233 private static final String FORMAT_SUMMARY_LINE3 =
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800234 "%-23s installReq= %7d compiling= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800235 private static final String FORMAT_SUMMARY_LINE4 =
236 "%-23s installing= %7d recompiling= %7d";
237 private static final String FORMAT_SUMMARY_LINE5 =
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800238 "%-23s withdrawReq= %7d withdrawing= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800239 private static final String FORMAT_SUMMARY_LINE6 =
240 "%-23s unknownState= %7d";
241
242 /**
243 * Constructor.
244 *
245 * @param intentType the scring describing the Intent type
246 */
247 IntentSummary(String intentType) {
248 this.intentType = intentType;
249 }
250
251 /**
252 * Updates the Intent Summary.
253 *
254 * @param intentState the state of the Intent
255 */
256 void update(IntentState intentState) {
257 total++;
258 switch (intentState) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800259 case INSTALL_REQ:
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800260 installReq++;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800261 break;
262 case COMPILING:
263 compiling++;
264 break;
265 case INSTALLING:
266 installing++;
267 break;
268 case INSTALLED:
269 installed++;
270 break;
271 case RECOMPILING:
272 recompiling++;
273 break;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800274 case WITHDRAW_REQ:
275 withdrawReq++;
276 break;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800277 case WITHDRAWING:
278 withdrawing++;
279 break;
280 case WITHDRAWN:
281 withdrawn++;
282 break;
283 case FAILED:
284 failed++;
285 break;
286 default:
287 unknownState++;
288 break;
289 }
290 }
291
292 /**
293 * Prints the Intent Summary.
294 */
295 void printState() {
296 print(FORMAT_SUMMARY_LINE1, intentType, total, installed);
297 print(FORMAT_SUMMARY_LINE2, intentType, withdrawn, failed);
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800298 print(FORMAT_SUMMARY_LINE3, intentType, installReq, compiling);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800299 print(FORMAT_SUMMARY_LINE4, intentType, installing, recompiling);
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800300 print(FORMAT_SUMMARY_LINE5, intentType, withdrawReq, withdrawing);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800301 if (unknownState != 0) {
302 print(FORMAT_SUMMARY_LINE6, intentType, unknownState);
303 }
304 }
305
306 /**
307 * Gets the JSON representation of the Intent Summary.
308 *
309 * @return the JSON representation of the Intent Summary
310 */
311 JsonNode json(ObjectMapper mapper) {
312 ObjectNode result = mapper.createObjectNode()
313 .put("total", total)
314 .put("installed", installed)
315 .put("failed", failed)
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800316 .put("installReq", installReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800317 .put("compiling", compiling)
318 .put("installing", installing)
319 .put("recompiling", recompiling)
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800320 .put("withdrawReq", withdrawReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800321 .put("withdrawing", withdrawing)
322 .put("withdrawn", withdrawn)
323 .put("unknownState", unknownState);
324
325 return result;
326 }
327 }
328 }
329
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700330 private void printDetails(IntentService service, Intent intent) {
Sho SHIMIZUd7d18002015-01-21 14:37:14 -0800331 if (!intent.resources().isEmpty()) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700332 print(" resources=%s", intent.resources());
333 }
334 if (intent instanceof ConnectivityIntent) {
335 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700336 if (!ci.selector().criteria().isEmpty()) {
337 print(" selector=%s", ci.selector().criteria());
338 }
339 if (!ci.treatment().instructions().isEmpty()) {
340 print(" treatment=%s", ci.treatment().instructions());
341 }
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800342 if (ci.constraints() != null && !ci.constraints().isEmpty()) {
343 print(" constraints=%s", ci.constraints());
344 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700345 }
346
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800347 if (intent instanceof HostToHostIntent) {
348 HostToHostIntent pi = (HostToHostIntent) intent;
349 print(" host1=%s, host2=%s", pi.one(), pi.two());
350 } else if (intent instanceof PointToPointIntent) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700351 PointToPointIntent pi = (PointToPointIntent) intent;
352 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint());
353 } else if (intent instanceof MultiPointToSinglePointIntent) {
354 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
355 print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint());
356 } else if (intent instanceof SinglePointToMultiPointIntent) {
357 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
358 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints());
359 } else if (intent instanceof PathIntent) {
360 PathIntent pi = (PathIntent) intent;
361 print(" path=%s, cost=%d", pi.path().links(), pi.path().cost());
362 } else if (intent instanceof LinkCollectionIntent) {
363 LinkCollectionIntent li = (LinkCollectionIntent) intent;
364 print(" links=%s", li.links());
Michele Santuari4a338072014-11-05 18:38:55 +0100365 print(" egress=%s", li.egressPoints());
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700366 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700367
368 List<Intent> installable = service.getInstallableIntents(intent.id());
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700369 if (showInstallable && installable != null && !installable.isEmpty()) {
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700370 print(" installable=%s", installable);
371 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700372 }
373
374 // Produces JSON array of the specified intents.
375 private JsonNode json(IntentService service, Iterable<Intent> intents) {
376 ObjectMapper mapper = new ObjectMapper();
377 ArrayNode result = mapper.createArrayNode();
378 for (Intent intent : intents) {
379 result.add(json(service, mapper, intent));
380 }
381 return result;
382 }
383
384 private JsonNode json(IntentService service, ObjectMapper mapper, Intent intent) {
385 ObjectNode result = mapper.createObjectNode()
386 .put("id", intent.id().toString())
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700387 .put("type", intent.getClass().getSimpleName())
388 .put("appId", intent.appId().name());
389
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700390 IntentState state = service.getIntentState(intent.id());
391 if (state != null) {
392 result.put("state", state.toString());
393 }
394
Sho SHIMIZUd7d18002015-01-21 14:37:14 -0800395 if (!intent.resources().isEmpty()) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700396 ArrayNode rnode = mapper.createArrayNode();
397 for (NetworkResource resource : intent.resources()) {
398 rnode.add(resource.toString());
399 }
400 result.set("resources", rnode);
401 }
402
403 if (intent instanceof ConnectivityIntent) {
404 ConnectivityIntent ci = (ConnectivityIntent) intent;
405 if (!ci.selector().criteria().isEmpty()) {
406 result.put("selector", ci.selector().criteria().toString());
407 }
408 if (!ci.treatment().instructions().isEmpty()) {
409 result.put("treatment", ci.treatment().instructions().toString());
410 }
411 }
412
413 if (intent instanceof PathIntent) {
414 PathIntent pi = (PathIntent) intent;
415 ArrayNode pnode = mapper.createArrayNode();
416 for (Link link : pi.path().links()) {
417 pnode.add(link.toString());
418 }
419 result.set("path", pnode);
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800420 } else if (intent instanceof HostToHostIntent) {
421 HostToHostIntent pi = (HostToHostIntent) intent;
422 result.set("host1", LinksListCommand.json(mapper, pi.one()));
423 result.set("host2", LinksListCommand.json(mapper, pi.two()));
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700424 } else if (intent instanceof PointToPointIntent) {
425 PointToPointIntent pi = (PointToPointIntent) intent;
426 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
427 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700428 } else if (intent instanceof MultiPointToSinglePointIntent) {
429 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
430 result.set("ingress", json(mapper, pi.ingressPoints()));
431 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700432 } else if (intent instanceof SinglePointToMultiPointIntent) {
433 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
434 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
435 result.set("egress", json(mapper, pi.egressPoints()));
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700436 } else if (intent instanceof LinkCollectionIntent) {
437 LinkCollectionIntent li = (LinkCollectionIntent) intent;
438 result.set("links", LinksListCommand.json(li.links()));
439 }
440
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700441 List<Intent> installable = service.getInstallableIntents(intent.id());
442 if (installable != null && !installable.isEmpty()) {
443 result.set("installable", json(service, installable));
444 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700445 return result;
446 }
447
448 private JsonNode json(ObjectMapper mapper, Set<ConnectPoint> connectPoints) {
449 ArrayNode result = mapper.createArrayNode();
450 for (ConnectPoint cp : connectPoints) {
451 result.add(LinksListCommand.json(mapper, cp));
452 }
453 return result;
454 }
tomf5c9d922014-10-03 15:22:03 -0700455}