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; |
| 20 | import org.onosproject.core.ApplicationId; |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 21 | import org.onosproject.net.ConnectPoint; |
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; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 36 | import org.onosproject.ui.table.AbstractTableRow; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 37 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 38 | import org.onosproject.ui.table.TableRow; |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 39 | |
| 40 | import java.util.ArrayList; |
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 | |
| 61 | @Override |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 62 | protected Collection<RequestHandler> getHandlers() { |
| 63 | return ImmutableSet.of(new IntentDataRequest()); |
| 64 | } |
| 65 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 66 | // handler for intent table requests |
| 67 | private final class IntentDataRequest extends TableRequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 68 | private IntentDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 69 | super(INTENT_DATA_REQ, INTENT_DATA_RESP, INTENTS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 73 | protected TableRow[] generateTableRows(ObjectNode payload) { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 74 | IntentService service = get(IntentService.class); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 75 | List<TableRow> list = new ArrayList<>(); |
| 76 | for (Intent intent : service.getIntents()) { |
| 77 | list.add(new IntentTableRow(intent)); |
| 78 | } |
| 79 | return list.toArray(new TableRow[list.size()]); |
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 |
| 83 | protected String defaultColId() { |
| 84 | return APP_ID; |
| 85 | } |
| 86 | } |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 87 | |
| 88 | /** |
| 89 | * TableRow implementation for {@link Intent intents}. |
| 90 | */ |
| 91 | private static class IntentTableRow extends AbstractTableRow { |
| 92 | |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 93 | private static final String[] COL_IDS = { |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 94 | APP_ID, KEY, TYPE, PRIORITY, RESOURCES, DETAILS |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 95 | }; |
| 96 | |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 97 | private StringBuilder details = new StringBuilder(); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 98 | |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 99 | private void appendMultiPointsDetails(Set<ConnectPoint> points) { |
| 100 | for (ConnectPoint point : points) { |
| 101 | details.append(point.elementId()) |
| 102 | .append('/') |
| 103 | .append(point.port()) |
| 104 | .append(' '); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private void buildConnectivityDetails(ConnectivityIntent intent) { |
| 109 | Set<Criterion> criteria = intent.selector().criteria(); |
| 110 | List<Instruction> instructions = intent.treatment().allInstructions(); |
| 111 | List<Constraint> constraints = intent.constraints(); |
| 112 | |
| 113 | if (!criteria.isEmpty()) { |
| 114 | details.append("selector=").append(criteria); |
| 115 | } |
| 116 | if (!instructions.isEmpty()) { |
| 117 | details.append("treatment=").append(instructions); |
| 118 | } |
| 119 | if (constraints != null && !constraints.isEmpty()) { |
| 120 | details.append("constraints=").append(constraints); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | private void buildHostToHostDetails(HostToHostIntent intent) { |
| 125 | details.append(" host1=") |
| 126 | .append(intent.one()) |
| 127 | .append(", host2=") |
| 128 | .append(intent.two()); |
| 129 | } |
| 130 | |
| 131 | private void buildPointToPointDetails(PointToPointIntent intent) { |
| 132 | ConnectPoint ingress = intent.ingressPoint(); |
| 133 | ConnectPoint egress = intent.egressPoint(); |
| 134 | details.append(" ingress=") |
| 135 | .append(ingress.elementId()) |
| 136 | .append('/') |
| 137 | .append(ingress.port()) |
| 138 | |
| 139 | .append(", egress=") |
| 140 | .append(egress.elementId()) |
| 141 | .append('/') |
| 142 | .append(egress.port()) |
| 143 | .append(' '); |
| 144 | } |
| 145 | |
| 146 | private void buildMPToSPDetails(MultiPointToSinglePointIntent intent) { |
| 147 | ConnectPoint egress = intent.egressPoint(); |
| 148 | |
| 149 | details.append(" ingress="); |
| 150 | appendMultiPointsDetails(intent.ingressPoints()); |
| 151 | |
| 152 | details.append(", egress=") |
| 153 | .append(egress.elementId()) |
| 154 | .append('/') |
| 155 | .append(egress.port()) |
| 156 | .append(' '); |
| 157 | } |
| 158 | |
| 159 | private void buildSPToMPDetails(SinglePointToMultiPointIntent intent) { |
| 160 | ConnectPoint ingress = intent.ingressPoint(); |
| 161 | |
| 162 | details.append(" ingress=") |
| 163 | .append(ingress.elementId()) |
| 164 | .append('/') |
| 165 | .append(ingress.port()) |
| 166 | .append(", egress="); |
| 167 | |
| 168 | appendMultiPointsDetails(intent.egressPoints()); |
| 169 | } |
| 170 | |
| 171 | private void buildPathDetails(PathIntent intent) { |
| 172 | details.append(" path=") |
| 173 | .append(intent.path().links()) |
| 174 | .append(", cost=") |
| 175 | .append(intent.path().cost()); |
| 176 | } |
| 177 | |
| 178 | private void buildLinkConnectionDetails(LinkCollectionIntent intent) { |
| 179 | details.append(" links=") |
| 180 | .append(intent.links()) |
| 181 | .append(", egress="); |
| 182 | |
| 183 | appendMultiPointsDetails(intent.egressPoints()); |
| 184 | } |
| 185 | |
| 186 | private String formatDetails(Intent intent) { |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 187 | if (intent instanceof ConnectivityIntent) { |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 188 | buildConnectivityDetails((ConnectivityIntent) intent); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | if (intent instanceof HostToHostIntent) { |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 192 | buildHostToHostDetails((HostToHostIntent) intent); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 193 | |
| 194 | } else if (intent instanceof PointToPointIntent) { |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 195 | buildPointToPointDetails((PointToPointIntent) intent); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 196 | |
| 197 | } else if (intent instanceof MultiPointToSinglePointIntent) { |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 198 | buildMPToSPDetails((MultiPointToSinglePointIntent) intent); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 199 | |
| 200 | } else if (intent instanceof SinglePointToMultiPointIntent) { |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 201 | buildSPToMPDetails((SinglePointToMultiPointIntent) intent); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 202 | |
| 203 | } else if (intent instanceof PathIntent) { |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 204 | buildPathDetails((PathIntent) intent); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 205 | |
| 206 | } else if (intent instanceof LinkCollectionIntent) { |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 207 | buildLinkConnectionDetails((LinkCollectionIntent) intent); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 208 | } |
| 209 | |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 210 | if (details.length() == 0) { |
| 211 | details.append("(No details for this intent)"); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 212 | } else { |
| 213 | details.insert(0, "Details: "); |
| 214 | } |
| 215 | return details.toString(); |
| 216 | } |
| 217 | |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 218 | private String formatResources(Intent intent) { |
| 219 | return (intent.resources().isEmpty() ? |
| 220 | "(No resources for this intent)" : |
| 221 | "Resources: " + intent.resources()); |
Bri Prebilic Cole | 2bd8c35 | 2015-03-31 16:18:50 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 224 | public IntentTableRow(Intent intent) { |
| 225 | ApplicationId appid = intent.appId(); |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 226 | |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 227 | add(APP_ID, concat(appid.id(), " : ", appid.name())); |
| 228 | add(KEY, intent.key()); |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 229 | add(TYPE, intent.getClass().getSimpleName()); |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 230 | add(PRIORITY, intent.priority()); |
Bri Prebilic Cole | d5df254 | 2015-04-01 10:36:09 -0700 | [diff] [blame] | 231 | add(RESOURCES, formatResources(intent)); |
| 232 | add(DETAILS, formatDetails(intent)); |
Bri Prebilic Cole | 96f2647 | 2015-03-31 13:07:05 -0700 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | @Override |
| 236 | protected String[] columnIds() { |
| 237 | return COL_IDS; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | } |