blob: 356b8b06f738c28f6565fb948d63d07fc1d4c381 [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;
20import org.onosproject.core.ApplicationId;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070021import org.onosproject.net.ConnectPoint;
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;
Simon Hunt44aa2f82015-04-30 15:01:35 -070036import org.onosproject.ui.table.AbstractTableRow;
37import org.onosproject.ui.table.RowComparator;
38import org.onosproject.ui.table.TableRow;
39import org.onosproject.ui.table.TableUtils;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070040
41import java.util.ArrayList;
42import java.util.Arrays;
Simon Huntd2747a02015-04-30 22:41:16 -070043import java.util.Collection;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070044import java.util.List;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070045import java.util.Set;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070046
47/**
48 * Message handler for intent view related messages.
49 */
Simon Hunta0ddb022015-05-01 09:53:01 -070050public class IntentViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070051
Simon Huntd2747a02015-04-30 22:41:16 -070052 private static final String INTENT_DATA_REQ = "intentDataRequest";
53
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070054
55 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070056 protected Collection<RequestHandler> getHandlers() {
57 return ImmutableSet.of(new IntentDataRequest());
58 }
59
60 // ======================================================================
61
62 private final class IntentDataRequest extends RequestHandler {
63
64 private IntentDataRequest() {
65 super(INTENT_DATA_REQ);
66 }
67
68 @Override
69 public void process(long sid, ObjectNode payload) {
70 RowComparator rc = TableUtils.createRowComparator(payload);
71
72 IntentService service = get(IntentService.class);
73 TableRow[] rows = generateTableRows(service);
74 Arrays.sort(rows, rc);
75 ObjectNode rootNode = MAPPER.createObjectNode();
76 rootNode.set("intents", TableUtils.generateArrayNode(rows));
77
78 sendMessage("intentDataResponse", 0, rootNode);
79 }
80
81 private TableRow[] generateTableRows(IntentService service) {
82 List<TableRow> list = new ArrayList<>();
83 for (Intent intent : service.getIntents()) {
84 list.add(new IntentTableRow(intent));
85 }
86 return list.toArray(new TableRow[list.size()]);
Simon Hunt44aa2f82015-04-30 15:01:35 -070087 }
88 }
89
Simon Huntd2747a02015-04-30 22:41:16 -070090 // ======================================================================
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070091
92 /**
93 * TableRow implementation for {@link Intent intents}.
94 */
95 private static class IntentTableRow extends AbstractTableRow {
96
97 private static final String APP_ID = "appId";
98 private static final String KEY = "key";
99 private static final String TYPE = "type";
100 private static final String PRIORITY = "priority";
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700101 private static final String RESOURCES = "resources";
102 private static final String DETAILS = "details";
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700103
104 private static final String[] COL_IDS = {
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700105 APP_ID, KEY, TYPE, PRIORITY, RESOURCES, DETAILS
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700106 };
107
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700108 private StringBuilder details = new StringBuilder();
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700109
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700110 private void appendMultiPointsDetails(Set<ConnectPoint> points) {
111 for (ConnectPoint point : points) {
112 details.append(point.elementId())
113 .append('/')
114 .append(point.port())
115 .append(' ');
116 }
117 }
118
119 private void buildConnectivityDetails(ConnectivityIntent intent) {
120 Set<Criterion> criteria = intent.selector().criteria();
121 List<Instruction> instructions = intent.treatment().allInstructions();
122 List<Constraint> constraints = intent.constraints();
123
124 if (!criteria.isEmpty()) {
125 details.append("selector=").append(criteria);
126 }
127 if (!instructions.isEmpty()) {
128 details.append("treatment=").append(instructions);
129 }
130 if (constraints != null && !constraints.isEmpty()) {
131 details.append("constraints=").append(constraints);
132 }
133 }
134
135 private void buildHostToHostDetails(HostToHostIntent intent) {
136 details.append(" host1=")
137 .append(intent.one())
138 .append(", host2=")
139 .append(intent.two());
140 }
141
142 private void buildPointToPointDetails(PointToPointIntent intent) {
143 ConnectPoint ingress = intent.ingressPoint();
144 ConnectPoint egress = intent.egressPoint();
145 details.append(" ingress=")
146 .append(ingress.elementId())
147 .append('/')
148 .append(ingress.port())
149
150 .append(", egress=")
151 .append(egress.elementId())
152 .append('/')
153 .append(egress.port())
154 .append(' ');
155 }
156
157 private void buildMPToSPDetails(MultiPointToSinglePointIntent intent) {
158 ConnectPoint egress = intent.egressPoint();
159
160 details.append(" ingress=");
161 appendMultiPointsDetails(intent.ingressPoints());
162
163 details.append(", egress=")
164 .append(egress.elementId())
165 .append('/')
166 .append(egress.port())
167 .append(' ');
168 }
169
170 private void buildSPToMPDetails(SinglePointToMultiPointIntent intent) {
171 ConnectPoint ingress = intent.ingressPoint();
172
173 details.append(" ingress=")
174 .append(ingress.elementId())
175 .append('/')
176 .append(ingress.port())
177 .append(", egress=");
178
179 appendMultiPointsDetails(intent.egressPoints());
180 }
181
182 private void buildPathDetails(PathIntent intent) {
183 details.append(" path=")
184 .append(intent.path().links())
185 .append(", cost=")
186 .append(intent.path().cost());
187 }
188
189 private void buildLinkConnectionDetails(LinkCollectionIntent intent) {
190 details.append(" links=")
191 .append(intent.links())
192 .append(", egress=");
193
194 appendMultiPointsDetails(intent.egressPoints());
195 }
196
197 private String formatDetails(Intent intent) {
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700198 if (intent instanceof ConnectivityIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700199 buildConnectivityDetails((ConnectivityIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700200 }
201
202 if (intent instanceof HostToHostIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700203 buildHostToHostDetails((HostToHostIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700204
205 } else if (intent instanceof PointToPointIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700206 buildPointToPointDetails((PointToPointIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700207
208 } else if (intent instanceof MultiPointToSinglePointIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700209 buildMPToSPDetails((MultiPointToSinglePointIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700210
211 } else if (intent instanceof SinglePointToMultiPointIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700212 buildSPToMPDetails((SinglePointToMultiPointIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700213
214 } else if (intent instanceof PathIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700215 buildPathDetails((PathIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700216
217 } else if (intent instanceof LinkCollectionIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700218 buildLinkConnectionDetails((LinkCollectionIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700219 }
220
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700221 if (details.length() == 0) {
222 details.append("(No details for this intent)");
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700223 } else {
224 details.insert(0, "Details: ");
225 }
226 return details.toString();
227 }
228
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700229 private String formatResources(Intent intent) {
230 return (intent.resources().isEmpty() ?
231 "(No resources for this intent)" :
232 "Resources: " + intent.resources());
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700233 }
234
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700235 public IntentTableRow(Intent intent) {
236 ApplicationId appid = intent.appId();
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700237
Simon Hunt44aa2f82015-04-30 15:01:35 -0700238 add(APP_ID, concat(appid.id(), " : ", appid.name()));
239 add(KEY, intent.key());
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700240 add(TYPE, intent.getClass().getSimpleName());
Simon Hunt44aa2f82015-04-30 15:01:35 -0700241 add(PRIORITY, intent.priority());
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700242 add(RESOURCES, formatResources(intent));
243 add(DETAILS, formatDetails(intent));
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700244 }
245
246 @Override
247 protected String[] columnIds() {
248 return COL_IDS;
249 }
250 }
251
252}