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