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