blob: 04450595fe58c3d4738c7da844d7115ecabd1b88 [file] [log] [blame]
Yuta HIGUCHIfadb9a32017-01-13 09:33:10 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Yuta HIGUCHIfadb9a32017-01-13 09:33:10 -08003 *
4 * 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
7 *
8 * 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.
15 */
16package org.onosproject.cli.net;
17
18import java.util.List;
19import java.util.Set;
20import java.util.stream.Collectors;
21import org.apache.karaf.shell.commands.Command;
22import org.apache.karaf.shell.commands.Option;
23import org.onlab.util.Tools;
24import org.onosproject.cli.AbstractShellCommand;
25import org.onosproject.net.Link;
26import org.onosproject.net.LinkKey;
27import org.onosproject.net.intent.Intent;
28import org.onosproject.net.intent.IntentData;
29import org.onosproject.net.intent.IntentId;
30import org.onosproject.net.intent.IntentService;
31
32/**
33 * Displays details about an Intent in the system.
34 */
35@Command(scope = "onos", name = "intent-details",
36 description = "Displays intent details")
37public class IntentDetailsCommand extends AbstractShellCommand {
38
39 @Option(name = "--id",
40 description = "Filter intent by specific Id", multiValued = true)
41 private List<String> idsStr;
42
43 private Set<IntentId> ids = null;
44
45
46 @Override
47 protected void execute() {
Carolina Fernandez0b1449d2016-12-04 13:30:36 +010048 detailIntents(idsStr);
49 }
50
51 /**
52 * Print detailed data for intents, given a list of IDs.
53 *
54 * @param intentsIds List of intent IDs
55 */
56 public void detailIntents(List<String> intentsIds) {
57 if (intentsIds != null) {
58 ids = intentsIds.stream()
Yuta HIGUCHIfadb9a32017-01-13 09:33:10 -080059 .map(IntentId::valueOf)
60 .collect(Collectors.toSet());
61 }
62
63 IntentService service = get(IntentService.class);
64
65 Tools.stream(service.getIntentData())
Carolina Fernandez0b1449d2016-12-04 13:30:36 +010066 .filter(this::filter)
67 .forEach(this::printIntentData);
Yuta HIGUCHIfadb9a32017-01-13 09:33:10 -080068 }
69
70 private boolean filter(IntentData data) {
71 if (ids != null && !ids.contains(data.intent().id())) {
72 return false;
73 }
74
75 return true;
76 }
77
78 private void printIntentData(IntentData data) {
79 print("Key: %s ID: %s", data.key(), data.intent().id());
80
81 print(" Request: %s Current: %s", data.request(), data.state());
82
83 print(" intent: %s", s(data.intent()));
84
85 data.installables().stream()
86 .forEach(this::printInstallable);
87
88 // empty line
89 print("");
90 }
91
92 private void printInstallable(Intent installable) {
93 print(" installable: %s %s", installable.getClass().getSimpleName(),
94 installable.id());
95
96 print(" resources: %s", installable.resources().stream()
97 .filter(r -> !(r instanceof Link))
98 .map(this::s)
99 .collect(Collectors.joining(", ")));
100
101 print(" links: %s", installable.resources().stream()
102 .filter(Link.class::isInstance)
103 .map(Link.class::cast)
104 .map(LinkKey::linkKey)
105 .map(l -> String.format("%s -> %s", l.src(), l.dst()))
106 .collect(Collectors.joining(", ")));
107 }
108
109 protected String s(Object o) {
110 return simplify(String.valueOf(o));
111 }
112
113 /**
114 * Simplify toString result for CLI.
115 *
116 * @param input String
117 * @return simplified String
118 */
119 public static String simplify(String input) {
120 String after = input
121 // omit redundant info
122 .replaceAll("treatment=DefaultTrafficTreatment", "treatment=")
123 .replaceAll("selector=DefaultTrafficSelector", "selector=")
124 // shorten AppId
125 .replaceAll("DefaultApplicationId\\{id=(\\d+), name=([.\\w]+)\\}", "$2($1)")
126 // omit empty list/array attribute
127 .replaceAll("(, )?\\w+=\\[\\]", "")
128 // omit empty map attribute
129 .replaceAll("(, )?\\w+=\\{\\}", "")
130 // omit Object which became empty
131 .replaceAll("(, )?\\w+\\{\\}", "\\{\\}")
132 // shorten FilteredConnectPoint
133 .replaceAll("FilteredConnectPoint", "")
134 // trim prefix Default
135 .replaceAll("Default(\\w+)\\{", "$1\\{")
136 .replaceAll(", , ", ", ");
137
138 return after.equals(input) ? input : simplify(after);
139 }
140
141}