blob: e20b0159455c406dc7332e5a6a3cd66709769d16 [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()) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -080079 IntentState state = service.getIntentState(intent.key());
Thomas Vachuska3ea690b2014-11-29 12:39:03 -080080 if (state != null) {
Jonathan Hart189f9bf2015-02-11 19:24:28 -080081 print("id=%s, state=%s, key=%s, type=%s, appId=%s",
82 intent.id(), state, intent.key(),
83 intent.getClass().getSimpleName(),
Thomas Vachuska3ea690b2014-11-29 12:39:03 -080084 intent.appId().name());
85 printDetails(service, intent);
86 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -070087 }
tomf5c9d922014-10-03 15:22:03 -070088 }
89 }
90
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080091 /**
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 Radoslavovaab51a32014-12-08 11:07:38 -080097 private IntentSummary summaryHostToHost;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080098 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 Radoslavovaab51a32014-12-08 11:07:38 -0800111 summaryHostToHost = new IntentSummary("HostToHost");
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800112 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 Milkeyf9af43c2015-02-09 16:45:48 -0800134 IntentState intentState = service.getIntentState(intent.key());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800135 if (intentState == null) {
136 continue;
137 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800138
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 Radoslavovaab51a32014-12-08 11:07:38 -0800147 if (intent instanceof HostToHostIntent) {
148 summaryHostToHost.update(intentState);
149 continue;
150 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800151 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 Milkey9d810f62015-02-13 11:20:58 -0800184 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 Radoslavov708e8202014-11-14 17:18:37 -0800188 summaryMultiPointToSinglePoint.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800189 result.set("singlePointToMultiPoint",
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800190 summarySinglePointToMultiPoint.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800191 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 Radoslavov708e8202014-11-14 17:18:37 -0800195 return result;
196 }
197
198 /**
199 * Prints summary of the intents.
200 */
201 private void printSummary() {
202 summaryConnectivity.printState();
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800203 summaryHostToHost.printState();
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800204 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 Radoslavov67c05142014-12-02 13:04:08 -0800219 private int installReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800220 private int compiling = 0;
221 private int installing = 0;
222 private int installed = 0;
223 private int recompiling = 0;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800224 private int withdrawReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800225 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 Radoslavov67c05142014-12-02 13:04:08 -0800235 "%-23s installReq= %7d compiling= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800236 private static final String FORMAT_SUMMARY_LINE4 =
237 "%-23s installing= %7d recompiling= %7d";
238 private static final String FORMAT_SUMMARY_LINE5 =
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800239 "%-23s withdrawReq= %7d withdrawing= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800240 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'Connor7a71d5d2014-12-02 00:12:27 -0800260 case INSTALL_REQ:
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800261 installReq++;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800262 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 Radoslavov67c05142014-12-02 13:04:08 -0800275 case WITHDRAW_REQ:
276 withdrawReq++;
277 break;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800278 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 Radoslavov67c05142014-12-02 13:04:08 -0800299 print(FORMAT_SUMMARY_LINE3, intentType, installReq, compiling);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800300 print(FORMAT_SUMMARY_LINE4, intentType, installing, recompiling);
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800301 print(FORMAT_SUMMARY_LINE5, intentType, withdrawReq, withdrawing);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800302 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 Radoslavov67c05142014-12-02 13:04:08 -0800317 .put("installReq", installReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800318 .put("compiling", compiling)
319 .put("installing", installing)
320 .put("recompiling", recompiling)
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800321 .put("withdrawReq", withdrawReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800322 .put("withdrawing", withdrawing)
323 .put("withdrawn", withdrawn)
324 .put("unknownState", unknownState);
325
326 return result;
327 }
328 }
329 }
330
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700331 private void printDetails(IntentService service, Intent intent) {
Sho SHIMIZUd7d18002015-01-21 14:37:14 -0800332 if (!intent.resources().isEmpty()) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700333 print(" resources=%s", intent.resources());
334 }
335 if (intent instanceof ConnectivityIntent) {
336 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700337 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 Vachuskaedc944c2014-11-04 15:42:25 -0800343 if (ci.constraints() != null && !ci.constraints().isEmpty()) {
344 print(" constraints=%s", ci.constraints());
345 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700346 }
347
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800348 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 Vachuska6ce73042014-10-21 10:01:49 -0700352 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 Santuari4a338072014-11-05 18:38:55 +0100366 print(" egress=%s", li.egressPoints());
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700367 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700368
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800369 List<Intent> installable = service.getInstallableIntents(intent.key());
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700370 if (showInstallable && installable != null && !installable.isEmpty()) {
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700371 print(" installable=%s", installable);
372 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700373 }
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 Vachuska6ce73042014-10-21 10:01:49 -0700388 .put("type", intent.getClass().getSimpleName())
389 .put("appId", intent.appId().name());
390
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800391 IntentState state = service.getIntentState(intent.key());
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700392 if (state != null) {
393 result.put("state", state.toString());
394 }
395
Sho SHIMIZUd7d18002015-01-21 14:37:14 -0800396 if (!intent.resources().isEmpty()) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700397 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 Radoslavovaab51a32014-12-08 11:07:38 -0800421 } 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 Vachuska6ce73042014-10-21 10:01:49 -0700425 } 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 Vachuska6ce73042014-10-21 10:01:49 -0700429 } 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 Vachuska6ce73042014-10-21 10:01:49 -0700433 } 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 Vachuska6ce73042014-10-21 10:01:49 -0700437 } else if (intent instanceof LinkCollectionIntent) {
438 LinkCollectionIntent li = (LinkCollectionIntent) intent;
439 result.set("links", LinksListCommand.json(li.links()));
440 }
441
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800442 List<Intent> installable = service.getInstallableIntents(intent.key());
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700443 if (installable != null && !installable.isEmpty()) {
444 result.set("installable", json(service, installable));
445 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700446 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 }
tomf5c9d922014-10-03 15:22:03 -0700456}