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