blob: 6d4ef6de34d80f7dbaeb51d5882e21e11978966e [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;
Simon Huntabd16f62015-05-01 13:14:40 -070037import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt44aa2f82015-04-30 15:01:35 -070038import org.onosproject.ui.table.TableRow;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070039
40import java.util.ArrayList;
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
61 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070062 protected Collection<RequestHandler> getHandlers() {
63 return ImmutableSet.of(new IntentDataRequest());
64 }
65
Simon Huntabd16f62015-05-01 13:14:40 -070066 // handler for intent table requests
67 private final class IntentDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070068 private IntentDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070069 super(INTENT_DATA_REQ, INTENT_DATA_RESP, INTENTS);
Simon Huntd2747a02015-04-30 22:41:16 -070070 }
71
72 @Override
Simon Huntabd16f62015-05-01 13:14:40 -070073 protected TableRow[] generateTableRows(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -070074 IntentService service = get(IntentService.class);
Simon Huntd2747a02015-04-30 22:41:16 -070075 List<TableRow> list = new ArrayList<>();
76 for (Intent intent : service.getIntents()) {
77 list.add(new IntentTableRow(intent));
78 }
79 return list.toArray(new TableRow[list.size()]);
Simon Hunt44aa2f82015-04-30 15:01:35 -070080 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070081
Simon Huntabd16f62015-05-01 13:14:40 -070082 @Override
83 protected String defaultColId() {
84 return APP_ID;
85 }
86 }
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070087
88 /**
89 * TableRow implementation for {@link Intent intents}.
90 */
91 private static class IntentTableRow extends AbstractTableRow {
92
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070093 private static final String[] COL_IDS = {
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070094 APP_ID, KEY, TYPE, PRIORITY, RESOURCES, DETAILS
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070095 };
96
Bri Prebilic Coled5df2542015-04-01 10:36:09 -070097 private StringBuilder details = new StringBuilder();
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070098
Bri Prebilic Coled5df2542015-04-01 10:36:09 -070099 private void appendMultiPointsDetails(Set<ConnectPoint> points) {
100 for (ConnectPoint point : points) {
101 details.append(point.elementId())
102 .append('/')
103 .append(point.port())
104 .append(' ');
105 }
106 }
107
108 private void buildConnectivityDetails(ConnectivityIntent intent) {
109 Set<Criterion> criteria = intent.selector().criteria();
110 List<Instruction> instructions = intent.treatment().allInstructions();
111 List<Constraint> constraints = intent.constraints();
112
113 if (!criteria.isEmpty()) {
114 details.append("selector=").append(criteria);
115 }
116 if (!instructions.isEmpty()) {
117 details.append("treatment=").append(instructions);
118 }
119 if (constraints != null && !constraints.isEmpty()) {
120 details.append("constraints=").append(constraints);
121 }
122 }
123
124 private void buildHostToHostDetails(HostToHostIntent intent) {
125 details.append(" host1=")
126 .append(intent.one())
127 .append(", host2=")
128 .append(intent.two());
129 }
130
131 private void buildPointToPointDetails(PointToPointIntent intent) {
132 ConnectPoint ingress = intent.ingressPoint();
133 ConnectPoint egress = intent.egressPoint();
134 details.append(" ingress=")
135 .append(ingress.elementId())
136 .append('/')
137 .append(ingress.port())
138
139 .append(", egress=")
140 .append(egress.elementId())
141 .append('/')
142 .append(egress.port())
143 .append(' ');
144 }
145
146 private void buildMPToSPDetails(MultiPointToSinglePointIntent intent) {
147 ConnectPoint egress = intent.egressPoint();
148
149 details.append(" ingress=");
150 appendMultiPointsDetails(intent.ingressPoints());
151
152 details.append(", egress=")
153 .append(egress.elementId())
154 .append('/')
155 .append(egress.port())
156 .append(' ');
157 }
158
159 private void buildSPToMPDetails(SinglePointToMultiPointIntent intent) {
160 ConnectPoint ingress = intent.ingressPoint();
161
162 details.append(" ingress=")
163 .append(ingress.elementId())
164 .append('/')
165 .append(ingress.port())
166 .append(", egress=");
167
168 appendMultiPointsDetails(intent.egressPoints());
169 }
170
171 private void buildPathDetails(PathIntent intent) {
172 details.append(" path=")
173 .append(intent.path().links())
174 .append(", cost=")
175 .append(intent.path().cost());
176 }
177
178 private void buildLinkConnectionDetails(LinkCollectionIntent intent) {
179 details.append(" links=")
180 .append(intent.links())
181 .append(", egress=");
182
183 appendMultiPointsDetails(intent.egressPoints());
184 }
185
186 private String formatDetails(Intent intent) {
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700187 if (intent instanceof ConnectivityIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700188 buildConnectivityDetails((ConnectivityIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700189 }
190
191 if (intent instanceof HostToHostIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700192 buildHostToHostDetails((HostToHostIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700193
194 } else if (intent instanceof PointToPointIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700195 buildPointToPointDetails((PointToPointIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700196
197 } else if (intent instanceof MultiPointToSinglePointIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700198 buildMPToSPDetails((MultiPointToSinglePointIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700199
200 } else if (intent instanceof SinglePointToMultiPointIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700201 buildSPToMPDetails((SinglePointToMultiPointIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700202
203 } else if (intent instanceof PathIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700204 buildPathDetails((PathIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700205
206 } else if (intent instanceof LinkCollectionIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700207 buildLinkConnectionDetails((LinkCollectionIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700208 }
209
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700210 if (details.length() == 0) {
211 details.append("(No details for this intent)");
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700212 } else {
213 details.insert(0, "Details: ");
214 }
215 return details.toString();
216 }
217
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700218 private String formatResources(Intent intent) {
219 return (intent.resources().isEmpty() ?
220 "(No resources for this intent)" :
221 "Resources: " + intent.resources());
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700222 }
223
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700224 public IntentTableRow(Intent intent) {
225 ApplicationId appid = intent.appId();
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700226
Simon Hunt44aa2f82015-04-30 15:01:35 -0700227 add(APP_ID, concat(appid.id(), " : ", appid.name()));
228 add(KEY, intent.key());
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700229 add(TYPE, intent.getClass().getSimpleName());
Simon Hunt44aa2f82015-04-30 15:01:35 -0700230 add(PRIORITY, intent.priority());
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700231 add(RESOURCES, formatResources(intent));
232 add(DETAILS, formatDetails(intent));
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700233 }
234
235 @Override
236 protected String[] columnIds() {
237 return COL_IDS;
238 }
239 }
240
241}