Simon Hunt | 0e16109 | 2017-05-08 17:41:38 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 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; |
| 27 | import org.onosproject.net.statistic.DefaultLoad; |
| 28 | import org.onosproject.net.statistic.Load; |
| 29 | import org.onosproject.ui.impl.AbstractUiImplTest; |
| 30 | import org.onosproject.ui.topo.TopoUtils; |
| 31 | |
| 32 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | import static org.junit.Assert.assertEquals; |
| 34 | import static org.onosproject.net.DeviceId.deviceId; |
| 35 | import static org.onosproject.net.PortNumber.portNumber; |
| 36 | |
| 37 | /** |
| 38 | * Unit tests for {@link TrafficLink}. |
| 39 | */ |
| 40 | public class TrafficLinkTest extends AbstractUiImplTest { |
| 41 | |
| 42 | private static final DeviceId D1 = deviceId("1"); |
| 43 | private static final DeviceId D2 = deviceId("2"); |
| 44 | private static final PortNumber P1 = portNumber(1); |
| 45 | private static final PortNumber P2 = portNumber(2); |
| 46 | |
| 47 | private static final ConnectPoint SRC1 = new ConnectPoint(D1, P1); |
| 48 | private static final ConnectPoint DST1 = new ConnectPoint(D2, P1); |
| 49 | private static final ConnectPoint DST2 = new ConnectPoint(D2, P2); |
| 50 | |
| 51 | private static final LinkKey X = LinkKey.linkKey(SRC1, DST2); |
| 52 | |
| 53 | |
| 54 | private TrafficLink createALink() { |
| 55 | Link linkIngress = DefaultEdgeLink.createEdgeLink(SRC1, true); |
| 56 | LinkKey key = TopoUtils.canonicalLinkKey(checkNotNull(linkIngress)); |
| 57 | TrafficLink tl = new TrafficLink(key, linkIngress); |
| 58 | Link linkEgress = DefaultEdgeLink.createEdgeLink(SRC1, false); |
| 59 | tl.setOther(linkEgress); |
| 60 | return tl; |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void basic() { |
| 65 | title("basic"); |
| 66 | |
| 67 | TrafficLink tl = createALink(); |
| 68 | Load bigLoad = new DefaultLoad(2000, 0); |
| 69 | tl.addLoad(bigLoad); |
| 70 | print(tl); |
| 71 | assertEquals("bad bytes value", 2000, tl.bytes()); |
| 72 | // NOTE: rate is bytes / period (10 seconds) |
| 73 | assertEquals("bad rate value", 200, tl.rate()); |
| 74 | // this load does not represent flows |
| 75 | assertEquals("bad flow count", 0, tl.flows()); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void copyConstructor() { |
| 80 | title("copy-constructor"); |
| 81 | TrafficLink tlOrig = createALink(); |
| 82 | TrafficLink tlCopy = new TrafficLink(tlOrig); |
| 83 | assertEquals("not copied correctly (1)", tlOrig, tlCopy); |
| 84 | |
| 85 | tlOrig.addLoad(new DefaultLoad(2000, 0)); |
| 86 | tlCopy = new TrafficLink(tlOrig); |
| 87 | assertEquals("not copied correctly (2)", tlOrig, tlCopy); |
| 88 | |
| 89 | tlOrig = createALink(); |
| 90 | tlOrig.addFlows(345); |
| 91 | tlCopy = new TrafficLink(tlOrig); |
| 92 | assertEquals("not copied correctly (3)", tlOrig, tlCopy); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void mergeStatsBytes() { |
| 97 | title("mergeStatsBytes"); |
| 98 | TrafficLink tla = createALink(); |
| 99 | tla.addLoad(new DefaultLoad(2000, 0)); |
| 100 | print(tla); |
| 101 | |
| 102 | TrafficLink tlb = createALink(); |
| 103 | tlb.addLoad(new DefaultLoad(3000, 0)); |
| 104 | print(tlb); |
| 105 | |
| 106 | tla.mergeStats(tlb); |
| 107 | print(tla); |
| 108 | assertEquals("mergedBytes", 5000, tla.bytes()); |
| 109 | assertEquals("mergeRate", 500, tla.rate()); |
| 110 | } |
| 111 | |
| 112 | @Test |
| 113 | public void mergeStatsFlows() { |
| 114 | title("mergeStatsFlows"); |
| 115 | TrafficLink tla = createALink(); |
| 116 | tla.addFlows(9); |
| 117 | print(tla); |
| 118 | |
| 119 | TrafficLink tlb = createALink(); |
| 120 | tlb.addFlows(16); |
| 121 | print(tlb); |
| 122 | |
| 123 | tla.mergeStats(tlb); |
| 124 | print(tla); |
| 125 | assertEquals("mergedFlows", 25, tla.flows()); |
| 126 | } |
| 127 | } |