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