Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2017-present Open Networking Foundation |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -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. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | package org.onosproject.ui.impl.topo.util; |
| 19 | |
| 20 | import org.junit.Test; |
| 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.DefaultEdgeLink; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.Link; |
| 25 | import org.onosproject.net.LinkKey; |
| 26 | import org.onosproject.net.PortNumber; |
Simon Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 27 | import org.onosproject.net.region.RegionId; |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 28 | import org.onosproject.net.statistic.DefaultLoad; |
| 29 | import org.onosproject.net.statistic.Load; |
| 30 | import org.onosproject.ui.impl.AbstractUiImplTest; |
Simon Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.model.topo.UiLinkId; |
Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 32 | import org.onosproject.ui.topo.TopoUtils; |
| 33 | |
| 34 | import static com.google.common.base.Preconditions.checkNotNull; |
| 35 | import static org.junit.Assert.assertEquals; |
| 36 | import static org.onosproject.net.DeviceId.deviceId; |
| 37 | import static org.onosproject.net.PortNumber.portNumber; |
| 38 | |
| 39 | /** |
| 40 | * Unit tests for {@link TrafficLink}. |
| 41 | */ |
| 42 | public 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 Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 50 | private static final ConnectPoint DST2 = new ConnectPoint(D2, P2); |
| 51 | |
Simon Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 52 | 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 Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 55 | |
| 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 Hunt | 05eba35 | 2017-05-11 20:17:08 -0700 | [diff] [blame] | 82 | 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 Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame] | 87 | } |
| 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 | } |