blob: 55b9ec9ca89c486d3c810876fa3affcf18c9240f [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Ray Milkey34c95902015-04-15 09:47:53 -07002 * Copyright 2014-2015 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
Ray Milkey3078fc02015-05-06 16:14:14 -070018import java.util.List;
19
tomf5c9d922014-10-03 15:22:03 -070020import org.apache.karaf.shell.commands.Command;
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070021import org.apache.karaf.shell.commands.Option;
Brian O'Connorabafb502014-12-02 22:26:20 -080022import org.onosproject.cli.AbstractShellCommand;
Brian O'Connorabafb502014-12-02 22:26:20 -080023import org.onosproject.net.intent.ConnectivityIntent;
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -080024import org.onosproject.net.intent.HostToHostIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080025import org.onosproject.net.intent.Intent;
26import org.onosproject.net.intent.IntentService;
27import org.onosproject.net.intent.IntentState;
28import org.onosproject.net.intent.LinkCollectionIntent;
29import org.onosproject.net.intent.MultiPointToSinglePointIntent;
30import org.onosproject.net.intent.PathIntent;
31import org.onosproject.net.intent.PointToPointIntent;
32import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070033
Ray Milkey3078fc02015-05-06 16:14:14 -070034import com.fasterxml.jackson.databind.JsonNode;
35import com.fasterxml.jackson.databind.ObjectMapper;
36import com.fasterxml.jackson.databind.node.ArrayNode;
37import com.fasterxml.jackson.databind.node.ObjectNode;
tomf5c9d922014-10-03 15:22:03 -070038
39/**
40 * Lists the inventory of intents and their states.
41 */
42@Command(scope = "onos", name = "intents",
43 description = "Lists the inventory of intents and their states")
44public class IntentsListCommand extends AbstractShellCommand {
45
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080046 @Option(name = "-i", aliases = "--installable",
47 description = "Output Installable Intents",
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070048 required = false, multiValued = false)
49 private boolean showInstallable = false;
50
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080051 @Option(name = "-s", aliases = "--summary",
52 description = "Intents summary",
53 required = false, multiValued = false)
54 private boolean intentsSummary = false;
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070055
Jonathan Hart34f1e382015-02-24 16:52:23 -080056 @Option(name = "-p", aliases = "--pending",
57 description = "Show inforamtion about pending intents",
58 required = false, multiValued = false)
59 private boolean pending = false;
60
tomf5c9d922014-10-03 15:22:03 -070061 @Override
62 protected void execute() {
63 IntentService service = get(IntentService.class);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080064
65 if (intentsSummary) {
66 IntentSummaries intentSummaries = new IntentSummaries();
67 intentSummaries.collectIntentSummary(service,
68 service.getIntents());
69 if (outputJson()) {
70 print("%s", intentSummaries.json());
71 } else {
72 intentSummaries.printSummary();
73 }
74 return;
Jonathan Hart34f1e382015-02-24 16:52:23 -080075 } else if (pending) {
Ray Milkey740c8a32015-03-17 13:41:03 -070076 if (outputJson()) {
77 print("%s", json(service, service.getPending()));
78 } else {
79 service.getPending().forEach(intent ->
80 print("id=%s, key=%s, type=%s, appId=%s",
81 intent.id(), intent.key(),
82 intent.getClass().getSimpleName(),
83 intent.appId().name())
84 );
85 }
Jonathan Hart34f1e382015-02-24 16:52:23 -080086 return;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080087 }
88
Thomas Vachuska6ce73042014-10-21 10:01:49 -070089 if (outputJson()) {
90 print("%s", json(service, service.getIntents()));
91 } else {
92 for (Intent intent : service.getIntents()) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -080093 IntentState state = service.getIntentState(intent.key());
Thomas Vachuska3ea690b2014-11-29 12:39:03 -080094 if (state != null) {
Jonathan Hart189f9bf2015-02-11 19:24:28 -080095 print("id=%s, state=%s, key=%s, type=%s, appId=%s",
96 intent.id(), state, intent.key(),
97 intent.getClass().getSimpleName(),
Thomas Vachuska3ea690b2014-11-29 12:39:03 -080098 intent.appId().name());
99 printDetails(service, intent);
100 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700101 }
tomf5c9d922014-10-03 15:22:03 -0700102 }
103 }
104
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800105 /**
106 * Internal local class to keep track of all intent summaries.
107 */
108 private class IntentSummaries {
109 private IntentSummary summaryAll;
110 private IntentSummary summaryConnectivity;
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800111 private IntentSummary summaryHostToHost;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800112 private IntentSummary summaryPointToPoint;
113 private IntentSummary summaryMultiPointToSinglePoint;
114 private IntentSummary summarySinglePointToMultiPoint;
115 private IntentSummary summaryPath;
116 private IntentSummary summaryLinkCollection;
117 private IntentSummary summaryUnknownType;
118
119 /**
120 * Initializes the internal state.
121 */
122 private void init() {
123 summaryAll = new IntentSummary("All");
124 summaryConnectivity = new IntentSummary("Connectivity");
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800125 summaryHostToHost = new IntentSummary("HostToHost");
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800126 summaryPointToPoint = new IntentSummary("PointToPoint");
127 summaryMultiPointToSinglePoint =
128 new IntentSummary("MultiPointToSinglePoint");
129 summarySinglePointToMultiPoint =
130 new IntentSummary("SinglePointToMultiPoint");
131 summaryPath = new IntentSummary("Path");
132 summaryLinkCollection = new IntentSummary("LinkCollection");
133 summaryUnknownType = new IntentSummary("UnknownType");
134 }
135
136 /**
137 * Collects summary of all intents.
138 *
139 * @param service the Intent Service to use
140 * @param intents the intents
141 */
142 private void collectIntentSummary(IntentService service,
143 Iterable<Intent> intents) {
144 init();
145
146 // Collect the summary for each intent type intents
147 for (Intent intent : intents) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800148 IntentState intentState = service.getIntentState(intent.key());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800149 if (intentState == null) {
150 continue;
151 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800152
153 // Update the summary for all Intents
154 summaryAll.update(intentState);
155
156 if (intent instanceof ConnectivityIntent) {
157 summaryConnectivity.update(intentState);
158 // NOTE: ConnectivityIntent is a base type Intent
159 // continue;
160 }
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800161 if (intent instanceof HostToHostIntent) {
162 summaryHostToHost.update(intentState);
163 continue;
164 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800165 if (intent instanceof PointToPointIntent) {
166 summaryPointToPoint.update(intentState);
167 continue;
168 }
169 if (intent instanceof MultiPointToSinglePointIntent) {
170 summaryMultiPointToSinglePoint.update(intentState);
171 continue;
172 }
173 if (intent instanceof SinglePointToMultiPointIntent) {
174 summarySinglePointToMultiPoint.update(intentState);
175 continue;
176 }
177 if (intent instanceof PathIntent) {
178 summaryPath.update(intentState);
179 continue;
180 }
181 if (intent instanceof LinkCollectionIntent) {
182 summaryLinkCollection.update(intentState);
183 continue;
184 }
185
186 summaryUnknownType.update(intentState);
187 }
188 }
189
190 /**
191 * Gets JSON representation of all Intents summary.
192 *
193 * @return JSON representation of all Intents summary
194 */
195 ObjectNode json() {
196 ObjectMapper mapper = new ObjectMapper();
197 ObjectNode result = mapper.createObjectNode();
Ray Milkey9d810f62015-02-13 11:20:58 -0800198 result.set("connectivity", summaryConnectivity.json(mapper));
199 result.set("hostToHost", summaryHostToHost.json(mapper));
200 result.set("pointToPoint", summaryPointToPoint.json(mapper));
201 result.set("multiPointToSinglePoint",
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800202 summaryMultiPointToSinglePoint.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800203 result.set("singlePointToMultiPoint",
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800204 summarySinglePointToMultiPoint.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800205 result.set("path", summaryPath.json(mapper));
206 result.set("linkCollection", summaryLinkCollection.json(mapper));
207 result.set("unknownType", summaryUnknownType.json(mapper));
208 result.set("all", summaryAll.json(mapper));
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800209 return result;
210 }
211
212 /**
213 * Prints summary of the intents.
214 */
215 private void printSummary() {
216 summaryConnectivity.printState();
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800217 summaryHostToHost.printState();
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800218 summaryPointToPoint.printState();
219 summaryMultiPointToSinglePoint.printState();
220 summarySinglePointToMultiPoint.printState();
221 summaryPath.printState();
222 summaryLinkCollection.printState();
223 summaryUnknownType.printState();
224 summaryAll.printState();
225 }
226
227 /**
228 * Internal local class to keep track of a single type Intent summary.
229 */
230 private class IntentSummary {
231 private final String intentType;
232 private int total = 0;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800233 private int installReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800234 private int compiling = 0;
235 private int installing = 0;
236 private int installed = 0;
237 private int recompiling = 0;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800238 private int withdrawReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800239 private int withdrawing = 0;
240 private int withdrawn = 0;
241 private int failed = 0;
242 private int unknownState = 0;
243
244 private static final String FORMAT_SUMMARY_LINE1 =
245 "%-23s total= %7d installed= %7d";
246 private static final String FORMAT_SUMMARY_LINE2 =
247 "%-23s withdrawn= %7d failed= %7d";
248 private static final String FORMAT_SUMMARY_LINE3 =
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800249 "%-23s installReq= %7d compiling= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800250 private static final String FORMAT_SUMMARY_LINE4 =
251 "%-23s installing= %7d recompiling= %7d";
252 private static final String FORMAT_SUMMARY_LINE5 =
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800253 "%-23s withdrawReq= %7d withdrawing= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800254 private static final String FORMAT_SUMMARY_LINE6 =
255 "%-23s unknownState= %7d";
256
257 /**
258 * Constructor.
259 *
260 * @param intentType the scring describing the Intent type
261 */
262 IntentSummary(String intentType) {
263 this.intentType = intentType;
264 }
265
266 /**
267 * Updates the Intent Summary.
268 *
269 * @param intentState the state of the Intent
270 */
271 void update(IntentState intentState) {
272 total++;
273 switch (intentState) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800274 case INSTALL_REQ:
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800275 installReq++;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800276 break;
277 case COMPILING:
278 compiling++;
279 break;
280 case INSTALLING:
281 installing++;
282 break;
283 case INSTALLED:
284 installed++;
285 break;
286 case RECOMPILING:
287 recompiling++;
288 break;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800289 case WITHDRAW_REQ:
290 withdrawReq++;
291 break;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800292 case WITHDRAWING:
293 withdrawing++;
294 break;
295 case WITHDRAWN:
296 withdrawn++;
297 break;
298 case FAILED:
299 failed++;
300 break;
301 default:
302 unknownState++;
303 break;
304 }
305 }
306
307 /**
308 * Prints the Intent Summary.
309 */
310 void printState() {
311 print(FORMAT_SUMMARY_LINE1, intentType, total, installed);
312 print(FORMAT_SUMMARY_LINE2, intentType, withdrawn, failed);
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800313 print(FORMAT_SUMMARY_LINE3, intentType, installReq, compiling);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800314 print(FORMAT_SUMMARY_LINE4, intentType, installing, recompiling);
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800315 print(FORMAT_SUMMARY_LINE5, intentType, withdrawReq, withdrawing);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800316 if (unknownState != 0) {
317 print(FORMAT_SUMMARY_LINE6, intentType, unknownState);
318 }
319 }
320
321 /**
322 * Gets the JSON representation of the Intent Summary.
323 *
324 * @return the JSON representation of the Intent Summary
325 */
326 JsonNode json(ObjectMapper mapper) {
327 ObjectNode result = mapper.createObjectNode()
328 .put("total", total)
329 .put("installed", installed)
330 .put("failed", failed)
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800331 .put("installReq", installReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800332 .put("compiling", compiling)
333 .put("installing", installing)
334 .put("recompiling", recompiling)
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800335 .put("withdrawReq", withdrawReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800336 .put("withdrawing", withdrawing)
337 .put("withdrawn", withdrawn)
338 .put("unknownState", unknownState);
339
340 return result;
341 }
342 }
343 }
344
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700345 private void printDetails(IntentService service, Intent intent) {
Sho SHIMIZUd7d18002015-01-21 14:37:14 -0800346 if (!intent.resources().isEmpty()) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700347 print(" resources=%s", intent.resources());
348 }
349 if (intent instanceof ConnectivityIntent) {
350 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700351 if (!ci.selector().criteria().isEmpty()) {
352 print(" selector=%s", ci.selector().criteria());
353 }
Ray Milkey42507352015-03-20 15:16:10 -0700354 if (!ci.treatment().allInstructions().isEmpty()) {
355 print(" treatment=%s", ci.treatment().allInstructions());
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700356 }
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800357 if (ci.constraints() != null && !ci.constraints().isEmpty()) {
358 print(" constraints=%s", ci.constraints());
359 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700360 }
361
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800362 if (intent instanceof HostToHostIntent) {
363 HostToHostIntent pi = (HostToHostIntent) intent;
364 print(" host1=%s, host2=%s", pi.one(), pi.two());
365 } else if (intent instanceof PointToPointIntent) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700366 PointToPointIntent pi = (PointToPointIntent) intent;
367 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint());
368 } else if (intent instanceof MultiPointToSinglePointIntent) {
369 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
370 print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint());
371 } else if (intent instanceof SinglePointToMultiPointIntent) {
372 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
373 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints());
374 } else if (intent instanceof PathIntent) {
375 PathIntent pi = (PathIntent) intent;
376 print(" path=%s, cost=%d", pi.path().links(), pi.path().cost());
377 } else if (intent instanceof LinkCollectionIntent) {
378 LinkCollectionIntent li = (LinkCollectionIntent) intent;
379 print(" links=%s", li.links());
Michele Santuari4a338072014-11-05 18:38:55 +0100380 print(" egress=%s", li.egressPoints());
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700381 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700382
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800383 List<Intent> installable = service.getInstallableIntents(intent.key());
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700384 if (showInstallable && installable != null && !installable.isEmpty()) {
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700385 print(" installable=%s", installable);
386 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700387 }
388
389 // Produces JSON array of the specified intents.
390 private JsonNode json(IntentService service, Iterable<Intent> intents) {
391 ObjectMapper mapper = new ObjectMapper();
392 ArrayNode result = mapper.createArrayNode();
Ray Milkey3078fc02015-05-06 16:14:14 -0700393
394 intents.forEach(intent -> result.add(jsonForEntity(intent, Intent.class)));
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700395 return result;
396 }
397
tomf5c9d922014-10-03 15:22:03 -0700398}