blob: 911b5f8ff92b0325ebb9a786f7bf122931ed6b67 [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 */
tomf5c9d922014-10-03 15:22:03 -070016package org.onlab.onos.cli.net;
17
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;
tomf5c9d922014-10-03 15:22:03 -070024import org.onlab.onos.cli.AbstractShellCommand;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070025import org.onlab.onos.net.ConnectPoint;
26import org.onlab.onos.net.Link;
27import org.onlab.onos.net.NetworkResource;
28import org.onlab.onos.net.intent.ConnectivityIntent;
tomf5c9d922014-10-03 15:22:03 -070029import org.onlab.onos.net.intent.Intent;
30import org.onlab.onos.net.intent.IntentService;
Brian O'Connora4cab072014-10-03 18:46:39 -070031import org.onlab.onos.net.intent.IntentState;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070032import org.onlab.onos.net.intent.LinkCollectionIntent;
33import org.onlab.onos.net.intent.MultiPointToSinglePointIntent;
34import org.onlab.onos.net.intent.PathIntent;
35import org.onlab.onos.net.intent.PointToPointIntent;
36import org.onlab.onos.net.intent.SinglePointToMultiPointIntent;
37
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;
209 private int submitted = 0;
210 private int compiling = 0;
211 private int installing = 0;
212 private int installed = 0;
213 private int recompiling = 0;
214 private int withdrawing = 0;
215 private int withdrawn = 0;
216 private int failed = 0;
217 private int unknownState = 0;
218
219 private static final String FORMAT_SUMMARY_LINE1 =
220 "%-23s total= %7d installed= %7d";
221 private static final String FORMAT_SUMMARY_LINE2 =
222 "%-23s withdrawn= %7d failed= %7d";
223 private static final String FORMAT_SUMMARY_LINE3 =
224 "%-23s submitted= %7d compiling= %7d";
225 private static final String FORMAT_SUMMARY_LINE4 =
226 "%-23s installing= %7d recompiling= %7d";
227 private static final String FORMAT_SUMMARY_LINE5 =
228 "%-23s withdrawing= %7d";
229 private static final String FORMAT_SUMMARY_LINE6 =
230 "%-23s unknownState= %7d";
231
232 /**
233 * Constructor.
234 *
235 * @param intentType the scring describing the Intent type
236 */
237 IntentSummary(String intentType) {
238 this.intentType = intentType;
239 }
240
241 /**
242 * Updates the Intent Summary.
243 *
244 * @param intentState the state of the Intent
245 */
246 void update(IntentState intentState) {
247 total++;
248 switch (intentState) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800249 case INSTALL_REQ:
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800250 submitted++;
251 break;
252 case COMPILING:
253 compiling++;
254 break;
255 case INSTALLING:
256 installing++;
257 break;
258 case INSTALLED:
259 installed++;
260 break;
261 case RECOMPILING:
262 recompiling++;
263 break;
264 case WITHDRAWING:
265 withdrawing++;
266 break;
267 case WITHDRAWN:
268 withdrawn++;
269 break;
270 case FAILED:
271 failed++;
272 break;
273 default:
274 unknownState++;
275 break;
276 }
277 }
278
279 /**
280 * Prints the Intent Summary.
281 */
282 void printState() {
283 print(FORMAT_SUMMARY_LINE1, intentType, total, installed);
284 print(FORMAT_SUMMARY_LINE2, intentType, withdrawn, failed);
285 print(FORMAT_SUMMARY_LINE3, intentType, submitted, compiling);
286 print(FORMAT_SUMMARY_LINE4, intentType, installing, recompiling);
287 print(FORMAT_SUMMARY_LINE5, intentType, withdrawing);
288 if (unknownState != 0) {
289 print(FORMAT_SUMMARY_LINE6, intentType, unknownState);
290 }
291 }
292
293 /**
294 * Gets the JSON representation of the Intent Summary.
295 *
296 * @return the JSON representation of the Intent Summary
297 */
298 JsonNode json(ObjectMapper mapper) {
299 ObjectNode result = mapper.createObjectNode()
300 .put("total", total)
301 .put("installed", installed)
302 .put("failed", failed)
303 .put("submitted", submitted)
304 .put("compiling", compiling)
305 .put("installing", installing)
306 .put("recompiling", recompiling)
307 .put("withdrawing", withdrawing)
308 .put("withdrawn", withdrawn)
309 .put("unknownState", unknownState);
310
311 return result;
312 }
313 }
314 }
315
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700316 private void printDetails(IntentService service, Intent intent) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700317 if (intent.resources() != null && !intent.resources().isEmpty()) {
318 print(" resources=%s", intent.resources());
319 }
320 if (intent instanceof ConnectivityIntent) {
321 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700322 if (!ci.selector().criteria().isEmpty()) {
323 print(" selector=%s", ci.selector().criteria());
324 }
325 if (!ci.treatment().instructions().isEmpty()) {
326 print(" treatment=%s", ci.treatment().instructions());
327 }
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800328 if (ci.constraints() != null && !ci.constraints().isEmpty()) {
329 print(" constraints=%s", ci.constraints());
330 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700331 }
332
333 if (intent instanceof PointToPointIntent) {
334 PointToPointIntent pi = (PointToPointIntent) intent;
335 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint());
336 } else if (intent instanceof MultiPointToSinglePointIntent) {
337 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
338 print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint());
339 } else if (intent instanceof SinglePointToMultiPointIntent) {
340 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
341 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints());
342 } else if (intent instanceof PathIntent) {
343 PathIntent pi = (PathIntent) intent;
344 print(" path=%s, cost=%d", pi.path().links(), pi.path().cost());
345 } else if (intent instanceof LinkCollectionIntent) {
346 LinkCollectionIntent li = (LinkCollectionIntent) intent;
347 print(" links=%s", li.links());
Michele Santuari4a338072014-11-05 18:38:55 +0100348 print(" egress=%s", li.egressPoints());
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700349 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700350
351 List<Intent> installable = service.getInstallableIntents(intent.id());
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700352 if (showInstallable && installable != null && !installable.isEmpty()) {
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700353 print(" installable=%s", installable);
354 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700355 }
356
357 // Produces JSON array of the specified intents.
358 private JsonNode json(IntentService service, Iterable<Intent> intents) {
359 ObjectMapper mapper = new ObjectMapper();
360 ArrayNode result = mapper.createArrayNode();
361 for (Intent intent : intents) {
362 result.add(json(service, mapper, intent));
363 }
364 return result;
365 }
366
367 private JsonNode json(IntentService service, ObjectMapper mapper, Intent intent) {
368 ObjectNode result = mapper.createObjectNode()
369 .put("id", intent.id().toString())
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700370 .put("type", intent.getClass().getSimpleName())
371 .put("appId", intent.appId().name());
372
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700373 IntentState state = service.getIntentState(intent.id());
374 if (state != null) {
375 result.put("state", state.toString());
376 }
377
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700378 if (intent.resources() != null && !intent.resources().isEmpty()) {
379 ArrayNode rnode = mapper.createArrayNode();
380 for (NetworkResource resource : intent.resources()) {
381 rnode.add(resource.toString());
382 }
383 result.set("resources", rnode);
384 }
385
386 if (intent instanceof ConnectivityIntent) {
387 ConnectivityIntent ci = (ConnectivityIntent) intent;
388 if (!ci.selector().criteria().isEmpty()) {
389 result.put("selector", ci.selector().criteria().toString());
390 }
391 if (!ci.treatment().instructions().isEmpty()) {
392 result.put("treatment", ci.treatment().instructions().toString());
393 }
394 }
395
396 if (intent instanceof PathIntent) {
397 PathIntent pi = (PathIntent) intent;
398 ArrayNode pnode = mapper.createArrayNode();
399 for (Link link : pi.path().links()) {
400 pnode.add(link.toString());
401 }
402 result.set("path", pnode);
403
404 } else if (intent instanceof PointToPointIntent) {
405 PointToPointIntent pi = (PointToPointIntent) intent;
406 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
407 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
408
409 } else if (intent instanceof MultiPointToSinglePointIntent) {
410 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
411 result.set("ingress", json(mapper, pi.ingressPoints()));
412 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
413
414 } else if (intent instanceof SinglePointToMultiPointIntent) {
415 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
416 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
417 result.set("egress", json(mapper, pi.egressPoints()));
418
419 } else if (intent instanceof LinkCollectionIntent) {
420 LinkCollectionIntent li = (LinkCollectionIntent) intent;
421 result.set("links", LinksListCommand.json(li.links()));
422 }
423
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700424 List<Intent> installable = service.getInstallableIntents(intent.id());
425 if (installable != null && !installable.isEmpty()) {
426 result.set("installable", json(service, installable));
427 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700428 return result;
429 }
430
431 private JsonNode json(ObjectMapper mapper, Set<ConnectPoint> connectPoints) {
432 ArrayNode result = mapper.createArrayNode();
433 for (ConnectPoint cp : connectPoints) {
434 result.add(LinksListCommand.json(mapper, cp));
435 }
436 return result;
437 }
tomf5c9d922014-10-03 15:22:03 -0700438}