blob: f594a921944bd6017bc2982b775ae4ea2dc573aa [file] [log] [blame]
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -07001/*
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
17package org.onosproject.ui.impl;
18
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070019import com.fasterxml.jackson.databind.node.ObjectNode;
20import com.google.common.collect.ImmutableSet;
Thomas Vachuska583bc632015-04-14 10:10:57 -070021import com.google.common.collect.Maps;
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070022import org.onosproject.net.Link;
Thomas Vachuska583bc632015-04-14 10:10:57 -070023import org.onosproject.net.LinkKey;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070024import org.onosproject.net.link.LinkService;
Simon Huntd2747a02015-04-30 22:41:16 -070025import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070026import org.onosproject.ui.UiMessageHandler;
Thomas Vachuska583bc632015-04-14 10:10:57 -070027import org.onosproject.ui.impl.TopologyViewMessageHandlerBase.BiLink;
Simon Hunt3d1b0652015-05-05 17:27:24 -070028import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070029import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070030import org.onosproject.ui.table.cell.ConnectPointFormatter;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070031import org.onosproject.ui.table.cell.EnumFormatter;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070032
Simon Huntd2747a02015-04-30 22:41:16 -070033import java.util.Collection;
Thomas Vachuska583bc632015-04-14 10:10:57 -070034import java.util.Map;
35
36import static org.onosproject.ui.impl.TopologyViewMessageHandlerBase.addLink;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070037
38/**
39 * Message handler for link view related messages.
40 */
Simon Hunta0ddb022015-05-01 09:53:01 -070041public class LinkViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070042
Simon Huntd2747a02015-04-30 22:41:16 -070043 private static final String LINK_DATA_REQ = "linkDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070044 private static final String LINK_DATA_RESP = "linkDataResponse";
45 private static final String LINKS = "links";
Simon Huntd2747a02015-04-30 22:41:16 -070046
Simon Huntabd16f62015-05-01 13:14:40 -070047 private static final String ONE = "one";
48 private static final String TWO = "two";
49 private static final String TYPE = "type";
50 private static final String STATE = "_iconid_state";
51 private static final String DIRECTION = "direction";
52 private static final String DURABLE = "durable";
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070053
Simon Hunt3d1b0652015-05-05 17:27:24 -070054 private static final String[] COL_IDS = {
55 ONE, TWO, TYPE, STATE, DIRECTION, DURABLE
56 };
57
58 private static final String ICON_ID_ONLINE = "active";
59 private static final String ICON_ID_OFFLINE = "inactive";
60
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070061 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070062 protected Collection<RequestHandler> getHandlers() {
63 return ImmutableSet.of(new LinkDataRequest());
64 }
65
Simon Huntabd16f62015-05-01 13:14:40 -070066 // handler for link table requests
67 private final class LinkDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070068 private LinkDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070069 super(LINK_DATA_REQ, LINK_DATA_RESP, LINKS);
Simon Huntd2747a02015-04-30 22:41:16 -070070 }
71
72 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070073 protected String[] getColumnIds() {
74 return COL_IDS;
Simon Hunt44aa2f82015-04-30 15:01:35 -070075 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070076
Simon Huntabd16f62015-05-01 13:14:40 -070077 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070078 protected String defaultColumnId() {
Simon Huntabd16f62015-05-01 13:14:40 -070079 return ONE;
80 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070081
Simon Hunt3d1b0652015-05-05 17:27:24 -070082 @Override
83 protected TableModel createTableModel() {
84 TableModel tm = super.createTableModel();
85 tm.setFormatter(ONE, ConnectPointFormatter.INSTANCE);
86 tm.setFormatter(TWO, ConnectPointFormatter.INSTANCE);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070087 tm.setFormatter(TYPE, EnumFormatter.INSTANCE);
Simon Hunt3d1b0652015-05-05 17:27:24 -070088 return tm;
89 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070090
Simon Hunt3d1b0652015-05-05 17:27:24 -070091 @Override
92 protected void populateTable(TableModel tm, ObjectNode payload) {
93 LinkService ls = get(LinkService.class);
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070094
Simon Hunt3d1b0652015-05-05 17:27:24 -070095 // First consolidate all uni-directional links into two-directional ones.
96 Map<LinkKey, BiLink> biLinks = Maps.newHashMap();
97 ls.getLinks().forEach(link -> addLink(biLinks, link));
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070098
Simon Hunt3d1b0652015-05-05 17:27:24 -070099 // Now scan over all bi-links and produce table rows from them.
100 biLinks.values().forEach(biLink -> populateRow(tm.addRow(), biLink));
101 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700102
Simon Hunt3d1b0652015-05-05 17:27:24 -0700103 private void populateRow(TableModel.Row row, BiLink biLink) {
104 row.cell(ONE, biLink.one.src())
105 .cell(TWO, biLink.one.dst())
106 .cell(TYPE, linkType(biLink))
107 .cell(STATE, linkState(biLink))
108 .cell(DIRECTION, linkDir(biLink))
109 .cell(DURABLE, biLink.one.isDurable());
110 }
111
112 private String linkType(BiLink link) {
113 StringBuilder sb = new StringBuilder();
114 sb.append(link.one.type());
115 if (link.two != null && link.two.type() != link.one.type()) {
116 sb.append(" / ").append(link.two.type());
117 }
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700118 return sb.toString();
Thomas Vachuska583bc632015-04-14 10:10:57 -0700119 }
120
121 private String linkState(BiLink link) {
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700122 return (link.one.state() == Link.State.ACTIVE ||
123 link.two.state() == Link.State.ACTIVE) ?
124 ICON_ID_ONLINE : ICON_ID_OFFLINE;
Thomas Vachuska583bc632015-04-14 10:10:57 -0700125 }
126
Simon Hunt3d1b0652015-05-05 17:27:24 -0700127 private String linkDir(BiLink link) {
128 return link.two != null ? "A <--> B" : "A --> B";
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700129 }
130 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700131}