blob: b46e3cd92f188d2178ecbd8c2f21d42050a835d9 [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;
Jian Li4689f812017-05-04 07:30:35 +090023import org.onosproject.mapping.MappingTreatment;
24import org.onosproject.mapping.MappingValue;
25import org.onosproject.mapping.addresses.MappingAddress;
Jian Lia23f46d2017-05-02 18:07:31 +090026import org.onosproject.net.DeviceId;
27import org.onosproject.ui.RequestHandler;
28import org.onosproject.ui.UiMessageHandler;
Jian Li4689f812017-05-04 07:30:35 +090029import org.onosproject.ui.table.CellFormatter;
Jian Lia23f46d2017-05-02 18:07:31 +090030import org.onosproject.ui.table.TableModel;
31import org.onosproject.ui.table.TableRequestHandler;
Jian Li4689f812017-05-04 07:30:35 +090032import org.onosproject.ui.table.cell.EnumFormatter;
33import org.onosproject.ui.table.cell.HexLongFormatter;
Jian Lia23f46d2017-05-02 18:07:31 +090034
35import java.util.Collection;
Jian Li4689f812017-05-04 07:30:35 +090036import java.util.List;
Jian Lia23f46d2017-05-02 18:07:31 +090037
Jian Li4689f812017-05-04 07:30:35 +090038import static org.onosproject.mapping.MappingStore.Type.MAP_CACHE;
Jian Lia23f46d2017-05-02 18:07:31 +090039import static org.onosproject.mapping.MappingStore.Type.MAP_DATABASE;
40
41/**
42 * Message handler for mapping management view related messages.
43 */
44public class MappingsViewMessageHandler extends UiMessageHandler {
45
46 private static final String MAPPING_DATA_REQ = "mappingDataRequest";
47 private static final String MAPPING_DATA_RESP = "mappingDataResponse";
48 private static final String MAPPINGS = "mappings";
49
50 private static final String ID = "id";
Jian Li4689f812017-05-04 07:30:35 +090051 private static final String MAPPING_KEY = "mappingKey";
52 private static final String MAPPING_VALUE = "mappingValue";
53 private static final String MAPPING_ACTION = "mappingAction";
54 private static final String TYPE = "type";
55 private static final String STATE = "state";
56 private static final String DATABASE = "database";
57 private static final String CACHE = "cache";
Jian Lia23f46d2017-05-02 18:07:31 +090058
Jian Li4689f812017-05-04 07:30:35 +090059 private static final String COMMA = ", ";
60 private static final String OX = "0x";
61 private static final String EMPTY = "";
62
63 private static final String NULL_ADDRESS_MSG = "(No mapping address for this mapping)";
64
65 private static final String[] COL_IDS = {
66 ID, MAPPING_KEY, MAPPING_VALUE, STATE, MAPPING_ACTION, TYPE
67 };
Jian Lia23f46d2017-05-02 18:07:31 +090068
69 @Override
70 protected Collection<RequestHandler> createRequestHandlers() {
71 return ImmutableSet.of(new MappingMessageRequest());
72 }
73
Jian Li4689f812017-05-04 07:30:35 +090074 /**
75 * Handler for mapping message requests.
76 */
Jian Lia23f46d2017-05-02 18:07:31 +090077 private final class MappingMessageRequest extends TableRequestHandler {
78
79 private static final String NO_ROWS_MESSAGE = "No mappings found";
80
81 private MappingMessageRequest() {
82 super(MAPPING_DATA_REQ, MAPPING_DATA_RESP, MAPPINGS);
83 }
84
85 @Override
86 protected String[] getColumnIds() {
87 return COL_IDS;
88 }
89
90 @Override
91 protected String noRowsMessage(ObjectNode payload) {
92 return NO_ROWS_MESSAGE;
93 }
94
95 @Override
Jian Li4689f812017-05-04 07:30:35 +090096 protected TableModel createTableModel() {
97 TableModel tm = super.createTableModel();
98 tm.setFormatter(ID, HexLongFormatter.INSTANCE);
99 tm.setFormatter(TYPE, EnumFormatter.INSTANCE);
100 tm.setFormatter(STATE, EnumFormatter.INSTANCE);
101 tm.setFormatter(MAPPING_KEY, new MappingKeyFormatter());
102 tm.setFormatter(MAPPING_VALUE, new MappingValueFormatter());
103 return tm;
104 }
105
106 @Override
Jian Lia23f46d2017-05-02 18:07:31 +0900107 protected void populateTable(TableModel tm, ObjectNode payload) {
108 String uri = string(payload, "devId");
109 if (!Strings.isNullOrEmpty(uri)) {
110 DeviceId deviceId = DeviceId.deviceId(uri);
111 MappingService ms = get(MappingService.class);
Jian Li4689f812017-05-04 07:30:35 +0900112
Jian Lia23f46d2017-05-02 18:07:31 +0900113 for (MappingEntry mapping : ms.getMappingEntries(MAP_DATABASE, deviceId)) {
Jian Li4689f812017-05-04 07:30:35 +0900114 populateRow(tm.addRow(), mapping, DATABASE);
115 }
116
117 for (MappingEntry mapping : ms.getMappingEntries(MAP_CACHE, deviceId)) {
118 populateRow(tm.addRow(), mapping, CACHE);
Jian Lia23f46d2017-05-02 18:07:31 +0900119 }
120 }
121 }
122
Jian Li4689f812017-05-04 07:30:35 +0900123 private void populateRow(TableModel.Row row, MappingEntry mapping,
124 String type) {
125 row.cell(ID, mapping.id().value())
126 .cell(STATE, mapping.state())
127 .cell(TYPE, type)
128 .cell(MAPPING_ACTION, mapping.value().action())
129 .cell(MAPPING_KEY, mapping)
130 .cell(MAPPING_VALUE, mapping);
131 }
132 }
133
134 /**
135 * A formatter for formatting mapping key.
136 */
137 private final class MappingKeyFormatter implements CellFormatter {
138
139 @Override
140 public String format(Object value) {
141 MappingEntry mapping = (MappingEntry) value;
142 MappingAddress address = mapping.key().address();
143
144 if (address == null) {
145 return NULL_ADDRESS_MSG;
146 }
147 StringBuilder sb = new StringBuilder("Mapping address: ");
148 sb.append(address.toString());
149
150 return sb.toString();
151 }
152 }
153
154 /**
155 * A formatter for formatting mapping value.
156 */
157 private final class MappingValueFormatter implements CellFormatter {
158
159 @Override
160 public String format(Object value) {
161 MappingEntry mapping = (MappingEntry) value;
162 MappingValue mappingValue = mapping.value();
163 List<MappingTreatment> treatments = mappingValue.treatments();
164
165 StringBuilder sb = new StringBuilder("Treatments: ");
166 formatTreatments(sb, treatments);
167
168 return sb.toString();
169 }
170
171 private void formatTreatments(StringBuilder sb,
172 List<MappingTreatment> treatments) {
173 if (!treatments.isEmpty()) {
174 for (MappingTreatment t : treatments) {
175 sb.append(t).append(COMMA);
176 }
177 }
Jian Lia23f46d2017-05-02 18:07:31 +0900178 }
179 }
180}