blob: 0d1c062d717296139ea8dd5409df53f5407158a4 [file] [log] [blame]
Bri Prebilic Cole96f26472015-03-31 13:07:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bri Prebilic Cole96f26472015-03-31 13:07:05 -07003 *
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 {
Jian Li69f66632016-01-15 12:27:42 -080074 private static final String NO_ROWS_MESSAGE = "No intents found";
75
Simon Huntd2747a02015-04-30 22:41:16 -070076 private IntentDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070077 super(INTENT_DATA_REQ, INTENT_DATA_RESP, INTENTS);
Simon Huntd2747a02015-04-30 22:41:16 -070078 }
79
80 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070081 protected String defaultColumnId() {
82 return APP_ID;
Simon Hunt44aa2f82015-04-30 15:01:35 -070083 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070084
Simon Huntabd16f62015-05-01 13:14:40 -070085 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070086 protected String[] getColumnIds() {
87 return COL_IDS;
Simon Huntabd16f62015-05-01 13:14:40 -070088 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070089
Simon Hunt3d1b0652015-05-05 17:27:24 -070090 @Override
Jian Li8baf4472016-01-15 15:08:09 -080091 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -080092 return NO_ROWS_MESSAGE;
93 }
94
95 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070096 protected TableModel createTableModel() {
97 TableModel tm = super.createTableModel();
Simon Hunt3d1b0652015-05-05 17:27:24 -070098 tm.setFormatter(APP_ID, AppIdFormatter.INSTANCE);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070099 tm.setFormatter(RESOURCES, new ResourcesFormatter());
100 tm.setFormatter(DETAILS, new DetailsFormatter());
Thomas Vachuskad8b07892015-05-27 16:56:45 -0700101 tm.setFormatter(STATE, EnumFormatter.INSTANCE);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700102 return tm;
103 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700104
Simon Hunt3d1b0652015-05-05 17:27:24 -0700105 @Override
106 protected void populateTable(TableModel tm, ObjectNode payload) {
107 IntentService is = get(IntentService.class);
108 for (Intent intent : is.getIntents()) {
Thomas Vachuskad8b07892015-05-27 16:56:45 -0700109 populateRow(tm.addRow(), intent, is);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700110 }
111 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700112
Thomas Vachuskad8b07892015-05-27 16:56:45 -0700113 private void populateRow(TableModel.Row row, Intent intent, IntentService is) {
Simon Hunt3d1b0652015-05-05 17:27:24 -0700114 row.cell(APP_ID, intent.appId())
Simon Hunt8a0429a2017-01-06 16:52:47 -0800115 .cell(KEY, intent.key())
116 .cell(TYPE, intent.getClass().getSimpleName())
117 .cell(PRIORITY, intent.priority())
118 .cell(STATE, is.getIntentState(intent.key()))
119 .cell(RESOURCES, intent)
120 .cell(DETAILS, intent);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700121 }
122
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700123 private final class ResourcesFormatter implements CellFormatter {
124 private static final String COMMA = ", ";
Simon Hunt3d1b0652015-05-05 17:27:24 -0700125
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700126 @Override
127 public String format(Object value) {
128 Intent intent = (Intent) value;
129 Collection<NetworkResource> resources = intent.resources();
130 if (resources.isEmpty()) {
131 return "(No resources for this intent)";
132 }
133 StringBuilder sb = new StringBuilder("Resources: ");
134 for (NetworkResource nr : resources) {
135 sb.append(nr).append(COMMA);
136 }
137 removeTrailingComma(sb);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700138
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700139 return sb.toString();
140 }
141
142 private StringBuilder removeTrailingComma(StringBuilder sb) {
143 int pos = sb.lastIndexOf(COMMA);
144 sb.delete(pos, sb.length());
145 return sb;
146 }
147 }
148
149 private final class DetailsFormatter implements CellFormatter {
150 @Override
151 public String format(Object value) {
152 return formatDetails((Intent) value, new StringBuilder()).toString();
153 }
154
155 private StringBuilder formatDetails(Intent intent, StringBuilder sb) {
156 if (intent instanceof ConnectivityIntent) {
157 buildConnectivityDetails((ConnectivityIntent) intent, sb);
158 }
159
160 if (intent instanceof HostToHostIntent) {
161 buildHostToHostDetails((HostToHostIntent) intent, sb);
162
163 } else if (intent instanceof PointToPointIntent) {
164 buildPointToPointDetails((PointToPointIntent) intent, sb);
165
166 } else if (intent instanceof MultiPointToSinglePointIntent) {
167 buildMPToSPDetails((MultiPointToSinglePointIntent) intent, sb);
168
169 } else if (intent instanceof SinglePointToMultiPointIntent) {
170 buildSPToMPDetails((SinglePointToMultiPointIntent) intent, sb);
171
172 } else if (intent instanceof PathIntent) {
173 buildPathDetails((PathIntent) intent, sb);
174
175 } else if (intent instanceof LinkCollectionIntent) {
176 buildLinkConnectionDetails((LinkCollectionIntent) intent, sb);
177 }
178
179 if (sb.length() == 0) {
180 sb.append("(No details for this intent)");
181 } else {
182 sb.insert(0, "Details: ");
183 }
184 return sb;
185 }
186
187 private void appendMultiPointsDetails(Set<ConnectPoint> points,
188 StringBuilder sb) {
189 for (ConnectPoint point : points) {
190 sb.append(point.elementId())
191 .append('/')
192 .append(point.port())
193 .append(' ');
194 }
195 }
196
197 private void buildConnectivityDetails(ConnectivityIntent intent,
198 StringBuilder sb) {
199 Set<Criterion> criteria = intent.selector().criteria();
200 List<Instruction> instructions = intent.treatment().allInstructions();
201 List<Constraint> constraints = intent.constraints();
202
203 if (!criteria.isEmpty()) {
204 sb.append("Selector: ").append(criteria);
205 }
206 if (!instructions.isEmpty()) {
207 sb.append("Treatment: ").append(instructions);
208 }
209 if (constraints != null && !constraints.isEmpty()) {
210 sb.append("Constraints: ").append(constraints);
211 }
212 }
213
214 private void buildHostToHostDetails(HostToHostIntent intent,
215 StringBuilder sb) {
216 sb.append(" Host 1: ")
217 .append(intent.one())
218 .append(", Host 2: ")
219 .append(intent.two());
220 }
221
222 private void buildPointToPointDetails(PointToPointIntent intent,
223 StringBuilder sb) {
224 ConnectPoint ingress = intent.ingressPoint();
225 ConnectPoint egress = intent.egressPoint();
226 sb.append(" Ingress: ")
227 .append(ingress.elementId())
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700228 .append('/')
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700229 .append(ingress.port())
230
231 .append(", Egress: ")
232 .append(egress.elementId())
233 .append('/')
234 .append(egress.port())
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700235 .append(' ');
236 }
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700237
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700238 private void buildMPToSPDetails(MultiPointToSinglePointIntent intent,
239 StringBuilder sb) {
240 ConnectPoint egress = intent.egressPoint();
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700241
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700242 sb.append(" Ingress=");
243 appendMultiPointsDetails(intent.ingressPoints(), sb);
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700244
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700245 sb.append(", Egress=")
246 .append(egress.elementId())
247 .append('/')
248 .append(egress.port())
249 .append(' ');
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700250 }
251
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700252 private void buildSPToMPDetails(SinglePointToMultiPointIntent intent,
253 StringBuilder sb) {
254 ConnectPoint ingress = intent.ingressPoint();
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700255
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700256 sb.append(" Ingress=")
257 .append(ingress.elementId())
258 .append('/')
259 .append(ingress.port())
260 .append(", Egress=");
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700261
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700262 appendMultiPointsDetails(intent.egressPoints(), sb);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700263 }
264
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700265 private void buildPathDetails(PathIntent intent, StringBuilder sb) {
266 sb.append(" path=")
267 .append(intent.path().links())
268 .append(", cost=")
269 .append(intent.path().cost());
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700270 }
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700271
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700272 private void buildLinkConnectionDetails(LinkCollectionIntent intent,
273 StringBuilder sb) {
274 sb.append(" links=")
275 .append(intent.links())
276 .append(", egress=");
277
278 appendMultiPointsDetails(intent.egressPoints(), sb);
279 }
280
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700281 }
282 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700283}