blob: b7f0dc327a6d6d3c77e7b24c1d89b1e1b4cc5376 [file] [log] [blame]
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -07003 *
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;
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070021import org.onosproject.net.Link;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070022import org.onosproject.net.link.LinkService;
Simon Huntd2747a02015-04-30 22:41:16 -070023import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070024import org.onosproject.ui.UiMessageHandler;
Simon Hunt191c84a2015-08-21 08:24:48 -070025import org.onosproject.ui.topo.BaseLink;
26import org.onosproject.ui.topo.BaseLinkMap;
Simon Hunt3d1b0652015-05-05 17:27:24 -070027import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070028import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070029import org.onosproject.ui.table.cell.ConnectPointFormatter;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070030import org.onosproject.ui.table.cell.EnumFormatter;
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 -070033
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070034/**
35 * Message handler for link view related messages.
36 */
Simon Hunta0ddb022015-05-01 09:53:01 -070037public class LinkViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070038
Simon Hunta17fa672015-08-19 18:42:22 -070039 private static final String A_BOTH_B = "A ↔ B";
40 private static final String A_SINGLE_B = "A → B";
Simon Hunt4fc86852015-08-20 17:57:52 -070041 private static final String SLASH = " / ";
Simon Hunta17fa672015-08-19 18:42:22 -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";
Ray Milkey8521f812017-05-24 09:49:26 -070052 private static final String EXPECTED = "expected";
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070053
Simon Hunt3d1b0652015-05-05 17:27:24 -070054 private static final String[] COL_IDS = {
Ray Milkey8521f812017-05-24 09:49:26 -070055 ONE, TWO, TYPE, STATE, DIRECTION, EXPECTED
Simon Hunt3d1b0652015-05-05 17:27:24 -070056 };
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 Huntda580882015-05-12 20:58:18 -070062 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -070063 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 {
Jian Li69f66632016-01-15 12:27:42 -080068 private static final String NO_ROWS_MESSAGE = "No links found";
69
Simon Huntd2747a02015-04-30 22:41:16 -070070 private LinkDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070071 super(LINK_DATA_REQ, LINK_DATA_RESP, LINKS);
Simon Huntd2747a02015-04-30 22:41:16 -070072 }
73
74 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070075 protected String[] getColumnIds() {
76 return COL_IDS;
Simon Hunt44aa2f82015-04-30 15:01:35 -070077 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070078
Simon Huntabd16f62015-05-01 13:14:40 -070079 @Override
Jian Li8baf4472016-01-15 15:08:09 -080080 protected String noRowsMessage(ObjectNode payload) {
Jian Li69f66632016-01-15 12:27:42 -080081 return NO_ROWS_MESSAGE;
82 }
83
84 @Override
Simon Hunt3d1b0652015-05-05 17:27:24 -070085 protected String defaultColumnId() {
Simon Huntabd16f62015-05-01 13:14:40 -070086 return ONE;
87 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070088
Simon Hunt3d1b0652015-05-05 17:27:24 -070089 @Override
90 protected TableModel createTableModel() {
91 TableModel tm = super.createTableModel();
92 tm.setFormatter(ONE, ConnectPointFormatter.INSTANCE);
93 tm.setFormatter(TWO, ConnectPointFormatter.INSTANCE);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070094 tm.setFormatter(TYPE, EnumFormatter.INSTANCE);
Simon Hunt3d1b0652015-05-05 17:27:24 -070095 return tm;
96 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070097
Simon Hunt3d1b0652015-05-05 17:27:24 -070098 @Override
99 protected void populateTable(TableModel tm, ObjectNode payload) {
100 LinkService ls = get(LinkService.class);
Simon Hunt4fc86852015-08-20 17:57:52 -0700101 BaseLinkMap linkMap = new BaseLinkMap();
102 ls.getLinks().forEach(linkMap::add);
103 linkMap.biLinks().forEach(blink -> populateRow(tm.addRow(), blink));
Simon Hunt3d1b0652015-05-05 17:27:24 -0700104 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700105
Simon Hunt4fc86852015-08-20 17:57:52 -0700106 private void populateRow(TableModel.Row row, BaseLink blink) {
107 row.cell(ONE, blink.one().src())
108 .cell(TWO, blink.one().dst())
109 .cell(TYPE, linkType(blink))
110 .cell(STATE, linkState(blink))
111 .cell(DIRECTION, linkDir(blink))
Ray Milkey8521f812017-05-24 09:49:26 -0700112 .cell(EXPECTED, blink.one().isExpected());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700113 }
114
Simon Hunt4fc86852015-08-20 17:57:52 -0700115 private String linkType(BaseLink link) {
Simon Hunt3d1b0652015-05-05 17:27:24 -0700116 StringBuilder sb = new StringBuilder();
Simon Hunta17fa672015-08-19 18:42:22 -0700117 sb.append(link.one().type());
118 if (link.two() != null && link.two().type() != link.one().type()) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700119 sb.append(SLASH).append(link.two().type());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700120 }
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700121 return sb.toString();
Thomas Vachuska583bc632015-04-14 10:10:57 -0700122 }
123
Simon Hunt4fc86852015-08-20 17:57:52 -0700124 private String linkState(BaseLink link) {
Charles Chana78aeb82016-02-11 16:42:04 -0800125 if (link.one() == null || link.two() == null) {
126 return ICON_ID_OFFLINE;
127 }
128
Simon Hunta17fa672015-08-19 18:42:22 -0700129 return (link.one().state() == Link.State.ACTIVE ||
130 link.two().state() == Link.State.ACTIVE) ?
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700131 ICON_ID_ONLINE : ICON_ID_OFFLINE;
Thomas Vachuska583bc632015-04-14 10:10:57 -0700132 }
133
Simon Hunt4fc86852015-08-20 17:57:52 -0700134 private String linkDir(BaseLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700135 return link.two() != null ? A_BOTH_B : A_SINGLE_B;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700136 }
137 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700138}