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