blob: cdd1e62ba87c23c9f884283e9f8a7f6657408e48 [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;
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;
Thomas Vachuska583bc632015-04-14 10:10:57 -070026import org.onosproject.ui.impl.TopologyViewMessageHandlerBase.BiLink;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070027
28import java.util.ArrayList;
29import java.util.Arrays;
30import java.util.List;
Thomas Vachuska583bc632015-04-14 10:10:57 -070031import java.util.Map;
32
33import static org.onosproject.ui.impl.TopologyViewMessageHandlerBase.addLink;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070034
35/**
36 * Message handler for link view related messages.
37 */
38public class LinkViewMessageHandler extends AbstractTabularViewMessageHandler {
39
40 /**
41 * Creates a new message handler for the link messages.
42 */
43 protected LinkViewMessageHandler() {
44 super(ImmutableSet.of("linkDataRequest"));
45 }
46
47 @Override
48 public void process(ObjectNode message) {
49 ObjectNode payload = payload(message);
Thomas Vachuska583bc632015-04-14 10:10:57 -070050 String sortCol = string(payload, "sortCol", "one");
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070051 String sortDir = string(payload, "sortDir", "asc");
52
53 LinkService service = get(LinkService.class);
54 TableRow[] rows = generateTableRows(service);
55 RowComparator rc =
56 new RowComparator(sortCol, RowComparator.direction(sortDir));
57 Arrays.sort(rows, rc);
58 ArrayNode links = generateArrayNode(rows);
59 ObjectNode rootNode = mapper.createObjectNode();
60 rootNode.set("links", links);
61
62 connection().sendMessage("linkDataResponse", 0, rootNode);
63 }
64
65 private TableRow[] generateTableRows(LinkService service) {
66 List<TableRow> list = new ArrayList<>();
Thomas Vachuska583bc632015-04-14 10:10:57 -070067
68 // First consolidate all uni-directional links into two-directional ones.
69 Map<LinkKey, BiLink> biLinks = Maps.newHashMap();
70 service.getLinks().forEach(link -> addLink(biLinks, link));
71
72 // Now scan over all bi-links and produce table rows from them.
73 biLinks.values().forEach(biLink -> list.add(new LinkTableRow(biLink)));
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070074 return list.toArray(new TableRow[list.size()]);
75 }
76
77 /**
Thomas Vachuska583bc632015-04-14 10:10:57 -070078 * TableRow implementation for {@link org.onosproject.net.Link links}.
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070079 */
80 private static class LinkTableRow extends AbstractTableRow {
81
Thomas Vachuska583bc632015-04-14 10:10:57 -070082 private static final String ONE = "one";
83 private static final String TWO = "two";
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070084 private static final String TYPE = "type";
85 private static final String STATE = "state";
Thomas Vachuska583bc632015-04-14 10:10:57 -070086 private static final String DIRECTION = "direction";
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070087 private static final String DURABLE = "durable";
88
89 private static final String[] COL_IDS = {
Thomas Vachuska583bc632015-04-14 10:10:57 -070090 ONE, TWO, TYPE, STATE, DIRECTION, DURABLE
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070091 };
92
Thomas Vachuska583bc632015-04-14 10:10:57 -070093 public LinkTableRow(BiLink link) {
94 ConnectPoint src = link.one.src();
95 ConnectPoint dst = link.one.dst();
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070096
Thomas Vachuska583bc632015-04-14 10:10:57 -070097 add(ONE, src.elementId().toString() + "/" + src.port().toString());
98 add(TWO, dst.elementId().toString() + "/" + dst.port().toString());
99 add(TYPE, linkType(link).toLowerCase());
100 add(STATE, linkState(link).toLowerCase());
101 add(DIRECTION, link.two != null ? "A <-> B" : "A -> B");
102 add(DURABLE, Boolean.toString(link.one.isDurable()));
103 }
104
105 private String linkState(BiLink link) {
106 return link.two == null || link.one.state() == link.two.state() ?
107 link.one.state().toString() :
108 link.one.state().toString() + "/" + link.two.state().toString();
109 }
110
111 private String linkType(BiLink link) {
112 return link.two == null || link.one.type() == link.two.type() ?
113 link.one.type().toString() :
114 link.one.type().toString() + "/" + link.two.type().toString();
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700115 }
116
117 @Override
118 protected String[] columnIds() {
119 return COL_IDS;
120 }
121 }
122
123}