Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.mapping.web.gui; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 19 | import com.google.common.base.Strings; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import org.onosproject.mapping.MappingEntry; |
| 22 | import org.onosproject.mapping.MappingService; |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame^] | 23 | import org.onosproject.mapping.MappingTreatment; |
| 24 | import org.onosproject.mapping.MappingValue; |
| 25 | import org.onosproject.mapping.addresses.MappingAddress; |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 26 | import org.onosproject.net.DeviceId; |
| 27 | import org.onosproject.ui.RequestHandler; |
| 28 | import org.onosproject.ui.UiMessageHandler; |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame^] | 29 | import org.onosproject.ui.table.CellFormatter; |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 30 | import org.onosproject.ui.table.TableModel; |
| 31 | import org.onosproject.ui.table.TableRequestHandler; |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame^] | 32 | import org.onosproject.ui.table.cell.EnumFormatter; |
| 33 | import org.onosproject.ui.table.cell.HexLongFormatter; |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 34 | |
| 35 | import java.util.Collection; |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame^] | 36 | import java.util.List; |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 37 | |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame^] | 38 | import static org.onosproject.mapping.MappingStore.Type.MAP_CACHE; |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 39 | import static org.onosproject.mapping.MappingStore.Type.MAP_DATABASE; |
| 40 | |
| 41 | /** |
| 42 | * Message handler for mapping management view related messages. |
| 43 | */ |
| 44 | public 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 Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame^] | 51 | 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 Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 58 | |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame^] | 59 | 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 Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 68 | |
| 69 | @Override |
| 70 | protected Collection<RequestHandler> createRequestHandlers() { |
| 71 | return ImmutableSet.of(new MappingMessageRequest()); |
| 72 | } |
| 73 | |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame^] | 74 | /** |
| 75 | * Handler for mapping message requests. |
| 76 | */ |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 77 | 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 Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame^] | 96 | 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 Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 107 | 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 Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame^] | 112 | |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 113 | for (MappingEntry mapping : ms.getMappingEntries(MAP_DATABASE, deviceId)) { |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame^] | 114 | populateRow(tm.addRow(), mapping, DATABASE); |
| 115 | } |
| 116 | |
| 117 | for (MappingEntry mapping : ms.getMappingEntries(MAP_CACHE, deviceId)) { |
| 118 | populateRow(tm.addRow(), mapping, CACHE); |
Jian Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 119 | } |
| 120 | } |
| 121 | } |
| 122 | |
Jian Li | 4689f81 | 2017-05-04 07:30:35 +0900 | [diff] [blame^] | 123 | 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 Li | a23f46d | 2017-05-02 18:07:31 +0900 | [diff] [blame] | 178 | } |
| 179 | } |
| 180 | } |