blob: ae7f4898e66e75f59621fd4c699fa5b99b0fa91a [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;
Simon Huntabd16f62015-05-01 13:14:40 -070030import org.onosproject.ui.table.TableRequestHandler;
Simon Hunt44aa2f82015-04-30 15:01:35 -070031import org.onosproject.ui.table.TableRow;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070032
33import java.util.ArrayList;
Simon Huntd2747a02015-04-30 22:41:16 -070034import java.util.Collection;
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070035import 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 Hunta0ddb022015-05-01 09:53:01 -070043public class LinkViewMessageHandler extends UiMessageHandler {
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070044
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
56 @Override
Simon Huntd2747a02015-04-30 22:41:16 -070057 protected Collection<RequestHandler> getHandlers() {
58 return ImmutableSet.of(new LinkDataRequest());
59 }
60
Simon Huntabd16f62015-05-01 13:14:40 -070061 // handler for link table requests
62 private final class LinkDataRequest extends TableRequestHandler {
Simon Huntd2747a02015-04-30 22:41:16 -070063 private LinkDataRequest() {
Simon Huntabd16f62015-05-01 13:14:40 -070064 super(LINK_DATA_REQ, LINK_DATA_RESP, LINKS);
Simon Huntd2747a02015-04-30 22:41:16 -070065 }
66
67 @Override
Simon Huntabd16f62015-05-01 13:14:40 -070068 protected TableRow[] generateTableRows(ObjectNode payload) {
Simon Huntd2747a02015-04-30 22:41:16 -070069 LinkService service = get(LinkService.class);
Simon Huntd2747a02015-04-30 22:41:16 -070070 List<TableRow> list = new ArrayList<>();
71
72 // First consolidate all uni-directional links into two-directional ones.
73 Map<LinkKey, BiLink> biLinks = Maps.newHashMap();
74 service.getLinks().forEach(link -> addLink(biLinks, link));
75
76 // Now scan over all bi-links and produce table rows from them.
77 biLinks.values().forEach(biLink -> list.add(new LinkTableRow(biLink)));
78 return list.toArray(new TableRow[list.size()]);
Simon Hunt44aa2f82015-04-30 15:01:35 -070079 }
Simon Hunt44aa2f82015-04-30 15:01:35 -070080
Simon Huntabd16f62015-05-01 13:14:40 -070081 @Override
82 protected String defaultColId() {
83 return ONE;
84 }
85 }
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070086
87 /**
Thomas Vachuska583bc632015-04-14 10:10:57 -070088 * TableRow implementation for {@link org.onosproject.net.Link links}.
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070089 */
90 private static class LinkTableRow extends AbstractTableRow {
91
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070092 private static final String[] COL_IDS = {
Thomas Vachuska583bc632015-04-14 10:10:57 -070093 ONE, TWO, TYPE, STATE, DIRECTION, DURABLE
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -070094 };
95
Bri Prebilic Coleab582b82015-04-14 15:08:22 -070096 private static final String ICON_ID_ONLINE = "active";
97 private static final String ICON_ID_OFFLINE = "inactive";
98
Thomas Vachuska583bc632015-04-14 10:10:57 -070099 public LinkTableRow(BiLink link) {
100 ConnectPoint src = link.one.src();
101 ConnectPoint dst = link.one.dst();
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700102 linkState(link);
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700103
Simon Hunt44aa2f82015-04-30 15:01:35 -0700104 add(ONE, concat(src.elementId(), "/", src.port()));
105 add(TWO, concat(dst.elementId(), "/", dst.port()));
Thomas Vachuska583bc632015-04-14 10:10:57 -0700106 add(TYPE, linkType(link).toLowerCase());
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700107 add(STATE, linkState(link));
108 add(DIRECTION, link.two != null ? "A <--> B" : "A --> B");
Thomas Vachuska583bc632015-04-14 10:10:57 -0700109 add(DURABLE, Boolean.toString(link.one.isDurable()));
110 }
111
112 private String linkState(BiLink link) {
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700113 return (link.one.state() == Link.State.ACTIVE ||
114 link.two.state() == Link.State.ACTIVE) ?
115 ICON_ID_ONLINE : ICON_ID_OFFLINE;
Thomas Vachuska583bc632015-04-14 10:10:57 -0700116 }
117
118 private String linkType(BiLink link) {
119 return link.two == null || link.one.type() == link.two.type() ?
120 link.one.type().toString() :
Bri Prebilic Coleab582b82015-04-14 15:08:22 -0700121 link.one.type().toString() + " / " + link.two.type().toString();
Bri Prebilic Cole9fb594a2015-04-14 09:15:54 -0700122 }
123
124 @Override
125 protected String[] columnIds() {
126 return COL_IDS;
127 }
128 }
129
130}