blob: 06349b485fc0fc2337a84d783324ff1588c01e3e [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 Huntd2747a02015-04-30 22:41:16 -070026import org.onosproject.ui.RequestHandler;
Simon Hunta0ddb022015-05-01 09:53:01 -070027import org.onosproject.ui.UiMessageHandler;
Thomas Vachuska583bc632015-04-14 10:10:57 -070028import org.onosproject.ui.impl.TopologyViewMessageHandlerBase.BiLink;
Simon Hunt44aa2f82015-04-30 15:01:35 -070029import org.onosproject.ui.table.AbstractTableRow;
30import org.onosproject.ui.table.RowComparator;
31import org.onosproject.ui.table.TableRow;
32import org.onosproject.ui.table.TableUtils;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070033
34import java.util.ArrayList;
35import java.util.Arrays;
Simon Huntd2747a02015-04-30 22:41:16 -070036import java.util.Collection;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070037import java.util.List;
Thomas Vachuska583bc632015-04-14 10:10:57 -070038import java.util.Map;
39
40import static org.onosproject.ui.impl.TopologyViewMessageHandlerBase.addLink;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070041
42/**
43 * Message handler for link view related messages.
44 */
Simon Hunta0ddb022015-05-01 09:53:01 -070045public class LinkViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070046
Simon Huntd2747a02015-04-30 22:41:16 -070047 private static final String LINK_DATA_REQ = "linkDataRequest";
48
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070049
50 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070051 protected Collection<RequestHandler> getHandlers() {
52 return ImmutableSet.of(new LinkDataRequest());
53 }
54
55 // ======================================================================
56
57 private final class LinkDataRequest extends RequestHandler {
58
59 private LinkDataRequest() {
60 super(LINK_DATA_REQ);
61 }
62
63 @Override
64 public void process(long sid, ObjectNode payload) {
65 RowComparator rc = TableUtils.createRowComparator(payload, "one");
66
67 LinkService service = get(LinkService.class);
68 TableRow[] rows = generateTableRows(service);
69 Arrays.sort(rows, rc);
70 ObjectNode rootNode = MAPPER.createObjectNode();
71 rootNode.set("links", TableUtils.generateArrayNode(rows));
72
73 sendMessage("linkDataResponse", 0, rootNode);
74 }
75
76 private TableRow[] generateTableRows(LinkService service) {
77 List<TableRow> list = new ArrayList<>();
78
79 // First consolidate all uni-directional links into two-directional ones.
80 Map<LinkKey, BiLink> biLinks = Maps.newHashMap();
81 service.getLinks().forEach(link -> addLink(biLinks, link));
82
83 // Now scan over all bi-links and produce table rows from them.
84 biLinks.values().forEach(biLink -> list.add(new LinkTableRow(biLink)));
85 return list.toArray(new TableRow[list.size()]);
Simon Hunt44aa2f82015-04-30 15:01:35 -070086 }
87 }
88
Simon Huntd2747a02015-04-30 22:41:16 -070089 // ======================================================================
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070090
91 /**
Thomas Vachuska583bc632015-04-14 10:10:57 -070092 * TableRow implementation for {@link org.onosproject.net.Link links}.
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070093 */
94 private static class LinkTableRow extends AbstractTableRow {
95
Thomas Vachuska583bc632015-04-14 10:10:57 -070096 private static final String ONE = "one";
97 private static final String TWO = "two";
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070098 private static final String TYPE = "type";
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070099 private static final String STATE = "_iconid_state";
Thomas Vachuska583bc632015-04-14 10:10:57 -0700100 private static final String DIRECTION = "direction";
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700101 private static final String DURABLE = "durable";
102
103 private static final String[] COL_IDS = {
Thomas Vachuska583bc632015-04-14 10:10:57 -0700104 ONE, TWO, TYPE, STATE, DIRECTION, DURABLE
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700105 };
106
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700107 private static final String ICON_ID_ONLINE = "active";
108 private static final String ICON_ID_OFFLINE = "inactive";
109
Thomas Vachuska583bc632015-04-14 10:10:57 -0700110 public LinkTableRow(BiLink link) {
111 ConnectPoint src = link.one.src();
112 ConnectPoint dst = link.one.dst();
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700113 linkState(link);
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700114
Simon Hunt44aa2f82015-04-30 15:01:35 -0700115 add(ONE, concat(src.elementId(), "/", src.port()));
116 add(TWO, concat(dst.elementId(), "/", dst.port()));
Thomas Vachuska583bc632015-04-14 10:10:57 -0700117 add(TYPE, linkType(link).toLowerCase());
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700118 add(STATE, linkState(link));
119 add(DIRECTION, link.two != null ? "A <--> B" : "A --> B");
Thomas Vachuska583bc632015-04-14 10:10:57 -0700120 add(DURABLE, Boolean.toString(link.one.isDurable()));
121 }
122
123 private String linkState(BiLink link) {
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700124 return (link.one.state() == Link.State.ACTIVE ||
125 link.two.state() == Link.State.ACTIVE) ?
126 ICON_ID_ONLINE : ICON_ID_OFFLINE;
Thomas Vachuska583bc632015-04-14 10:10:57 -0700127 }
128
129 private String linkType(BiLink link) {
130 return link.two == null || link.one.type() == link.two.type() ?
131 link.one.type().toString() :
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700132 link.one.type().toString() + " / " + link.two.type().toString();
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700133 }
134
135 @Override
136 protected String[] columnIds() {
137 return COL_IDS;
138 }
139 }
140
141}