blob: 2af458b90cbb5f7749b9fbfbdea07f2959793279 [file] [log] [blame]
Bri Prebilic Cole96f26472015-03-31 13:07:05 -07001/*
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 */
16package org.onosproject.ui.impl;
17
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070018import com.fasterxml.jackson.databind.node.ObjectNode;
19import com.google.common.collect.ImmutableSet;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070020import org.onosproject.net.ConnectPoint;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070021import org.onosproject.net.NetworkResource;
Bri Prebilic Coled5df2542015-04-01 10:36:09 -070022import org.onosproject.net.flow.criteria.Criterion;
23import org.onosproject.net.flow.instructions.Instruction;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070024import org.onosproject.net.intent.ConnectivityIntent;
Bri Prebilic Coled5df2542015-04-01 10:36:09 -070025import org.onosproject.net.intent.Constraint;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070026import org.onosproject.net.intent.HostToHostIntent;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070027import org.onosproject.net.intent.Intent;
28import org.onosproject.net.intent.IntentService;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070029import org.onosproject.net.intent.LinkCollectionIntent;
30import org.onosproject.net.intent.MultiPointToSinglePointIntent;
31import org.onosproject.net.intent.PathIntent;
32import org.onosproject.net.intent.PointToPointIntent;
33import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Simon Huntd2747a02015-04-30 22:41:16 -070034import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070035import org.onosproject.ui.UiMessageHandler;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070036import org.onosproject.ui.table.CellFormatter;
Simon Hunt3d1b0652015-05-05 17:27:24 -070037import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070038import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070039import org.onosproject.ui.table.cell.AppIdFormatter;
40import org.onosproject.ui.table.cell.IntComparator;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070041
Simon Huntd2747a02015-04-30 22:41:16 -070042import java.util.Collection;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070043import java.util.List;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070044import java.util.Set;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070045
46/**
47 * Message handler for intent view related messages.
48 */
Simon Hunta0ddb022015-05-01 09:53:01 -070049public class IntentViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070050
Simon Huntd2747a02015-04-30 22:41:16 -070051 private static final String INTENT_DATA_REQ = "intentDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070052 private static final String INTENT_DATA_RESP = "intentDataResponse";
53 private static final String INTENTS = "intents";
Simon Huntd2747a02015-04-30 22:41:16 -070054
Bri Prebilic Cole43f17c02015-05-01 10:43:38 -070055 private static final String APP_ID = "appId";
56 private static final String KEY = "key";
57 private static final String TYPE = "type";
58 private static final String PRIORITY = "priority";
59 private static final String RESOURCES = "resources";
60 private static final String DETAILS = "details";
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070061
Simon Hunt3d1b0652015-05-05 17:27:24 -070062 private static final String[] COL_IDS = {
63 APP_ID, KEY, TYPE, PRIORITY, RESOURCES, DETAILS
64 };
65
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070066 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070067 protected Collection<RequestHandler> getHandlers() {
68 return ImmutableSet.of(new IntentDataRequest());
69 }
70
Simon Huntabd16f62015-05-01 13:14:40 -070071 // handler for intent table requests
72 private final class IntentDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070073 private IntentDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070074 super(INTENT_DATA_REQ, INTENT_DATA_RESP, INTENTS);
Simon Huntd2747a02015-04-30 22:41:16 -070075 }
76
77 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070078 protected String defaultColumnId() {
79 return APP_ID;
Simon Hunt44aa2f82015-04-30 15:01:35 -070080 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070081
Simon Huntabd16f62015-05-01 13:14:40 -070082 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070083 protected String[] getColumnIds() {
84 return COL_IDS;
Simon Huntabd16f62015-05-01 13:14:40 -070085 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070086
Simon Hunt3d1b0652015-05-05 17:27:24 -070087 @Override
88 protected TableModel createTableModel() {
89 TableModel tm = super.createTableModel();
90 tm.setComparator(PRIORITY, IntComparator.INSTANCE);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070091
Simon Hunt3d1b0652015-05-05 17:27:24 -070092 tm.setFormatter(APP_ID, AppIdFormatter.INSTANCE);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070093 tm.setFormatter(RESOURCES, new ResourcesFormatter());
94 tm.setFormatter(DETAILS, new DetailsFormatter());
Simon Hunt3d1b0652015-05-05 17:27:24 -070095 return tm;
96 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070097
Simon Hunt3d1b0652015-05-05 17:27:24 -070098 @Override
99 protected void populateTable(TableModel tm, ObjectNode payload) {
100 IntentService is = get(IntentService.class);
101 for (Intent intent : is.getIntents()) {
102 populateRow(tm.addRow(), intent);
103 }
104 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700105
Simon Hunt3d1b0652015-05-05 17:27:24 -0700106 private void populateRow(TableModel.Row row, Intent intent) {
107 row.cell(APP_ID, intent.appId())
108 .cell(KEY, intent.key())
109 .cell(TYPE, intent.getClass().getSimpleName())
110 .cell(PRIORITY, intent.priority())
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700111 .cell(RESOURCES, intent)
112 .cell(DETAILS, intent);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700113 }
114
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700115 private final class ResourcesFormatter implements CellFormatter {
116 private static final String COMMA = ", ";
Simon Hunt3d1b0652015-05-05 17:27:24 -0700117
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700118 @Override
119 public String format(Object value) {
120 Intent intent = (Intent) value;
121 Collection<NetworkResource> resources = intent.resources();
122 if (resources.isEmpty()) {
123 return "(No resources for this intent)";
124 }
125 StringBuilder sb = new StringBuilder("Resources: ");
126 for (NetworkResource nr : resources) {
127 sb.append(nr).append(COMMA);
128 }
129 removeTrailingComma(sb);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700130
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700131 return sb.toString();
132 }
133
134 private StringBuilder removeTrailingComma(StringBuilder sb) {
135 int pos = sb.lastIndexOf(COMMA);
136 sb.delete(pos, sb.length());
137 return sb;
138 }
139 }
140
141 private final class DetailsFormatter implements CellFormatter {
142 @Override
143 public String format(Object value) {
144 return formatDetails((Intent) value, new StringBuilder()).toString();
145 }
146
147 private StringBuilder formatDetails(Intent intent, StringBuilder sb) {
148 if (intent instanceof ConnectivityIntent) {
149 buildConnectivityDetails((ConnectivityIntent) intent, sb);
150 }
151
152 if (intent instanceof HostToHostIntent) {
153 buildHostToHostDetails((HostToHostIntent) intent, sb);
154
155 } else if (intent instanceof PointToPointIntent) {
156 buildPointToPointDetails((PointToPointIntent) intent, sb);
157
158 } else if (intent instanceof MultiPointToSinglePointIntent) {
159 buildMPToSPDetails((MultiPointToSinglePointIntent) intent, sb);
160
161 } else if (intent instanceof SinglePointToMultiPointIntent) {
162 buildSPToMPDetails((SinglePointToMultiPointIntent) intent, sb);
163
164 } else if (intent instanceof PathIntent) {
165 buildPathDetails((PathIntent) intent, sb);
166
167 } else if (intent instanceof LinkCollectionIntent) {
168 buildLinkConnectionDetails((LinkCollectionIntent) intent, sb);
169 }
170
171 if (sb.length() == 0) {
172 sb.append("(No details for this intent)");
173 } else {
174 sb.insert(0, "Details: ");
175 }
176 return sb;
177 }
178
179 private void appendMultiPointsDetails(Set<ConnectPoint> points,
180 StringBuilder sb) {
181 for (ConnectPoint point : points) {
182 sb.append(point.elementId())
183 .append('/')
184 .append(point.port())
185 .append(' ');
186 }
187 }
188
189 private void buildConnectivityDetails(ConnectivityIntent intent,
190 StringBuilder sb) {
191 Set<Criterion> criteria = intent.selector().criteria();
192 List<Instruction> instructions = intent.treatment().allInstructions();
193 List<Constraint> constraints = intent.constraints();
194
195 if (!criteria.isEmpty()) {
196 sb.append("Selector: ").append(criteria);
197 }
198 if (!instructions.isEmpty()) {
199 sb.append("Treatment: ").append(instructions);
200 }
201 if (constraints != null && !constraints.isEmpty()) {
202 sb.append("Constraints: ").append(constraints);
203 }
204 }
205
206 private void buildHostToHostDetails(HostToHostIntent intent,
207 StringBuilder sb) {
208 sb.append(" Host 1: ")
209 .append(intent.one())
210 .append(", Host 2: ")
211 .append(intent.two());
212 }
213
214 private void buildPointToPointDetails(PointToPointIntent intent,
215 StringBuilder sb) {
216 ConnectPoint ingress = intent.ingressPoint();
217 ConnectPoint egress = intent.egressPoint();
218 sb.append(" Ingress: ")
219 .append(ingress.elementId())
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700220 .append('/')
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700221 .append(ingress.port())
222
223 .append(", Egress: ")
224 .append(egress.elementId())
225 .append('/')
226 .append(egress.port())
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700227 .append(' ');
228 }
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700229
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700230 private void buildMPToSPDetails(MultiPointToSinglePointIntent intent,
231 StringBuilder sb) {
232 ConnectPoint egress = intent.egressPoint();
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700233
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700234 sb.append(" Ingress=");
235 appendMultiPointsDetails(intent.ingressPoints(), sb);
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700236
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700237 sb.append(", Egress=")
238 .append(egress.elementId())
239 .append('/')
240 .append(egress.port())
241 .append(' ');
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700242 }
243
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700244 private void buildSPToMPDetails(SinglePointToMultiPointIntent intent,
245 StringBuilder sb) {
246 ConnectPoint ingress = intent.ingressPoint();
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700247
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700248 sb.append(" Ingress=")
249 .append(ingress.elementId())
250 .append('/')
251 .append(ingress.port())
252 .append(", Egress=");
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700253
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700254 appendMultiPointsDetails(intent.egressPoints(), sb);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700255 }
256
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700257 private void buildPathDetails(PathIntent intent, StringBuilder sb) {
258 sb.append(" path=")
259 .append(intent.path().links())
260 .append(", cost=")
261 .append(intent.path().cost());
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700262 }
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700263
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700264 private void buildLinkConnectionDetails(LinkCollectionIntent intent,
265 StringBuilder sb) {
266 sb.append(" links=")
267 .append(intent.links())
268 .append(", egress=");
269
270 appendMultiPointsDetails(intent.egressPoints(), sb);
271 }
272
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700273 }
274 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700275}