dvaddire | 95c84ed | 2017-06-14 15:42:24 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.net.intent.util; |
| 17 | |
| 18 | import org.onlab.util.Tools; |
| 19 | import org.onosproject.net.intent.Intent; |
| 20 | import org.onosproject.net.intent.IntentService; |
| 21 | import org.onosproject.net.intent.IntentState; |
| 22 | |
| 23 | import java.util.HashMap; |
| 24 | import java.util.List; |
| 25 | import java.util.Map; |
| 26 | import java.util.stream.Collectors; |
| 27 | |
| 28 | import static com.google.common.base.MoreObjects.firstNonNull; |
| 29 | |
| 30 | /** |
| 31 | * Lists the summary of intents and their states. |
| 32 | */ |
| 33 | public final class IntentMiniSummary { |
| 34 | |
| 35 | private String intentType; |
| 36 | private int total = 0; |
| 37 | private int installReq = 0; |
| 38 | private int compiling = 0; |
| 39 | private int installing = 0; |
| 40 | private int installed = 0; |
| 41 | private int recompiling = 0; |
| 42 | private int withdrawReq = 0; |
| 43 | private int withdrawing = 0; |
| 44 | private int withdrawn = 0; |
| 45 | private int failed = 0; |
| 46 | private int unknownState = 0; |
| 47 | |
| 48 | IntentMiniSummary(Intent intent, IntentService intentService) { |
| 49 | // remove "Intent" from intentType label |
| 50 | this.intentType = intentType(intent); |
| 51 | update(intentService.getIntentState(intent.key())); |
| 52 | } |
| 53 | |
| 54 | IntentMiniSummary(String intentType) { |
| 55 | // remove "Intent" from intentType label |
| 56 | this.intentType = intentType; |
| 57 | } |
| 58 | |
| 59 | public IntentMiniSummary() { |
| 60 | |
| 61 | } |
| 62 | |
| 63 | private static String intentType(Intent intent) { |
| 64 | return intent.getClass().getSimpleName().replace("Intent", ""); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Returns intent Type. |
| 69 | * @return intentType |
| 70 | */ |
| 71 | public String getIntentType() { |
| 72 | return intentType; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Returns total intent count. |
| 77 | * @return total |
| 78 | */ |
| 79 | public int getTotal() { |
| 80 | return total; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Returns InstallReq intent count. |
| 85 | * @return InstallReq |
| 86 | */ |
| 87 | public int getInstallReq() { |
| 88 | return installReq; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns Compiling intent count. |
| 93 | * @return Compiling |
| 94 | */ |
| 95 | public int getCompiling() { |
| 96 | return compiling; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Returns Installing intent count. |
| 101 | * @return Installing |
| 102 | */ |
| 103 | public int getInstalling() { |
| 104 | return installing; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns Installed intent count. |
| 109 | * @return Installed |
| 110 | */ |
| 111 | public int getInstalled() { |
| 112 | return installed; |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Returns Recompiling intent count. |
| 117 | * @return Recompiling |
| 118 | */ |
| 119 | public int getRecompiling() { |
| 120 | return recompiling; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Returns WithdrawReq intent count. |
| 125 | * @return WithdrawReq |
| 126 | */ |
| 127 | public int getWithdrawReq() { |
| 128 | return withdrawReq; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Returns Withdrawing intent count. |
| 133 | * @return Withdrawing |
| 134 | */ |
| 135 | public int getWithdrawing() { |
| 136 | return withdrawing; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Returns Withdrawn intent count. |
| 141 | * @return Withdrawn |
| 142 | */ |
| 143 | public int getWithdrawn() { |
| 144 | return withdrawn; |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Returns Failed intent count. |
| 149 | * @return Failed |
| 150 | */ |
| 151 | public int getFailed() { |
| 152 | return failed; |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Returns unknownState intent count. |
| 157 | * @return unknownState |
| 158 | */ |
| 159 | public int getUnknownState() { |
| 160 | return unknownState; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Updates the Intent Summary. |
| 165 | * |
| 166 | * @param intentState the state of the intent |
| 167 | */ |
| 168 | public void update(IntentState intentState) { |
| 169 | total++; |
| 170 | switch (intentState) { |
| 171 | case INSTALL_REQ: |
| 172 | installReq++; |
| 173 | break; |
| 174 | case COMPILING: |
| 175 | compiling++; |
| 176 | break; |
| 177 | case INSTALLING: |
| 178 | installing++; |
| 179 | break; |
| 180 | case INSTALLED: |
| 181 | installed++; |
| 182 | break; |
| 183 | case RECOMPILING: |
| 184 | recompiling++; |
| 185 | break; |
| 186 | case WITHDRAW_REQ: |
| 187 | withdrawReq++; |
| 188 | break; |
| 189 | case WITHDRAWING: |
| 190 | withdrawing++; |
| 191 | break; |
| 192 | case WITHDRAWN: |
| 193 | withdrawn++; |
| 194 | break; |
| 195 | case FAILED: |
| 196 | failed++; |
| 197 | break; |
| 198 | default: |
| 199 | unknownState++; |
| 200 | break; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Build summary of intents per intent type. |
| 206 | * |
| 207 | * @param intents to summarize |
| 208 | * @param intentService to get IntentState |
| 209 | * @return summaries per Intent type |
| 210 | */ |
| 211 | public Map<String, IntentMiniSummary> summarize(Iterable<Intent> intents, IntentService intentService) { |
| 212 | Map<String, List<Intent>> perIntent = Tools.stream(intents) |
| 213 | .collect(Collectors.groupingBy(IntentMiniSummary::intentType)); |
| 214 | |
| 215 | List<IntentMiniSummary> collect = perIntent.values().stream() |
| 216 | .map(il -> |
| 217 | il.stream() |
| 218 | .map(intent -> new IntentMiniSummary(intent, intentService)) |
| 219 | .reduce(new IntentMiniSummary(), this::merge) |
| 220 | ).collect(Collectors.toList()); |
| 221 | |
| 222 | Map<String, IntentMiniSummary> summaries = new HashMap<>(); |
| 223 | |
| 224 | // individual |
| 225 | collect.forEach(is -> summaries.put(is.intentType, is)); |
| 226 | |
| 227 | // all summarised |
| 228 | summaries.put("All", collect.stream() |
| 229 | .reduce(new IntentMiniSummary("All"), this::merge)); |
| 230 | return summaries; |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Merges 2 {@link IntentMiniSummary} together. |
| 235 | * |
| 236 | * @param a element to merge |
| 237 | * @param b element to merge |
| 238 | * @return merged {@link IntentMiniSummary} |
| 239 | */ |
| 240 | IntentMiniSummary merge(IntentMiniSummary a, IntentMiniSummary b) { |
| 241 | IntentMiniSummary m = new IntentMiniSummary(firstNonNull(a.getIntentType(), b.getIntentType())); |
| 242 | m.total = a.total + b.total; |
| 243 | m.installReq = a.installReq + b.installReq; |
| 244 | m.compiling = a.compiling + b.compiling; |
| 245 | m.installing = a.installing + b.installing; |
| 246 | m.installed = a.installed + b.installed; |
| 247 | m.recompiling = a.recompiling + b.recompiling; |
| 248 | m.withdrawing = a.withdrawing + b.withdrawing; |
| 249 | m.withdrawReq = a.withdrawReq + b.withdrawReq; |
| 250 | m.withdrawn = a.withdrawn + b.withdrawn; |
| 251 | m.failed = a.failed + b.failed; |
| 252 | m.unknownState = a.unknownState + b.unknownState; |
| 253 | return m; |
| 254 | } |
| 255 | } |