blob: be7fa90594fc56f28e9052eadee66f8af65166ae [file] [log] [blame]
Simon Hunta17fa672015-08-19 18:42:22 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Simon Hunta17fa672015-08-19 18:42:22 -07003 *
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.
Simon Hunta17fa672015-08-19 18:42:22 -070015 */
16
Simon Hunt191c84a2015-08-21 08:24:48 -070017package org.onosproject.ui.topo;
Simon Hunta17fa672015-08-19 18:42:22 -070018
19import org.onosproject.net.Link;
20import org.onosproject.net.LinkKey;
Simon Hunt05eba352017-05-11 20:17:08 -070021import org.onosproject.ui.model.topo.UiLinkId;
Simon Hunta17fa672015-08-19 18:42:22 -070022
Simon Hunt4fc86852015-08-20 17:57:52 -070023import static com.google.common.base.Preconditions.checkNotNull;
Simon Hunta17fa672015-08-19 18:42:22 -070024
25/**
Simon Hunt4fc86852015-08-20 17:57:52 -070026 * Representation of a link and its inverse, as a partial implementation.
27 * <p>
28 * Subclasses will decide how to generate the link highlighting (coloring
29 * and labeling) for the topology view.
Simon Hunt05eba352017-05-11 20:17:08 -070030 * <p>
31 * As an alternative, a bi-link can be initialized with a {@link UiLinkId}
32 * (ignoring the LinkKey and links one and two), which will be reported as
33 * its identifier instead.
Simon Hunta17fa672015-08-19 18:42:22 -070034 */
Simon Hunt4fc86852015-08-20 17:57:52 -070035public abstract class BiLink {
Simon Hunta17fa672015-08-19 18:42:22 -070036
Simon Hunt05eba352017-05-11 20:17:08 -070037 private final UiLinkId uiLinkId;
Simon Hunta17fa672015-08-19 18:42:22 -070038 private final LinkKey key;
39 private final Link one;
40 private Link two;
41
Simon Hunta17fa672015-08-19 18:42:22 -070042 /**
Simon Hunt4fc86852015-08-20 17:57:52 -070043 * Constructs a bi-link for the given key and initial link. It is expected
44 * that the caller will have used {@link TopoUtils#canonicalLinkKey(Link)}
45 * to generate the key.
Simon Hunta17fa672015-08-19 18:42:22 -070046 *
Simon Huntf01826c2017-05-09 17:28:13 -070047 * @param key canonical key for this bi-link
Simon Hunta17fa672015-08-19 18:42:22 -070048 * @param link first link
49 */
50 public BiLink(LinkKey key, Link link) {
Simon Hunt4fc86852015-08-20 17:57:52 -070051 this.key = checkNotNull(key);
52 this.one = checkNotNull(link);
Simon Hunt05eba352017-05-11 20:17:08 -070053 this.uiLinkId = null;
54 }
55
56 /**
57 * Constructs a bi-link for the given UI link identifier; sets remaining
58 * fields to null.
59 *
60 * @param uilinkId canonical ID for this bi-link
61 */
62 public BiLink(UiLinkId uilinkId) {
63 this.uiLinkId = checkNotNull(uilinkId);
64 this.key = null;
65 this.one = null;
Simon Hunta17fa672015-08-19 18:42:22 -070066 }
67
68 /**
Simon Hunt4fc86852015-08-20 17:57:52 -070069 * Sets the second link for this bi-link.
Simon Hunta17fa672015-08-19 18:42:22 -070070 *
71 * @param link second link
72 */
73 public void setOther(Link link) {
Simon Hunt4fc86852015-08-20 17:57:52 -070074 this.two = checkNotNull(link);
Simon Hunta17fa672015-08-19 18:42:22 -070075 }
76
77 /**
Simon Hunt4fc86852015-08-20 17:57:52 -070078 * Returns the link identifier in the form expected on the Topology View
79 * in the web client.
Simon Hunta17fa672015-08-19 18:42:22 -070080 *
Simon Hunt4fc86852015-08-20 17:57:52 -070081 * @return link identifier
Simon Hunta17fa672015-08-19 18:42:22 -070082 */
Simon Hunt4fc86852015-08-20 17:57:52 -070083 public String linkId() {
Simon Hunt05eba352017-05-11 20:17:08 -070084 return uiLinkId != null ? uiLinkId.toString() : key.asId();
85 }
86
87 /**
88 * Returns the UI link identifier for this bi-link (if set).
89 *
90 * @return the UI link ID
91 */
92 public UiLinkId uiLinkId() {
93 return uiLinkId;
Simon Hunta17fa672015-08-19 18:42:22 -070094 }
95
Simon Hunt4fc86852015-08-20 17:57:52 -070096 /**
97 * Returns the key for this bi-link.
98 *
99 * @return the key
100 */
Simon Hunta17fa672015-08-19 18:42:22 -0700101 public LinkKey key() {
102 return key;
103 }
104
Simon Hunt4fc86852015-08-20 17:57:52 -0700105 /**
106 * Returns the first link in this bi-link.
107 *
108 * @return the first link
109 */
Simon Hunta17fa672015-08-19 18:42:22 -0700110 public Link one() {
111 return one;
112 }
113
Simon Hunt4fc86852015-08-20 17:57:52 -0700114 /**
115 * Returns the second link in this bi-link.
116 *
117 * @return the second link
118 */
Simon Hunta17fa672015-08-19 18:42:22 -0700119 public Link two() {
120 return two;
121 }
122
Simon Huntf01826c2017-05-09 17:28:13 -0700123 @Override
124 public String toString() {
Simon Hunt05eba352017-05-11 20:17:08 -0700125 return linkId();
Simon Huntf01826c2017-05-09 17:28:13 -0700126 }
127
Simon Hunt4fc86852015-08-20 17:57:52 -0700128 /**
129 * Returns the link highlighting to use, based on this bi-link's current
130 * state.
131 *
132 * @param type optional highlighting type parameter
133 * @return link highlighting model
134 */
135 public abstract LinkHighlight highlight(Enum<?> type);
Simon Hunta17fa672015-08-19 18:42:22 -0700136}