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