blob: 82528e449395ee52039da54f40ac162c921b66d6 [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;
23import org.onosproject.net.intent.ConnectivityIntent;
24import org.onosproject.net.intent.HostToHostIntent;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070025import org.onosproject.net.intent.Intent;
26import org.onosproject.net.intent.IntentService;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070027import org.onosproject.net.intent.LinkCollectionIntent;
28import org.onosproject.net.intent.MultiPointToSinglePointIntent;
29import org.onosproject.net.intent.PathIntent;
30import org.onosproject.net.intent.PointToPointIntent;
31import org.onosproject.net.intent.SinglePointToMultiPointIntent;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070032
33import java.util.ArrayList;
34import java.util.Arrays;
35import java.util.List;
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070036import java.util.Set;
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070037
38/**
39 * Message handler for intent view related messages.
40 */
41public class IntentViewMessageHandler extends AbstractTabularViewMessageHandler {
42
43 /**
44 * Creates a new message handler for the intent messages.
45 */
46 protected IntentViewMessageHandler() {
47 super(ImmutableSet.of("intentDataRequest"));
48 }
49
50 @Override
51 public void process(ObjectNode message) {
52 ObjectNode payload = payload(message);
53 String sortCol = string(payload, "sortCol", "appId");
54 String sortDir = string(payload, "sortDir", "asc");
55
56 IntentService service = get(IntentService.class);
57 TableRow[] rows = generateTableRows(service);
58 RowComparator rc =
59 new RowComparator(sortCol, RowComparator.direction(sortDir));
60 Arrays.sort(rows, rc);
61 ArrayNode intents = generateArrayNode(rows);
62 ObjectNode rootNode = mapper.createObjectNode();
63 rootNode.set("intents", intents);
64
65 connection().sendMessage("intentDataResponse", 0, rootNode);
66 }
67
68 private TableRow[] generateTableRows(IntentService service) {
69 List<TableRow> list = new ArrayList<>();
70 for (Intent intent : service.getIntents()) {
71 list.add(new IntentTableRow(intent));
72 }
73 return list.toArray(new TableRow[list.size()]);
74 }
75
76 /**
77 * TableRow implementation for {@link Intent intents}.
78 */
79 private static class IntentTableRow extends AbstractTableRow {
80
81 private static final String APP_ID = "appId";
82 private static final String KEY = "key";
83 private static final String TYPE = "type";
84 private static final String PRIORITY = "priority";
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070085 private static final String RESOURCES = "resources";
86 private static final String DETAILS = "details";
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070087
88 private static final String[] COL_IDS = {
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070089 APP_ID, KEY, TYPE, PRIORITY, RESOURCES, DETAILS
Bri Prebilic Cole96f26472015-03-31 13:07:05 -070090 };
91
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -070092 private String formatDetails(Intent intent) {
93 StringBuilder details = new StringBuilder("");
94
95 if (intent instanceof ConnectivityIntent) {
96 ConnectivityIntent ci = (ConnectivityIntent) intent;
97 if (!ci.selector().criteria().isEmpty()) {
98 details.append("selector=")
99 .append(ci.selector().criteria().toString());
100 }
101 if (!ci.treatment().allInstructions().isEmpty()) {
102 details.append("treatment=")
103 .append(ci.treatment().allInstructions().toString());
104 }
105 if (ci.constraints() != null && !ci.constraints().isEmpty()) {
106 details.append("constraints=")
107 .append(ci.constraints().toString());
108 }
109 }
110
111 if (intent instanceof HostToHostIntent) {
112 HostToHostIntent pi = (HostToHostIntent) intent;
113 details.append(" host1=")
114 .append(pi.one().toString())
115 .append(", host2=")
116 .append(pi.two().toString());
117
118 } else if (intent instanceof PointToPointIntent) {
119 PointToPointIntent pi = (PointToPointIntent) intent;
120 ConnectPoint ingress = pi.ingressPoint();
121 ConnectPoint egress = pi.egressPoint();
122 details.append(" ingress=")
123 .append(ingress.elementId().toString())
124 .append('/')
125 .append(ingress.port().toString())
126
127 .append(", egress=")
128 .append(egress.elementId().toString())
129 .append('/')
130 .append(egress.port().toString())
131 .append(' ');
132
133 } else if (intent instanceof MultiPointToSinglePointIntent) {
134 MultiPointToSinglePointIntent pi
135 = (MultiPointToSinglePointIntent) intent;
136 Set<ConnectPoint> ingresses = pi.ingressPoints();
137 ConnectPoint egress = pi.egressPoint();
138
139 details.append(" ingress=");
140 for (ConnectPoint ingress : ingresses) {
141 details.append(ingress.elementId().toString())
142 .append('/')
143 .append(ingress.port().toString())
144 .append(' ');
145 }
146
147 details.append(", egress=")
148 .append(egress.elementId().toString())
149 .append('/')
150 .append(egress.port().toString())
151 .append(' ');
152
153 } else if (intent instanceof SinglePointToMultiPointIntent) {
154 SinglePointToMultiPointIntent pi
155 = (SinglePointToMultiPointIntent) intent;
156 ConnectPoint ingress = pi.ingressPoint();
157 Set<ConnectPoint> egresses = pi.egressPoints();
158
159 details.append(" ingress=")
160 .append(ingress.elementId().toString())
161 .append('/')
162 .append(ingress.port().toString())
163 .append(", egress=");
164
165 for (ConnectPoint egress : egresses) {
166 details.append(egress.elementId().toString())
167 .append('/')
168 .append(egress.port().toString())
169 .append(' ');
170 }
171
172 } else if (intent instanceof PathIntent) {
173 PathIntent pi = (PathIntent) intent;
174 details.append(" path=")
175 .append(pi.path().links().toString())
176 .append(", cost=")
177 .append(pi.path().cost());
178
179 } else if (intent instanceof LinkCollectionIntent) {
180 LinkCollectionIntent li = (LinkCollectionIntent) intent;
181 Set<ConnectPoint> egresses = li.egressPoints();
182
183 details.append(" links=")
184 .append(li.links().toString())
185 .append(", egress=");
186
187 for (ConnectPoint egress : egresses) {
188 details.append(egress.elementId().toString())
189 .append('/')
190 .append(egress.port().toString())
191 .append(' ');
192 }
193 }
194
195 if (details.toString().equals("")) {
196 details.append("No details for this intent");
197 } else {
198 details.insert(0, "Details: ");
199 }
200 return details.toString();
201 }
202
203 private String formatResources(Intent i) {
204 if (!i.resources().isEmpty()) {
205 return "Resources: " + i.resources();
206 }
207 return "No resources for this intent.";
208 }
209
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700210 public IntentTableRow(Intent i) {
211 ApplicationId appid = i.appId();
212
213 add(APP_ID, String.valueOf(appid.id()) + " : " + appid.name());
214 add(KEY, i.key().toString());
215 add(TYPE, i.getClass().getSimpleName());
216 add(PRIORITY, Integer.toString(i.priority()));
Bri Prebilic Cole2bd8c352015-03-31 16:18:50 -0700217 add(RESOURCES, formatResources(i));
218 add(DETAILS, formatDetails(i));
Bri Prebilic Cole96f26472015-03-31 13:07:05 -0700219 }
220
221 @Override
222 protected String[] columnIds() {
223 return COL_IDS;
224 }
225 }
226
227}