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