Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame^] | 2 | * Copyright 2017-present Open Networking Foundation |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 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 | */ |
| 16 | package org.onosproject.mapping.web.gui; |
| 17 | |
Jian Li | bdf8ae5 | 2017-05-05 01:31:55 +0900 | [diff] [blame] | 18 | import com.fasterxml.jackson.databind.node.ArrayNode; |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.base.Strings; |
| 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.onosproject.mapping.MappingEntry; |
| 23 | import org.onosproject.mapping.MappingService; |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 24 | import org.onosproject.mapping.MappingTreatment; |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 25 | import org.onosproject.mapping.addresses.MappingAddress; |
Jian Li | bdf8ae5 | 2017-05-05 01:31:55 +0900 | [diff] [blame] | 26 | import org.onosproject.mapping.instructions.MappingInstruction; |
| 27 | import org.onosproject.mapping.instructions.MulticastMappingInstruction; |
| 28 | import org.onosproject.mapping.instructions.MulticastMappingInstruction.MulticastType; |
| 29 | import org.onosproject.mapping.instructions.UnicastMappingInstruction; |
| 30 | import org.onosproject.mapping.instructions.UnicastMappingInstruction.UnicastType; |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 31 | import org.onosproject.net.DeviceId; |
| 32 | import org.onosproject.ui.RequestHandler; |
| 33 | import org.onosproject.ui.UiMessageHandler; |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 34 | import org.onosproject.ui.table.CellFormatter; |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 35 | import org.onosproject.ui.table.TableModel; |
| 36 | import org.onosproject.ui.table.TableRequestHandler; |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 37 | import org.onosproject.ui.table.cell.EnumFormatter; |
| 38 | import org.onosproject.ui.table.cell.HexLongFormatter; |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 39 | |
| 40 | import java.util.Collection; |
| 41 | |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 42 | import static org.onosproject.mapping.MappingStore.Type.MAP_CACHE; |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 43 | import static org.onosproject.mapping.MappingStore.Type.MAP_DATABASE; |
Jian Li | bdf8ae5 | 2017-05-05 01:31:55 +0900 | [diff] [blame] | 44 | import static org.onosproject.mapping.instructions.MappingInstruction.Type.MULTICAST; |
| 45 | import static org.onosproject.mapping.instructions.MappingInstruction.Type.UNICAST; |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Message handler for mapping management view related messages. |
| 49 | */ |
| 50 | public 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 Li | bdf8ae5 | 2017-05-05 01:31:55 +0900 | [diff] [blame] | 56 | 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 Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 60 | private static final String ID = "id"; |
Jian Li | bdf8ae5 | 2017-05-05 01:31:55 +0900 | [diff] [blame] | 61 | private static final String MAPPING_ID = "mappingId"; |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 62 | 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 Li | bdf8ae5 | 2017-05-05 01:31:55 +0900 | [diff] [blame] | 69 | 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 Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 76 | |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 77 | 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 Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 86 | |
| 87 | @Override |
| 88 | protected Collection<RequestHandler> createRequestHandlers() { |
Jian Li | bdf8ae5 | 2017-05-05 01:31:55 +0900 | [diff] [blame] | 89 | return ImmutableSet.of( |
| 90 | new MappingMessageRequest(), |
| 91 | new DetailRequestHandler() |
| 92 | ); |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 93 | } |
| 94 | |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 95 | /** |
| 96 | * Handler for mapping message requests. |
| 97 | */ |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 98 | 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 Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 117 | 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 Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 123 | return tm; |
| 124 | } |
| 125 | |
| 126 | @Override |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 127 | 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 Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 132 | |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 133 | for (MappingEntry mapping : ms.getMappingEntries(MAP_DATABASE, deviceId)) { |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 134 | populateRow(tm.addRow(), mapping, DATABASE); |
| 135 | } |
| 136 | |
| 137 | for (MappingEntry mapping : ms.getMappingEntries(MAP_CACHE, deviceId)) { |
| 138 | populateRow(tm.addRow(), mapping, CACHE); |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 143 | 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 Li | bdf8ae5 | 2017-05-05 01:31:55 +0900 | [diff] [blame] | 149 | .cell(MAPPING_KEY, mapping); |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 150 | } |
| 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 Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 166 | |
Jian Li | bdf8ae5 | 2017-05-05 01:31:55 +0900 | [diff] [blame] | 167 | return address.toString(); |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | /** |
Jian Li | bdf8ae5 | 2017-05-05 01:31:55 +0900 | [diff] [blame] | 172 | * Handler for detailed mapping message requests. |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 173 | */ |
Jian Li | bdf8ae5 | 2017-05-05 01:31:55 +0900 | [diff] [blame] | 174 | private final class DetailRequestHandler extends RequestHandler { |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 175 | |
Jian Li | bdf8ae5 | 2017-05-05 01:31:55 +0900 | [diff] [blame] | 176 | private DetailRequestHandler() { |
| 177 | super(MAPPING_DETAIL_REQ); |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 178 | } |
| 179 | |
Jian Li | bdf8ae5 | 2017-05-05 01:31:55 +0900 | [diff] [blame] | 180 | 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 Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame] | 188 | } |
| 189 | } |
Jian Li | bdf8ae5 | 2017-05-05 01:31:55 +0900 | [diff] [blame] | 190 | |
| 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 Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | } |