Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Foundation |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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 |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 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. |
Thomas Vachuska | 7d693f5 | 2014-10-21 19:17:57 -0700 | [diff] [blame] | 15 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.cli.net; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 17 | |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 22 | import com.google.common.base.Strings; |
| 23 | import com.google.common.collect.Lists; |
| 24 | import com.google.common.collect.Sets; |
| 25 | import org.apache.commons.lang.StringUtils; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 26 | import org.apache.karaf.shell.commands.Command; |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 27 | import org.apache.karaf.shell.commands.Option; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 28 | import org.onlab.util.StringFilter; |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 29 | import org.onlab.util.Tools; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 30 | import org.onosproject.cli.AbstractShellCommand; |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 31 | import org.onosproject.net.ConnectPoint; |
| 32 | import org.onosproject.net.FilteredConnectPoint; |
| 33 | import org.onosproject.net.flow.TrafficSelector; |
| 34 | import org.onosproject.net.flow.criteria.Criterion; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 35 | import org.onosproject.net.intent.ConnectivityIntent; |
Pavlin Radoslavov | aab51a3 | 2014-12-08 11:07:38 -0800 | [diff] [blame] | 36 | import org.onosproject.net.intent.HostToHostIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 37 | import org.onosproject.net.intent.Intent; |
| 38 | import org.onosproject.net.intent.IntentService; |
| 39 | import org.onosproject.net.intent.IntentState; |
| 40 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 41 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 42 | import org.onosproject.net.intent.OpticalCircuitIntent; |
| 43 | import org.onosproject.net.intent.OpticalConnectivityIntent; |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 44 | import org.onosproject.net.intent.OpticalOduIntent; |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 45 | import org.onosproject.net.intent.PathIntent; |
| 46 | import org.onosproject.net.intent.PointToPointIntent; |
| 47 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 48 | |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 49 | import static com.google.common.base.MoreObjects.firstNonNull; |
| 50 | import static java.lang.String.format; |
| 51 | import static org.apache.commons.lang3.text.WordUtils.uncapitalize; |
| 52 | |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 53 | import java.util.ArrayList; |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 54 | import java.util.HashMap; |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 55 | import java.util.List; |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 56 | import java.util.Map; |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 57 | import java.util.Set; |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 58 | import java.util.stream.Collectors; |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Lists the inventory of intents and their states. |
| 62 | */ |
| 63 | @Command(scope = "onos", name = "intents", |
| 64 | description = "Lists the inventory of intents and their states") |
| 65 | public class IntentsListCommand extends AbstractShellCommand { |
| 66 | |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 67 | // Color codes and style |
| 68 | private static final String BOLD = "\u001B[1m"; |
| 69 | private static final String RESET = "\u001B[0m"; |
| 70 | |
| 71 | // Messages and string formatter |
| 72 | private static final String APP_ID = BOLD + "Application Id:" + RESET + " %s"; |
| 73 | |
| 74 | private static final String COMMON_SELECTOR = BOLD + "Common ingress " + |
| 75 | "selector:" + RESET + " %s"; |
| 76 | |
| 77 | private static final String CP = BOLD + "Connect Point:" + RESET + " %s"; |
| 78 | |
| 79 | private static final String CONSTRAINTS = BOLD + "Constraints:" + RESET + " %s"; |
| 80 | |
| 81 | private static final String DST = BOLD + "Destination " + RESET; |
| 82 | |
| 83 | private static final String EGRESS = BOLD + "Egress "; |
| 84 | |
| 85 | private static final String FILTERED_CPS = "connect points and individual selectors" + RESET; |
| 86 | |
| 87 | private static final String HOST = "host:" + RESET + " %s"; |
| 88 | |
| 89 | private static final String ID = BOLD + "Id:" + RESET + " %s"; |
| 90 | |
| 91 | private static final String INHERITED = "Inherited"; |
| 92 | |
| 93 | private static final String INGRESS = BOLD + "Ingress "; |
| 94 | |
| 95 | private static final String INDENTATION = " -> "; |
| 96 | |
| 97 | private static final String INSTALLABLE = BOLD + "Installable:" + RESET + " %s"; |
| 98 | |
| 99 | private static final String KEY = BOLD + "Key:" + RESET + " %s"; |
| 100 | |
| 101 | private static final String RESOURCES = BOLD + "Resources:" + RESET + " %s"; |
| 102 | |
| 103 | private static final String SELECTOR = BOLD + "Selector:" + RESET + " %s"; |
| 104 | |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 105 | private static final String SEPARATOR = StringUtils.repeat("-", 172); |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 106 | |
| 107 | private static final String SPACE = " "; |
| 108 | |
| 109 | private static final String SRC = BOLD + "Source "; |
| 110 | |
| 111 | private static final String STATE = BOLD + "State:" + RESET + " %s"; |
| 112 | |
| 113 | private static final String TREATMENT = BOLD + "Treatment:" + RESET + " %s"; |
| 114 | |
| 115 | private static final String TYPE = BOLD + "Intent type:" + RESET + " %s"; |
| 116 | |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 117 | /** |
| 118 | * {@value #SUMMARY_TITLES}. |
| 119 | */ |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 120 | private static final String SUMMARY_TITLES = |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 121 | BOLD + format( |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 122 | "\n%1s%21s%14s%14s%14s%14s%14s%14s%14s%14s%14s%14s", |
| 123 | "Intent type", |
| 124 | "Total", |
| 125 | "Installed", |
| 126 | "Withdrawn", |
| 127 | "Failed", |
| 128 | "InstallReq", |
| 129 | "Compiling", |
| 130 | "Installing", |
| 131 | "Recompiling", |
| 132 | "WithdrawReq", |
| 133 | "Withdrawing", |
| 134 | "UnknownState") + |
| 135 | RESET; |
| 136 | |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 137 | @Option(name = "-i", aliases = "--installable", |
| 138 | description = "Output Installable Intents", |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 139 | required = false, multiValued = false) |
| 140 | private boolean showInstallable = false; |
| 141 | |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 142 | @Option(name = "-s", aliases = "--summary", |
| 143 | description = "Intents summary", |
| 144 | required = false, multiValued = false) |
| 145 | private boolean intentsSummary = false; |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 146 | |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 147 | @Option(name = "-m", aliases = "--mini-summary", |
| 148 | description = "Intents mini summary", |
| 149 | required = false, multiValued = false) |
| 150 | private boolean miniSummary = false; |
| 151 | |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 152 | @Option(name = "-p", aliases = "--pending", |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 153 | description = "Show information about pending intents", |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 154 | required = false, multiValued = false) |
| 155 | private boolean pending = false; |
| 156 | |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 157 | @Option(name = "-d", aliases = "--details", |
| 158 | description = "Show details for intents, filtered by ID", |
| 159 | required = false, multiValued = true) |
| 160 | private List<String> intentIds = new ArrayList<>(); |
| 161 | |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 162 | @Option(name = "-f", aliases = "--filter", |
Yuta HIGUCHI | fadb9a3 | 2017-01-13 09:33:10 -0800 | [diff] [blame] | 163 | description = "Filter intents by specific keyword", |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 164 | required = false, multiValued = true) |
| 165 | private List<String> filter = new ArrayList<>(); |
| 166 | |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 167 | @Option(name = "-r", aliases = "--remove", |
| 168 | description = "Remove and purge intents by specific keyword", |
| 169 | required = false, multiValued = false) |
| 170 | private String remove = null; |
| 171 | |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 172 | private StringFilter contentFilter; |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 173 | private IntentService service; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 174 | |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 175 | @Override |
| 176 | protected void execute() { |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 177 | service = get(IntentService.class); |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 178 | contentFilter = new StringFilter(filter, StringFilter.Strategy.AND); |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 179 | |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 180 | Iterable<Intent> intents; |
| 181 | if (pending) { |
| 182 | intents = service.getPending(); |
| 183 | } else { |
| 184 | intents = service.getIntents(); |
| 185 | } |
| 186 | |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 187 | // Remove intents |
| 188 | if (remove != null && !remove.isEmpty()) { |
| 189 | filter.add(remove); |
| 190 | contentFilter = new StringFilter(filter, StringFilter.Strategy.AND); |
| 191 | IntentRemoveCommand intentRemoveCmd = new IntentRemoveCommand(); |
| 192 | if (!remove.isEmpty()) { |
| 193 | intentRemoveCmd.purgeIntentsInteractive(filterIntents(service)); |
| 194 | } |
| 195 | return; |
| 196 | } |
| 197 | |
| 198 | // Show detailed intents |
| 199 | if (!intentIds.isEmpty()) { |
| 200 | IntentDetailsCommand intentDetailsCmd = new IntentDetailsCommand(); |
| 201 | intentDetailsCmd.detailIntents(intentIds); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | // Show brief intents |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 206 | if (intentsSummary || miniSummary) { |
| 207 | Map<String, IntentSummary> summarized = summarize(intents); |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 208 | if (outputJson()) { |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 209 | ObjectNode summaries = mapper().createObjectNode(); |
| 210 | summarized.forEach((n, s) -> summaries.set(uncapitalize(n), s.json(mapper()))); |
| 211 | print("%s", summaries); |
| 212 | } else if (miniSummary) { |
| 213 | StringBuilder builder = new StringBuilder(); |
| 214 | builder.append(summarized.remove("All").miniSummary()); |
| 215 | summarized.values().forEach(s -> builder.append(s.miniSummary())); |
| 216 | print("%s", builder.toString()); |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 217 | } else { |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 218 | StringBuilder builder = new StringBuilder(); |
| 219 | builder.append(SUMMARY_TITLES); |
| 220 | builder.append('\n').append(SEPARATOR); |
| 221 | builder.append(summarized.remove("All").summary()); |
| 222 | summarized.values().forEach(s -> builder.append(s.summary())); |
| 223 | print("%s", builder.toString()); |
Ray Milkey | 740c8a3 | 2015-03-17 13:41:03 -0700 | [diff] [blame] | 224 | } |
Jonathan Hart | 34f1e38 | 2015-02-24 16:52:23 -0800 | [diff] [blame] | 225 | return; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 228 | // JSON or default output |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 229 | if (outputJson()) { |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 230 | print("%s", json(intents)); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 231 | } else { |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 232 | for (Intent intent : intents) { |
| 233 | IntentState state = service.getIntentState(intent.key()); |
| 234 | StringBuilder intentFormat = fullFormat(intent, state); |
| 235 | StringBuilder detailsIntentFormat = detailsFormat(intent, state); |
| 236 | String formatted = intentFormat.append(detailsIntentFormat).toString(); |
| 237 | if (contentFilter.filter(formatted)) { |
| 238 | print("%s\n", formatted); |
| 239 | } |
| 240 | } |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 244 | /** |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 245 | * Filter a given list of intents based on the existing content filter. |
| 246 | * |
| 247 | * @param service IntentService object |
| 248 | * @return further filtered list of intents |
| 249 | */ |
| 250 | private List<Intent> filterIntents(IntentService service) { |
| 251 | return filterIntents(service.getIntents()); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Filter a given list of intents based on the existing content filter. |
| 256 | * |
| 257 | * @param intents Iterable of intents |
| 258 | * @return further filtered list of intents |
| 259 | */ |
| 260 | private List<Intent> filterIntents(Iterable<Intent> intents) { |
| 261 | return Tools.stream(intents) |
| 262 | .filter(i -> contentFilter.filter(i)).collect(Collectors.toList()); |
| 263 | } |
| 264 | |
| 265 | /** |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 266 | * Internal local class to keep track of a single type Intent summary. |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 267 | */ |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 268 | private class IntentSummary { |
| 269 | private final String intentType; |
| 270 | private int total = 0; |
| 271 | private int installReq = 0; |
| 272 | private int compiling = 0; |
| 273 | private int installing = 0; |
| 274 | private int installed = 0; |
| 275 | private int recompiling = 0; |
| 276 | private int withdrawReq = 0; |
| 277 | private int withdrawing = 0; |
| 278 | private int withdrawn = 0; |
| 279 | private int failed = 0; |
| 280 | private int unknownState = 0; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 281 | |
| 282 | /** |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 283 | * Creates empty {@link IntentSummary} for specified {@code intentType}. |
| 284 | * |
| 285 | * @param intentType the string describing the Intent type |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 286 | */ |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 287 | IntentSummary(String intentType) { |
| 288 | this.intentType = intentType; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /** |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 292 | * Creates {@link IntentSummary} initialized with given {@code intent}. |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 293 | * |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 294 | * @param intent to initialize with |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 295 | */ |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 296 | IntentSummary(Intent intent) { |
| 297 | // remove "Intent" from intentType label |
| 298 | this(intentType(intent)); |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 299 | if (contentFilter.filter(intent)) { |
| 300 | update(service.getIntentState(intent.key())); |
| 301 | } |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 302 | } |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 303 | |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 304 | // for identity element, when reducing |
| 305 | IntentSummary() { |
| 306 | this.intentType = null; |
| 307 | } |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 308 | |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 309 | /** |
| 310 | * Updates the Intent Summary. |
| 311 | * |
| 312 | * @param intentState the state of the intent |
| 313 | */ |
| 314 | void update(IntentState intentState) { |
| 315 | total++; |
| 316 | switch (intentState) { |
| 317 | case INSTALL_REQ: |
| 318 | installReq++; |
| 319 | break; |
| 320 | case COMPILING: |
| 321 | compiling++; |
| 322 | break; |
| 323 | case INSTALLING: |
| 324 | installing++; |
| 325 | break; |
| 326 | case INSTALLED: |
| 327 | installed++; |
| 328 | break; |
| 329 | case RECOMPILING: |
| 330 | recompiling++; |
| 331 | break; |
| 332 | case WITHDRAW_REQ: |
| 333 | withdrawReq++; |
| 334 | break; |
| 335 | case WITHDRAWING: |
| 336 | withdrawing++; |
| 337 | break; |
| 338 | case WITHDRAWN: |
| 339 | withdrawn++; |
| 340 | break; |
| 341 | case FAILED: |
| 342 | failed++; |
| 343 | break; |
| 344 | default: |
| 345 | unknownState++; |
| 346 | break; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 347 | } |
| 348 | } |
| 349 | |
| 350 | /** |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 351 | * Prints the Intent Summary. |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 352 | * |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 353 | */ |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 354 | StringBuilder summary() { |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 355 | StringBuilder builder = new StringBuilder(); |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 356 | |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 357 | builder.append(format( |
| 358 | "\n%1s%s%14d%14d%14d%14d%14d%14d%14d%14d%14d%14d", |
| 359 | BOLD + intentType + RESET, |
| 360 | Strings.padStart(String.valueOf(total), |
| 361 | (32 - intentType.length()), |
| 362 | ' '), |
| 363 | installed, |
| 364 | withdrawn, |
| 365 | failed, |
| 366 | installReq, |
| 367 | compiling, |
| 368 | installing, |
| 369 | recompiling, |
| 370 | withdrawReq, |
| 371 | withdrawing, |
| 372 | unknownState)); |
| 373 | builder.append('\n').append(SEPARATOR); |
| 374 | |
| 375 | return builder; |
| 376 | } |
| 377 | |
| 378 | StringBuilder miniSummary() { |
| 379 | StringBuilder builder = new StringBuilder(); |
| 380 | builder.append(BOLD).append(intentType).append(RESET) |
| 381 | .append(" (").append(total).append(')').append('\n'); |
| 382 | builder.append('\t') |
| 383 | .append("installed: ").append(installed).append(' ') |
| 384 | .append("withdrawn: ").append(withdrawn).append(' ') |
| 385 | .append("failed: ").append(failed) |
| 386 | .append('\n'); |
| 387 | builder.append('\t') |
| 388 | .append("compiling: ").append(compiling).append(' ') |
| 389 | .append("installing: ").append(installing).append(' ') |
| 390 | .append("recompiling: ").append(recompiling).append(' ') |
| 391 | .append("withdrawing: ").append(withdrawing) |
| 392 | .append('\n'); |
| 393 | builder.append('\t') |
| 394 | .append("installReq: ").append(installReq).append(' ') |
| 395 | .append("withdrawReq: ").append(withdrawReq).append(' ') |
| 396 | .append("unknownState: ").append(unknownState) |
| 397 | .append('\n') |
| 398 | .append('\n'); |
| 399 | return builder; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /** |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 403 | * Gets the JSON representation of the Intent Summary. |
| 404 | * |
| 405 | * @param mapper the object mapper |
| 406 | * @return the JSON representation of the Intent Summary |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 407 | */ |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 408 | JsonNode json(ObjectMapper mapper) { |
| 409 | ObjectNode result = mapper.createObjectNode() |
| 410 | .put("total", total) |
| 411 | .put("installed", installed) |
| 412 | .put("failed", failed) |
| 413 | .put("installReq", installReq) |
| 414 | .put("installing", installing) |
| 415 | .put("compiling", compiling) |
| 416 | .put("recompiling", recompiling) |
| 417 | .put("withdrawReq", withdrawReq) |
| 418 | .put("withdrawing", withdrawing) |
| 419 | .put("withdrawn", withdrawn) |
| 420 | .put("unknownState", unknownState); |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 421 | |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 422 | return result; |
Pavlin Radoslavov | 708e820 | 2014-11-14 17:18:37 -0800 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 426 | /** |
| 427 | * Merges 2 {@link IntentSummary} together. |
| 428 | * |
| 429 | * @param a element to merge |
| 430 | * @param b element to merge |
| 431 | * @return merged {@link IntentSummary} |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 432 | */ |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 433 | IntentSummary merge(IntentSummary a, IntentSummary b) { |
| 434 | IntentSummary m = new IntentSummary(firstNonNull(a.intentType, b.intentType)); |
| 435 | m.total = a.total + b.total; |
| 436 | m.installReq = a.installReq + b.installReq; |
| 437 | m.compiling = a.compiling + b.compiling; |
| 438 | m.installing = a.installing + b.installing; |
| 439 | m.installed = a.installed + b.installed; |
| 440 | m.recompiling = a.recompiling + b.recompiling; |
| 441 | m.withdrawing = a.withdrawing + b.withdrawing; |
| 442 | m.withdrawReq = a.withdrawReq + b.withdrawReq; |
| 443 | m.withdrawn = a.withdrawn + b.withdrawn; |
| 444 | m.failed = a.failed + b.failed; |
| 445 | m.unknownState = a.unknownState + b.unknownState; |
| 446 | return m; |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Returns IntentType string. |
| 451 | * |
| 452 | * @param intent input |
| 453 | * @return IntentType string |
| 454 | */ |
| 455 | private static String intentType(Intent intent) { |
| 456 | return intent.getClass().getSimpleName().replace("Intent", ""); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Build summary of intents per intent type. |
| 461 | * |
| 462 | * @param intents to summarize |
| 463 | * @return summaries per Intent type |
| 464 | */ |
| 465 | private Map<String, IntentSummary> summarize(Iterable<Intent> intents) { |
| 466 | Map<String, List<Intent>> perIntent = Tools.stream(intents) |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 467 | .collect(Collectors.groupingBy(IntentsListCommand::intentType)); |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 468 | |
| 469 | List<IntentSummary> collect = perIntent.values().stream() |
| 470 | .map(il -> |
| 471 | il.stream() |
| 472 | .map(IntentSummary::new) |
| 473 | .reduce(new IntentSummary(), this::merge) |
| 474 | ).collect(Collectors.toList()); |
| 475 | |
| 476 | Map<String, IntentSummary> summaries = new HashMap<>(); |
| 477 | |
| 478 | // individual |
| 479 | collect.forEach(is -> summaries.put(is.intentType, is)); |
| 480 | |
| 481 | // all summarised |
| 482 | summaries.put("All", collect.stream() |
| 483 | .reduce(new IntentSummary("All"), this::merge)); |
| 484 | return summaries; |
| 485 | } |
| 486 | |
| 487 | /** |
| 488 | * Returns detailed information text about a specific intent. |
| 489 | * |
| 490 | * @param intent to print |
| 491 | * @param state of intent |
| 492 | * @return detailed information or "" if {@code state} was null |
| 493 | */ |
| 494 | private StringBuilder detailsFormat(Intent intent, IntentState state) { |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 495 | StringBuilder builder = new StringBuilder(); |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 496 | if (state == null) { |
| 497 | return builder; |
| 498 | } |
Sho SHIMIZU | d7d1800 | 2015-01-21 14:37:14 -0800 | [diff] [blame] | 499 | if (!intent.resources().isEmpty()) { |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 500 | builder.append('\n').append(format(RESOURCES, intent.resources())); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 501 | } |
| 502 | if (intent instanceof ConnectivityIntent) { |
| 503 | ConnectivityIntent ci = (ConnectivityIntent) intent; |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 504 | if (!ci.selector().criteria().isEmpty()) { |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 505 | builder.append('\n').append(format(COMMON_SELECTOR, formatSelector(ci.selector()))); |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 506 | } |
Ray Milkey | 4250735 | 2015-03-20 15:16:10 -0700 | [diff] [blame] | 507 | if (!ci.treatment().allInstructions().isEmpty()) { |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 508 | builder.append('\n').append(format(TREATMENT, ci.treatment().allInstructions())); |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 509 | } |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 510 | if (ci.constraints() != null && !ci.constraints().isEmpty()) { |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 511 | builder.append('\n').append(format(CONSTRAINTS, ci.constraints())); |
Thomas Vachuska | edc944c | 2014-11-04 15:42:25 -0800 | [diff] [blame] | 512 | } |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Pavlin Radoslavov | aab51a3 | 2014-12-08 11:07:38 -0800 | [diff] [blame] | 515 | if (intent instanceof HostToHostIntent) { |
| 516 | HostToHostIntent pi = (HostToHostIntent) intent; |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 517 | builder.append('\n').append(format(SRC + HOST, pi.one())); |
| 518 | builder.append('\n').append(format(DST + HOST, pi.two())); |
Pavlin Radoslavov | aab51a3 | 2014-12-08 11:07:38 -0800 | [diff] [blame] | 519 | } else if (intent instanceof PointToPointIntent) { |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 520 | PointToPointIntent pi = (PointToPointIntent) intent; |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 521 | builder.append('\n').append(formatFilteredCps(Sets.newHashSet(pi.filteredIngressPoint()), INGRESS)); |
| 522 | builder.append('\n').append(formatFilteredCps(Sets.newHashSet(pi.filteredEgressPoint()), EGRESS)); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 523 | } else if (intent instanceof MultiPointToSinglePointIntent) { |
| 524 | MultiPointToSinglePointIntent pi = (MultiPointToSinglePointIntent) intent; |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 525 | builder.append('\n').append(formatFilteredCps(pi.filteredIngressPoints(), INGRESS)); |
| 526 | builder.append('\n').append(formatFilteredCps(Sets.newHashSet(pi.filteredEgressPoint()), EGRESS)); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 527 | } else if (intent instanceof SinglePointToMultiPointIntent) { |
| 528 | SinglePointToMultiPointIntent pi = (SinglePointToMultiPointIntent) intent; |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 529 | builder.append('\n').append(formatFilteredCps(Sets.newHashSet(pi.filteredIngressPoint()), INGRESS)); |
| 530 | builder.append('\n').append(formatFilteredCps(pi.filteredEgressPoints(), EGRESS)); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 531 | } else if (intent instanceof PathIntent) { |
| 532 | PathIntent pi = (PathIntent) intent; |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 533 | builder.append(format("path=%s, cost=%f", pi.path().links(), pi.path().cost())); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 534 | } else if (intent instanceof LinkCollectionIntent) { |
| 535 | LinkCollectionIntent li = (LinkCollectionIntent) intent; |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 536 | builder.append('\n').append(format("links=%s", li.links())); |
| 537 | builder.append('\n').append(format(CP, li.egressPoints())); |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 538 | } else if (intent instanceof OpticalCircuitIntent) { |
| 539 | OpticalCircuitIntent ci = (OpticalCircuitIntent) intent; |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 540 | builder.append('\n').append(format("src=%s, dst=%s", ci.getSrc(), ci.getDst())); |
Andrea Campanella | 449e63c | 2017-04-20 18:25:14 +0200 | [diff] [blame] | 541 | builder.append('\n').append(format("signal type=%s", ci.getSignalType())); |
| 542 | builder.append('\n').append(format("bidirectional=%s", ci.isBidirectional())); |
Rimon Ashkenazy | f069970 | 2016-01-17 19:28:49 +0200 | [diff] [blame] | 543 | } else if (intent instanceof OpticalConnectivityIntent) { |
| 544 | OpticalConnectivityIntent ci = (OpticalConnectivityIntent) intent; |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 545 | builder.append('\n').append(format("src=%s, dst=%s", ci.getSrc(), ci.getDst())); |
Andrea Campanella | 449e63c | 2017-04-20 18:25:14 +0200 | [diff] [blame] | 546 | builder.append('\n').append(format("signal type=%s", ci.getSignalType())); |
| 547 | builder.append('\n').append(format("bidirectional=%s", ci.isBidirectional())); |
Marc De Leenheer | adfeffd | 2017-06-22 16:05:34 -0700 | [diff] [blame] | 548 | builder.append('\n').append(format("ochSignal=%s", ci.ochSignal())); |
Rimon Ashkenazy | 27438ff | 2016-03-22 15:57:45 +0200 | [diff] [blame] | 549 | } else if (intent instanceof OpticalOduIntent) { |
| 550 | OpticalOduIntent ci = (OpticalOduIntent) intent; |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 551 | builder.append('\n').append(format("src=%s, dst=%s", ci.getSrc(), ci.getDst())); |
Andrea Campanella | 449e63c | 2017-04-20 18:25:14 +0200 | [diff] [blame] | 552 | builder.append('\n').append(format("signal type=%s", ci.getSignalType())); |
| 553 | builder.append('\n').append(format("bidirectional=%s", ci.isBidirectional())); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 554 | } |
Thomas Vachuska | 10d4abc | 2014-10-21 12:47:26 -0700 | [diff] [blame] | 555 | |
Ray Milkey | f9af43c | 2015-02-09 16:45:48 -0800 | [diff] [blame] | 556 | List<Intent> installable = service.getInstallableIntents(intent.key()); |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 557 | installable.stream().filter(i -> contentFilter.filter(i)); |
Brian O'Connor | fe0f4b1 | 2014-10-30 21:19:02 -0700 | [diff] [blame] | 558 | if (showInstallable && installable != null && !installable.isEmpty()) { |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 559 | builder.append('\n').append(format(INSTALLABLE, installable)); |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 560 | } |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 561 | return builder; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 562 | } |
| 563 | |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 564 | /* |
| 565 | * Prints out a formatted string, given a list of connect points. |
| 566 | */ |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 567 | private StringBuilder formatFilteredCps(Set<FilteredConnectPoint> fCps, String prefix) { |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 568 | StringBuilder builder = new StringBuilder(); |
| 569 | builder.append(prefix); |
| 570 | builder.append(FILTERED_CPS); |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 571 | fCps.forEach(fCp -> builder.append('\n').append(formatFilteredCp(fCp))); |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 572 | |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 573 | return builder; |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Prints out a formatted string, given a filtered connect point. |
| 578 | */ |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 579 | private StringBuilder formatFilteredCp(FilteredConnectPoint fCp) { |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 580 | ConnectPoint connectPoint = fCp.connectPoint(); |
| 581 | TrafficSelector selector = fCp.trafficSelector(); |
| 582 | StringBuilder builder = new StringBuilder(); |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 583 | builder.append(INDENTATION).append(format(CP, connectPoint)); |
| 584 | builder.append(SPACE).append(format(SELECTOR, formatSelector(selector))); |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 585 | |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 586 | return builder; |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Prints out a formatted string, given a traffic selector |
| 591 | */ |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 592 | private StringBuilder formatSelector(TrafficSelector ts) { |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 593 | StringBuilder builder = new StringBuilder(); |
| 594 | List<Criterion> criteria = Lists.newArrayList(ts.criteria()); |
| 595 | |
| 596 | if (criteria == null || criteria.isEmpty()) { |
| 597 | builder.append(INHERITED); |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 598 | return builder; |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | criteria.forEach(c -> { |
| 602 | builder.append(c.toString()); |
| 603 | if (criteria.indexOf(c) < criteria.size() - 1) { |
| 604 | builder.append(", "); |
| 605 | } |
| 606 | }); |
| 607 | |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 608 | return builder; |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 609 | } |
| 610 | |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 611 | /* |
| 612 | * Prints information about the intent state, given an intent. |
| 613 | */ |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 614 | private StringBuilder fullFormat(Intent intent, IntentState state) { |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 615 | StringBuilder builder = new StringBuilder(); |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 616 | builder.append(format(ID, intent.id())); |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 617 | if (state != null) { |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 618 | builder.append('\n').append(format(STATE, state)); |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 619 | } |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 620 | builder.append('\n').append(format(KEY, intent.key())); |
| 621 | builder.append('\n').append(format(TYPE, intent.getClass().getSimpleName())); |
| 622 | builder.append('\n').append(format(APP_ID, intent.appId().name())); |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 623 | |
Yuta HIGUCHI | f0e09cd | 2017-01-27 10:52:36 -0800 | [diff] [blame] | 624 | return builder; |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 625 | } |
| 626 | |
Luca Prete | 5db2e87 | 2016-12-07 18:31:00 -0800 | [diff] [blame] | 627 | /* |
| 628 | * Produces a JSON array from the intents specified. |
| 629 | */ |
| 630 | private JsonNode json(Iterable<Intent> intents) { |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 631 | ObjectMapper mapper = new ObjectMapper(); |
| 632 | ArrayNode result = mapper.createArrayNode(); |
Carolina Fernandez | 0b1449d | 2016-12-04 13:30:36 +0100 | [diff] [blame] | 633 | Tools.stream(intents) |
Carolina Fernandez | fa56d14 | 2016-11-14 01:13:26 +0100 | [diff] [blame] | 634 | .filter(intent -> contentFilter.filter(jsonForEntity(intent, Intent.class).toString())) |
| 635 | .forEach(intent -> result.add(jsonForEntity(intent, Intent.class))); |
Thomas Vachuska | 6ce7304 | 2014-10-21 10:01:49 -0700 | [diff] [blame] | 636 | return result; |
| 637 | } |
tom | f5c9d92 | 2014-10-03 15:22:03 -0700 | [diff] [blame] | 638 | } |