blob: 7de6d62409ff3d19811ca16909d8ded57173f4ae [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;
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";
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 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 {
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);
Simon Hunt4fc86852015-08-20 17:57:52 -070094 BaseLinkMap linkMap = new BaseLinkMap();
95 ls.getLinks().forEach(linkMap::add);
96 linkMap.biLinks().forEach(blink -> populateRow(tm.addRow(), blink));
Simon Hunt3d1b0652015-05-05 17:27:24 -070097 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070098
Simon Hunt4fc86852015-08-20 17:57:52 -070099 private void populateRow(TableModel.Row row, BaseLink blink) {
100 row.cell(ONE, blink.one().src())
101 .cell(TWO, blink.one().dst())
102 .cell(TYPE, linkType(blink))
103 .cell(STATE, linkState(blink))
104 .cell(DIRECTION, linkDir(blink))
105 .cell(DURABLE, blink.one().isDurable());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700106 }
107
Simon Hunt4fc86852015-08-20 17:57:52 -0700108 private String linkType(BaseLink link) {
Simon Hunt3d1b0652015-05-05 17:27:24 -0700109 StringBuilder sb = new StringBuilder();
Simon Hunta17fa672015-08-19 18:42:22 -0700110 sb.append(link.one().type());
111 if (link.two() != null && link.two().type() != link.one().type()) {
Simon Hunt4fc86852015-08-20 17:57:52 -0700112 sb.append(SLASH).append(link.two().type());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700113 }
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700114 return sb.toString();
Thomas Vachuska583bc632015-04-14 10:10:57 -0700115 }
116
Simon Hunt4fc86852015-08-20 17:57:52 -0700117 private String linkState(BaseLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700118 return (link.one().state() == Link.State.ACTIVE ||
119 link.two().state() == Link.State.ACTIVE) ?
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700120 ICON_ID_ONLINE : ICON_ID_OFFLINE;
Thomas Vachuska583bc632015-04-14 10:10:57 -0700121 }
122
Simon Hunt4fc86852015-08-20 17:57:52 -0700123 private String linkDir(BaseLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700124 return link.two() != null ? A_BOTH_B : A_SINGLE_B;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700125 }
126 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700127}