blob: e8712dc8138f16743466f81378d27eb01c84fef0 [file] [log] [blame]
Simon Hunt21281fd2017-03-30 22:28:28 -07001/*
2 * Copyright 2017-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.topo;
18
19import org.junit.Test;
20import org.onosproject.net.ConnectPoint;
21import org.onosproject.net.DefaultLink;
22import org.onosproject.net.Link;
23import org.onosproject.net.LinkKey;
24import org.onosproject.net.provider.ProviderId;
Simon Huntf01826c2017-05-09 17:28:13 -070025import org.onosproject.ui.AbstractUiTest;
Simon Hunt21281fd2017-03-30 22:28:28 -070026
27import static org.junit.Assert.assertEquals;
28import static org.junit.Assert.assertFalse;
29import static org.junit.Assert.assertTrue;
30import static org.onosproject.net.ConnectPoint.deviceConnectPoint;
31
32/**
33 * Unit tests for {@link TopoUtils}.
34 */
Simon Huntf01826c2017-05-09 17:28:13 -070035public class TopoUtilsTest extends AbstractUiTest {
Simon Hunt21281fd2017-03-30 22:28:28 -070036 private static final String AM_WL = "wrong label";
37 private static final String AM_WM = "wrong magnitude";
38 private static final String AM_CL = "clipped?";
39 private static final String AM_NCL = "not clipped?";
40
41 private static final ConnectPoint CP_FU = deviceConnectPoint("fu:001/3");
42 private static final ConnectPoint CP_BAH = deviceConnectPoint("bah:002/5");
43
44 private static final Link LINK_FU_BAH = DefaultLink.builder()
45 .src(CP_FU)
46 .dst(CP_BAH)
47 .type(Link.Type.DIRECT)
48 .providerId(ProviderId.NONE)
49 .build();
50
51 private static final Link LINK_BAH_FU = DefaultLink.builder()
52 .src(CP_BAH)
53 .dst(CP_FU)
54 .type(Link.Type.DIRECT)
55 .providerId(ProviderId.NONE)
56 .build();
57
Simon Huntf01826c2017-05-09 17:28:13 -070058 private static final Link LINK_7_TO_3 = DefaultLink.builder()
59 .src(deviceConnectPoint("of:0000000000000007/2"))
60 .dst(deviceConnectPoint("of:0000000000000003/2"))
61 .type(Link.Type.DIRECT)
62 .providerId(ProviderId.NONE)
63 .build();
64
65 private static final Link LINK_3_TO_7 = DefaultLink.builder()
66 .src(deviceConnectPoint("of:0000000000000003/2"))
67 .dst(deviceConnectPoint("of:0000000000000007/2"))
68 .type(Link.Type.DIRECT)
69 .providerId(ProviderId.NONE)
70 .build();
71
72
Simon Hunt21281fd2017-03-30 22:28:28 -070073 private TopoUtils.ValueLabel vl;
74
75 @Test
76 public void linkStringFuBah() {
77 String compact = TopoUtils.compactLinkString(LINK_FU_BAH);
78 assertEquals("wrong link id", "fu:001/3-bah:002/5", compact);
79 }
80
81 @Test
82 public void linkStringBahFu() {
83 String compact = TopoUtils.compactLinkString(LINK_BAH_FU);
84 assertEquals("wrong link id", "bah:002/5-fu:001/3", compact);
85 }
86
87 @Test
88 public void canonLinkKey() {
89 LinkKey fb = TopoUtils.canonicalLinkKey(LINK_FU_BAH);
90 LinkKey bf = TopoUtils.canonicalLinkKey(LINK_BAH_FU);
91 assertEquals("not canonical", fb, bf);
92 }
93
94 @Test
Simon Huntf01826c2017-05-09 17:28:13 -070095 public void canon723() {
96 LinkKey lk1 = TopoUtils.canonicalLinkKey(LINK_7_TO_3);
97 print(lk1);
98 LinkKey lk2 = TopoUtils.canonicalLinkKey(LINK_3_TO_7);
99 print(lk2);
100 assertEquals("not canonical 3/7", lk1, lk2);
101 }
102
103 @Test
Simon Hunt21281fd2017-03-30 22:28:28 -0700104 public void formatSmallBytes() {
105 vl = TopoUtils.formatBytes(1_000L);
106 assertEquals(AM_WM, TopoUtils.Magnitude.ONE, vl.magnitude());
107 assertEquals(AM_WL, "1,000 B", vl.toString());
108 }
109
110 @Test
111 public void formatKiloBytes() {
112 vl = TopoUtils.formatBytes(2_000L);
113 assertEquals(AM_WM, TopoUtils.Magnitude.KILO, vl.magnitude());
114 assertEquals(AM_WL, "1.95 KB", vl.toString());
115 }
116
117 @Test
118 public void formatMegaBytes() {
119 vl = TopoUtils.formatBytes(3_000_000L);
120 assertEquals(AM_WM, TopoUtils.Magnitude.MEGA, vl.magnitude());
121 assertEquals(AM_WL, "2.86 MB", vl.toString());
122 }
123
124 @Test
125 public void formatGigaBytes() {
126 vl = TopoUtils.formatBytes(4_000_000_000L);
127 assertEquals(AM_WM, TopoUtils.Magnitude.GIGA, vl.magnitude());
128 assertEquals(AM_WL, "3.73 GB", vl.toString());
129 }
130
131 @Test
132 public void formatTeraBytes() {
133 vl = TopoUtils.formatBytes(5_000_000_000_000L);
134 assertEquals(AM_WM, TopoUtils.Magnitude.GIGA, vl.magnitude());
135 assertEquals(AM_WL, "4,656.61 GB", vl.toString());
136 }
137
138 @Test
139 public void formatPacketRateSmall() {
140 vl = TopoUtils.formatPacketRate(37);
141 assertEquals(AM_WL, "37 pps", vl.toString());
142 }
143
144 @Test
145 public void formatPacketRateKilo() {
146 vl = TopoUtils.formatPacketRate(1024);
147 assertEquals(AM_WL, "1 Kpps", vl.toString());
148 }
149
150 @Test
151 public void formatPacketRateKilo2() {
152 vl = TopoUtils.formatPacketRate(1034);
153 assertEquals(AM_WL, "1.01 Kpps", vl.toString());
154 }
155
156 @Test
157 public void formatPacketRateMega() {
158 vl = TopoUtils.formatPacketRate(9_000_000);
159 assertEquals(AM_WL, "8.58 Mpps", vl.toString());
160 }
161
162 // remember for the following method calls, the input is in bytes!
163 @Test
164 public void formatClippedBitsSmall() {
165 vl = TopoUtils.formatClippedBitRate(8);
166 assertEquals(AM_WL, "64 bps", vl.toString());
167 assertFalse(AM_CL, vl.clipped());
168 }
169
170 @Test
171 public void formatClippedBitsKilo() {
172 vl = TopoUtils.formatClippedBitRate(2_004);
173 assertEquals(AM_WL, "15.66 Kbps", vl.toString());
174 assertFalse(AM_CL, vl.clipped());
175 }
176
177 @Test
178 public void formatClippedBitsMega() {
179 vl = TopoUtils.formatClippedBitRate(3_123_123);
180 assertEquals(AM_WL, "23.83 Mbps", vl.toString());
181 assertFalse(AM_CL, vl.clipped());
182 }
183
184 @Test
185 public void formatClippedBitsGiga() {
186 vl = TopoUtils.formatClippedBitRate(500_000_000);
187 assertEquals(AM_WL, "3.73 Gbps", vl.toString());
188 assertFalse(AM_CL, vl.clipped());
189 }
190
191 @Test
192 public void formatClippedBitsGigaExceedThreshold() {
193 vl = TopoUtils.formatClippedBitRate(5_000_000_000L);
194 // approx. 37.25 Gbps
195 assertEquals(AM_WL, "10 Gbps", vl.toString());
196 assertTrue(AM_NCL, vl.clipped());
197 }
198
199 @Test
200 public void formatNoFlows() {
201 String f = TopoUtils.formatFlows(0);
202 assertEquals(AM_WL, "", f);
203 }
204
205 @Test
206 public void formatNegativeFlows() {
207 String f = TopoUtils.formatFlows(-3);
208 assertEquals(AM_WL, "", f);
209 }
210
211 @Test
212 public void formatOneFlow() {
213 String f = TopoUtils.formatFlows(1);
214 assertEquals(AM_WL, "1 flow", f);
215 }
216
217 @Test
218 public void formatManyFlows() {
219 String f = TopoUtils.formatFlows(42);
220 assertEquals(AM_WL, "42 flows", f);
221 }
222}