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