Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [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 | |
| 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; |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 21 | import org.onosproject.net.Link; |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 22 | import org.onosproject.net.link.LinkService; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 23 | import org.onosproject.ui.RequestHandler; |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 24 | import org.onosproject.ui.UiMessageHandler; |
Simon Hunt | 191c84a | 2015-08-21 08:24:48 -0700 | [diff] [blame] | 25 | import org.onosproject.ui.topo.BaseLink; |
| 26 | import org.onosproject.ui.topo.BaseLinkMap; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.table.TableModel; |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.table.TableRequestHandler; |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.table.cell.ConnectPointFormatter; |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.table.cell.EnumFormatter; |
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 | |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 34 | /** |
| 35 | * Message handler for link view related messages. |
| 36 | */ |
Simon Hunt | a0ddb02 | 2015-05-01 09:53:01 -0700 | [diff] [blame] | 37 | public class LinkViewMessageHandler extends UiMessageHandler { |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 38 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 39 | private static final String A_BOTH_B = "A ↔ B"; |
| 40 | private static final String A_SINGLE_B = "A → B"; |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 41 | private static final String SLASH = " / "; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -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"; |
Ray Milkey | 8521f81 | 2017-05-24 09:49:26 -0700 | [diff] [blame] | 52 | private static final String EXPECTED = "expected"; |
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 = { |
Ray Milkey | 8521f81 | 2017-05-24 09:49:26 -0700 | [diff] [blame] | 55 | ONE, TWO, TYPE, STATE, DIRECTION, EXPECTED |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 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 { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 68 | private static final String NO_ROWS_MESSAGE = "No links found"; |
| 69 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 70 | private LinkDataRequest() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 71 | super(LINK_DATA_REQ, LINK_DATA_RESP, LINKS); |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 75 | protected String[] getColumnIds() { |
| 76 | return COL_IDS; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 77 | } |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 78 | |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 79 | @Override |
Jian Li | 8baf447 | 2016-01-15 15:08:09 -0800 | [diff] [blame] | 80 | protected String noRowsMessage(ObjectNode payload) { |
Jian Li | 69f6663 | 2016-01-15 12:27:42 -0800 | [diff] [blame] | 81 | return NO_ROWS_MESSAGE; |
| 82 | } |
| 83 | |
| 84 | @Override |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 85 | protected String defaultColumnId() { |
Simon Hunt | abd16f6 | 2015-05-01 13:14:40 -0700 | [diff] [blame] | 86 | return ONE; |
| 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 TableModel createTableModel() { |
| 91 | TableModel tm = super.createTableModel(); |
| 92 | tm.setFormatter(ONE, ConnectPointFormatter.INSTANCE); |
| 93 | tm.setFormatter(TWO, ConnectPointFormatter.INSTANCE); |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 94 | tm.setFormatter(TYPE, EnumFormatter.INSTANCE); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 95 | return tm; |
| 96 | } |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 97 | |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 98 | @Override |
| 99 | protected void populateTable(TableModel tm, ObjectNode payload) { |
| 100 | LinkService ls = get(LinkService.class); |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 101 | BaseLinkMap linkMap = new BaseLinkMap(); |
| 102 | ls.getLinks().forEach(linkMap::add); |
| 103 | linkMap.biLinks().forEach(blink -> populateRow(tm.addRow(), blink)); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 104 | } |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 105 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 106 | private void populateRow(TableModel.Row row, BaseLink blink) { |
| 107 | row.cell(ONE, blink.one().src()) |
| 108 | .cell(TWO, blink.one().dst()) |
| 109 | .cell(TYPE, linkType(blink)) |
| 110 | .cell(STATE, linkState(blink)) |
| 111 | .cell(DIRECTION, linkDir(blink)) |
Ray Milkey | 8521f81 | 2017-05-24 09:49:26 -0700 | [diff] [blame] | 112 | .cell(EXPECTED, blink.one().isExpected()); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 115 | private String linkType(BaseLink link) { |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 116 | StringBuilder sb = new StringBuilder(); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 117 | sb.append(link.one().type()); |
| 118 | if (link.two() != null && link.two().type() != link.one().type()) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 119 | sb.append(SLASH).append(link.two().type()); |
Simon Hunt | 3d1b065 | 2015-05-05 17:27:24 -0700 | [diff] [blame] | 120 | } |
Bri Prebilic Cole | 9467a23 | 2015-05-06 16:59:05 -0700 | [diff] [blame] | 121 | return sb.toString(); |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 124 | private String linkState(BaseLink link) { |
Charles Chan | a78aeb8 | 2016-02-11 16:42:04 -0800 | [diff] [blame] | 125 | if (link.one() == null || link.two() == null) { |
| 126 | return ICON_ID_OFFLINE; |
| 127 | } |
| 128 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 129 | return (link.one().state() == Link.State.ACTIVE || |
| 130 | link.two().state() == Link.State.ACTIVE) ? |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 131 | ICON_ID_ONLINE : ICON_ID_OFFLINE; |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 134 | private String linkDir(BaseLink link) { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 135 | return link.two() != null ? A_BOTH_B : A_SINGLE_B; |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 136 | } |
| 137 | } |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 138 | } |