blob: 4924d4bbc0fb1349471f537132434d5c05c048a5 [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 Hunt44aa2f82015-04-30 15:01:35 -070034import org.onosproject.ui.UiMessageHandler;
35import org.onosproject.ui.table.AbstractTableRow;
36import org.onosproject.ui.table.RowComparator;
37import org.onosproject.ui.table.TableRow;
38import org.onosproject.ui.table.TableUtils;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070039
40import java.util.ArrayList;
41import java.util.Arrays;
42import 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 Hunt44aa2f82015-04-30 15:01:35 -070048public class IntentViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070049
50 /**
51 * Creates a new message handler for the intent messages.
52 */
53 protected IntentViewMessageHandler() {
54 super(ImmutableSet.of("intentDataRequest"));
55 }
56
57 @Override
58 public void process(ObjectNode message) {
Simon Hunt44aa2f82015-04-30 15:01:35 -070059 String type = eventType(message);
60 if (type.equals("intentDataRequest")) {
61 sendIntentList(message);
62 }
63 }
64
65 private void sendIntentList(ObjectNode message) {
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070066 ObjectNode payload = payload(message);
Simon Hunt44aa2f82015-04-30 15:01:35 -070067 RowComparator rc = TableUtils.createRowComparator(payload);
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070068
69 IntentService service = get(IntentService.class);
70 TableRow[] rows = generateTableRows(service);
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070071 Arrays.sort(rows, rc);
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070072 ObjectNode rootNode = mapper.createObjectNode();
Simon Hunt44aa2f82015-04-30 15:01:35 -070073 rootNode.set("intents", TableUtils.generateArrayNode(rows));
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070074
75 connection().sendMessage("intentDataResponse", 0, rootNode);
76 }
77
78 private TableRow[] generateTableRows(IntentService service) {
79 List<TableRow> list = new ArrayList<>();
80 for (Intent intent : service.getIntents()) {
81 list.add(new IntentTableRow(intent));
82 }
83 return list.toArray(new TableRow[list.size()]);
84 }
85
86 /**
87 * TableRow implementation for {@link Intent intents}.
88 */
89 private static class IntentTableRow extends AbstractTableRow {
90
91 private static final String APP_ID = "appId";
92 private static final String KEY = "key";
93 private static final String TYPE = "type";
94 private static final String PRIORITY = "priority";
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070095 private static final String RESOURCES = "resources";
96 private static final String DETAILS = "details";
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070097
98 private static final String[] COL_IDS = {
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070099 APP_ID, KEY, TYPE, PRIORITY, RESOURCES, DETAILS
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700100 };
101
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700102 private StringBuilder details = new StringBuilder();
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700103
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700104 private void appendMultiPointsDetails(Set<ConnectPoint> points) {
105 for (ConnectPoint point : points) {
106 details.append(point.elementId())
107 .append('/')
108 .append(point.port())
109 .append(' ');
110 }
111 }
112
113 private void buildConnectivityDetails(ConnectivityIntent intent) {
114 Set<Criterion> criteria = intent.selector().criteria();
115 List<Instruction> instructions = intent.treatment().allInstructions();
116 List<Constraint> constraints = intent.constraints();
117
118 if (!criteria.isEmpty()) {
119 details.append("selector=").append(criteria);
120 }
121 if (!instructions.isEmpty()) {
122 details.append("treatment=").append(instructions);
123 }
124 if (constraints != null && !constraints.isEmpty()) {
125 details.append("constraints=").append(constraints);
126 }
127 }
128
129 private void buildHostToHostDetails(HostToHostIntent intent) {
130 details.append(" host1=")
131 .append(intent.one())
132 .append(", host2=")
133 .append(intent.two());
134 }
135
136 private void buildPointToPointDetails(PointToPointIntent intent) {
137 ConnectPoint ingress = intent.ingressPoint();
138 ConnectPoint egress = intent.egressPoint();
139 details.append(" ingress=")
140 .append(ingress.elementId())
141 .append('/')
142 .append(ingress.port())
143
144 .append(", egress=")
145 .append(egress.elementId())
146 .append('/')
147 .append(egress.port())
148 .append(' ');
149 }
150
151 private void buildMPToSPDetails(MultiPointToSinglePointIntent intent) {
152 ConnectPoint egress = intent.egressPoint();
153
154 details.append(" ingress=");
155 appendMultiPointsDetails(intent.ingressPoints());
156
157 details.append(", egress=")
158 .append(egress.elementId())
159 .append('/')
160 .append(egress.port())
161 .append(' ');
162 }
163
164 private void buildSPToMPDetails(SinglePointToMultiPointIntent intent) {
165 ConnectPoint ingress = intent.ingressPoint();
166
167 details.append(" ingress=")
168 .append(ingress.elementId())
169 .append('/')
170 .append(ingress.port())
171 .append(", egress=");
172
173 appendMultiPointsDetails(intent.egressPoints());
174 }
175
176 private void buildPathDetails(PathIntent intent) {
177 details.append(" path=")
178 .append(intent.path().links())
179 .append(", cost=")
180 .append(intent.path().cost());
181 }
182
183 private void buildLinkConnectionDetails(LinkCollectionIntent intent) {
184 details.append(" links=")
185 .append(intent.links())
186 .append(", egress=");
187
188 appendMultiPointsDetails(intent.egressPoints());
189 }
190
191 private String formatDetails(Intent intent) {
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700192 if (intent instanceof ConnectivityIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700193 buildConnectivityDetails((ConnectivityIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700194 }
195
196 if (intent instanceof HostToHostIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700197 buildHostToHostDetails((HostToHostIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700198
199 } else if (intent instanceof PointToPointIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700200 buildPointToPointDetails((PointToPointIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700201
202 } else if (intent instanceof MultiPointToSinglePointIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700203 buildMPToSPDetails((MultiPointToSinglePointIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700204
205 } else if (intent instanceof SinglePointToMultiPointIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700206 buildSPToMPDetails((SinglePointToMultiPointIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700207
208 } else if (intent instanceof PathIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700209 buildPathDetails((PathIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700210
211 } else if (intent instanceof LinkCollectionIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700212 buildLinkConnectionDetails((LinkCollectionIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700213 }
214
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700215 if (details.length() == 0) {
216 details.append("(No details for this intent)");
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700217 } else {
218 details.insert(0, "Details: ");
219 }
220 return details.toString();
221 }
222
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700223 private String formatResources(Intent intent) {
224 return (intent.resources().isEmpty() ?
225 "(No resources for this intent)" :
226 "Resources: " + intent.resources());
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700227 }
228
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700229 public IntentTableRow(Intent intent) {
230 ApplicationId appid = intent.appId();
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700231
Simon Hunt44aa2f82015-04-30 15:01:35 -0700232 add(APP_ID, concat(appid.id(), " : ", appid.name()));
233 add(KEY, intent.key());
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700234 add(TYPE, intent.getClass().getSimpleName());
Simon Hunt44aa2f82015-04-30 15:01:35 -0700235 add(PRIORITY, intent.priority());
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700236 add(RESOURCES, formatResources(intent));
237 add(DETAILS, formatDetails(intent));
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700238 }
239
240 @Override
241 protected String[] columnIds() {
242 return COL_IDS;
243 }
244 }
245
246}