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