blob: 8277465f1a1793b5dbe6176301f35b2f8939bcd1 [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
18import com.fasterxml.jackson.databind.node.ArrayNode;
19import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
21import org.onosproject.core.ApplicationId;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070022import org.onosproject.net.ConnectPoint;
Bri Prebilic Coled5df2542015-04-01 10:36:09 -070023import org.onosproject.net.flow.criteria.Criterion;
24import org.onosproject.net.flow.instructions.Instruction;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070025import org.onosproject.net.intent.ConnectivityIntent;
Bri Prebilic Coled5df2542015-04-01 10:36:09 -070026import org.onosproject.net.intent.Constraint;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070027import org.onosproject.net.intent.HostToHostIntent;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070028import org.onosproject.net.intent.Intent;
29import org.onosproject.net.intent.IntentService;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070030import org.onosproject.net.intent.LinkCollectionIntent;
31import org.onosproject.net.intent.MultiPointToSinglePointIntent;
32import org.onosproject.net.intent.PathIntent;
33import org.onosproject.net.intent.PointToPointIntent;
34import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070035
36import java.util.ArrayList;
37import java.util.Arrays;
38import java.util.List;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070039import java.util.Set;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070040
41/**
42 * Message handler for intent view related messages.
43 */
44public class IntentViewMessageHandler extends AbstractTabularViewMessageHandler {
45
46 /**
47 * Creates a new message handler for the intent messages.
48 */
49 protected IntentViewMessageHandler() {
50 super(ImmutableSet.of("intentDataRequest"));
51 }
52
53 @Override
54 public void process(ObjectNode message) {
55 ObjectNode payload = payload(message);
56 String sortCol = string(payload, "sortCol", "appId");
57 String sortDir = string(payload, "sortDir", "asc");
58
59 IntentService service = get(IntentService.class);
60 TableRow[] rows = generateTableRows(service);
61 RowComparator rc =
62 new RowComparator(sortCol, RowComparator.direction(sortDir));
63 Arrays.sort(rows, rc);
64 ArrayNode intents = generateArrayNode(rows);
65 ObjectNode rootNode = mapper.createObjectNode();
66 rootNode.set("intents", intents);
67
68 connection().sendMessage("intentDataResponse", 0, rootNode);
69 }
70
71 private TableRow[] generateTableRows(IntentService service) {
72 List<TableRow> list = new ArrayList<>();
73 for (Intent intent : service.getIntents()) {
74 list.add(new IntentTableRow(intent));
75 }
76 return list.toArray(new TableRow[list.size()]);
77 }
78
79 /**
80 * TableRow implementation for {@link Intent intents}.
81 */
82 private static class IntentTableRow extends AbstractTableRow {
83
84 private static final String APP_ID = "appId";
85 private static final String KEY = "key";
86 private static final String TYPE = "type";
87 private static final String PRIORITY = "priority";
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070088 private static final String RESOURCES = "resources";
89 private static final String DETAILS = "details";
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070090
91 private static final String[] COL_IDS = {
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070092 APP_ID, KEY, TYPE, PRIORITY, RESOURCES, DETAILS
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070093 };
94
Bri Prebilic Coled5df2542015-04-01 10:36:09 -070095 private StringBuilder details = new StringBuilder();
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070096
Bri Prebilic Coled5df2542015-04-01 10:36:09 -070097 private void appendMultiPointsDetails(Set<ConnectPoint> points) {
98 for (ConnectPoint point : points) {
99 details.append(point.elementId())
100 .append('/')
101 .append(point.port())
102 .append(' ');
103 }
104 }
105
106 private void buildConnectivityDetails(ConnectivityIntent intent) {
107 Set<Criterion> criteria = intent.selector().criteria();
108 List<Instruction> instructions = intent.treatment().allInstructions();
109 List<Constraint> constraints = intent.constraints();
110
111 if (!criteria.isEmpty()) {
112 details.append("selector=").append(criteria);
113 }
114 if (!instructions.isEmpty()) {
115 details.append("treatment=").append(instructions);
116 }
117 if (constraints != null && !constraints.isEmpty()) {
118 details.append("constraints=").append(constraints);
119 }
120 }
121
122 private void buildHostToHostDetails(HostToHostIntent intent) {
123 details.append(" host1=")
124 .append(intent.one())
125 .append(", host2=")
126 .append(intent.two());
127 }
128
129 private void buildPointToPointDetails(PointToPointIntent intent) {
130 ConnectPoint ingress = intent.ingressPoint();
131 ConnectPoint egress = intent.egressPoint();
132 details.append(" ingress=")
133 .append(ingress.elementId())
134 .append('/')
135 .append(ingress.port())
136
137 .append(", egress=")
138 .append(egress.elementId())
139 .append('/')
140 .append(egress.port())
141 .append(' ');
142 }
143
144 private void buildMPToSPDetails(MultiPointToSinglePointIntent intent) {
145 ConnectPoint egress = intent.egressPoint();
146
147 details.append(" ingress=");
148 appendMultiPointsDetails(intent.ingressPoints());
149
150 details.append(", egress=")
151 .append(egress.elementId())
152 .append('/')
153 .append(egress.port())
154 .append(' ');
155 }
156
157 private void buildSPToMPDetails(SinglePointToMultiPointIntent intent) {
158 ConnectPoint ingress = intent.ingressPoint();
159
160 details.append(" ingress=")
161 .append(ingress.elementId())
162 .append('/')
163 .append(ingress.port())
164 .append(", egress=");
165
166 appendMultiPointsDetails(intent.egressPoints());
167 }
168
169 private void buildPathDetails(PathIntent intent) {
170 details.append(" path=")
171 .append(intent.path().links())
172 .append(", cost=")
173 .append(intent.path().cost());
174 }
175
176 private void buildLinkConnectionDetails(LinkCollectionIntent intent) {
177 details.append(" links=")
178 .append(intent.links())
179 .append(", egress=");
180
181 appendMultiPointsDetails(intent.egressPoints());
182 }
183
184 private String formatDetails(Intent intent) {
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700185 if (intent instanceof ConnectivityIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700186 buildConnectivityDetails((ConnectivityIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700187 }
188
189 if (intent instanceof HostToHostIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700190 buildHostToHostDetails((HostToHostIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700191
192 } else if (intent instanceof PointToPointIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700193 buildPointToPointDetails((PointToPointIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700194
195 } else if (intent instanceof MultiPointToSinglePointIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700196 buildMPToSPDetails((MultiPointToSinglePointIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700197
198 } else if (intent instanceof SinglePointToMultiPointIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700199 buildSPToMPDetails((SinglePointToMultiPointIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700200
201 } else if (intent instanceof PathIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700202 buildPathDetails((PathIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700203
204 } else if (intent instanceof LinkCollectionIntent) {
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700205 buildLinkConnectionDetails((LinkCollectionIntent) intent);
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700206 }
207
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700208 if (details.length() == 0) {
209 details.append("(No details for this intent)");
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700210 } else {
211 details.insert(0, "Details: ");
212 }
213 return details.toString();
214 }
215
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700216 private String formatResources(Intent intent) {
217 return (intent.resources().isEmpty() ?
218 "(No resources for this intent)" :
219 "Resources: " + intent.resources());
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700220 }
221
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700222 public IntentTableRow(Intent intent) {
223 ApplicationId appid = intent.appId();
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700224
225 add(APP_ID, String.valueOf(appid.id()) + " : " + appid.name());
Bri Prebilic Coled5df2542015-04-01 10:36:09 -0700226 add(KEY, intent.key().toString());
227 add(TYPE, intent.getClass().getSimpleName());
228 add(PRIORITY, Integer.toString(intent.priority()));
229 add(RESOURCES, formatResources(intent));
230 add(DETAILS, formatDetails(intent));
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700231 }
232
233 @Override
234 protected String[] columnIds() {
235 return COL_IDS;
236 }
237 }
238
239}