blob: 7f84207b8172b1f766e77d476c70a8579e5d6037 [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());
79 print("id=%s, state=%s, type=%s, appId=%s",
80 intent.id(), state, intent.getClass().getSimpleName(),
81 intent.appId().name());
Thomas Vachuska10d4abc2014-10-21 12:47:26 -070082 printDetails(service, intent);
Thomas Vachuska6ce73042014-10-21 10:01:49 -070083 }
tomf5c9d922014-10-03 15:22:03 -070084 }
85 }
86
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080087 /**
88 * Internal local class to keep track of all intent summaries.
89 */
90 private class IntentSummaries {
91 private IntentSummary summaryAll;
92 private IntentSummary summaryConnectivity;
93 private IntentSummary summaryPointToPoint;
94 private IntentSummary summaryMultiPointToSinglePoint;
95 private IntentSummary summarySinglePointToMultiPoint;
96 private IntentSummary summaryPath;
97 private IntentSummary summaryLinkCollection;
98 private IntentSummary summaryUnknownType;
99
100 /**
101 * Initializes the internal state.
102 */
103 private void init() {
104 summaryAll = new IntentSummary("All");
105 summaryConnectivity = new IntentSummary("Connectivity");
106 summaryPointToPoint = new IntentSummary("PointToPoint");
107 summaryMultiPointToSinglePoint =
108 new IntentSummary("MultiPointToSinglePoint");
109 summarySinglePointToMultiPoint =
110 new IntentSummary("SinglePointToMultiPoint");
111 summaryPath = new IntentSummary("Path");
112 summaryLinkCollection = new IntentSummary("LinkCollection");
113 summaryUnknownType = new IntentSummary("UnknownType");
114 }
115
116 /**
117 * Collects summary of all intents.
118 *
119 * @param service the Intent Service to use
120 * @param intents the intents
121 */
122 private void collectIntentSummary(IntentService service,
123 Iterable<Intent> intents) {
124 init();
125
126 // Collect the summary for each intent type intents
127 for (Intent intent : intents) {
128 IntentState intentState = service.getIntentState(intent.id());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800129 if (intentState == null) {
130 continue;
131 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800132
133 // Update the summary for all Intents
134 summaryAll.update(intentState);
135
136 if (intent instanceof ConnectivityIntent) {
137 summaryConnectivity.update(intentState);
138 // NOTE: ConnectivityIntent is a base type Intent
139 // continue;
140 }
141 if (intent instanceof PointToPointIntent) {
142 summaryPointToPoint.update(intentState);
143 continue;
144 }
145 if (intent instanceof MultiPointToSinglePointIntent) {
146 summaryMultiPointToSinglePoint.update(intentState);
147 continue;
148 }
149 if (intent instanceof SinglePointToMultiPointIntent) {
150 summarySinglePointToMultiPoint.update(intentState);
151 continue;
152 }
153 if (intent instanceof PathIntent) {
154 summaryPath.update(intentState);
155 continue;
156 }
157 if (intent instanceof LinkCollectionIntent) {
158 summaryLinkCollection.update(intentState);
159 continue;
160 }
161
162 summaryUnknownType.update(intentState);
163 }
164 }
165
166 /**
167 * Gets JSON representation of all Intents summary.
168 *
169 * @return JSON representation of all Intents summary
170 */
171 ObjectNode json() {
172 ObjectMapper mapper = new ObjectMapper();
173 ObjectNode result = mapper.createObjectNode();
174 result.put("connectivity", summaryConnectivity.json(mapper));
175 result.put("pointToPoint", summaryPointToPoint.json(mapper));
176 result.put("multiPointToSinglePoint",
177 summaryMultiPointToSinglePoint.json(mapper));
178 result.put("singlePointToMultiPoint",
179 summarySinglePointToMultiPoint.json(mapper));
180 result.put("path", summaryPath.json(mapper));
181 result.put("linkCollection", summaryLinkCollection.json(mapper));
182 result.put("unknownType", summaryUnknownType.json(mapper));
183 result.put("all", summaryAll.json(mapper));
184 return result;
185 }
186
187 /**
188 * Prints summary of the intents.
189 */
190 private void printSummary() {
191 summaryConnectivity.printState();
192 summaryPointToPoint.printState();
193 summaryMultiPointToSinglePoint.printState();
194 summarySinglePointToMultiPoint.printState();
195 summaryPath.printState();
196 summaryLinkCollection.printState();
197 summaryUnknownType.printState();
198 summaryAll.printState();
199 }
200
201 /**
202 * Internal local class to keep track of a single type Intent summary.
203 */
204 private class IntentSummary {
205 private final String intentType;
206 private int total = 0;
207 private int submitted = 0;
208 private int compiling = 0;
209 private int installing = 0;
210 private int installed = 0;
211 private int recompiling = 0;
212 private int withdrawing = 0;
213 private int withdrawn = 0;
214 private int failed = 0;
215 private int unknownState = 0;
216
217 private static final String FORMAT_SUMMARY_LINE1 =
218 "%-23s total= %7d installed= %7d";
219 private static final String FORMAT_SUMMARY_LINE2 =
220 "%-23s withdrawn= %7d failed= %7d";
221 private static final String FORMAT_SUMMARY_LINE3 =
222 "%-23s submitted= %7d compiling= %7d";
223 private static final String FORMAT_SUMMARY_LINE4 =
224 "%-23s installing= %7d recompiling= %7d";
225 private static final String FORMAT_SUMMARY_LINE5 =
226 "%-23s withdrawing= %7d";
227 private static final String FORMAT_SUMMARY_LINE6 =
228 "%-23s unknownState= %7d";
229
230 /**
231 * Constructor.
232 *
233 * @param intentType the scring describing the Intent type
234 */
235 IntentSummary(String intentType) {
236 this.intentType = intentType;
237 }
238
239 /**
240 * Updates the Intent Summary.
241 *
242 * @param intentState the state of the Intent
243 */
244 void update(IntentState intentState) {
245 total++;
246 switch (intentState) {
247 case SUBMITTED:
248 submitted++;
249 break;
250 case COMPILING:
251 compiling++;
252 break;
253 case INSTALLING:
254 installing++;
255 break;
256 case INSTALLED:
257 installed++;
258 break;
259 case RECOMPILING:
260 recompiling++;
261 break;
262 case WITHDRAWING:
263 withdrawing++;
264 break;
265 case WITHDRAWN:
266 withdrawn++;
267 break;
268 case FAILED:
269 failed++;
270 break;
271 default:
272 unknownState++;
273 break;
274 }
275 }
276
277 /**
278 * Prints the Intent Summary.
279 */
280 void printState() {
281 print(FORMAT_SUMMARY_LINE1, intentType, total, installed);
282 print(FORMAT_SUMMARY_LINE2, intentType, withdrawn, failed);
283 print(FORMAT_SUMMARY_LINE3, intentType, submitted, compiling);
284 print(FORMAT_SUMMARY_LINE4, intentType, installing, recompiling);
285 print(FORMAT_SUMMARY_LINE5, intentType, withdrawing);
286 if (unknownState != 0) {
287 print(FORMAT_SUMMARY_LINE6, intentType, unknownState);
288 }
289 }
290
291 /**
292 * Gets the JSON representation of the Intent Summary.
293 *
294 * @return the JSON representation of the Intent Summary
295 */
296 JsonNode json(ObjectMapper mapper) {
297 ObjectNode result = mapper.createObjectNode()
298 .put("total", total)
299 .put("installed", installed)
300 .put("failed", failed)
301 .put("submitted", submitted)
302 .put("compiling", compiling)
303 .put("installing", installing)
304 .put("recompiling", recompiling)
305 .put("withdrawing", withdrawing)
306 .put("withdrawn", withdrawn)
307 .put("unknownState", unknownState);
308
309 return result;
310 }
311 }
312 }
313
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700314 private void printDetails(IntentService service, Intent intent) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700315 if (intent.resources() != null && !intent.resources().isEmpty()) {
316 print(" resources=%s", intent.resources());
317 }
318 if (intent instanceof ConnectivityIntent) {
319 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700320 if (!ci.selector().criteria().isEmpty()) {
321 print(" selector=%s", ci.selector().criteria());
322 }
323 if (!ci.treatment().instructions().isEmpty()) {
324 print(" treatment=%s", ci.treatment().instructions());
325 }
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800326 if (ci.constraints() != null && !ci.constraints().isEmpty()) {
327 print(" constraints=%s", ci.constraints());
328 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700329 }
330
331 if (intent instanceof PointToPointIntent) {
332 PointToPointIntent pi = (PointToPointIntent) intent;
333 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint());
334 } else if (intent instanceof MultiPointToSinglePointIntent) {
335 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
336 print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint());
337 } else if (intent instanceof SinglePointToMultiPointIntent) {
338 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
339 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints());
340 } else if (intent instanceof PathIntent) {
341 PathIntent pi = (PathIntent) intent;
342 print(" path=%s, cost=%d", pi.path().links(), pi.path().cost());
343 } else if (intent instanceof LinkCollectionIntent) {
344 LinkCollectionIntent li = (LinkCollectionIntent) intent;
345 print(" links=%s", li.links());
Michele Santuari4a338072014-11-05 18:38:55 +0100346 print(" egress=%s", li.egressPoints());
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700347 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700348
349 List<Intent> installable = service.getInstallableIntents(intent.id());
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700350 if (showInstallable && installable != null && !installable.isEmpty()) {
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700351 print(" installable=%s", installable);
352 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700353 }
354
355 // Produces JSON array of the specified intents.
356 private JsonNode json(IntentService service, Iterable<Intent> intents) {
357 ObjectMapper mapper = new ObjectMapper();
358 ArrayNode result = mapper.createArrayNode();
359 for (Intent intent : intents) {
360 result.add(json(service, mapper, intent));
361 }
362 return result;
363 }
364
365 private JsonNode json(IntentService service, ObjectMapper mapper, Intent intent) {
366 ObjectNode result = mapper.createObjectNode()
367 .put("id", intent.id().toString())
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700368 .put("type", intent.getClass().getSimpleName())
369 .put("appId", intent.appId().name());
370
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700371 IntentState state = service.getIntentState(intent.id());
372 if (state != null) {
373 result.put("state", state.toString());
374 }
375
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700376 if (intent.resources() != null && !intent.resources().isEmpty()) {
377 ArrayNode rnode = mapper.createArrayNode();
378 for (NetworkResource resource : intent.resources()) {
379 rnode.add(resource.toString());
380 }
381 result.set("resources", rnode);
382 }
383
384 if (intent instanceof ConnectivityIntent) {
385 ConnectivityIntent ci = (ConnectivityIntent) intent;
386 if (!ci.selector().criteria().isEmpty()) {
387 result.put("selector", ci.selector().criteria().toString());
388 }
389 if (!ci.treatment().instructions().isEmpty()) {
390 result.put("treatment", ci.treatment().instructions().toString());
391 }
392 }
393
394 if (intent instanceof PathIntent) {
395 PathIntent pi = (PathIntent) intent;
396 ArrayNode pnode = mapper.createArrayNode();
397 for (Link link : pi.path().links()) {
398 pnode.add(link.toString());
399 }
400 result.set("path", pnode);
401
402 } else if (intent instanceof PointToPointIntent) {
403 PointToPointIntent pi = (PointToPointIntent) intent;
404 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
405 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
406
407 } else if (intent instanceof MultiPointToSinglePointIntent) {
408 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
409 result.set("ingress", json(mapper, pi.ingressPoints()));
410 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
411
412 } else if (intent instanceof SinglePointToMultiPointIntent) {
413 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
414 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
415 result.set("egress", json(mapper, pi.egressPoints()));
416
417 } else if (intent instanceof LinkCollectionIntent) {
418 LinkCollectionIntent li = (LinkCollectionIntent) intent;
419 result.set("links", LinksListCommand.json(li.links()));
420 }
421
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700422 List<Intent> installable = service.getInstallableIntents(intent.id());
423 if (installable != null && !installable.isEmpty()) {
424 result.set("installable", json(service, installable));
425 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700426 return result;
427 }
428
429 private JsonNode json(ObjectMapper mapper, Set<ConnectPoint> connectPoints) {
430 ArrayNode result = mapper.createArrayNode();
431 for (ConnectPoint cp : connectPoints) {
432 result.add(LinksListCommand.json(mapper, cp));
433 }
434 return result;
435 }
tomf5c9d922014-10-03 15:22:03 -0700436}