blob: 609c5f97242615e4ab6f95f44d8fb9d1df14acf3 [file] [log] [blame]
Thomas Vachuska7d693f52014-10-21 19:17:57 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present 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;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020030import org.onosproject.net.intent.OpticalCircuitIntent;
31import org.onosproject.net.intent.OpticalConnectivityIntent;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +020032import org.onosproject.net.intent.OpticalOduIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080033import org.onosproject.net.intent.PathIntent;
34import org.onosproject.net.intent.PointToPointIntent;
35import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070036
Ray Milkey3078fc02015-05-06 16:14:14 -070037import com.fasterxml.jackson.databind.JsonNode;
38import com.fasterxml.jackson.databind.ObjectMapper;
39import com.fasterxml.jackson.databind.node.ArrayNode;
40import com.fasterxml.jackson.databind.node.ObjectNode;
tomf5c9d922014-10-03 15:22:03 -070041
42/**
43 * Lists the inventory of intents and their states.
44 */
45@Command(scope = "onos", name = "intents",
46 description = "Lists the inventory of intents and their states")
47public class IntentsListCommand extends AbstractShellCommand {
48
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080049 @Option(name = "-i", aliases = "--installable",
50 description = "Output Installable Intents",
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070051 required = false, multiValued = false)
52 private boolean showInstallable = false;
53
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080054 @Option(name = "-s", aliases = "--summary",
55 description = "Intents summary",
56 required = false, multiValued = false)
57 private boolean intentsSummary = false;
Brian O'Connorfe0f4b12014-10-30 21:19:02 -070058
Jonathan Hart34f1e382015-02-24 16:52:23 -080059 @Option(name = "-p", aliases = "--pending",
60 description = "Show inforamtion about pending intents",
61 required = false, multiValued = false)
62 private boolean pending = false;
63
tomf5c9d922014-10-03 15:22:03 -070064 @Override
65 protected void execute() {
66 IntentService service = get(IntentService.class);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080067
68 if (intentsSummary) {
69 IntentSummaries intentSummaries = new IntentSummaries();
70 intentSummaries.collectIntentSummary(service,
71 service.getIntents());
72 if (outputJson()) {
73 print("%s", intentSummaries.json());
74 } else {
75 intentSummaries.printSummary();
76 }
77 return;
Jonathan Hart34f1e382015-02-24 16:52:23 -080078 } else if (pending) {
Ray Milkey740c8a32015-03-17 13:41:03 -070079 if (outputJson()) {
80 print("%s", json(service, service.getPending()));
81 } else {
82 service.getPending().forEach(intent ->
83 print("id=%s, key=%s, type=%s, appId=%s",
84 intent.id(), intent.key(),
85 intent.getClass().getSimpleName(),
86 intent.appId().name())
87 );
88 }
Jonathan Hart34f1e382015-02-24 16:52:23 -080089 return;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080090 }
91
Thomas Vachuska6ce73042014-10-21 10:01:49 -070092 if (outputJson()) {
93 print("%s", json(service, service.getIntents()));
94 } else {
95 for (Intent intent : service.getIntents()) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -080096 IntentState state = service.getIntentState(intent.key());
Thomas Vachuska3ea690b2014-11-29 12:39:03 -080097 if (state != null) {
Jonathan Hart189f9bf2015-02-11 19:24:28 -080098 print("id=%s, state=%s, key=%s, type=%s, appId=%s",
99 intent.id(), state, intent.key(),
100 intent.getClass().getSimpleName(),
Thomas Vachuska3ea690b2014-11-29 12:39:03 -0800101 intent.appId().name());
102 printDetails(service, intent);
103 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700104 }
tomf5c9d922014-10-03 15:22:03 -0700105 }
106 }
107
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800108 /**
109 * Internal local class to keep track of all intent summaries.
110 */
111 private class IntentSummaries {
112 private IntentSummary summaryAll;
113 private IntentSummary summaryConnectivity;
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800114 private IntentSummary summaryHostToHost;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800115 private IntentSummary summaryPointToPoint;
116 private IntentSummary summaryMultiPointToSinglePoint;
117 private IntentSummary summarySinglePointToMultiPoint;
118 private IntentSummary summaryPath;
119 private IntentSummary summaryLinkCollection;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200120 private IntentSummary summaryOpticalCircuit;
121 private IntentSummary summaryOpticalConnectivity;
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200122 private IntentSummary summaryOpticalOdu;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800123 private IntentSummary summaryUnknownType;
124
125 /**
126 * Initializes the internal state.
127 */
128 private void init() {
129 summaryAll = new IntentSummary("All");
130 summaryConnectivity = new IntentSummary("Connectivity");
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800131 summaryHostToHost = new IntentSummary("HostToHost");
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800132 summaryPointToPoint = new IntentSummary("PointToPoint");
133 summaryMultiPointToSinglePoint =
134 new IntentSummary("MultiPointToSinglePoint");
135 summarySinglePointToMultiPoint =
136 new IntentSummary("SinglePointToMultiPoint");
137 summaryPath = new IntentSummary("Path");
138 summaryLinkCollection = new IntentSummary("LinkCollection");
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200139 summaryOpticalCircuit = new IntentSummary("OpticalCircuit");
140 summaryOpticalConnectivity = new IntentSummary("OpticalConnectivity");
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200141 summaryOpticalOdu = new IntentSummary("OpticalOdu");
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800142 summaryUnknownType = new IntentSummary("UnknownType");
143 }
144
145 /**
146 * Collects summary of all intents.
147 *
148 * @param service the Intent Service to use
149 * @param intents the intents
150 */
151 private void collectIntentSummary(IntentService service,
152 Iterable<Intent> intents) {
153 init();
154
155 // Collect the summary for each intent type intents
156 for (Intent intent : intents) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800157 IntentState intentState = service.getIntentState(intent.key());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800158 if (intentState == null) {
159 continue;
160 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800161
162 // Update the summary for all Intents
163 summaryAll.update(intentState);
164
165 if (intent instanceof ConnectivityIntent) {
166 summaryConnectivity.update(intentState);
167 // NOTE: ConnectivityIntent is a base type Intent
168 // continue;
169 }
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800170 if (intent instanceof HostToHostIntent) {
171 summaryHostToHost.update(intentState);
172 continue;
173 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800174 if (intent instanceof PointToPointIntent) {
175 summaryPointToPoint.update(intentState);
176 continue;
177 }
178 if (intent instanceof MultiPointToSinglePointIntent) {
179 summaryMultiPointToSinglePoint.update(intentState);
180 continue;
181 }
182 if (intent instanceof SinglePointToMultiPointIntent) {
183 summarySinglePointToMultiPoint.update(intentState);
184 continue;
185 }
186 if (intent instanceof PathIntent) {
187 summaryPath.update(intentState);
188 continue;
189 }
190 if (intent instanceof LinkCollectionIntent) {
191 summaryLinkCollection.update(intentState);
192 continue;
193 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200194 if (intent instanceof OpticalCircuitIntent) {
195 summaryOpticalCircuit.update(intentState);
196 continue;
197 }
198 if (intent instanceof OpticalConnectivityIntent) {
199 summaryOpticalConnectivity.update(intentState);
200 continue;
201 }
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200202 if (intent instanceof OpticalOduIntent) {
203 summaryOpticalOdu.update(intentState);
204 continue;
205 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800206 summaryUnknownType.update(intentState);
207 }
208 }
209
210 /**
211 * Gets JSON representation of all Intents summary.
212 *
213 * @return JSON representation of all Intents summary
214 */
215 ObjectNode json() {
216 ObjectMapper mapper = new ObjectMapper();
217 ObjectNode result = mapper.createObjectNode();
Ray Milkey9d810f62015-02-13 11:20:58 -0800218 result.set("connectivity", summaryConnectivity.json(mapper));
219 result.set("hostToHost", summaryHostToHost.json(mapper));
220 result.set("pointToPoint", summaryPointToPoint.json(mapper));
221 result.set("multiPointToSinglePoint",
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800222 summaryMultiPointToSinglePoint.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800223 result.set("singlePointToMultiPoint",
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800224 summarySinglePointToMultiPoint.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800225 result.set("path", summaryPath.json(mapper));
226 result.set("linkCollection", summaryLinkCollection.json(mapper));
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200227 result.set("opticalCircuit", summaryOpticalCircuit.json(mapper));
228 result.set("opticalConnectivity", summaryOpticalConnectivity.json(mapper));
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200229 result.set("opticalOdu", summaryOpticalOdu.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800230 result.set("unknownType", summaryUnknownType.json(mapper));
231 result.set("all", summaryAll.json(mapper));
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800232 return result;
233 }
234
235 /**
236 * Prints summary of the intents.
237 */
238 private void printSummary() {
239 summaryConnectivity.printState();
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800240 summaryHostToHost.printState();
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800241 summaryPointToPoint.printState();
242 summaryMultiPointToSinglePoint.printState();
243 summarySinglePointToMultiPoint.printState();
244 summaryPath.printState();
245 summaryLinkCollection.printState();
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200246 summaryOpticalCircuit.printState();
247 summaryOpticalConnectivity.printState();
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200248 summaryOpticalOdu.printState();
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800249 summaryUnknownType.printState();
250 summaryAll.printState();
251 }
252
253 /**
254 * Internal local class to keep track of a single type Intent summary.
255 */
256 private class IntentSummary {
257 private final String intentType;
258 private int total = 0;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800259 private int installReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800260 private int compiling = 0;
261 private int installing = 0;
262 private int installed = 0;
263 private int recompiling = 0;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800264 private int withdrawReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800265 private int withdrawing = 0;
266 private int withdrawn = 0;
267 private int failed = 0;
268 private int unknownState = 0;
269
270 private static final String FORMAT_SUMMARY_LINE1 =
271 "%-23s total= %7d installed= %7d";
272 private static final String FORMAT_SUMMARY_LINE2 =
273 "%-23s withdrawn= %7d failed= %7d";
274 private static final String FORMAT_SUMMARY_LINE3 =
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800275 "%-23s installReq= %7d compiling= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800276 private static final String FORMAT_SUMMARY_LINE4 =
277 "%-23s installing= %7d recompiling= %7d";
278 private static final String FORMAT_SUMMARY_LINE5 =
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800279 "%-23s withdrawReq= %7d withdrawing= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800280 private static final String FORMAT_SUMMARY_LINE6 =
281 "%-23s unknownState= %7d";
282
283 /**
284 * Constructor.
285 *
286 * @param intentType the scring describing the Intent type
287 */
288 IntentSummary(String intentType) {
289 this.intentType = intentType;
290 }
291
292 /**
293 * Updates the Intent Summary.
294 *
295 * @param intentState the state of the Intent
296 */
297 void update(IntentState intentState) {
298 total++;
299 switch (intentState) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800300 case INSTALL_REQ:
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800301 installReq++;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800302 break;
303 case COMPILING:
304 compiling++;
305 break;
306 case INSTALLING:
307 installing++;
308 break;
309 case INSTALLED:
310 installed++;
311 break;
312 case RECOMPILING:
313 recompiling++;
314 break;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800315 case WITHDRAW_REQ:
316 withdrawReq++;
317 break;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800318 case WITHDRAWING:
319 withdrawing++;
320 break;
321 case WITHDRAWN:
322 withdrawn++;
323 break;
324 case FAILED:
325 failed++;
326 break;
327 default:
328 unknownState++;
329 break;
330 }
331 }
332
333 /**
334 * Prints the Intent Summary.
335 */
336 void printState() {
337 print(FORMAT_SUMMARY_LINE1, intentType, total, installed);
338 print(FORMAT_SUMMARY_LINE2, intentType, withdrawn, failed);
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800339 print(FORMAT_SUMMARY_LINE3, intentType, installReq, compiling);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800340 print(FORMAT_SUMMARY_LINE4, intentType, installing, recompiling);
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800341 print(FORMAT_SUMMARY_LINE5, intentType, withdrawReq, withdrawing);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800342 if (unknownState != 0) {
343 print(FORMAT_SUMMARY_LINE6, intentType, unknownState);
344 }
345 }
346
347 /**
348 * Gets the JSON representation of the Intent Summary.
349 *
350 * @return the JSON representation of the Intent Summary
351 */
352 JsonNode json(ObjectMapper mapper) {
353 ObjectNode result = mapper.createObjectNode()
354 .put("total", total)
355 .put("installed", installed)
356 .put("failed", failed)
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800357 .put("installReq", installReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800358 .put("compiling", compiling)
359 .put("installing", installing)
360 .put("recompiling", recompiling)
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800361 .put("withdrawReq", withdrawReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800362 .put("withdrawing", withdrawing)
363 .put("withdrawn", withdrawn)
364 .put("unknownState", unknownState);
365
366 return result;
367 }
368 }
369 }
370
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700371 private void printDetails(IntentService service, Intent intent) {
Sho SHIMIZUd7d18002015-01-21 14:37:14 -0800372 if (!intent.resources().isEmpty()) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700373 print(" resources=%s", intent.resources());
374 }
375 if (intent instanceof ConnectivityIntent) {
376 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700377 if (!ci.selector().criteria().isEmpty()) {
378 print(" selector=%s", ci.selector().criteria());
379 }
Ray Milkey42507352015-03-20 15:16:10 -0700380 if (!ci.treatment().allInstructions().isEmpty()) {
381 print(" treatment=%s", ci.treatment().allInstructions());
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700382 }
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800383 if (ci.constraints() != null && !ci.constraints().isEmpty()) {
384 print(" constraints=%s", ci.constraints());
385 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700386 }
387
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800388 if (intent instanceof HostToHostIntent) {
389 HostToHostIntent pi = (HostToHostIntent) intent;
390 print(" host1=%s, host2=%s", pi.one(), pi.two());
391 } else if (intent instanceof PointToPointIntent) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700392 PointToPointIntent pi = (PointToPointIntent) intent;
393 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint());
394 } else if (intent instanceof MultiPointToSinglePointIntent) {
395 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
396 print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint());
397 } else if (intent instanceof SinglePointToMultiPointIntent) {
398 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
399 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints());
400 } else if (intent instanceof PathIntent) {
401 PathIntent pi = (PathIntent) intent;
402 print(" path=%s, cost=%d", pi.path().links(), pi.path().cost());
403 } else if (intent instanceof LinkCollectionIntent) {
404 LinkCollectionIntent li = (LinkCollectionIntent) intent;
405 print(" links=%s", li.links());
Michele Santuari4a338072014-11-05 18:38:55 +0100406 print(" egress=%s", li.egressPoints());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200407 } else if (intent instanceof OpticalCircuitIntent) {
408 OpticalCircuitIntent ci = (OpticalCircuitIntent) intent;
409 print(" src=%s, dst=%s", ci.getSrc(), ci.getDst());
410 } else if (intent instanceof OpticalConnectivityIntent) {
411 OpticalConnectivityIntent ci = (OpticalConnectivityIntent) intent;
412 print(" src=%s, dst=%s", ci.getSrc(), ci.getDst());
Rimon Ashkenazy27438ff2016-03-22 15:57:45 +0200413 } else if (intent instanceof OpticalOduIntent) {
414 OpticalOduIntent ci = (OpticalOduIntent) intent;
415 print(" src=%s, dst=%s", ci.getSrc(), ci.getDst());
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700416 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700417
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800418 List<Intent> installable = service.getInstallableIntents(intent.key());
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700419 if (showInstallable && installable != null && !installable.isEmpty()) {
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700420 print(" installable=%s", installable);
421 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700422 }
423
424 // Produces JSON array of the specified intents.
425 private JsonNode json(IntentService service, Iterable<Intent> intents) {
426 ObjectMapper mapper = new ObjectMapper();
427 ArrayNode result = mapper.createArrayNode();
Ray Milkey3078fc02015-05-06 16:14:14 -0700428
429 intents.forEach(intent -> result.add(jsonForEntity(intent, Intent.class)));
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700430 return result;
431 }
432
tomf5c9d922014-10-03 15:22:03 -0700433}