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