Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.ui.model.topo; |
| 18 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 19 | import org.junit.Before; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 20 | import org.junit.Test; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 21 | import org.onosproject.net.ConnectPoint; |
| 22 | import org.onosproject.net.DefaultLink; |
| 23 | import org.onosproject.net.DeviceId; |
| 24 | import org.onosproject.net.Link; |
| 25 | import org.onosproject.net.PortNumber; |
| 26 | import org.onosproject.net.provider.ProviderId; |
| 27 | import org.onosproject.net.region.RegionId; |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 28 | import org.onosproject.ui.AbstractUiTest; |
| 29 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 30 | import java.util.ArrayList; |
| 31 | import java.util.Collections; |
| 32 | import java.util.List; |
| 33 | |
| 34 | import static org.junit.Assert.assertEquals; |
| 35 | import static org.onosproject.net.DeviceId.deviceId; |
| 36 | import static org.onosproject.net.PortNumber.portNumber; |
| 37 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 38 | /** |
| 39 | * Unit tests for {@link UiTopology}. |
| 40 | */ |
| 41 | public class UiTopologyTest extends AbstractUiTest { |
| 42 | |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 43 | private static final DeviceId DEV_X = deviceId("dev-X"); |
| 44 | private static final DeviceId DEV_Y = deviceId("dev-Y"); |
| 45 | private static final PortNumber P1 = portNumber(1); |
| 46 | private static final PortNumber P2 = portNumber(2); |
| 47 | |
| 48 | private static final String DEV_X_ID = "dev-x/1"; |
| 49 | private static final String DEV_Y_ID = "dev-y/2"; |
| 50 | |
| 51 | private static final ConnectPoint CP_X1 = new ConnectPoint(DEV_X, P1); |
| 52 | private static final ConnectPoint CP_Y2 = new ConnectPoint(DEV_Y, P2); |
| 53 | |
| 54 | private static final Link LINK_X1_TO_Y2 = DefaultLink.builder() |
| 55 | .providerId(ProviderId.NONE) |
| 56 | .src(CP_X1) |
| 57 | .dst(CP_Y2) |
| 58 | .type(Link.Type.DIRECT) |
| 59 | .build(); |
| 60 | |
| 61 | private static final UiLinkId DX1_DY2 = UiLinkId.uiLinkId(LINK_X1_TO_Y2); |
| 62 | |
| 63 | private static final RegionId ROOT = UiRegion.NULL_ID; |
| 64 | private static final RegionId R1 = RegionId.regionId("R1"); |
| 65 | private static final RegionId R2 = RegionId.regionId("R2"); |
| 66 | private static final RegionId R3 = RegionId.regionId("R3"); |
| 67 | |
| 68 | private static final String DEV_LINK_CLASS = "UiDeviceLink"; |
| 69 | private static final String REG_LINK_CLASS = "UiRegionLink"; |
| 70 | private static final String REG_DEV_LINK_CLASS = "UiRegionDeviceLink"; |
| 71 | |
| 72 | |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 73 | private UiTopology topo; |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 74 | private UiDeviceLink devLink; |
| 75 | |
| 76 | private List<RegionId> xBranch; |
| 77 | private List<RegionId> yBranch; |
| 78 | private UiSynthLink synth; |
| 79 | |
| 80 | @Before |
| 81 | public void setUp() { |
| 82 | topo = new UiTopology(); |
| 83 | devLink = new UiDeviceLink(null, DX1_DY2); |
| 84 | devLink.attachBackingLink(LINK_X1_TO_Y2); |
| 85 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 86 | |
| 87 | @Test |
| 88 | public void basic() { |
| 89 | title("basic"); |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 90 | print(topo); |
| 91 | } |
Simon Hunt | c13082f | 2016-08-03 21:20:23 -0700 | [diff] [blame] | 92 | |
| 93 | private List<RegionId> branch(RegionId... ids) { |
| 94 | List<RegionId> result = new ArrayList<>(ids.length); |
| 95 | Collections.addAll(result, ids); |
| 96 | return result; |
| 97 | } |
| 98 | |
| 99 | private void verifySynth(RegionId id, String cls, String epA, String epB) { |
| 100 | synth = topo.makeSynthLink(devLink, xBranch, yBranch); |
| 101 | UiLink ulink = synth.link(); |
| 102 | print(synth); |
| 103 | print("EpA{%s} EpB{%s}", ulink.endPointA(), ulink.endPointB()); |
| 104 | |
| 105 | assertEquals("wrong region", id, synth.regionId()); |
| 106 | assertEquals("wrong link class", cls, ulink.type()); |
| 107 | assertEquals("wrong EP A", epA, ulink.endPointA()); |
| 108 | assertEquals("wrong EP B", epB, ulink.endPointB()); |
| 109 | } |
| 110 | |
| 111 | @Test |
| 112 | public void makeSynthDevToDevRoot() { |
| 113 | title("makeSynthDevToDevRoot"); |
| 114 | xBranch = branch(ROOT); |
| 115 | yBranch = branch(ROOT); |
| 116 | verifySynth(ROOT, DEV_LINK_CLASS, DEV_X_ID, DEV_Y_ID); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void makeSynthDevToDevR1() { |
| 121 | title("makeSynthDevToDevR1"); |
| 122 | xBranch = branch(ROOT, R1); |
| 123 | yBranch = branch(ROOT, R1); |
| 124 | verifySynth(R1, DEV_LINK_CLASS, DEV_X_ID, DEV_Y_ID); |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void makeSynthDevToDevR2() { |
| 129 | title("makeSynthDevToDevR2"); |
| 130 | xBranch = branch(ROOT, R1, R2); |
| 131 | yBranch = branch(ROOT, R1, R2); |
| 132 | verifySynth(R2, DEV_LINK_CLASS, DEV_X_ID, DEV_Y_ID); |
| 133 | } |
| 134 | |
| 135 | @Test |
| 136 | public void makeSynthRegToRegRoot() { |
| 137 | title("makeSynthRegToRegRoot"); |
| 138 | xBranch = branch(ROOT, R1); |
| 139 | yBranch = branch(ROOT, R2); |
| 140 | verifySynth(ROOT, REG_LINK_CLASS, R1.id(), R2.id()); |
| 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void makeSynthRegToRegR1() { |
| 145 | title("makeSynthRegToRegR1"); |
| 146 | xBranch = branch(ROOT, R1, R2); |
| 147 | yBranch = branch(ROOT, R1, R3); |
| 148 | verifySynth(R1, REG_LINK_CLASS, R2.id(), R3.id()); |
| 149 | } |
| 150 | |
| 151 | @Test |
| 152 | public void makeSynthRegToDevRoot() { |
| 153 | title("makeSynthRegToDevRoot"); |
| 154 | |
| 155 | // Note: link is canonicalized to region--device order |
| 156 | |
| 157 | xBranch = branch(ROOT); |
| 158 | yBranch = branch(ROOT, R1); |
| 159 | verifySynth(ROOT, REG_DEV_LINK_CLASS, R1.id(), DEV_X_ID); |
| 160 | |
| 161 | xBranch = branch(ROOT, R1); |
| 162 | yBranch = branch(ROOT); |
| 163 | verifySynth(ROOT, REG_DEV_LINK_CLASS, R1.id(), DEV_Y_ID); |
| 164 | } |
| 165 | |
| 166 | @Test |
| 167 | public void makeSynthRegToDevR3() { |
| 168 | title("makeSynthRegToDevR3"); |
| 169 | |
| 170 | // Note: link is canonicalized to region--device order |
| 171 | |
| 172 | xBranch = branch(ROOT, R3); |
| 173 | yBranch = branch(ROOT, R3, R1); |
| 174 | verifySynth(R3, REG_DEV_LINK_CLASS, R1.id(), DEV_X_ID); |
| 175 | |
| 176 | xBranch = branch(ROOT, R3, R1); |
| 177 | yBranch = branch(ROOT, R3); |
| 178 | verifySynth(R3, REG_DEV_LINK_CLASS, R1.id(), DEV_Y_ID); |
| 179 | } |
Simon Hunt | c0f20c1 | 2016-05-09 09:30:20 -0700 | [diff] [blame] | 180 | } |