blob: 7d496f5ecf26e89febff83d88f60b01f4d62278d [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
19import com.fasterxml.jackson.databind.node.ArrayNode;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableSet;
Thomas Vachuska583bc632015-04-14 10:10:57 -070022import com.google.common.collect.Maps;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070023import org.onosproject.net.ConnectPoint;
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070024import org.onosproject.net.Link;
Thomas Vachuska583bc632015-04-14 10:10:57 -070025import org.onosproject.net.LinkKey;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070026import org.onosproject.net.link.LinkService;
Thomas Vachuska583bc632015-04-14 10:10:57 -070027import org.onosproject.ui.impl.TopologyViewMessageHandlerBase.BiLink;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070028
29import java.util.ArrayList;
30import java.util.Arrays;
31import java.util.List;
Thomas Vachuska583bc632015-04-14 10:10:57 -070032import java.util.Map;
33
34import static org.onosproject.ui.impl.TopologyViewMessageHandlerBase.addLink;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070035
36/**
37 * Message handler for link view related messages.
38 */
39public class LinkViewMessageHandler extends AbstractTabularViewMessageHandler {
40
41 /**
42 * Creates a new message handler for the link messages.
43 */
44 protected LinkViewMessageHandler() {
45 super(ImmutableSet.of("linkDataRequest"));
46 }
47
48 @Override
49 public void process(ObjectNode message) {
50 ObjectNode payload = payload(message);
Thomas Vachuska583bc632015-04-14 10:10:57 -070051 String sortCol = string(payload, "sortCol", "one");
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070052 String sortDir = string(payload, "sortDir", "asc");
53
54 LinkService service = get(LinkService.class);
55 TableRow[] rows = generateTableRows(service);
56 RowComparator rc =
57 new RowComparator(sortCol, RowComparator.direction(sortDir));
58 Arrays.sort(rows, rc);
59 ArrayNode links = generateArrayNode(rows);
60 ObjectNode rootNode = mapper.createObjectNode();
61 rootNode.set("links", links);
62
63 connection().sendMessage("linkDataResponse", 0, rootNode);
64 }
65
66 private TableRow[] generateTableRows(LinkService service) {
67 List<TableRow> list = new ArrayList<>();
Thomas Vachuska583bc632015-04-14 10:10:57 -070068
69 // First consolidate all uni-directional links into two-directional ones.
70 Map<LinkKey, BiLink> biLinks = Maps.newHashMap();
71 service.getLinks().forEach(link -> addLink(biLinks, link));
72
73 // Now scan over all bi-links and produce table rows from them.
74 biLinks.values().forEach(biLink -> list.add(new LinkTableRow(biLink)));
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070075 return list.toArray(new TableRow[list.size()]);
76 }
77
78 /**
Thomas Vachuska583bc632015-04-14 10:10:57 -070079 * TableRow implementation for {@link org.onosproject.net.Link links}.
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070080 */
81 private static class LinkTableRow extends AbstractTableRow {
82
Thomas Vachuska583bc632015-04-14 10:10:57 -070083 private static final String ONE = "one";
84 private static final String TWO = "two";
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070085 private static final String TYPE = "type";
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070086 private static final String STATE = "_iconid_state";
Thomas Vachuska583bc632015-04-14 10:10:57 -070087 private static final String DIRECTION = "direction";
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070088 private static final String DURABLE = "durable";
89
90 private static final String[] COL_IDS = {
Thomas Vachuska583bc632015-04-14 10:10:57 -070091 ONE, TWO, TYPE, STATE, DIRECTION, DURABLE
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070092 };
93
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070094 private static final String ICON_ID_ONLINE = "active";
95 private static final String ICON_ID_OFFLINE = "inactive";
96
Thomas Vachuska583bc632015-04-14 10:10:57 -070097 public LinkTableRow(BiLink link) {
98 ConnectPoint src = link.one.src();
99 ConnectPoint dst = link.one.dst();
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700100 linkState(link);
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700101
Thomas Vachuska583bc632015-04-14 10:10:57 -0700102 add(ONE, src.elementId().toString() + "/" + src.port().toString());
103 add(TWO, dst.elementId().toString() + "/" + dst.port().toString());
104 add(TYPE, linkType(link).toLowerCase());
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700105 add(STATE, linkState(link));
106 add(DIRECTION, link.two != null ? "A <--> B" : "A --> B");
Thomas Vachuska583bc632015-04-14 10:10:57 -0700107 add(DURABLE, Boolean.toString(link.one.isDurable()));
108 }
109
110 private String linkState(BiLink link) {
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700111 return (link.one.state() == Link.State.ACTIVE ||
112 link.two.state() == Link.State.ACTIVE) ?
113 ICON_ID_ONLINE : ICON_ID_OFFLINE;
Thomas Vachuska583bc632015-04-14 10:10:57 -0700114 }
115
116 private String linkType(BiLink link) {
117 return link.two == null || link.one.type() == link.two.type() ?
118 link.one.type().toString() :
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700119 link.one.type().toString() + " / " + link.two.type().toString();
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700120 }
121
122 @Override
123 protected String[] columnIds() {
124 return COL_IDS;
125 }
126 }
127
128}