blob: e530db45a76ee985e9912a72f7ebd5d0f21a2548 [file] [log] [blame]
Simon Huntc0f20c12016-05-09 09:30:20 -07001/*
2 * Copyright 2016-present 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.model.topo;
18
19import org.onosproject.net.ConnectPoint;
20import org.onosproject.net.ElementId;
21import org.onosproject.net.Link;
Simon Hunt58a0dd02016-05-17 11:54:23 -070022import org.onosproject.net.PortNumber;
Simon Huntc0f20c12016-05-09 09:30:20 -070023
24/**
25 * A canonical representation of an identifier for {@link UiLink}s.
26 */
27public final class UiLinkId {
28
29 /**
30 * Designates the directionality of an underlying (uni-directional) link.
31 */
32 public enum Direction {
33 A_TO_B,
34 B_TO_A
35 }
36
Simon Hunt58a0dd02016-05-17 11:54:23 -070037 private static final String CP_DELIMITER = "~";
38 private static final String ID_PORT_DELIMITER = "/";
Simon Huntc0f20c12016-05-09 09:30:20 -070039
40 private final ElementId idA;
Simon Hunt58a0dd02016-05-17 11:54:23 -070041 private final PortNumber portA;
Simon Huntc0f20c12016-05-09 09:30:20 -070042 private final ElementId idB;
Simon Hunt58a0dd02016-05-17 11:54:23 -070043 private final PortNumber portB;
44
Simon Huntc0f20c12016-05-09 09:30:20 -070045 private final String idStr;
46
47 /**
48 * Creates a UI link identifier. It is expected that A comes before B when
49 * the two identifiers are naturally sorted, thus providing a representation
50 * which is invariant to whether A or B is source or destination of the
51 * underlying link.
52 *
53 * @param a first element ID
Simon Hunt58a0dd02016-05-17 11:54:23 -070054 * @param pa first element port
Simon Huntc0f20c12016-05-09 09:30:20 -070055 * @param b second element ID
Simon Hunt58a0dd02016-05-17 11:54:23 -070056 * @param pb second element port
Simon Huntc0f20c12016-05-09 09:30:20 -070057 */
Simon Hunt58a0dd02016-05-17 11:54:23 -070058 private UiLinkId(ElementId a, PortNumber pa, ElementId b, PortNumber pb) {
Simon Huntc0f20c12016-05-09 09:30:20 -070059 idA = a;
Simon Hunt58a0dd02016-05-17 11:54:23 -070060 portA = pa;
Simon Huntc0f20c12016-05-09 09:30:20 -070061 idB = b;
Simon Hunt58a0dd02016-05-17 11:54:23 -070062 portB = pb;
Simon Huntc0f20c12016-05-09 09:30:20 -070063
Simon Hunt58a0dd02016-05-17 11:54:23 -070064 idStr = a + ID_PORT_DELIMITER + pa + CP_DELIMITER +
65 b + ID_PORT_DELIMITER + pb;
Simon Huntc0f20c12016-05-09 09:30:20 -070066 }
67
68 @Override
69 public String toString() {
70 return idStr;
71 }
72
73 /**
74 * Returns the identifier of the first element.
75 *
76 * @return first element identity
77 */
78 public ElementId elementA() {
79 return idA;
80 }
81
82 /**
Simon Hunt58a0dd02016-05-17 11:54:23 -070083 * Returns the port of the first element.
84 *
85 * @return first element port
86 */
87 public PortNumber portA() {
88 return portA;
89 }
90
91 /**
Simon Huntc0f20c12016-05-09 09:30:20 -070092 * Returns the identifier of the second element.
93 *
94 * @return second element identity
95 */
96 public ElementId elementB() {
97 return idB;
98 }
99
Simon Hunt58a0dd02016-05-17 11:54:23 -0700100 /**
101 * Returns the port of the second element.
102 *
103 * @return second element port
104 */
105 public PortNumber portB() {
106 return portB;
107 }
108
Simon Huntc0f20c12016-05-09 09:30:20 -0700109 @Override
110 public boolean equals(Object o) {
111 if (this == o) {
112 return true;
113 }
114 if (o == null || getClass() != o.getClass()) {
115 return false;
116 }
117
118 UiLinkId uiLinkId = (UiLinkId) o;
119 return idStr.equals(uiLinkId.idStr);
120 }
121
122 @Override
123 public int hashCode() {
124 return idStr.hashCode();
125 }
126
127 /**
128 * Returns the direction of the given link, or null if this link ID does
129 * not correspond to the given link.
130 *
131 * @param link the link to examine
132 * @return corresponding direction
133 */
134 Direction directionOf(Link link) {
135 ConnectPoint src = link.src();
136 ElementId srcId = src.elementId();
137 return idA.equals(srcId) ? Direction.A_TO_B
138 : idB.equals(srcId) ? Direction.B_TO_A
139 : null;
140 }
141
142 /**
143 * Generates the canonical link identifier for the given link.
144 *
145 * @param link link for which the identifier is required
146 * @return link identifier
147 * @throws NullPointerException if any of the required fields are null
148 */
149 public static UiLinkId uiLinkId(Link link) {
150 ConnectPoint src = link.src();
151 ConnectPoint dst = link.dst();
152 if (src == null || dst == null) {
153 throw new NullPointerException("null src or dst connect point: " + link);
154 }
155
156 ElementId srcId = src.elementId();
157 ElementId dstId = dst.elementId();
Simon Huntc0f20c12016-05-09 09:30:20 -0700158
159 // canonicalize
160 int comp = srcId.toString().compareTo(dstId.toString());
Simon Hunt58a0dd02016-05-17 11:54:23 -0700161 return comp <= 0 ? new UiLinkId(srcId, src.port(), dstId, dst.port())
162 : new UiLinkId(dstId, dst.port(), srcId, src.port());
Simon Huntc0f20c12016-05-09 09:30:20 -0700163 }
164}