blob: 6683188d772242f2067e860fb4a6dce774d7a44d [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 Cole9fb594a2015-04-14 09:15:54 -070022import org.onosproject.net.ConnectPoint;
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070023import org.onosproject.net.Link;
Thomas Vachuska583bc632015-04-14 10:10:57 -070024import org.onosproject.net.LinkKey;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070025import org.onosproject.net.link.LinkService;
Simon Hunt44aa2f82015-04-30 15:01:35 -070026import org.onosproject.ui.UiMessageHandler;
Thomas Vachuska583bc632015-04-14 10:10:57 -070027import org.onosproject.ui.impl.TopologyViewMessageHandlerBase.BiLink;
Simon Hunt44aa2f82015-04-30 15:01:35 -070028import org.onosproject.ui.table.AbstractTableRow;
29import org.onosproject.ui.table.RowComparator;
30import org.onosproject.ui.table.TableRow;
31import org.onosproject.ui.table.TableUtils;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070032
33import java.util.ArrayList;
34import java.util.Arrays;
35import java.util.List;
Thomas Vachuska583bc632015-04-14 10:10:57 -070036import java.util.Map;
37
38import static org.onosproject.ui.impl.TopologyViewMessageHandlerBase.addLink;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070039
40/**
41 * Message handler for link view related messages.
42 */
Simon Hunt44aa2f82015-04-30 15:01:35 -070043public class LinkViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070044
45 /**
46 * Creates a new message handler for the link messages.
47 */
48 protected LinkViewMessageHandler() {
49 super(ImmutableSet.of("linkDataRequest"));
50 }
51
52 @Override
53 public void process(ObjectNode message) {
Simon Hunt44aa2f82015-04-30 15:01:35 -070054 String type = eventType(message);
55 if (type.equals("linkDataRequest")) {
56 sendLinkList(message);
57 }
58 }
59
60 private void sendLinkList(ObjectNode message) {
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070061 ObjectNode payload = payload(message);
Simon Hunt44aa2f82015-04-30 15:01:35 -070062 RowComparator rc = TableUtils.createRowComparator(payload, "one");
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070063
64 LinkService service = get(LinkService.class);
65 TableRow[] rows = generateTableRows(service);
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070066 Arrays.sort(rows, rc);
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070067 ObjectNode rootNode = mapper.createObjectNode();
Simon Hunt44aa2f82015-04-30 15:01:35 -070068 rootNode.set("links", TableUtils.generateArrayNode(rows));
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070069
70 connection().sendMessage("linkDataResponse", 0, rootNode);
71 }
72
73 private TableRow[] generateTableRows(LinkService service) {
74 List<TableRow> list = new ArrayList<>();
Thomas Vachuska583bc632015-04-14 10:10:57 -070075
76 // First consolidate all uni-directional links into two-directional ones.
77 Map<LinkKey, BiLink> biLinks = Maps.newHashMap();
78 service.getLinks().forEach(link -> addLink(biLinks, link));
79
80 // Now scan over all bi-links and produce table rows from them.
81 biLinks.values().forEach(biLink -> list.add(new LinkTableRow(biLink)));
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070082 return list.toArray(new TableRow[list.size()]);
83 }
84
85 /**
Thomas Vachuska583bc632015-04-14 10:10:57 -070086 * TableRow implementation for {@link org.onosproject.net.Link links}.
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070087 */
88 private static class LinkTableRow extends AbstractTableRow {
89
Thomas Vachuska583bc632015-04-14 10:10:57 -070090 private static final String ONE = "one";
91 private static final String TWO = "two";
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070092 private static final String TYPE = "type";
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070093 private static final String STATE = "_iconid_state";
Thomas Vachuska583bc632015-04-14 10:10:57 -070094 private static final String DIRECTION = "direction";
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070095 private static final String DURABLE = "durable";
96
97 private static final String[] COL_IDS = {
Thomas Vachuska583bc632015-04-14 10:10:57 -070098 ONE, TWO, TYPE, STATE, DIRECTION, DURABLE
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070099 };
100
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700101 private static final String ICON_ID_ONLINE = "active";
102 private static final String ICON_ID_OFFLINE = "inactive";
103
Thomas Vachuska583bc632015-04-14 10:10:57 -0700104 public LinkTableRow(BiLink link) {
105 ConnectPoint src = link.one.src();
106 ConnectPoint dst = link.one.dst();
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700107 linkState(link);
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700108
Simon Hunt44aa2f82015-04-30 15:01:35 -0700109 add(ONE, concat(src.elementId(), "/", src.port()));
110 add(TWO, concat(dst.elementId(), "/", dst.port()));
Thomas Vachuska583bc632015-04-14 10:10:57 -0700111 add(TYPE, linkType(link).toLowerCase());
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700112 add(STATE, linkState(link));
113 add(DIRECTION, link.two != null ? "A <--> B" : "A --> B");
Thomas Vachuska583bc632015-04-14 10:10:57 -0700114 add(DURABLE, Boolean.toString(link.one.isDurable()));
115 }
116
117 private String linkState(BiLink link) {
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700118 return (link.one.state() == Link.State.ACTIVE ||
119 link.two.state() == Link.State.ACTIVE) ?
120 ICON_ID_ONLINE : ICON_ID_OFFLINE;
Thomas Vachuska583bc632015-04-14 10:10:57 -0700121 }
122
123 private String linkType(BiLink link) {
124 return link.two == null || link.one.type() == link.two.type() ?
125 link.one.type().toString() :
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700126 link.one.type().toString() + " / " + link.two.type().toString();
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700127 }
128
129 @Override
130 protected String[] columnIds() {
131 return COL_IDS;
132 }
133 }
134
135}