blob: 01710347b0e1fc25e45af1673fc07507d6c57ae6 [file] [log] [blame]
Jian Lia23f46d2017-05-02 18:07:31 +09001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2017-present Open Networking Foundation
Jian Lia23f46d2017-05-02 18:07:31 +09003 *
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.mapping.web.gui;
17
Jian Libdf8ae52017-05-05 01:31:55 +090018import com.fasterxml.jackson.databind.node.ArrayNode;
Jian Lia23f46d2017-05-02 18:07:31 +090019import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.base.Strings;
21import com.google.common.collect.ImmutableSet;
22import org.onosproject.mapping.MappingEntry;
23import org.onosproject.mapping.MappingService;
Jian Li4689f812017-05-04 07:30:35 +090024import org.onosproject.mapping.MappingTreatment;
Jian Li4689f812017-05-04 07:30:35 +090025import org.onosproject.mapping.addresses.MappingAddress;
Jian Libdf8ae52017-05-05 01:31:55 +090026import org.onosproject.mapping.instructions.MappingInstruction;
27import org.onosproject.mapping.instructions.MulticastMappingInstruction;
28import org.onosproject.mapping.instructions.MulticastMappingInstruction.MulticastType;
29import org.onosproject.mapping.instructions.UnicastMappingInstruction;
30import org.onosproject.mapping.instructions.UnicastMappingInstruction.UnicastType;
Jian Lia23f46d2017-05-02 18:07:31 +090031import org.onosproject.net.DeviceId;
32import org.onosproject.ui.RequestHandler;
33import org.onosproject.ui.UiMessageHandler;
Jian Li4689f812017-05-04 07:30:35 +090034import org.onosproject.ui.table.CellFormatter;
Jian Lia23f46d2017-05-02 18:07:31 +090035import org.onosproject.ui.table.TableModel;
36import org.onosproject.ui.table.TableRequestHandler;
Jian Li4689f812017-05-04 07:30:35 +090037import org.onosproject.ui.table.cell.EnumFormatter;
38import org.onosproject.ui.table.cell.HexLongFormatter;
Jian Lia23f46d2017-05-02 18:07:31 +090039
40import java.util.Collection;
41
Jian Li4689f812017-05-04 07:30:35 +090042import static org.onosproject.mapping.MappingStore.Type.MAP_CACHE;
Jian Lia23f46d2017-05-02 18:07:31 +090043import static org.onosproject.mapping.MappingStore.Type.MAP_DATABASE;
Jian Libdf8ae52017-05-05 01:31:55 +090044import static org.onosproject.mapping.instructions.MappingInstruction.Type.MULTICAST;
45import static org.onosproject.mapping.instructions.MappingInstruction.Type.UNICAST;
Jian Lia23f46d2017-05-02 18:07:31 +090046
47/**
48 * Message handler for mapping management view related messages.
49 */
50public class MappingsViewMessageHandler extends UiMessageHandler {
51
52 private static final String MAPPING_DATA_REQ = "mappingDataRequest";
53 private static final String MAPPING_DATA_RESP = "mappingDataResponse";
54 private static final String MAPPINGS = "mappings";
55
Jian Libdf8ae52017-05-05 01:31:55 +090056 private static final String MAPPING_DETAIL_REQ = "mappingDetailsRequest";
57 private static final String MAPPING_DETAIL_RESP = "mappingDetailsResponse";
58 private static final String DETAILS = "details";
59
Jian Lia23f46d2017-05-02 18:07:31 +090060 private static final String ID = "id";
Jian Libdf8ae52017-05-05 01:31:55 +090061 private static final String MAPPING_ID = "mappingId";
Jian Li4689f812017-05-04 07:30:35 +090062 private static final String MAPPING_KEY = "mappingKey";
63 private static final String MAPPING_VALUE = "mappingValue";
64 private static final String MAPPING_ACTION = "mappingAction";
65 private static final String TYPE = "type";
66 private static final String STATE = "state";
67 private static final String DATABASE = "database";
68 private static final String CACHE = "cache";
Jian Libdf8ae52017-05-05 01:31:55 +090069 private static final String MAPPING_TREATMENTS = "mappingTreatments";
70
71 private static final String MAPPING_ADDRESS = "address";
72 private static final String UNICAST_WEIGHT = "unicastWeight";
73 private static final String UNICAST_PRIORITY = "unicastPriority";
74 private static final String MULTICAST_WEIGHT = "multicastWeight";
75 private static final String MULTICAST_PRIORITY = "multicastPriority";
Jian Lia23f46d2017-05-02 18:07:31 +090076
Jian Li4689f812017-05-04 07:30:35 +090077 private static final String COMMA = ", ";
78 private static final String OX = "0x";
79 private static final String EMPTY = "";
80
81 private static final String NULL_ADDRESS_MSG = "(No mapping address for this mapping)";
82
83 private static final String[] COL_IDS = {
84 ID, MAPPING_KEY, MAPPING_VALUE, STATE, MAPPING_ACTION, TYPE
85 };
Jian Lia23f46d2017-05-02 18:07:31 +090086
87 @Override
88 protected Collection<RequestHandler> createRequestHandlers() {
Jian Libdf8ae52017-05-05 01:31:55 +090089 return ImmutableSet.of(
90 new MappingMessageRequest(),
91 new DetailRequestHandler()
92 );
Jian Lia23f46d2017-05-02 18:07:31 +090093 }
94
Jian Li4689f812017-05-04 07:30:35 +090095 /**
96 * Handler for mapping message requests.
97 */
Jian Lia23f46d2017-05-02 18:07:31 +090098 private final class MappingMessageRequest extends TableRequestHandler {
99
100 private static final String NO_ROWS_MESSAGE = "No mappings found";
101
102 private MappingMessageRequest() {
103 super(MAPPING_DATA_REQ, MAPPING_DATA_RESP, MAPPINGS);
104 }
105
106 @Override
107 protected String[] getColumnIds() {
108 return COL_IDS;
109 }
110
111 @Override
112 protected String noRowsMessage(ObjectNode payload) {
113 return NO_ROWS_MESSAGE;
114 }
115
116 @Override
Jian Li4689f812017-05-04 07:30:35 +0900117 protected TableModel createTableModel() {
118 TableModel tm = super.createTableModel();
119 tm.setFormatter(ID, HexLongFormatter.INSTANCE);
120 tm.setFormatter(TYPE, EnumFormatter.INSTANCE);
121 tm.setFormatter(STATE, EnumFormatter.INSTANCE);
122 tm.setFormatter(MAPPING_KEY, new MappingKeyFormatter());
Jian Li4689f812017-05-04 07:30:35 +0900123 return tm;
124 }
125
126 @Override
Jian Lia23f46d2017-05-02 18:07:31 +0900127 protected void populateTable(TableModel tm, ObjectNode payload) {
128 String uri = string(payload, "devId");
129 if (!Strings.isNullOrEmpty(uri)) {
130 DeviceId deviceId = DeviceId.deviceId(uri);
131 MappingService ms = get(MappingService.class);
Jian Li4689f812017-05-04 07:30:35 +0900132
Jian Lia23f46d2017-05-02 18:07:31 +0900133 for (MappingEntry mapping : ms.getMappingEntries(MAP_DATABASE, deviceId)) {
Jian Li4689f812017-05-04 07:30:35 +0900134 populateRow(tm.addRow(), mapping, DATABASE);
135 }
136
137 for (MappingEntry mapping : ms.getMappingEntries(MAP_CACHE, deviceId)) {
138 populateRow(tm.addRow(), mapping, CACHE);
Jian Lia23f46d2017-05-02 18:07:31 +0900139 }
140 }
141 }
142
Jian Li4689f812017-05-04 07:30:35 +0900143 private void populateRow(TableModel.Row row, MappingEntry mapping,
144 String type) {
145 row.cell(ID, mapping.id().value())
146 .cell(STATE, mapping.state())
147 .cell(TYPE, type)
148 .cell(MAPPING_ACTION, mapping.value().action())
Jian Libdf8ae52017-05-05 01:31:55 +0900149 .cell(MAPPING_KEY, mapping);
Jian Li4689f812017-05-04 07:30:35 +0900150 }
151 }
152
153 /**
154 * A formatter for formatting mapping key.
155 */
156 private final class MappingKeyFormatter implements CellFormatter {
157
158 @Override
159 public String format(Object value) {
160 MappingEntry mapping = (MappingEntry) value;
161 MappingAddress address = mapping.key().address();
162
163 if (address == null) {
164 return NULL_ADDRESS_MSG;
165 }
Jian Li4689f812017-05-04 07:30:35 +0900166
Jian Libdf8ae52017-05-05 01:31:55 +0900167 return address.toString();
Jian Li4689f812017-05-04 07:30:35 +0900168 }
169 }
170
171 /**
Jian Libdf8ae52017-05-05 01:31:55 +0900172 * Handler for detailed mapping message requests.
Jian Li4689f812017-05-04 07:30:35 +0900173 */
Jian Libdf8ae52017-05-05 01:31:55 +0900174 private final class DetailRequestHandler extends RequestHandler {
Jian Li4689f812017-05-04 07:30:35 +0900175
Jian Libdf8ae52017-05-05 01:31:55 +0900176 private DetailRequestHandler() {
177 super(MAPPING_DETAIL_REQ);
Jian Li4689f812017-05-04 07:30:35 +0900178 }
179
Jian Libdf8ae52017-05-05 01:31:55 +0900180 private MappingEntry findMappingById(String mappingId) {
181 MappingService ms = get(MappingService.class);
182 Iterable<MappingEntry> dbEntries = ms.getAllMappingEntries(MAP_DATABASE);
183 Iterable<MappingEntry> cacheEntries = ms.getAllMappingEntries(MAP_CACHE);
184
185 for (MappingEntry entry : dbEntries) {
186 if (entry.id().toString().equals(mappingId)) {
187 return entry;
Jian Li4689f812017-05-04 07:30:35 +0900188 }
189 }
Jian Libdf8ae52017-05-05 01:31:55 +0900190
191 for (MappingEntry entry : cacheEntries) {
192 if (entry.id().toString().equals(mappingId)) {
193 return entry;
194 }
195 }
196
197 return null;
198 }
199
200 /**
201 * Generates a node object of a given mapping treatment.
202 *
203 * @param treatment mapping treatment
204 * @return node object
205 */
206 private ObjectNode getTreatmentNode(MappingTreatment treatment) {
207 ObjectNode data = objectNode();
208
209 data.put(MAPPING_ADDRESS, treatment.address().toString());
210
211 for (MappingInstruction instruct : treatment.instructions()) {
212 if (instruct.type() == UNICAST) {
213 UnicastMappingInstruction unicastInstruct =
214 (UnicastMappingInstruction) instruct;
215 if (unicastInstruct.subtype() == UnicastType.WEIGHT) {
216 data.put(UNICAST_WEIGHT,
217 ((UnicastMappingInstruction.WeightMappingInstruction)
218 unicastInstruct).weight());
219 }
220 if (unicastInstruct.subtype() == UnicastType.PRIORITY) {
221 data.put(UNICAST_PRIORITY,
222 ((UnicastMappingInstruction.PriorityMappingInstruction)
223 unicastInstruct).priority());
224 }
225 }
226
227 if (instruct.type() == MULTICAST) {
228 MulticastMappingInstruction multicastInstruct =
229 (MulticastMappingInstruction) instruct;
230 if (multicastInstruct.subtype() == MulticastType.WEIGHT) {
231 data.put(MULTICAST_WEIGHT,
232 ((MulticastMappingInstruction.WeightMappingInstruction)
233 multicastInstruct).weight());
234 }
235 if (multicastInstruct.subtype() == MulticastType.PRIORITY) {
236 data.put(MULTICAST_PRIORITY,
237 ((MulticastMappingInstruction.PriorityMappingInstruction)
238 multicastInstruct).priority());
239 }
240 }
241
242 // TODO: extension address will be handled later
243 }
244
245 return data;
246 }
247
248 @Override
249 public void process(ObjectNode payload) {
250 String mappingId = string(payload, MAPPING_ID);
251 String type = string(payload, TYPE);
252 String strippedFlowId = mappingId.replaceAll(OX, EMPTY);
253
254 MappingEntry mapping = findMappingById(strippedFlowId);
255 if (mapping != null) {
256 ArrayNode arrayNode = arrayNode();
257
258 for (MappingTreatment treatment : mapping.value().treatments()) {
259 arrayNode.add(getTreatmentNode(treatment));
260 }
261
262 ObjectNode detailsNode = objectNode();
263 detailsNode.put(MAPPING_ID, mappingId);
264 detailsNode.put(STATE, mapping.state().name());
265 detailsNode.put(TYPE, type);
266 detailsNode.put(MAPPING_ACTION, mapping.value().action().toString());
267
268 ObjectNode keyNode = objectNode();
269 keyNode.put(MAPPING_ADDRESS, mapping.key().address().toString());
270
271 ObjectNode valueNode = objectNode();
272 valueNode.set(MAPPING_TREATMENTS, arrayNode);
273
274 detailsNode.set(MAPPING_KEY, keyNode);
275 detailsNode.set(MAPPING_VALUE, valueNode);
276
277 ObjectNode rootNode = objectNode();
278 rootNode.set(DETAILS, detailsNode);
279 sendMessage(MAPPING_DETAIL_RESP, rootNode);
280 }
Jian Lia23f46d2017-05-02 18:07:31 +0900281 }
282 }
283}