Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [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.ui.impl; |
| 17 | |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.collect.ImmutableSet; |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 20 | import org.onosproject.net.ConnectPoint; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 21 | import org.onosproject.net.NetworkResource; |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 22 | import org.onosproject.net.flow.criteria.Criterion; |
| 23 | import org.onosproject.net.flow.instructions.Instruction; |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 24 | import org.onosproject.net.intent.ConnectivityIntent; |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 25 | import org.onosproject.net.intent.Constraint; |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 26 | import org.onosproject.net.intent.HostToHostIntent; |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 27 | import org.onosproject.net.intent.Intent; |
| 28 | import org.onosproject.net.intent.IntentService; |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 29 | import org.onosproject.net.intent.LinkCollectionIntent; |
| 30 | import org.onosproject.net.intent.MultiPointToSinglePointIntent; |
| 31 | import org.onosproject.net.intent.PathIntent; |
| 32 | import org.onosproject.net.intent.PointToPointIntent; |
| 33 | import org.onosproject.net.intent.SinglePointToMultiPointIntent; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.UiMessageHandler; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 36 | import org.onosproject.ui.table.CellFormatter; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 37 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 38 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 39 | import org.onosproject.ui.table.cell.AppIdFormatter; |
Thomas Vachuska | d8b0789 | 2015-05-27 16:56:45 -0700 | [diff] [blame] | 40 | import org.onosproject.ui.table.cell.EnumFormatter; |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 41 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 42 | import java.util.Collection; |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 43 | import java.util.List; |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 44 | import java.util.Set; |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Message handler for intent view related messages. |
| 48 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 49 | public class IntentViewMessageHandler extends UiMessageHandler { |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 50 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 51 | private static final String INTENT_DATA_REQ = "intentDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 52 | private static final String INTENT_DATA_RESP = "intentDataResponse"; |
| 53 | private static final String INTENTS = "intents"; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 54 | |
Bri Prebilic Cole | 43f17c0 | 2015-05-01 10:43:38 -0700 | [diff] [blame] | 55 | private static final String APP_ID = "appId"; |
| 56 | private static final String KEY = "key"; |
| 57 | private static final String TYPE = "type"; |
| 58 | private static final String PRIORITY = "priority"; |
Thomas Vachuska | d8b0789 | 2015-05-27 16:56:45 -0700 | [diff] [blame] | 59 | private static final String STATE = "state"; |
Bri Prebilic Cole | 43f17c0 | 2015-05-01 10:43:38 -0700 | [diff] [blame] | 60 | private static final String RESOURCES = "resources"; |
| 61 | private static final String DETAILS = "details"; |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 62 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 63 | private static final String[] COL_IDS = { |
Thomas Vachuska | d8b0789 | 2015-05-27 16:56:45 -0700 | [diff] [blame] | 64 | APP_ID, KEY, TYPE, PRIORITY, STATE, RESOURCES, DETAILS |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 67 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 68 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 69 | return ImmutableSet.of(new IntentDataRequest()); |
| 70 | } |
| 71 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 72 | // handler for intent table requests |
| 73 | private final class IntentDataRequest extends TableRequestHandler { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 74 | private static final String NO_ROWS_MESSAGE = "No intents found"; |
| 75 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 76 | private IntentDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 77 | super(INTENT_DATA_REQ, INTENT_DATA_RESP, INTENTS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 81 | protected String defaultColumnId() { |
| 82 | return APP_ID; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 83 | } |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 84 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 85 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 86 | protected String[] getColumnIds() { |
| 87 | return COL_IDS; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 88 | } |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 89 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 90 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 91 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 92 | return NO_ROWS_MESSAGE; |
| 93 | } |
| 94 | |
| 95 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 96 | protected TableModel createTableModel() { |
| 97 | TableModel tm = super.createTableModel(); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 98 | tm.setFormatter(APP_ID, AppIdFormatter.INSTANCE); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 99 | tm.setFormatter(RESOURCES, new ResourcesFormatter()); |
| 100 | tm.setFormatter(DETAILS, new DetailsFormatter()); |
Thomas Vachuska | d8b0789 | 2015-05-27 16:56:45 -0700 | [diff] [blame] | 101 | tm.setFormatter(STATE, EnumFormatter.INSTANCE); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 102 | return tm; |
| 103 | } |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 104 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 105 | @Override |
| 106 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 107 | IntentService is = get(IntentService.class); |
| 108 | for (Intent intent : is.getIntents()) { |
Thomas Vachuska | d8b0789 | 2015-05-27 16:56:45 -0700 | [diff] [blame] | 109 | populateRow(tm.addRow(), intent, is); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 112 | |
Thomas Vachuska | d8b0789 | 2015-05-27 16:56:45 -0700 | [diff] [blame] | 113 | private void populateRow(TableModel.Row row, Intent intent, IntentService is) { |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 114 | row.cell(APP_ID, intent.appId()) |
Simon Hunt | 8a0429a | 2017-01-06 16:52:47 -0800 | [diff] [blame] | 115 | .cell(KEY, intent.key()) |
| 116 | .cell(TYPE, intent.getClass().getSimpleName()) |
| 117 | .cell(PRIORITY, intent.priority()) |
| 118 | .cell(STATE, is.getIntentState(intent.key())) |
| 119 | .cell(RESOURCES, intent) |
| 120 | .cell(DETAILS, intent); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 123 | private final class ResourcesFormatter implements CellFormatter { |
| 124 | private static final String COMMA = ", "; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 125 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 126 | @Override |
| 127 | public String format(Object value) { |
| 128 | Intent intent = (Intent) value; |
| 129 | Collection<NetworkResource> resources = intent.resources(); |
| 130 | if (resources.isEmpty()) { |
| 131 | return "(No resources for this intent)"; |
| 132 | } |
| 133 | StringBuilder sb = new StringBuilder("Resources: "); |
| 134 | for (NetworkResource nr : resources) { |
| 135 | sb.append(nr).append(COMMA); |
| 136 | } |
| 137 | removeTrailingComma(sb); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 138 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 139 | return sb.toString(); |
| 140 | } |
| 141 | |
| 142 | private StringBuilder removeTrailingComma(StringBuilder sb) { |
| 143 | int pos = sb.lastIndexOf(COMMA); |
| 144 | sb.delete(pos, sb.length()); |
| 145 | return sb; |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | private final class DetailsFormatter implements CellFormatter { |
| 150 | @Override |
| 151 | public String format(Object value) { |
| 152 | return formatDetails((Intent) value, new StringBuilder()).toString(); |
| 153 | } |
| 154 | |
| 155 | private StringBuilder formatDetails(Intent intent, StringBuilder sb) { |
| 156 | if (intent instanceof ConnectivityIntent) { |
| 157 | buildConnectivityDetails((ConnectivityIntent) intent, sb); |
| 158 | } |
| 159 | |
| 160 | if (intent instanceof HostToHostIntent) { |
| 161 | buildHostToHostDetails((HostToHostIntent) intent, sb); |
| 162 | |
| 163 | } else if (intent instanceof PointToPointIntent) { |
| 164 | buildPointToPointDetails((PointToPointIntent) intent, sb); |
| 165 | |
| 166 | } else if (intent instanceof MultiPointToSinglePointIntent) { |
| 167 | buildMPToSPDetails((MultiPointToSinglePointIntent) intent, sb); |
| 168 | |
| 169 | } else if (intent instanceof SinglePointToMultiPointIntent) { |
| 170 | buildSPToMPDetails((SinglePointToMultiPointIntent) intent, sb); |
| 171 | |
| 172 | } else if (intent instanceof PathIntent) { |
| 173 | buildPathDetails((PathIntent) intent, sb); |
| 174 | |
| 175 | } else if (intent instanceof LinkCollectionIntent) { |
| 176 | buildLinkConnectionDetails((LinkCollectionIntent) intent, sb); |
| 177 | } |
| 178 | |
| 179 | if (sb.length() == 0) { |
| 180 | sb.append("(No details for this intent)"); |
| 181 | } else { |
| 182 | sb.insert(0, "Details: "); |
| 183 | } |
| 184 | return sb; |
| 185 | } |
| 186 | |
| 187 | private void appendMultiPointsDetails(Set<ConnectPoint> points, |
| 188 | StringBuilder sb) { |
| 189 | for (ConnectPoint point : points) { |
| 190 | sb.append(point.elementId()) |
| 191 | .append('/') |
| 192 | .append(point.port()) |
| 193 | .append(' '); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | private void buildConnectivityDetails(ConnectivityIntent intent, |
| 198 | StringBuilder sb) { |
| 199 | Set<Criterion> criteria = intent.selector().criteria(); |
| 200 | List<Instruction> instructions = intent.treatment().allInstructions(); |
| 201 | List<Constraint> constraints = intent.constraints(); |
| 202 | |
| 203 | if (!criteria.isEmpty()) { |
| 204 | sb.append("Selector: ").append(criteria); |
| 205 | } |
| 206 | if (!instructions.isEmpty()) { |
| 207 | sb.append("Treatment: ").append(instructions); |
| 208 | } |
| 209 | if (constraints != null && !constraints.isEmpty()) { |
| 210 | sb.append("Constraints: ").append(constraints); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | private void buildHostToHostDetails(HostToHostIntent intent, |
| 215 | StringBuilder sb) { |
| 216 | sb.append(" Host 1: ") |
| 217 | .append(intent.one()) |
| 218 | .append(", Host 2: ") |
| 219 | .append(intent.two()); |
| 220 | } |
| 221 | |
| 222 | private void buildPointToPointDetails(PointToPointIntent intent, |
| 223 | StringBuilder sb) { |
| 224 | ConnectPoint ingress = intent.ingressPoint(); |
| 225 | ConnectPoint egress = intent.egressPoint(); |
| 226 | sb.append(" Ingress: ") |
| 227 | .append(ingress.elementId()) |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 228 | .append('/') |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 229 | .append(ingress.port()) |
| 230 | |
| 231 | .append(", Egress: ") |
| 232 | .append(egress.elementId()) |
| 233 | .append('/') |
| 234 | .append(egress.port()) |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 235 | .append(' '); |
| 236 | } |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 237 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 238 | private void buildMPToSPDetails(MultiPointToSinglePointIntent intent, |
| 239 | StringBuilder sb) { |
| 240 | ConnectPoint egress = intent.egressPoint(); |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 241 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 242 | sb.append(" Ingress="); |
| 243 | appendMultiPointsDetails(intent.ingressPoints(), sb); |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 244 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 245 | sb.append(", Egress=") |
| 246 | .append(egress.elementId()) |
| 247 | .append('/') |
| 248 | .append(egress.port()) |
| 249 | .append(' '); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 252 | private void buildSPToMPDetails(SinglePointToMultiPointIntent intent, |
| 253 | StringBuilder sb) { |
| 254 | ConnectPoint ingress = intent.ingressPoint(); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 255 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 256 | sb.append(" Ingress=") |
| 257 | .append(ingress.elementId()) |
| 258 | .append('/') |
| 259 | .append(ingress.port()) |
| 260 | .append(", Egress="); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 261 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 262 | appendMultiPointsDetails(intent.egressPoints(), sb); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 265 | private void buildPathDetails(PathIntent intent, StringBuilder sb) { |
| 266 | sb.append(" path=") |
| 267 | .append(intent.path().links()) |
| 268 | .append(", cost=") |
| 269 | .append(intent.path().cost()); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 270 | } |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 271 | |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 272 | private void buildLinkConnectionDetails(LinkCollectionIntent intent, |
| 273 | StringBuilder sb) { |
| 274 | sb.append(" links=") |
| 275 | .append(intent.links()) |
| 276 | .append(", egress="); |
| 277 | |
| 278 | appendMultiPointsDetails(intent.egressPoints(), sb); |
| 279 | } |
| 280 | |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 281 | } |
| 282 | } |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 283 | } |