blob: bacb76d2447c99c3f41d80175727e2f4aef86e64 [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;
Thomas Vachuskad8b07892015-05-27 16:56:45 -070040import org.onosproject.ui.table.cell.EnumFormatter;
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";
Thomas Vachuskad8b07892015-05-27 16:56:45 -070059 private static final String STATE = "state";
Bri Prebilic Cole43f17c02015-05-01 10:43:38 -070060 private static final String RESOURCES = "resources";
61 private static final String DETAILS = "details";
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070062
Simon Hunt3d1b0652015-05-05 17:27:24 -070063 private static final String[] COL_IDS = {
Thomas Vachuskad8b07892015-05-27 16:56:45 -070064 APP_ID, KEY, TYPE, PRIORITY, STATE, RESOURCES, DETAILS
Simon Hunt3d1b0652015-05-05 17:27:24 -070065 };
66
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070067 @Override
Simon Huntda580882015-05-12 20:58:18 -070068 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -070069 return ImmutableSet.of(new IntentDataRequest());
70 }
71
Simon Huntabd16f62015-05-01 13:14:40 -070072 // handler for intent table requests
73 private final class IntentDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070074 private IntentDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070075 super(INTENT_DATA_REQ, INTENT_DATA_RESP, INTENTS);
Simon Huntd2747a02015-04-30 22:41:16 -070076 }
77
78 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070079 protected String defaultColumnId() {
80 return APP_ID;
Simon Hunt44aa2f82015-04-30 15:01:35 -070081 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070082
Simon Huntabd16f62015-05-01 13:14:40 -070083 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070084 protected String[] getColumnIds() {
85 return COL_IDS;
Simon Huntabd16f62015-05-01 13:14:40 -070086 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070087
Simon Hunt3d1b0652015-05-05 17:27:24 -070088 @Override
89 protected TableModel createTableModel() {
90 TableModel tm = super.createTableModel();
Simon Hunt3d1b0652015-05-05 17:27:24 -070091 tm.setFormatter(APP_ID, AppIdFormatter.INSTANCE);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070092 tm.setFormatter(RESOURCES, new ResourcesFormatter());
93 tm.setFormatter(DETAILS, new DetailsFormatter());
Thomas Vachuskad8b07892015-05-27 16:56:45 -070094 tm.setFormatter(STATE, EnumFormatter.INSTANCE);
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()) {
Thomas Vachuskad8b07892015-05-27 16:56:45 -0700102 populateRow(tm.addRow(), intent, is);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700103 }
104 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700105
Thomas Vachuskad8b07892015-05-27 16:56:45 -0700106 private void populateRow(TableModel.Row row, Intent intent, IntentService is) {
Simon Hunt3d1b0652015-05-05 17:27:24 -0700107 row.cell(APP_ID, intent.appId())
108 .cell(KEY, intent.key())
109 .cell(TYPE, intent.getClass().getSimpleName())
110 .cell(PRIORITY, intent.priority())
Thomas Vachuskad8b07892015-05-27 16:56:45 -0700111 .cell(STATE, is.getIntentState(intent.key()))
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700112 .cell(RESOURCES, intent)
113 .cell(DETAILS, intent);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700114 }
115
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700116 private final class ResourcesFormatter implements CellFormatter {
117 private static final String COMMA = ", ";
Simon Hunt3d1b0652015-05-05 17:27:24 -0700118
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700119 @Override
120 public String format(Object value) {
121 Intent intent = (Intent) value;
122 Collection<NetworkResource> resources = intent.resources();
123 if (resources.isEmpty()) {
124 return "(No resources for this intent)";
125 }
126 StringBuilder sb = new StringBuilder("Resources: ");
127 for (NetworkResource nr : resources) {
128 sb.append(nr).append(COMMA);
129 }
130 removeTrailingComma(sb);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700131
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700132 return sb.toString();
133 }
134
135 private StringBuilder removeTrailingComma(StringBuilder sb) {
136 int pos = sb.lastIndexOf(COMMA);
137 sb.delete(pos, sb.length());
138 return sb;
139 }
140 }
141
142 private final class DetailsFormatter implements CellFormatter {
143 @Override
144 public String format(Object value) {
145 return formatDetails((Intent) value, new StringBuilder()).toString();
146 }
147
148 private StringBuilder formatDetails(Intent intent, StringBuilder sb) {
149 if (intent instanceof ConnectivityIntent) {
150 buildConnectivityDetails((ConnectivityIntent) intent, sb);
151 }
152
153 if (intent instanceof HostToHostIntent) {
154 buildHostToHostDetails((HostToHostIntent) intent, sb);
155
156 } else if (intent instanceof PointToPointIntent) {
157 buildPointToPointDetails((PointToPointIntent) intent, sb);
158
159 } else if (intent instanceof MultiPointToSinglePointIntent) {
160 buildMPToSPDetails((MultiPointToSinglePointIntent) intent, sb);
161
162 } else if (intent instanceof SinglePointToMultiPointIntent) {
163 buildSPToMPDetails((SinglePointToMultiPointIntent) intent, sb);
164
165 } else if (intent instanceof PathIntent) {
166 buildPathDetails((PathIntent) intent, sb);
167
168 } else if (intent instanceof LinkCollectionIntent) {
169 buildLinkConnectionDetails((LinkCollectionIntent) intent, sb);
170 }
171
172 if (sb.length() == 0) {
173 sb.append("(No details for this intent)");
174 } else {
175 sb.insert(0, "Details: ");
176 }
177 return sb;
178 }
179
180 private void appendMultiPointsDetails(Set<ConnectPoint> points,
181 StringBuilder sb) {
182 for (ConnectPoint point : points) {
183 sb.append(point.elementId())
184 .append('/')
185 .append(point.port())
186 .append(' ');
187 }
188 }
189
190 private void buildConnectivityDetails(ConnectivityIntent intent,
191 StringBuilder sb) {
192 Set<Criterion> criteria = intent.selector().criteria();
193 List<Instruction> instructions = intent.treatment().allInstructions();
194 List<Constraint> constraints = intent.constraints();
195
196 if (!criteria.isEmpty()) {
197 sb.append("Selector: ").append(criteria);
198 }
199 if (!instructions.isEmpty()) {
200 sb.append("Treatment: ").append(instructions);
201 }
202 if (constraints != null && !constraints.isEmpty()) {
203 sb.append("Constraints: ").append(constraints);
204 }
205 }
206
207 private void buildHostToHostDetails(HostToHostIntent intent,
208 StringBuilder sb) {
209 sb.append(" Host 1: ")
210 .append(intent.one())
211 .append(", Host 2: ")
212 .append(intent.two());
213 }
214
215 private void buildPointToPointDetails(PointToPointIntent intent,
216 StringBuilder sb) {
217 ConnectPoint ingress = intent.ingressPoint();
218 ConnectPoint egress = intent.egressPoint();
219 sb.append(" Ingress: ")
220 .append(ingress.elementId())
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700221 .append('/')
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700222 .append(ingress.port())
223
224 .append(", Egress: ")
225 .append(egress.elementId())
226 .append('/')
227 .append(egress.port())
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700228 .append(' ');
229 }
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700230
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700231 private void buildMPToSPDetails(MultiPointToSinglePointIntent intent,
232 StringBuilder sb) {
233 ConnectPoint egress = intent.egressPoint();
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700234
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700235 sb.append(" Ingress=");
236 appendMultiPointsDetails(intent.ingressPoints(), sb);
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700237
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700238 sb.append(", Egress=")
239 .append(egress.elementId())
240 .append('/')
241 .append(egress.port())
242 .append(' ');
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700243 }
244
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700245 private void buildSPToMPDetails(SinglePointToMultiPointIntent intent,
246 StringBuilder sb) {
247 ConnectPoint ingress = intent.ingressPoint();
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700248
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700249 sb.append(" Ingress=")
250 .append(ingress.elementId())
251 .append('/')
252 .append(ingress.port())
253 .append(", Egress=");
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700254
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700255 appendMultiPointsDetails(intent.egressPoints(), sb);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700256 }
257
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700258 private void buildPathDetails(PathIntent intent, StringBuilder sb) {
259 sb.append(" path=")
260 .append(intent.path().links())
261 .append(", cost=")
262 .append(intent.path().cost());
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700263 }
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700264
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700265 private void buildLinkConnectionDetails(LinkCollectionIntent intent,
266 StringBuilder sb) {
267 sb.append(" links=")
268 .append(intent.links())
269 .append(", egress=");
270
271 appendMultiPointsDetails(intent.egressPoints(), sb);
272 }
273
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700274 }
275 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700276}