blob: 1cd0a1b2382c56fb1cb0c0e667e63e46b03d46a0 [file] [log] [blame]
Yuta HIGUCHIfadb9a32017-01-13 09:33:10 -08001/*
2 * Copyright 2017-present Open Networking Laboratory
3 *
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() {
48 if (idsStr != null) {
49 ids = idsStr.stream()
50 .map(IntentId::valueOf)
51 .collect(Collectors.toSet());
52 }
53
54 IntentService service = get(IntentService.class);
55
56 Tools.stream(service.getIntentData())
57 .filter(this::filter)
58 .forEach(this::printIntentData);
59 }
60
61 private boolean filter(IntentData data) {
62 if (ids != null && !ids.contains(data.intent().id())) {
63 return false;
64 }
65
66 return true;
67 }
68
69 private void printIntentData(IntentData data) {
70 print("Key: %s ID: %s", data.key(), data.intent().id());
71
72 print(" Request: %s Current: %s", data.request(), data.state());
73
74 print(" intent: %s", s(data.intent()));
75
76 data.installables().stream()
77 .forEach(this::printInstallable);
78
79 // empty line
80 print("");
81 }
82
83 private void printInstallable(Intent installable) {
84 print(" installable: %s %s", installable.getClass().getSimpleName(),
85 installable.id());
86
87 print(" resources: %s", installable.resources().stream()
88 .filter(r -> !(r instanceof Link))
89 .map(this::s)
90 .collect(Collectors.joining(", ")));
91
92 print(" links: %s", installable.resources().stream()
93 .filter(Link.class::isInstance)
94 .map(Link.class::cast)
95 .map(LinkKey::linkKey)
96 .map(l -> String.format("%s -> %s", l.src(), l.dst()))
97 .collect(Collectors.joining(", ")));
98 }
99
100 protected String s(Object o) {
101 return simplify(String.valueOf(o));
102 }
103
104 /**
105 * Simplify toString result for CLI.
106 *
107 * @param input String
108 * @return simplified String
109 */
110 public static String simplify(String input) {
111 String after = input
112 // omit redundant info
113 .replaceAll("treatment=DefaultTrafficTreatment", "treatment=")
114 .replaceAll("selector=DefaultTrafficSelector", "selector=")
115 // shorten AppId
116 .replaceAll("DefaultApplicationId\\{id=(\\d+), name=([.\\w]+)\\}", "$2($1)")
117 // omit empty list/array attribute
118 .replaceAll("(, )?\\w+=\\[\\]", "")
119 // omit empty map attribute
120 .replaceAll("(, )?\\w+=\\{\\}", "")
121 // omit Object which became empty
122 .replaceAll("(, )?\\w+\\{\\}", "\\{\\}")
123 // shorten FilteredConnectPoint
124 .replaceAll("FilteredConnectPoint", "")
125 // trim prefix Default
126 .replaceAll("Default(\\w+)\\{", "$1\\{")
127 .replaceAll(", , ", ", ");
128
129 return after.equals(input) ? input : simplify(after);
130 }
131
132}