Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 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 | |
| 17 | package org.onosproject.ui.impl; |
| 18 | |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import com.google.common.collect.ImmutableSet; |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 21 | import com.google.common.collect.Maps; |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 22 | import org.onosproject.net.Link; |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 23 | import org.onosproject.net.LinkKey; |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 24 | import org.onosproject.net.link.LinkService; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 26 | import org.onosproject.ui.UiMessageHandler; |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.impl.TopologyViewMessageHandlerBase.BiLink; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.table.cell.ConnectPointFormatter; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.table.cell.EnumFormatter; |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 32 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 33 | import java.util.Collection; |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 34 | import java.util.Map; |
| 35 | |
| 36 | import static org.onosproject.ui.impl.TopologyViewMessageHandlerBase.addLink; |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Message handler for link view related messages. |
| 40 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 41 | public class LinkViewMessageHandler extends UiMessageHandler { |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 42 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 43 | private static final String LINK_DATA_REQ = "linkDataRequest"; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 44 | private static final String LINK_DATA_RESP = "linkDataResponse"; |
| 45 | private static final String LINKS = "links"; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 46 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 47 | private static final String ONE = "one"; |
| 48 | private static final String TWO = "two"; |
| 49 | private static final String TYPE = "type"; |
| 50 | private static final String STATE = "_iconid_state"; |
| 51 | private static final String DIRECTION = "direction"; |
| 52 | private static final String DURABLE = "durable"; |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 53 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 54 | private static final String[] COL_IDS = { |
| 55 | ONE, TWO, TYPE, STATE, DIRECTION, DURABLE |
| 56 | }; |
| 57 | |
| 58 | private static final String ICON_ID_ONLINE = "active"; |
| 59 | private static final String ICON_ID_OFFLINE = "inactive"; |
| 60 | |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 61 | @Override |
Simon Hunt | da58088 | 2015-05-12 20:58:18 -0700 | [diff] [blame] | 62 | protected Collection<RequestHandler> createRequestHandlers() { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 63 | return ImmutableSet.of(new LinkDataRequest()); |
| 64 | } |
| 65 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 66 | // handler for link table requests |
| 67 | private final class LinkDataRequest extends TableRequestHandler { |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 68 | private LinkDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 69 | super(LINK_DATA_REQ, LINK_DATA_RESP, LINKS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 73 | protected String[] getColumnIds() { |
| 74 | return COL_IDS; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 75 | } |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 76 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 77 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 78 | protected String defaultColumnId() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 79 | return ONE; |
| 80 | } |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 81 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 82 | @Override |
| 83 | protected TableModel createTableModel() { |
| 84 | TableModel tm = super.createTableModel(); |
| 85 | tm.setFormatter(ONE, ConnectPointFormatter.INSTANCE); |
| 86 | tm.setFormatter(TWO, ConnectPointFormatter.INSTANCE); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 87 | tm.setFormatter(TYPE, EnumFormatter.INSTANCE); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 88 | return tm; |
| 89 | } |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 90 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 91 | @Override |
| 92 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 93 | LinkService ls = get(LinkService.class); |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 94 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 95 | // First consolidate all uni-directional links into two-directional ones. |
| 96 | Map<LinkKey, BiLink> biLinks = Maps.newHashMap(); |
| 97 | ls.getLinks().forEach(link -> addLink(biLinks, link)); |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 98 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 99 | // Now scan over all bi-links and produce table rows from them. |
| 100 | biLinks.values().forEach(biLink -> populateRow(tm.addRow(), biLink)); |
| 101 | } |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 102 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 103 | private void populateRow(TableModel.Row row, BiLink biLink) { |
| 104 | row.cell(ONE, biLink.one.src()) |
| 105 | .cell(TWO, biLink.one.dst()) |
| 106 | .cell(TYPE, linkType(biLink)) |
| 107 | .cell(STATE, linkState(biLink)) |
| 108 | .cell(DIRECTION, linkDir(biLink)) |
| 109 | .cell(DURABLE, biLink.one.isDurable()); |
| 110 | } |
| 111 | |
| 112 | private String linkType(BiLink link) { |
| 113 | StringBuilder sb = new StringBuilder(); |
| 114 | sb.append(link.one.type()); |
| 115 | if (link.two != null && link.two.type() != link.one.type()) { |
| 116 | sb.append(" / ").append(link.two.type()); |
| 117 | } |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 118 | return sb.toString(); |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | private String linkState(BiLink link) { |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 122 | return (link.one.state() == Link.State.ACTIVE || |
| 123 | link.two.state() == Link.State.ACTIVE) ? |
| 124 | ICON_ID_ONLINE : ICON_ID_OFFLINE; |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 127 | private String linkDir(BiLink link) { |
Bri Prebilic Cole | 76f632c | 2015-05-28 17:06:02 -0700 | [diff] [blame] | 128 | return link.two != null ? "A ↔ B" : "A → B"; |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 131 | } |