blob: 3bed89b4283f596ebd1acab9d8644c4a90d3d51b [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;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +020030import org.onosproject.net.intent.OpticalCircuitIntent;
31import org.onosproject.net.intent.OpticalConnectivityIntent;
Brian O'Connorabafb502014-12-02 22:26:20 -080032import org.onosproject.net.intent.PathIntent;
33import org.onosproject.net.intent.PointToPointIntent;
34import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Thomas Vachuska6ce73042014-10-21 10:01:49 -070035
Ray Milkey3078fc02015-05-06 16:14:14 -070036import com.fasterxml.jackson.databind.JsonNode;
37import com.fasterxml.jackson.databind.ObjectMapper;
38import com.fasterxml.jackson.databind.node.ArrayNode;
39import com.fasterxml.jackson.databind.node.ObjectNode;
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
Jonathan Hart34f1e382015-02-24 16:52:23 -080058 @Option(name = "-p", aliases = "--pending",
59 description = "Show inforamtion about pending intents",
60 required = false, multiValued = false)
61 private boolean pending = false;
62
tomf5c9d922014-10-03 15:22:03 -070063 @Override
64 protected void execute() {
65 IntentService service = get(IntentService.class);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080066
67 if (intentsSummary) {
68 IntentSummaries intentSummaries = new IntentSummaries();
69 intentSummaries.collectIntentSummary(service,
70 service.getIntents());
71 if (outputJson()) {
72 print("%s", intentSummaries.json());
73 } else {
74 intentSummaries.printSummary();
75 }
76 return;
Jonathan Hart34f1e382015-02-24 16:52:23 -080077 } else if (pending) {
Ray Milkey740c8a32015-03-17 13:41:03 -070078 if (outputJson()) {
79 print("%s", json(service, service.getPending()));
80 } else {
81 service.getPending().forEach(intent ->
82 print("id=%s, key=%s, type=%s, appId=%s",
83 intent.id(), intent.key(),
84 intent.getClass().getSimpleName(),
85 intent.appId().name())
86 );
87 }
Jonathan Hart34f1e382015-02-24 16:52:23 -080088 return;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -080089 }
90
Thomas Vachuska6ce73042014-10-21 10:01:49 -070091 if (outputJson()) {
92 print("%s", json(service, service.getIntents()));
93 } else {
94 for (Intent intent : service.getIntents()) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -080095 IntentState state = service.getIntentState(intent.key());
Thomas Vachuska3ea690b2014-11-29 12:39:03 -080096 if (state != null) {
Jonathan Hart189f9bf2015-02-11 19:24:28 -080097 print("id=%s, state=%s, key=%s, type=%s, appId=%s",
98 intent.id(), state, intent.key(),
99 intent.getClass().getSimpleName(),
Thomas Vachuska3ea690b2014-11-29 12:39:03 -0800100 intent.appId().name());
101 printDetails(service, intent);
102 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700103 }
tomf5c9d922014-10-03 15:22:03 -0700104 }
105 }
106
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800107 /**
108 * Internal local class to keep track of all intent summaries.
109 */
110 private class IntentSummaries {
111 private IntentSummary summaryAll;
112 private IntentSummary summaryConnectivity;
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800113 private IntentSummary summaryHostToHost;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800114 private IntentSummary summaryPointToPoint;
115 private IntentSummary summaryMultiPointToSinglePoint;
116 private IntentSummary summarySinglePointToMultiPoint;
117 private IntentSummary summaryPath;
118 private IntentSummary summaryLinkCollection;
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200119 private IntentSummary summaryOpticalCircuit;
120 private IntentSummary summaryOpticalConnectivity;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800121 private IntentSummary summaryUnknownType;
122
123 /**
124 * Initializes the internal state.
125 */
126 private void init() {
127 summaryAll = new IntentSummary("All");
128 summaryConnectivity = new IntentSummary("Connectivity");
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800129 summaryHostToHost = new IntentSummary("HostToHost");
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800130 summaryPointToPoint = new IntentSummary("PointToPoint");
131 summaryMultiPointToSinglePoint =
132 new IntentSummary("MultiPointToSinglePoint");
133 summarySinglePointToMultiPoint =
134 new IntentSummary("SinglePointToMultiPoint");
135 summaryPath = new IntentSummary("Path");
136 summaryLinkCollection = new IntentSummary("LinkCollection");
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200137 summaryOpticalCircuit = new IntentSummary("OpticalCircuit");
138 summaryOpticalConnectivity = new IntentSummary("OpticalConnectivity");
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800139 summaryUnknownType = new IntentSummary("UnknownType");
140 }
141
142 /**
143 * Collects summary of all intents.
144 *
145 * @param service the Intent Service to use
146 * @param intents the intents
147 */
148 private void collectIntentSummary(IntentService service,
149 Iterable<Intent> intents) {
150 init();
151
152 // Collect the summary for each intent type intents
153 for (Intent intent : intents) {
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800154 IntentState intentState = service.getIntentState(intent.key());
Pavlin Radoslavovdeb8a102014-11-26 13:31:36 -0800155 if (intentState == null) {
156 continue;
157 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800158
159 // Update the summary for all Intents
160 summaryAll.update(intentState);
161
162 if (intent instanceof ConnectivityIntent) {
163 summaryConnectivity.update(intentState);
164 // NOTE: ConnectivityIntent is a base type Intent
165 // continue;
166 }
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800167 if (intent instanceof HostToHostIntent) {
168 summaryHostToHost.update(intentState);
169 continue;
170 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800171 if (intent instanceof PointToPointIntent) {
172 summaryPointToPoint.update(intentState);
173 continue;
174 }
175 if (intent instanceof MultiPointToSinglePointIntent) {
176 summaryMultiPointToSinglePoint.update(intentState);
177 continue;
178 }
179 if (intent instanceof SinglePointToMultiPointIntent) {
180 summarySinglePointToMultiPoint.update(intentState);
181 continue;
182 }
183 if (intent instanceof PathIntent) {
184 summaryPath.update(intentState);
185 continue;
186 }
187 if (intent instanceof LinkCollectionIntent) {
188 summaryLinkCollection.update(intentState);
189 continue;
190 }
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200191 if (intent instanceof OpticalCircuitIntent) {
192 summaryOpticalCircuit.update(intentState);
193 continue;
194 }
195 if (intent instanceof OpticalConnectivityIntent) {
196 summaryOpticalConnectivity.update(intentState);
197 continue;
198 }
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800199 summaryUnknownType.update(intentState);
200 }
201 }
202
203 /**
204 * Gets JSON representation of all Intents summary.
205 *
206 * @return JSON representation of all Intents summary
207 */
208 ObjectNode json() {
209 ObjectMapper mapper = new ObjectMapper();
210 ObjectNode result = mapper.createObjectNode();
Ray Milkey9d810f62015-02-13 11:20:58 -0800211 result.set("connectivity", summaryConnectivity.json(mapper));
212 result.set("hostToHost", summaryHostToHost.json(mapper));
213 result.set("pointToPoint", summaryPointToPoint.json(mapper));
214 result.set("multiPointToSinglePoint",
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800215 summaryMultiPointToSinglePoint.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800216 result.set("singlePointToMultiPoint",
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800217 summarySinglePointToMultiPoint.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800218 result.set("path", summaryPath.json(mapper));
219 result.set("linkCollection", summaryLinkCollection.json(mapper));
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200220 result.set("opticalCircuit", summaryOpticalCircuit.json(mapper));
221 result.set("opticalConnectivity", summaryOpticalConnectivity.json(mapper));
Ray Milkey9d810f62015-02-13 11:20:58 -0800222 result.set("unknownType", summaryUnknownType.json(mapper));
223 result.set("all", summaryAll.json(mapper));
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800224 return result;
225 }
226
227 /**
228 * Prints summary of the intents.
229 */
230 private void printSummary() {
231 summaryConnectivity.printState();
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800232 summaryHostToHost.printState();
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800233 summaryPointToPoint.printState();
234 summaryMultiPointToSinglePoint.printState();
235 summarySinglePointToMultiPoint.printState();
236 summaryPath.printState();
237 summaryLinkCollection.printState();
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200238 summaryOpticalCircuit.printState();
239 summaryOpticalConnectivity.printState();
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800240 summaryUnknownType.printState();
241 summaryAll.printState();
242 }
243
244 /**
245 * Internal local class to keep track of a single type Intent summary.
246 */
247 private class IntentSummary {
248 private final String intentType;
249 private int total = 0;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800250 private int installReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800251 private int compiling = 0;
252 private int installing = 0;
253 private int installed = 0;
254 private int recompiling = 0;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800255 private int withdrawReq = 0;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800256 private int withdrawing = 0;
257 private int withdrawn = 0;
258 private int failed = 0;
259 private int unknownState = 0;
260
261 private static final String FORMAT_SUMMARY_LINE1 =
262 "%-23s total= %7d installed= %7d";
263 private static final String FORMAT_SUMMARY_LINE2 =
264 "%-23s withdrawn= %7d failed= %7d";
265 private static final String FORMAT_SUMMARY_LINE3 =
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800266 "%-23s installReq= %7d compiling= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800267 private static final String FORMAT_SUMMARY_LINE4 =
268 "%-23s installing= %7d recompiling= %7d";
269 private static final String FORMAT_SUMMARY_LINE5 =
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800270 "%-23s withdrawReq= %7d withdrawing= %7d";
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800271 private static final String FORMAT_SUMMARY_LINE6 =
272 "%-23s unknownState= %7d";
273
274 /**
275 * Constructor.
276 *
277 * @param intentType the scring describing the Intent type
278 */
279 IntentSummary(String intentType) {
280 this.intentType = intentType;
281 }
282
283 /**
284 * Updates the Intent Summary.
285 *
286 * @param intentState the state of the Intent
287 */
288 void update(IntentState intentState) {
289 total++;
290 switch (intentState) {
Brian O'Connor7a71d5d2014-12-02 00:12:27 -0800291 case INSTALL_REQ:
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800292 installReq++;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800293 break;
294 case COMPILING:
295 compiling++;
296 break;
297 case INSTALLING:
298 installing++;
299 break;
300 case INSTALLED:
301 installed++;
302 break;
303 case RECOMPILING:
304 recompiling++;
305 break;
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800306 case WITHDRAW_REQ:
307 withdrawReq++;
308 break;
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800309 case WITHDRAWING:
310 withdrawing++;
311 break;
312 case WITHDRAWN:
313 withdrawn++;
314 break;
315 case FAILED:
316 failed++;
317 break;
318 default:
319 unknownState++;
320 break;
321 }
322 }
323
324 /**
325 * Prints the Intent Summary.
326 */
327 void printState() {
328 print(FORMAT_SUMMARY_LINE1, intentType, total, installed);
329 print(FORMAT_SUMMARY_LINE2, intentType, withdrawn, failed);
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800330 print(FORMAT_SUMMARY_LINE3, intentType, installReq, compiling);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800331 print(FORMAT_SUMMARY_LINE4, intentType, installing, recompiling);
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800332 print(FORMAT_SUMMARY_LINE5, intentType, withdrawReq, withdrawing);
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800333 if (unknownState != 0) {
334 print(FORMAT_SUMMARY_LINE6, intentType, unknownState);
335 }
336 }
337
338 /**
339 * Gets the JSON representation of the Intent Summary.
340 *
341 * @return the JSON representation of the Intent Summary
342 */
343 JsonNode json(ObjectMapper mapper) {
344 ObjectNode result = mapper.createObjectNode()
345 .put("total", total)
346 .put("installed", installed)
347 .put("failed", failed)
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800348 .put("installReq", installReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800349 .put("compiling", compiling)
350 .put("installing", installing)
351 .put("recompiling", recompiling)
Pavlin Radoslavov67c05142014-12-02 13:04:08 -0800352 .put("withdrawReq", withdrawReq)
Pavlin Radoslavov708e8202014-11-14 17:18:37 -0800353 .put("withdrawing", withdrawing)
354 .put("withdrawn", withdrawn)
355 .put("unknownState", unknownState);
356
357 return result;
358 }
359 }
360 }
361
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700362 private void printDetails(IntentService service, Intent intent) {
Sho SHIMIZUd7d18002015-01-21 14:37:14 -0800363 if (!intent.resources().isEmpty()) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700364 print(" resources=%s", intent.resources());
365 }
366 if (intent instanceof ConnectivityIntent) {
367 ConnectivityIntent ci = (ConnectivityIntent) intent;
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700368 if (!ci.selector().criteria().isEmpty()) {
369 print(" selector=%s", ci.selector().criteria());
370 }
Ray Milkey42507352015-03-20 15:16:10 -0700371 if (!ci.treatment().allInstructions().isEmpty()) {
372 print(" treatment=%s", ci.treatment().allInstructions());
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700373 }
Thomas Vachuskaedc944c2014-11-04 15:42:25 -0800374 if (ci.constraints() != null && !ci.constraints().isEmpty()) {
375 print(" constraints=%s", ci.constraints());
376 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700377 }
378
Pavlin Radoslavovaab51a32014-12-08 11:07:38 -0800379 if (intent instanceof HostToHostIntent) {
380 HostToHostIntent pi = (HostToHostIntent) intent;
381 print(" host1=%s, host2=%s", pi.one(), pi.two());
382 } else if (intent instanceof PointToPointIntent) {
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700383 PointToPointIntent pi = (PointToPointIntent) intent;
384 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoint());
385 } else if (intent instanceof MultiPointToSinglePointIntent) {
386 MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent;
387 print(" ingress=%s, egress=%s", pi.ingressPoints(), pi.egressPoint());
388 } else if (intent instanceof SinglePointToMultiPointIntent) {
389 SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent;
390 print(" ingress=%s, egress=%s", pi.ingressPoint(), pi.egressPoints());
391 } else if (intent instanceof PathIntent) {
392 PathIntent pi = (PathIntent) intent;
393 print(" path=%s, cost=%d", pi.path().links(), pi.path().cost());
394 } else if (intent instanceof LinkCollectionIntent) {
395 LinkCollectionIntent li = (LinkCollectionIntent) intent;
396 print(" links=%s", li.links());
Michele Santuari4a338072014-11-05 18:38:55 +0100397 print(" egress=%s", li.egressPoints());
Rimon Ashkenazyf0699702016-01-17 19:28:49 +0200398 } else if (intent instanceof OpticalCircuitIntent) {
399 OpticalCircuitIntent ci = (OpticalCircuitIntent) intent;
400 print(" src=%s, dst=%s", ci.getSrc(), ci.getDst());
401 } else if (intent instanceof OpticalConnectivityIntent) {
402 OpticalConnectivityIntent ci = (OpticalConnectivityIntent) intent;
403 print(" src=%s, dst=%s", ci.getSrc(), ci.getDst());
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700404 }
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700405
Ray Milkeyf9af43c2015-02-09 16:45:48 -0800406 List<Intent> installable = service.getInstallableIntents(intent.key());
Brian O'Connorfe0f4b12014-10-30 21:19:02 -0700407 if (showInstallable && installable != null && !installable.isEmpty()) {
Thomas Vachuska10d4abc2014-10-21 12:47:26 -0700408 print(" installable=%s", installable);
409 }
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700410 }
411
412 // Produces JSON array of the specified intents.
413 private JsonNode json(IntentService service, Iterable<Intent> intents) {
414 ObjectMapper mapper = new ObjectMapper();
415 ArrayNode result = mapper.createArrayNode();
Ray Milkey3078fc02015-05-06 16:14:14 -0700416
417 intents.forEach(intent -> result.add(jsonForEntity(intent, Intent.class)));
Thomas Vachuska6ce73042014-10-21 10:01:49 -0700418 return result;
419 }
420
tomf5c9d922014-10-03 15:22:03 -0700421}