blob: e361563bb54284b6b7ff7c6a5f8e19e6240fb1d3 [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 Cole9fb594a2015-04-14 09:15:54 -070031
Simon Huntd2747a02015-04-30 22:41:16 -070032import java.util.Collection;
Thomas Vachuska583bc632015-04-14 10:10:57 -070033import java.util.Map;
34
35import static org.onosproject.ui.impl.TopologyViewMessageHandlerBase.addLink;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070036
37/**
38 * Message handler for link view related messages.
39 */
Simon Hunta0ddb022015-05-01 09:53:01 -070040public class LinkViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070041
Simon Huntd2747a02015-04-30 22:41:16 -070042 private static final String LINK_DATA_REQ = "linkDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070043 private static final String LINK_DATA_RESP = "linkDataResponse";
44 private static final String LINKS = "links";
Simon Huntd2747a02015-04-30 22:41:16 -070045
Simon Huntabd16f62015-05-01 13:14:40 -070046 private static final String ONE = "one";
47 private static final String TWO = "two";
48 private static final String TYPE = "type";
49 private static final String STATE = "_iconid_state";
50 private static final String DIRECTION = "direction";
51 private static final String DURABLE = "durable";
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070052
Simon Hunt3d1b0652015-05-05 17:27:24 -070053 private static final String[] COL_IDS = {
54 ONE, TWO, TYPE, STATE, DIRECTION, DURABLE
55 };
56
57 private static final String ICON_ID_ONLINE = "active";
58 private static final String ICON_ID_OFFLINE = "inactive";
59
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070060 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070061 protected Collection<RequestHandler> getHandlers() {
62 return ImmutableSet.of(new LinkDataRequest());
63 }
64
Simon Huntabd16f62015-05-01 13:14:40 -070065 // handler for link table requests
66 private final class LinkDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070067 private LinkDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070068 super(LINK_DATA_REQ, LINK_DATA_RESP, LINKS);
Simon Huntd2747a02015-04-30 22:41:16 -070069 }
70
71 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070072 protected String[] getColumnIds() {
73 return COL_IDS;
Simon Hunt44aa2f82015-04-30 15:01:35 -070074 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070075
Simon Huntabd16f62015-05-01 13:14:40 -070076 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070077 protected String defaultColumnId() {
Simon Huntabd16f62015-05-01 13:14:40 -070078 return ONE;
79 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070080
Simon Hunt3d1b0652015-05-05 17:27:24 -070081 @Override
82 protected TableModel createTableModel() {
83 TableModel tm = super.createTableModel();
84 tm.setFormatter(ONE, ConnectPointFormatter.INSTANCE);
85 tm.setFormatter(TWO, ConnectPointFormatter.INSTANCE);
86 return tm;
87 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070088
Simon Hunt3d1b0652015-05-05 17:27:24 -070089 @Override
90 protected void populateTable(TableModel tm, ObjectNode payload) {
91 LinkService ls = get(LinkService.class);
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070092
Simon Hunt3d1b0652015-05-05 17:27:24 -070093 // First consolidate all uni-directional links into two-directional ones.
94 Map<LinkKey, BiLink> biLinks = Maps.newHashMap();
95 ls.getLinks().forEach(link -> addLink(biLinks, link));
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070096
Simon Hunt3d1b0652015-05-05 17:27:24 -070097 // Now scan over all bi-links and produce table rows from them.
98 biLinks.values().forEach(biLink -> populateRow(tm.addRow(), biLink));
99 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700100
Simon Hunt3d1b0652015-05-05 17:27:24 -0700101 private void populateRow(TableModel.Row row, BiLink biLink) {
102 row.cell(ONE, biLink.one.src())
103 .cell(TWO, biLink.one.dst())
104 .cell(TYPE, linkType(biLink))
105 .cell(STATE, linkState(biLink))
106 .cell(DIRECTION, linkDir(biLink))
107 .cell(DURABLE, biLink.one.isDurable());
108 }
109
110 private String linkType(BiLink link) {
111 StringBuilder sb = new StringBuilder();
112 sb.append(link.one.type());
113 if (link.two != null && link.two.type() != link.one.type()) {
114 sb.append(" / ").append(link.two.type());
115 }
116 return sb.toString().toLowerCase();
Thomas Vachuska583bc632015-04-14 10:10:57 -0700117 }
118
119 private String linkState(BiLink link) {
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700120 return (link.one.state() == Link.State.ACTIVE ||
121 link.two.state() == Link.State.ACTIVE) ?
122 ICON_ID_ONLINE : ICON_ID_OFFLINE;
Thomas Vachuska583bc632015-04-14 10:10:57 -0700123 }
124
Simon Hunt3d1b0652015-05-05 17:27:24 -0700125 private String linkDir(BiLink link) {
126 return link.two != null ? "A <--> B" : "A --> B";
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700127 }
128 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700129}