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