blob: a1f3d71f47a704ea91fd6b1c84d5b1b587ce1087 [file] [log] [blame]
Simon Hunt0e161092017-05-08 17:41:38 -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 */
17
18package org.onosproject.ui.impl.topo.util;
19
20import org.junit.Test;
21import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.DefaultEdgeLink;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.Link;
25import org.onosproject.net.LinkKey;
26import org.onosproject.net.PortNumber;
Simon Hunt05eba352017-05-11 20:17:08 -070027import org.onosproject.net.region.RegionId;
Simon Hunt0e161092017-05-08 17:41:38 -070028import org.onosproject.net.statistic.DefaultLoad;
29import org.onosproject.net.statistic.Load;
30import org.onosproject.ui.impl.AbstractUiImplTest;
Simon Hunt05eba352017-05-11 20:17:08 -070031import org.onosproject.ui.model.topo.UiLinkId;
Simon Hunt0e161092017-05-08 17:41:38 -070032import org.onosproject.ui.topo.TopoUtils;
33
34import static com.google.common.base.Preconditions.checkNotNull;
35import static org.junit.Assert.assertEquals;
36import static org.onosproject.net.DeviceId.deviceId;
37import static org.onosproject.net.PortNumber.portNumber;
38
39/**
40 * Unit tests for {@link TrafficLink}.
41 */
42public class TrafficLinkTest extends AbstractUiImplTest {
43
44 private static final DeviceId D1 = deviceId("1");
45 private static final DeviceId D2 = deviceId("2");
46 private static final PortNumber P1 = portNumber(1);
47 private static final PortNumber P2 = portNumber(2);
48
49 private static final ConnectPoint SRC1 = new ConnectPoint(D1, P1);
Simon Hunt0e161092017-05-08 17:41:38 -070050 private static final ConnectPoint DST2 = new ConnectPoint(D2, P2);
51
Simon Hunt05eba352017-05-11 20:17:08 -070052 private static final RegionId RA = RegionId.regionId("rA");
53 private static final RegionId RB = RegionId.regionId("rB");
54 private static final String EXP_RA_RB = "rA~rB";
Simon Hunt0e161092017-05-08 17:41:38 -070055
56
57 private TrafficLink createALink() {
58 Link linkIngress = DefaultEdgeLink.createEdgeLink(SRC1, true);
59 LinkKey key = TopoUtils.canonicalLinkKey(checkNotNull(linkIngress));
60 TrafficLink tl = new TrafficLink(key, linkIngress);
61 Link linkEgress = DefaultEdgeLink.createEdgeLink(SRC1, false);
62 tl.setOther(linkEgress);
63 return tl;
64 }
65
66 @Test
67 public void basic() {
68 title("basic");
69
70 TrafficLink tl = createALink();
71 Load bigLoad = new DefaultLoad(2000, 0);
72 tl.addLoad(bigLoad);
73 print(tl);
74 assertEquals("bad bytes value", 2000, tl.bytes());
75 // NOTE: rate is bytes / period (10 seconds)
76 assertEquals("bad rate value", 200, tl.rate());
77 // this load does not represent flows
78 assertEquals("bad flow count", 0, tl.flows());
79 }
80
81 @Test
Simon Hunt05eba352017-05-11 20:17:08 -070082 public void emptyStats() {
83 title("emptyStats");
84 UiLinkId uiLinkId = UiLinkId.uiLinkId(RA, RB);
85 TrafficLink tl = new TrafficLink(uiLinkId);
86 assertEquals("wrong id", EXP_RA_RB, tl.linkId());
Simon Hunt0e161092017-05-08 17:41:38 -070087 }
88
89 @Test
90 public void mergeStatsBytes() {
91 title("mergeStatsBytes");
92 TrafficLink tla = createALink();
93 tla.addLoad(new DefaultLoad(2000, 0));
94 print(tla);
95
96 TrafficLink tlb = createALink();
97 tlb.addLoad(new DefaultLoad(3000, 0));
98 print(tlb);
99
100 tla.mergeStats(tlb);
101 print(tla);
102 assertEquals("mergedBytes", 5000, tla.bytes());
103 assertEquals("mergeRate", 500, tla.rate());
104 }
105
106 @Test
107 public void mergeStatsFlows() {
108 title("mergeStatsFlows");
109 TrafficLink tla = createALink();
110 tla.addFlows(9);
111 print(tla);
112
113 TrafficLink tlb = createALink();
114 tlb.addFlows(16);
115 print(tlb);
116
117 tla.mergeStats(tlb);
118 print(tla);
119 assertEquals("mergedFlows", 25, tla.flows());
120 }
121}