blob: 67b570e8d535e93b2c8b8a219b13aa3ca2babfe1 [file] [log] [blame]
Jian Lia23f46d2017-05-02 18:07:31 +09001/*
2 * Copyright 2017-present 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.mapping.web.gui;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import com.google.common.base.Strings;
20import com.google.common.collect.ImmutableSet;
21import org.onosproject.mapping.MappingEntry;
22import org.onosproject.mapping.MappingService;
23import org.onosproject.net.DeviceId;
24import org.onosproject.ui.RequestHandler;
25import org.onosproject.ui.UiMessageHandler;
26import org.onosproject.ui.table.TableModel;
27import org.onosproject.ui.table.TableRequestHandler;
28
29import java.util.Collection;
30
31import static org.onosproject.mapping.MappingStore.Type.MAP_DATABASE;
32
33/**
34 * Message handler for mapping management view related messages.
35 */
36public class MappingsViewMessageHandler extends UiMessageHandler {
37
38 private static final String MAPPING_DATA_REQ = "mappingDataRequest";
39 private static final String MAPPING_DATA_RESP = "mappingDataResponse";
40 private static final String MAPPINGS = "mappings";
41
42 private static final String ID = "id";
43
44 private static final String[] COL_IDS = {ID};
45
46 @Override
47 protected Collection<RequestHandler> createRequestHandlers() {
48 return ImmutableSet.of(new MappingMessageRequest());
49 }
50
51 private final class MappingMessageRequest extends TableRequestHandler {
52
53 private static final String NO_ROWS_MESSAGE = "No mappings found";
54
55 private MappingMessageRequest() {
56 super(MAPPING_DATA_REQ, MAPPING_DATA_RESP, MAPPINGS);
57 }
58
59 @Override
60 protected String[] getColumnIds() {
61 return COL_IDS;
62 }
63
64 @Override
65 protected String noRowsMessage(ObjectNode payload) {
66 return NO_ROWS_MESSAGE;
67 }
68
69 @Override
70 protected void populateTable(TableModel tm, ObjectNode payload) {
71 String uri = string(payload, "devId");
72 if (!Strings.isNullOrEmpty(uri)) {
73 DeviceId deviceId = DeviceId.deviceId(uri);
74 MappingService ms = get(MappingService.class);
75 for (MappingEntry mapping : ms.getMappingEntries(MAP_DATABASE, deviceId)) {
76 populateRow(tm.addRow(), mapping);
77 }
78 }
79 }
80
81 private void populateRow(TableModel.Row row, MappingEntry mapping) {
82 row.cell(ID, mapping.id().value());
83 }
84 }
85}