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 | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 22 | import org.onosproject.net.ConnectPoint; |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 23 | import org.onosproject.net.Link; |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 24 | import org.onosproject.net.LinkKey; |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 25 | import org.onosproject.net.link.LinkService; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame^] | 26 | import org.onosproject.ui.RequestHandler; |
| 27 | import org.onosproject.ui.UiMessageHandlerTwo; |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.impl.TopologyViewMessageHandlerBase.BiLink; |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 29 | import org.onosproject.ui.table.AbstractTableRow; |
| 30 | import org.onosproject.ui.table.RowComparator; |
| 31 | import org.onosproject.ui.table.TableRow; |
| 32 | import org.onosproject.ui.table.TableUtils; |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.Arrays; |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame^] | 36 | import java.util.Collection; |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 37 | import java.util.List; |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 38 | import java.util.Map; |
| 39 | |
| 40 | import static org.onosproject.ui.impl.TopologyViewMessageHandlerBase.addLink; |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * Message handler for link view related messages. |
| 44 | */ |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame^] | 45 | public class LinkViewMessageHandler extends UiMessageHandlerTwo { |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 46 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame^] | 47 | private static final String LINK_DATA_REQ = "linkDataRequest"; |
| 48 | |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 49 | |
| 50 | @Override |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame^] | 51 | protected Collection<RequestHandler> getHandlers() { |
| 52 | return ImmutableSet.of(new LinkDataRequest()); |
| 53 | } |
| 54 | |
| 55 | // ====================================================================== |
| 56 | |
| 57 | private final class LinkDataRequest extends RequestHandler { |
| 58 | |
| 59 | private LinkDataRequest() { |
| 60 | super(LINK_DATA_REQ); |
| 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public void process(long sid, ObjectNode payload) { |
| 65 | RowComparator rc = TableUtils.createRowComparator(payload, "one"); |
| 66 | |
| 67 | LinkService service = get(LinkService.class); |
| 68 | TableRow[] rows = generateTableRows(service); |
| 69 | Arrays.sort(rows, rc); |
| 70 | ObjectNode rootNode = MAPPER.createObjectNode(); |
| 71 | rootNode.set("links", TableUtils.generateArrayNode(rows)); |
| 72 | |
| 73 | sendMessage("linkDataResponse", 0, rootNode); |
| 74 | } |
| 75 | |
| 76 | private TableRow[] generateTableRows(LinkService service) { |
| 77 | List<TableRow> list = new ArrayList<>(); |
| 78 | |
| 79 | // First consolidate all uni-directional links into two-directional ones. |
| 80 | Map<LinkKey, BiLink> biLinks = Maps.newHashMap(); |
| 81 | service.getLinks().forEach(link -> addLink(biLinks, link)); |
| 82 | |
| 83 | // Now scan over all bi-links and produce table rows from them. |
| 84 | biLinks.values().forEach(biLink -> list.add(new LinkTableRow(biLink))); |
| 85 | return list.toArray(new TableRow[list.size()]); |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Simon Hunt | d2747a0 | 2015-04-30 22:41:16 -0700 | [diff] [blame^] | 89 | // ====================================================================== |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 90 | |
| 91 | /** |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 92 | * TableRow implementation for {@link org.onosproject.net.Link links}. |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 93 | */ |
| 94 | private static class LinkTableRow extends AbstractTableRow { |
| 95 | |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 96 | private static final String ONE = "one"; |
| 97 | private static final String TWO = "two"; |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 98 | private static final String TYPE = "type"; |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 99 | private static final String STATE = "_iconid_state"; |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 100 | private static final String DIRECTION = "direction"; |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 101 | private static final String DURABLE = "durable"; |
| 102 | |
| 103 | private static final String[] COL_IDS = { |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 104 | ONE, TWO, TYPE, STATE, DIRECTION, DURABLE |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 107 | private static final String ICON_ID_ONLINE = "active"; |
| 108 | private static final String ICON_ID_OFFLINE = "inactive"; |
| 109 | |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 110 | public LinkTableRow(BiLink link) { |
| 111 | ConnectPoint src = link.one.src(); |
| 112 | ConnectPoint dst = link.one.dst(); |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 113 | linkState(link); |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 114 | |
Simon Hunt | 44aa2f8 | 2015-04-30 15:01:35 -0700 | [diff] [blame] | 115 | add(ONE, concat(src.elementId(), "/", src.port())); |
| 116 | add(TWO, concat(dst.elementId(), "/", dst.port())); |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 117 | add(TYPE, linkType(link).toLowerCase()); |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 118 | add(STATE, linkState(link)); |
| 119 | add(DIRECTION, link.two != null ? "A <--> B" : "A --> B"); |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 120 | add(DURABLE, Boolean.toString(link.one.isDurable())); |
| 121 | } |
| 122 | |
| 123 | private String linkState(BiLink link) { |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 124 | return (link.one.state() == Link.State.ACTIVE || |
| 125 | link.two.state() == Link.State.ACTIVE) ? |
| 126 | ICON_ID_ONLINE : ICON_ID_OFFLINE; |
Thomas Vachuska | 583bc63 | 2015-04-14 10:10:57 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | private String linkType(BiLink link) { |
| 130 | return link.two == null || link.one.type() == link.two.type() ? |
| 131 | link.one.type().toString() : |
Bri Prebilic Cole | ab582b8 | 2015-04-14 15:08:22 -0700 | [diff] [blame] | 132 | link.one.type().toString() + " / " + link.two.type().toString(); |
Bri Prebilic Cole | 9fb594a | 2015-04-14 09:15:54 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | @Override |
| 136 | protected String[] columnIds() { |
| 137 | return COL_IDS; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | } |