blob: 5e5ce129a69870eb0d79afe78b27b05eb22d531d [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;
29import org.onosproject.net.intent.Intent;
30import org.onosproject.net.intent.IntentService;
31import org.onosproject.net.intent.IntentState;
32import org.onosproject.net.intent.LinkCollectionIntent;
33import org.onosproject.net.intent.MultiPointToSinglePointIntent;
34import org.onosproject.net.intent.PathIntent;
35import org.onosproject.net.intent.PointToPointIntent;
36import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070037
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070038import java.util.List;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070039import java.util.Set;
tomf5c9d922014-10-03 15:22:03 -070040
41/**
42 * Lists the inventory of intents and their states.
43 */
44@Command(scope = "onos", name = "intents",
45 description = "Lists the inventory of intents and their states")
46public class IntentsListCommand extends AbstractShellCommand {
47
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080048 @Option(name = "-i", aliases = "--installable",
49 description = "Output Installable Intents",
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070050 required = false, multiValued = false)
51 private boolean showInstallable = false;
52
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080053 @Option(name = "-s", aliases = "--summary",
54 description = "Intents summary",
55 required = false, multiValued = false)
56 private boolean intentsSummary = false;
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070057
tomf5c9d922014-10-03 15:22:03 -070058 @Override
59 protected void execute() {
60 IntentService service = get(IntentService.class);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080061
62 if (intentsSummary) {
63 IntentSummaries intentSummaries = new IntentSummaries();
64 intentSummaries.collectIntentSummary(service,
65 service.getIntents());
66 if (outputJson()) {
67 print("%s", intentSummaries.json());
68 } else {
69 intentSummaries.printSummary();
70 }
71 return;
72 }
73
Thomas Vachuska6ce73042014-10-21 10:01:49 -070074 if (outputJson()) {
75 print("%s", json(service, service.getIntents()));
76 } else {
77 for (Intent intent : service.getIntents()) {
78 IntentState state = service.getIntentState(intent.id());
Thomas Vachuska3ea690b2014-11-29 12:39:03 -080079 if (state != null) {
80 print("id=%s, state=%s, type=%s, appId=%s",
81 intent.id(), state, intent.getClass().getSimpleName(),
82 intent.appId().name());
83 printDetails(service, intent);
84 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -070085 }
tomf5c9d922014-10-03 15:22:03 -070086 }
87 }
88
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080089 /**
90 * Internal local class to keep track of all intent summaries.
91 */
92 private class IntentSummaries {
93 private IntentSummary summaryAll;
94 private IntentSummary summaryConnectivity;
95 private IntentSummary summaryPointToPoint;
96 private IntentSummary summaryMultiPointToSinglePoint;
97 private IntentSummary summarySinglePointToMultiPoint;
98 private IntentSummary summaryPath;
99 private IntentSummary summaryLinkCollection;
100 private IntentSummary summaryUnknownType;
101
102 /**
103 * Initializes the internal state.
104 */
105 private void init() {
106 summaryAll = new IntentSummary("All");
107 summaryConnectivity = new IntentSummary("Connectivity");
108 summaryPointToPoint = new IntentSummary("PointToPoint");
109 summaryMultiPointToSinglePoint =
110 new IntentSummary("MultiPointToSinglePoint");
111 summarySinglePointToMultiPoint =
112 new IntentSummary("SinglePointToMultiPoint");
113 summaryPath = new IntentSummary("Path");
114 summaryLinkCollection = new IntentSummary("LinkCollection");
115 summaryUnknownType = new IntentSummary("UnknownType");
116 }
117
118 /**
119 * Collects summary of all intents.
120 *
121 * @param service the Intent Service to use
122 * @param intents the intents
123 */
124 private void collectIntentSummary(IntentService service,
125 Iterable<Intent> intents) {
126 init();
127
128 // Collect the summary for each intent type intents
129 for (Intent intent : intents) {
130 IntentState intentState = service.getIntentState(intent.id());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800131 if (intentState == null) {
132 continue;
133 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800134
135 // Update the summary for all Intents
136 summaryAll.update(intentState);
137
138 if (intent instanceof ConnectivityIntent) {
139 summaryConnectivity.update(intentState);
140 // NOTE: ConnectivityIntent is a base type Intent
141 // continue;
142 }
143 if (intent instanceof PointToPointIntent) {
144 summaryPointToPoint.update(intentState);
145 continue;
146 }
147 if (intent instanceof MultiPointToSinglePointIntent) {
148 summaryMultiPointToSinglePoint.update(intentState);
149 continue;
150 }
151 if (intent instanceof SinglePointToMultiPointIntent) {
152 summarySinglePointToMultiPoint.update(intentState);
153 continue;
154 }
155 if (intent instanceof PathIntent) {
156 summaryPath.update(intentState);
157 continue;
158 }
159 if (intent instanceof LinkCollectionIntent) {
160 summaryLinkCollection.update(intentState);
161 continue;
162 }
163
164 summaryUnknownType.update(intentState);
165 }
166 }
167
168 /**
169 * Gets JSON representation of all Intents summary.
170 *
171 * @return JSON representation of all Intents summary
172 */
173 ObjectNode json() {
174 ObjectMapper mapper = new ObjectMapper();
175 ObjectNode result = mapper.createObjectNode();
176 result.put("connectivity", summaryConnectivity.json(mapper));
177 result.put("pointToPoint", summaryPointToPoint.json(mapper));
178 result.put("multiPointToSinglePoint",
179 summaryMultiPointToSinglePoint.json(mapper));
180 result.put("singlePointToMultiPoint",
181 summarySinglePointToMultiPoint.json(mapper));
182 result.put("path", summaryPath.json(mapper));
183 result.put("linkCollection", summaryLinkCollection.json(mapper));
184 result.put("unknownType", summaryUnknownType.json(mapper));
185 result.put("all", summaryAll.json(mapper));
186 return result;
187 }
188
189 /**
190 * Prints summary of the intents.
191 */
192 private void printSummary() {
193 summaryConnectivity.printState();
194 summaryPointToPoint.printState();
195 summaryMultiPointToSinglePoint.printState();
196 summarySinglePointToMultiPoint.printState();
197 summaryPath.printState();
198 summaryLinkCollection.printState();
199 summaryUnknownType.printState();
200 summaryAll.printState();
201 }
202
203 /**
204 * Internal local class to keep track of a single type Intent summary.
205 */
206 private class IntentSummary {
207 private final String intentType;
208 private int total = 0;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800209 private int installReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800210 private int compiling = 0;
211 private int installing = 0;
212 private int installed = 0;
213 private int recompiling = 0;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800214 private int withdrawReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800215 private int withdrawing = 0;
216 private int withdrawn = 0;
217 private int failed = 0;
218 private int unknownState = 0;
219
220 private static final String FORMAT_SUMMARY_LINE1 =
221 "%-23s total= %7d installed= %7d";
222 private static final String FORMAT_SUMMARY_LINE2 =
223 "%-23s withdrawn= %7d failed= %7d";
224 private static final String FORMAT_SUMMARY_LINE3 =
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800225 "%-23s installReq= %7d compiling= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800226 private static final String FORMAT_SUMMARY_LINE4 =
227 "%-23s installing= %7d recompiling= %7d";
228 private static final String FORMAT_SUMMARY_LINE5 =
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800229 "%-23s withdrawReq= %7d withdrawing= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800230 private static final String FORMAT_SUMMARY_LINE6 =
231 "%-23s unknownState= %7d";
232
233 /**
234 * Constructor.
235 *
236 * @param intentType the scring describing the Intent type
237 */
238 IntentSummary(String intentType) {
239 this.intentType = intentType;
240 }
241
242 /**
243 * Updates the Intent Summary.
244 *
245 * @param intentState the state of the Intent
246 */
247 void update(IntentState intentState) {
248 total++;
249 switch (intentState) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800250 case INSTALL_REQ:
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800251 installReq++;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800252 break;
253 case COMPILING:
254 compiling++;
255 break;
256 case INSTALLING:
257 installing++;
258 break;
259 case INSTALLED:
260 installed++;
261 break;
262 case RECOMPILING:
263 recompiling++;
264 break;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800265 case WITHDRAW_REQ:
266 withdrawReq++;
267 break;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800268 case WITHDRAWING:
269 withdrawing++;
270 break;
271 case WITHDRAWN:
272 withdrawn++;
273 break;
274 case FAILED:
275 failed++;
276 break;
277 default:
278 unknownState++;
279 break;
280 }
281 }
282
283 /**
284 * Prints the Intent Summary.
285 */
286 void printState() {
287 print(FORMAT_SUMMARY_LINE1, intentType, total, installed);
288 print(FORMAT_SUMMARY_LINE2, intentType, withdrawn, failed);
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800289 print(FORMAT_SUMMARY_LINE3, intentType, installReq, compiling);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800290 print(FORMAT_SUMMARY_LINE4, intentType, installing, recompiling);
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800291 print(FORMAT_SUMMARY_LINE5, intentType, withdrawReq, withdrawing);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800292 if (unknownState != 0) {
293 print(FORMAT_SUMMARY_LINE6, intentType, unknownState);
294 }
295 }
296
297 /**
298 * Gets the JSON representation of the Intent Summary.
299 *
300 * @return the JSON representation of the Intent Summary
301 */
302 JsonNode json(ObjectMapper mapper) {
303 ObjectNode result = mapper.createObjectNode()
304 .put("total", total)
305 .put("installed", installed)
306 .put("failed", failed)
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800307 .put("installReq", installReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800308 .put("compiling", compiling)
309 .put("installing", installing)
310 .put("recompiling", recompiling)
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800311 .put("withdrawReq", withdrawReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800312 .put("withdrawing", withdrawing)
313 .put("withdrawn", withdrawn)
314 .put("unknownState", unknownState);
315
316 return result;
317 }
318 }
319 }
320
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700321 private void printDetails(IntentService service, Intent intent) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700322 if (intent.resources() != null && !intent.resources().isEmpty()) {
323 print(" resources=%s", intent.resources());
324 }
325 if (intent instanceof ConnectivityIntent) {
326 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700327 if (!ci.selector().criteria().isEmpty()) {
328 print(" selector=%s", ci.selector().criteria());
329 }
330 if (!ci.treatment().instructions().isEmpty()) {
331 print(" treatment=%s", ci.treatment().instructions());
332 }
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800333 if (ci.constraints() != null && !ci.constraints().isEmpty()) {
334 print(" constraints=%s", ci.constraints());
335 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700336 }
337
338 if (intent instanceof PointToPointIntent) {
339 PointToPointIntent pi = (PointToPointIntent) intent;
340 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint());
341 } else if (intent instanceof MultiPointToSinglePointIntent) {
342 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
343 print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint());
344 } else if (intent instanceof SinglePointToMultiPointIntent) {
345 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
346 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints());
347 } else if (intent instanceof PathIntent) {
348 PathIntent pi = (PathIntent) intent;
349 print(" path=%s, cost=%d", pi.path().links(), pi.path().cost());
350 } else if (intent instanceof LinkCollectionIntent) {
351 LinkCollectionIntent li = (LinkCollectionIntent) intent;
352 print(" links=%s", li.links());
Michele Santuari4a338072014-11-05 18:38:55 +0100353 print(" egress=%s", li.egressPoints());
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700354 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700355
356 List<Intent> installable = service.getInstallableIntents(intent.id());
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700357 if (showInstallable && installable != null && !installable.isEmpty()) {
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700358 print(" installable=%s", installable);
359 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700360 }
361
362 // Produces JSON array of the specified intents.
363 private JsonNode json(IntentService service, Iterable<Intent> intents) {
364 ObjectMapper mapper = new ObjectMapper();
365 ArrayNode result = mapper.createArrayNode();
366 for (Intent intent : intents) {
367 result.add(json(service, mapper, intent));
368 }
369 return result;
370 }
371
372 private JsonNode json(IntentService service, ObjectMapper mapper, Intent intent) {
373 ObjectNode result = mapper.createObjectNode()
374 .put("id", intent.id().toString())
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700375 .put("type", intent.getClass().getSimpleName())
376 .put("appId", intent.appId().name());
377
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700378 IntentState state = service.getIntentState(intent.id());
379 if (state != null) {
380 result.put("state", state.toString());
381 }
382
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700383 if (intent.resources() != null && !intent.resources().isEmpty()) {
384 ArrayNode rnode = mapper.createArrayNode();
385 for (NetworkResource resource : intent.resources()) {
386 rnode.add(resource.toString());
387 }
388 result.set("resources", rnode);
389 }
390
391 if (intent instanceof ConnectivityIntent) {
392 ConnectivityIntent ci = (ConnectivityIntent) intent;
393 if (!ci.selector().criteria().isEmpty()) {
394 result.put("selector", ci.selector().criteria().toString());
395 }
396 if (!ci.treatment().instructions().isEmpty()) {
397 result.put("treatment", ci.treatment().instructions().toString());
398 }
399 }
400
401 if (intent instanceof PathIntent) {
402 PathIntent pi = (PathIntent) intent;
403 ArrayNode pnode = mapper.createArrayNode();
404 for (Link link : pi.path().links()) {
405 pnode.add(link.toString());
406 }
407 result.set("path", pnode);
408
409 } else if (intent instanceof PointToPointIntent) {
410 PointToPointIntent pi = (PointToPointIntent) intent;
411 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
412 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
413
414 } else if (intent instanceof MultiPointToSinglePointIntent) {
415 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
416 result.set("ingress", json(mapper, pi.ingressPoints()));
417 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
418
419 } else if (intent instanceof SinglePointToMultiPointIntent) {
420 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
421 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
422 result.set("egress", json(mapper, pi.egressPoints()));
423
424 } else if (intent instanceof LinkCollectionIntent) {
425 LinkCollectionIntent li = (LinkCollectionIntent) intent;
426 result.set("links", LinksListCommand.json(li.links()));
427 }
428
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700429 List<Intent> installable = service.getInstallableIntents(intent.id());
430 if (installable != null && !installable.isEmpty()) {
431 result.set("installable", json(service, installable));
432 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700433 return result;
434 }
435
436 private JsonNode json(ObjectMapper mapper, Set<ConnectPoint> connectPoints) {
437 ArrayNode result = mapper.createArrayNode();
438 for (ConnectPoint cp : connectPoints) {
439 result.add(LinksListCommand.json(mapper, cp));
440 }
441 return result;
442 }
tomf5c9d922014-10-03 15:22:03 -0700443}