blob: 18a5acda19e99f0319bfb149170346c9b6038e8c [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;
Simon Hunta17fa672015-08-19 18:42:22 -070027import org.onosproject.ui.impl.topo.BiLink;
28import org.onosproject.ui.impl.topo.TopoUtils;
Simon Hunt3d1b0652015-05-05 17:27:24 -070029import org.onosproject.ui.table.TableModel;
Simon Huntabd16f62015-05-01 13:14:40 -070030import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt3d1b0652015-05-05 17:27:24 -070031import org.onosproject.ui.table.cell.ConnectPointFormatter;
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070032import org.onosproject.ui.table.cell.EnumFormatter;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070033
Simon Huntd2747a02015-04-30 22:41:16 -070034import java.util.Collection;
Thomas Vachuska583bc632015-04-14 10:10:57 -070035import java.util.Map;
36
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070037/**
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 Hunta17fa672015-08-19 18:42:22 -070042 private static final String A_BOTH_B = "A ↔ B";
43 private static final String A_SINGLE_B = "A → B";
44
Simon Huntd2747a02015-04-30 22:41:16 -070045 private static final String LINK_DATA_REQ = "linkDataRequest";
Simon Huntabd16f62015-05-01 13:14:40 -070046 private static final String LINK_DATA_RESP = "linkDataResponse";
47 private static final String LINKS = "links";
Simon Huntd2747a02015-04-30 22:41:16 -070048
Simon Huntabd16f62015-05-01 13:14:40 -070049 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 Cole9fb594a2015-04-14 09:15:54 -070055
Simon Hunt3d1b0652015-05-05 17:27:24 -070056 private static final String[] COL_IDS = {
57 ONE, TWO, TYPE, STATE, DIRECTION, DURABLE
58 };
59
60 private static final String ICON_ID_ONLINE = "active";
61 private static final String ICON_ID_OFFLINE = "inactive";
62
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070063 @Override
Simon Huntda580882015-05-12 20:58:18 -070064 protected Collection<RequestHandler> createRequestHandlers() {
Simon Huntd2747a02015-04-30 22:41:16 -070065 return ImmutableSet.of(new LinkDataRequest());
66 }
67
Simon Huntabd16f62015-05-01 13:14:40 -070068 // handler for link table requests
69 private final class LinkDataRequest extends TableRequestHandler {
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
Simon Hunt3d1b0652015-05-05 17:27:24 -070080 protected String defaultColumnId() {
Simon Huntabd16f62015-05-01 13:14:40 -070081 return ONE;
82 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070083
Simon Hunt3d1b0652015-05-05 17:27:24 -070084 @Override
85 protected TableModel createTableModel() {
86 TableModel tm = super.createTableModel();
87 tm.setFormatter(ONE, ConnectPointFormatter.INSTANCE);
88 tm.setFormatter(TWO, ConnectPointFormatter.INSTANCE);
Bri Prebilic Cole9467a232015-05-06 16:59:05 -070089 tm.setFormatter(TYPE, EnumFormatter.INSTANCE);
Simon Hunt3d1b0652015-05-05 17:27:24 -070090 return tm;
91 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070092
Simon Hunt3d1b0652015-05-05 17:27:24 -070093 @Override
94 protected void populateTable(TableModel tm, ObjectNode payload) {
95 LinkService ls = get(LinkService.class);
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070096
Simon Hunt3d1b0652015-05-05 17:27:24 -070097 // First consolidate all uni-directional links into two-directional ones.
98 Map<LinkKey, BiLink> biLinks = Maps.newHashMap();
Simon Hunta17fa672015-08-19 18:42:22 -070099 ls.getLinks().forEach(link -> TopoUtils.addLink(biLinks, link));
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700100
Simon Hunt3d1b0652015-05-05 17:27:24 -0700101 // Now scan over all bi-links and produce table rows from them.
102 biLinks.values().forEach(biLink -> populateRow(tm.addRow(), biLink));
103 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700104
Simon Hunt3d1b0652015-05-05 17:27:24 -0700105 private void populateRow(TableModel.Row row, BiLink biLink) {
Simon Hunta17fa672015-08-19 18:42:22 -0700106 row.cell(ONE, biLink.one().src())
107 .cell(TWO, biLink.one().dst())
Simon Hunt3d1b0652015-05-05 17:27:24 -0700108 .cell(TYPE, linkType(biLink))
109 .cell(STATE, linkState(biLink))
110 .cell(DIRECTION, linkDir(biLink))
Simon Hunta17fa672015-08-19 18:42:22 -0700111 .cell(DURABLE, biLink.one().isDurable());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700112 }
113
114 private String linkType(BiLink link) {
115 StringBuilder sb = new StringBuilder();
Simon Hunta17fa672015-08-19 18:42:22 -0700116 sb.append(link.one().type());
117 if (link.two() != null && link.two().type() != link.one().type()) {
118 sb.append(" / ").append(link.two().type());
Simon Hunt3d1b0652015-05-05 17:27:24 -0700119 }
Bri Prebilic Cole9467a232015-05-06 16:59:05 -0700120 return sb.toString();
Thomas Vachuska583bc632015-04-14 10:10:57 -0700121 }
122
123 private String linkState(BiLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700124 return (link.one().state() == Link.State.ACTIVE ||
125 link.two().state() == Link.State.ACTIVE) ?
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700126 ICON_ID_ONLINE : ICON_ID_OFFLINE;
Thomas Vachuska583bc632015-04-14 10:10:57 -0700127 }
128
Simon Hunt3d1b0652015-05-05 17:27:24 -0700129 private String linkDir(BiLink link) {
Simon Hunta17fa672015-08-19 18:42:22 -0700130 return link.two() != null ? A_BOTH_B : A_SINGLE_B;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700131 }
132 }
Simon Hunta17fa672015-08-19 18:42:22 -0700133
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700134}