blob: cb6a316d41376661265676cfdd14ca22811cc0bf [file] [log] [blame]
Simon Huntc0f20c12016-05-09 09:30:20 -07001/*
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
17package org.onosproject.ui.model.topo;
18
Simon Huntc13082f2016-08-03 21:20:23 -070019import org.junit.Before;
Simon Huntc0f20c12016-05-09 09:30:20 -070020import org.junit.Test;
Simon Huntc13082f2016-08-03 21:20:23 -070021import org.onosproject.net.ConnectPoint;
22import org.onosproject.net.DefaultLink;
23import org.onosproject.net.DeviceId;
24import org.onosproject.net.Link;
25import org.onosproject.net.PortNumber;
26import org.onosproject.net.provider.ProviderId;
27import org.onosproject.net.region.RegionId;
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070028import org.onosproject.ui.model.AbstractUiModelTest;
Simon Huntc0f20c12016-05-09 09:30:20 -070029
Simon Huntc13082f2016-08-03 21:20:23 -070030import java.util.ArrayList;
31import java.util.Collections;
32import java.util.List;
33
34import static org.junit.Assert.assertEquals;
35import static org.onosproject.net.DeviceId.deviceId;
36import static org.onosproject.net.PortNumber.portNumber;
37
Simon Huntc0f20c12016-05-09 09:30:20 -070038/**
39 * Unit tests for {@link UiTopology}.
40 */
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070041public class UiTopologyTest extends AbstractUiModelTest {
Simon Huntc0f20c12016-05-09 09:30:20 -070042
Simon Huntc13082f2016-08-03 21:20:23 -070043 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 Huntc0f20c12016-05-09 09:30:20 -070073 private UiTopology topo;
Simon Huntc13082f2016-08-03 21:20:23 -070074 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() {
Thomas Vachuskab877a6f2017-04-14 11:43:30 -070082 topo = new UiTopology(MOCK_SERVICES);
Simon Huntc13082f2016-08-03 21:20:23 -070083 devLink = new UiDeviceLink(null, DX1_DY2);
84 devLink.attachBackingLink(LINK_X1_TO_Y2);
85 }
Simon Huntc0f20c12016-05-09 09:30:20 -070086
87 @Test
88 public void basic() {
89 title("basic");
Simon Huntc0f20c12016-05-09 09:30:20 -070090 print(topo);
91 }
Simon Huntc13082f2016-08-03 21:20:23 -070092
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 Huntc0f20c12016-05-09 09:30:20 -0700180}