Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 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. |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 15 | */ |
| 16 | |
Simon Hunt | 191c84a | 2015-08-21 08:24:48 -0700 | [diff] [blame] | 17 | package org.onosproject.ui.topo; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 18 | |
| 19 | import org.onosproject.net.Link; |
| 20 | import org.onosproject.net.LinkKey; |
Simon Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 21 | import org.onosproject.ui.model.topo.UiLinkId; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 22 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 23 | import static com.google.common.base.Preconditions.checkNotNull; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 26 | * 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 Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 30 | * <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 Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 34 | */ |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 35 | public abstract class BiLink { |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 36 | |
Simon Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 37 | private final UiLinkId uiLinkId; |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 38 | private final LinkKey key; |
| 39 | private final Link one; |
| 40 | private Link two; |
| 41 | |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 42 | /** |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 43 | * 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 Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 46 | * |
Simon Hunt | f01826c | 2017-05-09 17:28:13 -0700 | [diff] [blame] | 47 | * @param key canonical key for this bi-link |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 48 | * @param link first link |
| 49 | */ |
| 50 | public BiLink(LinkKey key, Link link) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 51 | this.key = checkNotNull(key); |
| 52 | this.one = checkNotNull(link); |
Simon Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 53 | 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 Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | /** |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 69 | * Sets the second link for this bi-link. |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 70 | * |
| 71 | * @param link second link |
| 72 | */ |
| 73 | public void setOther(Link link) { |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 74 | this.two = checkNotNull(link); |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /** |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 78 | * Returns the link identifier in the form expected on the Topology View |
| 79 | * in the web client. |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 80 | * |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 81 | * @return link identifier |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 82 | */ |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 83 | public String linkId() { |
Simon Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 84 | 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 Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 96 | /** |
| 97 | * Returns the key for this bi-link. |
| 98 | * |
| 99 | * @return the key |
| 100 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 101 | public LinkKey key() { |
| 102 | return key; |
| 103 | } |
| 104 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 105 | /** |
| 106 | * Returns the first link in this bi-link. |
| 107 | * |
| 108 | * @return the first link |
| 109 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 110 | public Link one() { |
| 111 | return one; |
| 112 | } |
| 113 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 114 | /** |
| 115 | * Returns the second link in this bi-link. |
| 116 | * |
| 117 | * @return the second link |
| 118 | */ |
Simon Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 119 | public Link two() { |
| 120 | return two; |
| 121 | } |
| 122 | |
Simon Hunt | f01826c | 2017-05-09 17:28:13 -0700 | [diff] [blame] | 123 | @Override |
| 124 | public String toString() { |
Simon Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 125 | return linkId(); |
Simon Hunt | f01826c | 2017-05-09 17:28:13 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Simon Hunt | 4fc8685 | 2015-08-20 17:57:52 -0700 | [diff] [blame] | 128 | /** |
| 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 Hunt | a17fa67 | 2015-08-19 18:42:22 -0700 | [diff] [blame] | 136 | } |