blob: 8ac51dd82060cad5e64ae6ebde103ec223fee83d [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;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070040
Simon Huntd2747a02015-04-30 22:41:16 -070041import java.util.Collection;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070042import java.util.List;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070043import java.util.Set;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070044
45/**
46 * Message handler for intent view related messages.
47 */
Simon Hunta0ddb022015-05-01 09:53:01 -070048public class IntentViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070049
Simon Huntd2747a02015-04-30 22:41:16 -070050 private static final String INTENT_DATA_REQ = "intentDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070051 private static final String INTENT_DATA_RESP = "intentDataResponse";
52 private static final String INTENTS = "intents";
Simon Huntd2747a02015-04-30 22:41:16 -070053
Bri Prebilic Cole43f17c02015-05-01 10:43:38 -070054 private static final String APP_ID = "appId";
55 private static final String KEY = "key";
56 private static final String TYPE = "type";
57 private static final String PRIORITY = "priority";
58 private static final String RESOURCES = "resources";
59 private static final String DETAILS = "details";
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070060
Simon Hunt3d1b0652015-05-05 17:27:24 -070061 private static final String[] COL_IDS = {
62 APP_ID, KEY, TYPE, PRIORITY, RESOURCES, DETAILS
63 };
64
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070065 @Override
Simon Huntda580882015-05-12 20:58:18 -070066 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -070067 return ImmutableSet.of(new IntentDataRequest());
68 }
69
Simon Huntabd16f62015-05-01 13:14:40 -070070 // handler for intent table requests
71 private final class IntentDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070072 private IntentDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070073 super(INTENT_DATA_REQ, INTENT_DATA_RESP, INTENTS);
Simon Huntd2747a02015-04-30 22:41:16 -070074 }
75
76 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070077 protected String defaultColumnId() {
78 return APP_ID;
Simon Hunt44aa2f82015-04-30 15:01:35 -070079 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070080
Simon Huntabd16f62015-05-01 13:14:40 -070081 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070082 protected String[] getColumnIds() {
83 return COL_IDS;
Simon Huntabd16f62015-05-01 13:14:40 -070084 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070085
Simon Hunt3d1b0652015-05-05 17:27:24 -070086 @Override
87 protected TableModel createTableModel() {
88 TableModel tm = super.createTableModel();
Simon Hunt3d1b0652015-05-05 17:27:24 -070089 tm.setFormatter(APP_ID, AppIdFormatter.INSTANCE);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070090 tm.setFormatter(RESOURCES, new ResourcesFormatter());
91 tm.setFormatter(DETAILS, new DetailsFormatter());
Simon Hunt3d1b0652015-05-05 17:27:24 -070092 return tm;
93 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070094
Simon Hunt3d1b0652015-05-05 17:27:24 -070095 @Override
96 protected void populateTable(TableModel tm, ObjectNode payload) {
97 IntentService is = get(IntentService.class);
98 for (Intent intent : is.getIntents()) {
99 populateRow(tm.addRow(), intent);
100 }
101 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700102
Simon Hunt3d1b0652015-05-05 17:27:24 -0700103 private void populateRow(TableModel.Row row, Intent intent) {
104 row.cell(APP_ID, intent.appId())
105 .cell(KEY, intent.key())
106 .cell(TYPE, intent.getClass().getSimpleName())
107 .cell(PRIORITY, intent.priority())
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700108 .cell(RESOURCES, intent)
109 .cell(DETAILS, intent);
Simon Hunt3d1b0652015-05-05 17:27:24 -0700110 }
111
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700112 private final class ResourcesFormatter implements CellFormatter {
113 private static final String COMMA = ", ";
Simon Hunt3d1b0652015-05-05 17:27:24 -0700114
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700115 @Override
116 public String format(Object value) {
117 Intent intent = (Intent) value;
118 Collection<NetworkResource> resources = intent.resources();
119 if (resources.isEmpty()) {
120 return "(No resources for this intent)";
121 }
122 StringBuilder sb = new StringBuilder("Resources: ");
123 for (NetworkResource nr : resources) {
124 sb.append(nr).append(COMMA);
125 }
126 removeTrailingComma(sb);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700127
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700128 return sb.toString();
129 }
130
131 private StringBuilder removeTrailingComma(StringBuilder sb) {
132 int pos = sb.lastIndexOf(COMMA);
133 sb.delete(pos, sb.length());
134 return sb;
135 }
136 }
137
138 private final class DetailsFormatter implements CellFormatter {
139 @Override
140 public String format(Object value) {
141 return formatDetails((Intent) value, new StringBuilder()).toString();
142 }
143
144 private StringBuilder formatDetails(Intent intent, StringBuilder sb) {
145 if (intent instanceof ConnectivityIntent) {
146 buildConnectivityDetails((ConnectivityIntent) intent, sb);
147 }
148
149 if (intent instanceof HostToHostIntent) {
150 buildHostToHostDetails((HostToHostIntent) intent, sb);
151
152 } else if (intent instanceof PointToPointIntent) {
153 buildPointToPointDetails((PointToPointIntent) intent, sb);
154
155 } else if (intent instanceof MultiPointToSinglePointIntent) {
156 buildMPToSPDetails((MultiPointToSinglePointIntent) intent, sb);
157
158 } else if (intent instanceof SinglePointToMultiPointIntent) {
159 buildSPToMPDetails((SinglePointToMultiPointIntent) intent, sb);
160
161 } else if (intent instanceof PathIntent) {
162 buildPathDetails((PathIntent) intent, sb);
163
164 } else if (intent instanceof LinkCollectionIntent) {
165 buildLinkConnectionDetails((LinkCollectionIntent) intent, sb);
166 }
167
168 if (sb.length() == 0) {
169 sb.append("(No details for this intent)");
170 } else {
171 sb.insert(0, "Details: ");
172 }
173 return sb;
174 }
175
176 private void appendMultiPointsDetails(Set<ConnectPoint> points,
177 StringBuilder sb) {
178 for (ConnectPoint point : points) {
179 sb.append(point.elementId())
180 .append('/')
181 .append(point.port())
182 .append(' ');
183 }
184 }
185
186 private void buildConnectivityDetails(ConnectivityIntent intent,
187 StringBuilder sb) {
188 Set<Criterion> criteria = intent.selector().criteria();
189 List<Instruction> instructions = intent.treatment().allInstructions();
190 List<Constraint> constraints = intent.constraints();
191
192 if (!criteria.isEmpty()) {
193 sb.append("Selector: ").append(criteria);
194 }
195 if (!instructions.isEmpty()) {
196 sb.append("Treatment: ").append(instructions);
197 }
198 if (constraints != null && !constraints.isEmpty()) {
199 sb.append("Constraints: ").append(constraints);
200 }
201 }
202
203 private void buildHostToHostDetails(HostToHostIntent intent,
204 StringBuilder sb) {
205 sb.append(" Host 1: ")
206 .append(intent.one())
207 .append(", Host 2: ")
208 .append(intent.two());
209 }
210
211 private void buildPointToPointDetails(PointToPointIntent intent,
212 StringBuilder sb) {
213 ConnectPoint ingress = intent.ingressPoint();
214 ConnectPoint egress = intent.egressPoint();
215 sb.append(" Ingress: ")
216 .append(ingress.elementId())
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700217 .append('/')
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700218 .append(ingress.port())
219
220 .append(", Egress: ")
221 .append(egress.elementId())
222 .append('/')
223 .append(egress.port())
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700224 .append(' ');
225 }
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700226
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700227 private void buildMPToSPDetails(MultiPointToSinglePointIntent intent,
228 StringBuilder sb) {
229 ConnectPoint egress = intent.egressPoint();
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700230
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700231 sb.append(" Ingress=");
232 appendMultiPointsDetails(intent.ingressPoints(), sb);
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700233
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700234 sb.append(", Egress=")
235 .append(egress.elementId())
236 .append('/')
237 .append(egress.port())
238 .append(' ');
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700239 }
240
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700241 private void buildSPToMPDetails(SinglePointToMultiPointIntent intent,
242 StringBuilder sb) {
243 ConnectPoint ingress = intent.ingressPoint();
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700244
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700245 sb.append(" Ingress=")
246 .append(ingress.elementId())
247 .append('/')
248 .append(ingress.port())
249 .append(", Egress=");
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700250
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700251 appendMultiPointsDetails(intent.egressPoints(), sb);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700252 }
253
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700254 private void buildPathDetails(PathIntent intent, StringBuilder sb) {
255 sb.append(" path=")
256 .append(intent.path().links())
257 .append(", cost=")
258 .append(intent.path().cost());
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700259 }
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700260
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700261 private void buildLinkConnectionDetails(LinkCollectionIntent intent,
262 StringBuilder sb) {
263 sb.append(" links=")
264 .append(intent.links())
265 .append(", egress=");
266
267 appendMultiPointsDetails(intent.egressPoints(), sb);
268 }
269
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700270 }
271 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700272}