blob: 0f2464776c2ba89e7f9f5fc3599eddaee48dca90 [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());
129
130 // Update the summary for all Intents
131 summaryAll.update(intentState);
132
133 if (intent instanceof ConnectivityIntent) {
134 summaryConnectivity.update(intentState);
135 // NOTE: ConnectivityIntent is a base type Intent
136 // continue;
137 }
138 if (intent instanceof PointToPointIntent) {
139 summaryPointToPoint.update(intentState);
140 continue;
141 }
142 if (intent instanceof MultiPointToSinglePointIntent) {
143 summaryMultiPointToSinglePoint.update(intentState);
144 continue;
145 }
146 if (intent instanceof SinglePointToMultiPointIntent) {
147 summarySinglePointToMultiPoint.update(intentState);
148 continue;
149 }
150 if (intent instanceof PathIntent) {
151 summaryPath.update(intentState);
152 continue;
153 }
154 if (intent instanceof LinkCollectionIntent) {
155 summaryLinkCollection.update(intentState);
156 continue;
157 }
158
159 summaryUnknownType.update(intentState);
160 }
161 }
162
163 /**
164 * Gets JSON representation of all Intents summary.
165 *
166 * @return JSON representation of all Intents summary
167 */
168 ObjectNode json() {
169 ObjectMapper mapper = new ObjectMapper();
170 ObjectNode result = mapper.createObjectNode();
171 result.put("connectivity", summaryConnectivity.json(mapper));
172 result.put("pointToPoint", summaryPointToPoint.json(mapper));
173 result.put("multiPointToSinglePoint",
174 summaryMultiPointToSinglePoint.json(mapper));
175 result.put("singlePointToMultiPoint",
176 summarySinglePointToMultiPoint.json(mapper));
177 result.put("path", summaryPath.json(mapper));
178 result.put("linkCollection", summaryLinkCollection.json(mapper));
179 result.put("unknownType", summaryUnknownType.json(mapper));
180 result.put("all", summaryAll.json(mapper));
181 return result;
182 }
183
184 /**
185 * Prints summary of the intents.
186 */
187 private void printSummary() {
188 summaryConnectivity.printState();
189 summaryPointToPoint.printState();
190 summaryMultiPointToSinglePoint.printState();
191 summarySinglePointToMultiPoint.printState();
192 summaryPath.printState();
193 summaryLinkCollection.printState();
194 summaryUnknownType.printState();
195 summaryAll.printState();
196 }
197
198 /**
199 * Internal local class to keep track of a single type Intent summary.
200 */
201 private class IntentSummary {
202 private final String intentType;
203 private int total = 0;
204 private int submitted = 0;
205 private int compiling = 0;
206 private int installing = 0;
207 private int installed = 0;
208 private int recompiling = 0;
209 private int withdrawing = 0;
210 private int withdrawn = 0;
211 private int failed = 0;
212 private int unknownState = 0;
213
214 private static final String FORMAT_SUMMARY_LINE1 =
215 "%-23s total= %7d installed= %7d";
216 private static final String FORMAT_SUMMARY_LINE2 =
217 "%-23s withdrawn= %7d failed= %7d";
218 private static final String FORMAT_SUMMARY_LINE3 =
219 "%-23s submitted= %7d compiling= %7d";
220 private static final String FORMAT_SUMMARY_LINE4 =
221 "%-23s installing= %7d recompiling= %7d";
222 private static final String FORMAT_SUMMARY_LINE5 =
223 "%-23s withdrawing= %7d";
224 private static final String FORMAT_SUMMARY_LINE6 =
225 "%-23s unknownState= %7d";
226
227 /**
228 * Constructor.
229 *
230 * @param intentType the scring describing the Intent type
231 */
232 IntentSummary(String intentType) {
233 this.intentType = intentType;
234 }
235
236 /**
237 * Updates the Intent Summary.
238 *
239 * @param intentState the state of the Intent
240 */
241 void update(IntentState intentState) {
242 total++;
243 switch (intentState) {
244 case SUBMITTED:
245 submitted++;
246 break;
247 case COMPILING:
248 compiling++;
249 break;
250 case INSTALLING:
251 installing++;
252 break;
253 case INSTALLED:
254 installed++;
255 break;
256 case RECOMPILING:
257 recompiling++;
258 break;
259 case WITHDRAWING:
260 withdrawing++;
261 break;
262 case WITHDRAWN:
263 withdrawn++;
264 break;
265 case FAILED:
266 failed++;
267 break;
268 default:
269 unknownState++;
270 break;
271 }
272 }
273
274 /**
275 * Prints the Intent Summary.
276 */
277 void printState() {
278 print(FORMAT_SUMMARY_LINE1, intentType, total, installed);
279 print(FORMAT_SUMMARY_LINE2, intentType, withdrawn, failed);
280 print(FORMAT_SUMMARY_LINE3, intentType, submitted, compiling);
281 print(FORMAT_SUMMARY_LINE4, intentType, installing, recompiling);
282 print(FORMAT_SUMMARY_LINE5, intentType, withdrawing);
283 if (unknownState != 0) {
284 print(FORMAT_SUMMARY_LINE6, intentType, unknownState);
285 }
286 }
287
288 /**
289 * Gets the JSON representation of the Intent Summary.
290 *
291 * @return the JSON representation of the Intent Summary
292 */
293 JsonNode json(ObjectMapper mapper) {
294 ObjectNode result = mapper.createObjectNode()
295 .put("total", total)
296 .put("installed", installed)
297 .put("failed", failed)
298 .put("submitted", submitted)
299 .put("compiling", compiling)
300 .put("installing", installing)
301 .put("recompiling", recompiling)
302 .put("withdrawing", withdrawing)
303 .put("withdrawn", withdrawn)
304 .put("unknownState", unknownState);
305
306 return result;
307 }
308 }
309 }
310
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700311 private void printDetails(IntentService service, Intent intent) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700312 if (intent.resources() != null && !intent.resources().isEmpty()) {
313 print(" resources=%s", intent.resources());
314 }
315 if (intent instanceof ConnectivityIntent) {
316 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700317 if (!ci.selector().criteria().isEmpty()) {
318 print(" selector=%s", ci.selector().criteria());
319 }
320 if (!ci.treatment().instructions().isEmpty()) {
321 print(" treatment=%s", ci.treatment().instructions());
322 }
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800323 if (ci.constraints() != null && !ci.constraints().isEmpty()) {
324 print(" constraints=%s", ci.constraints());
325 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700326 }
327
328 if (intent instanceof PointToPointIntent) {
329 PointToPointIntent pi = (PointToPointIntent) intent;
330 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint());
331 } else if (intent instanceof MultiPointToSinglePointIntent) {
332 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
333 print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint());
334 } else if (intent instanceof SinglePointToMultiPointIntent) {
335 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
336 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints());
337 } else if (intent instanceof PathIntent) {
338 PathIntent pi = (PathIntent) intent;
339 print(" path=%s, cost=%d", pi.path().links(), pi.path().cost());
340 } else if (intent instanceof LinkCollectionIntent) {
341 LinkCollectionIntent li = (LinkCollectionIntent) intent;
342 print(" links=%s", li.links());
Michele Santuari4a338072014-11-05 18:38:55 +0100343 print(" egress=%s", li.egressPoints());
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700344 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700345
346 List<Intent> installable = service.getInstallableIntents(intent.id());
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700347 if (showInstallable && installable != null && !installable.isEmpty()) {
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700348 print(" installable=%s", installable);
349 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700350 }
351
352 // Produces JSON array of the specified intents.
353 private JsonNode json(IntentService service, Iterable<Intent> intents) {
354 ObjectMapper mapper = new ObjectMapper();
355 ArrayNode result = mapper.createArrayNode();
356 for (Intent intent : intents) {
357 result.add(json(service, mapper, intent));
358 }
359 return result;
360 }
361
362 private JsonNode json(IntentService service, ObjectMapper mapper, Intent intent) {
363 ObjectNode result = mapper.createObjectNode()
364 .put("id", intent.id().toString())
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700365 .put("type", intent.getClass().getSimpleName())
366 .put("appId", intent.appId().name());
367
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700368 IntentState state = service.getIntentState(intent.id());
369 if (state != null) {
370 result.put("state", state.toString());
371 }
372
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700373 if (intent.resources() != null && !intent.resources().isEmpty()) {
374 ArrayNode rnode = mapper.createArrayNode();
375 for (NetworkResource resource : intent.resources()) {
376 rnode.add(resource.toString());
377 }
378 result.set("resources", rnode);
379 }
380
381 if (intent instanceof ConnectivityIntent) {
382 ConnectivityIntent ci = (ConnectivityIntent) intent;
383 if (!ci.selector().criteria().isEmpty()) {
384 result.put("selector", ci.selector().criteria().toString());
385 }
386 if (!ci.treatment().instructions().isEmpty()) {
387 result.put("treatment", ci.treatment().instructions().toString());
388 }
389 }
390
391 if (intent instanceof PathIntent) {
392 PathIntent pi = (PathIntent) intent;
393 ArrayNode pnode = mapper.createArrayNode();
394 for (Link link : pi.path().links()) {
395 pnode.add(link.toString());
396 }
397 result.set("path", pnode);
398
399 } else if (intent instanceof PointToPointIntent) {
400 PointToPointIntent pi = (PointToPointIntent) intent;
401 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
402 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
403
404 } else if (intent instanceof MultiPointToSinglePointIntent) {
405 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
406 result.set("ingress", json(mapper, pi.ingressPoints()));
407 result.set("egress", LinksListCommand.json(mapper, pi.egressPoint()));
408
409 } else if (intent instanceof SinglePointToMultiPointIntent) {
410 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
411 result.set("ingress", LinksListCommand.json(mapper, pi.ingressPoint()));
412 result.set("egress", json(mapper, pi.egressPoints()));
413
414 } else if (intent instanceof LinkCollectionIntent) {
415 LinkCollectionIntent li = (LinkCollectionIntent) intent;
416 result.set("links", LinksListCommand.json(li.links()));
417 }
418
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700419 List<Intent> installable = service.getInstallableIntents(intent.id());
420 if (installable != null && !installable.isEmpty()) {
421 result.set("installable", json(service, installable));
422 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700423 return result;
424 }
425
426 private JsonNode json(ObjectMapper mapper, Set<ConnectPoint> connectPoints) {
427 ArrayNode result = mapper.createArrayNode();
428 for (ConnectPoint cp : connectPoints) {
429 result.add(LinksListCommand.json(mapper, cp));
430 }
431 return result;
432 }
tomf5c9d922014-10-03 15:22:03 -0700433}