blob: c1ff3de7240b78ce61b1e82372f97dd91d13b4eb [file] [log] [blame]
Simon Hunt977aa052016-07-20 17:08:29 -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.impl.topo;
18
Simon Huntbf59db22017-05-12 13:26:35 -070019import com.fasterxml.jackson.databind.JsonNode;
Simon Hunt977aa052016-07-20 17:08:29 -070020import com.google.common.collect.ImmutableList;
21import com.google.common.collect.ImmutableSet;
22import org.junit.Test;
Simon Hunt6a8cb4f2016-08-09 15:08:57 -070023import org.onosproject.net.Annotated;
24import org.onosproject.net.Annotations;
Simon Huntbf59db22017-05-12 13:26:35 -070025import org.onosproject.net.DeviceId;
26import org.onosproject.net.PortNumber;
27import org.onosproject.net.region.RegionId;
Simon Hunt977aa052016-07-20 17:08:29 -070028import org.onosproject.ui.impl.AbstractUiImplTest;
Simon Huntbf59db22017-05-12 13:26:35 -070029import org.onosproject.ui.model.topo.UiDeviceLink;
30import org.onosproject.ui.model.topo.UiLink;
31import org.onosproject.ui.model.topo.UiLinkId;
Simon Hunt977aa052016-07-20 17:08:29 -070032import org.onosproject.ui.model.topo.UiNode;
Simon Huntbf59db22017-05-12 13:26:35 -070033import org.onosproject.ui.model.topo.UiRegionLink;
34import org.onosproject.ui.model.topo.UiSynthLink;
Simon Hunt977aa052016-07-20 17:08:29 -070035
Simon Huntbf59db22017-05-12 13:26:35 -070036import java.util.ArrayList;
Simon Hunt977aa052016-07-20 17:08:29 -070037import java.util.List;
38import java.util.Set;
39
40import static org.junit.Assert.assertEquals;
Simon Hunt6a8cb4f2016-08-09 15:08:57 -070041import static org.junit.Assert.assertNull;
Simon Huntbf59db22017-05-12 13:26:35 -070042import static org.onosproject.net.DeviceId.deviceId;
43import static org.onosproject.net.region.RegionId.regionId;
44import static org.onosproject.ui.model.topo.UiLinkId.uiLinkId;
Simon Hunt977aa052016-07-20 17:08:29 -070045import static org.onosproject.ui.model.topo.UiNode.LAYER_DEFAULT;
46import static org.onosproject.ui.model.topo.UiNode.LAYER_OPTICAL;
47import static org.onosproject.ui.model.topo.UiNode.LAYER_PACKET;
48
49/**
50 * Unit tests for {@link Topo2ViewMessageHandler}.
51 */
52public class Topo2JsonifierTest extends AbstractUiImplTest {
53
54 // mock node class for testing
55 private static class MockNode extends UiNode {
56 private final String id;
57
58 MockNode(String id, String layer) {
59 this.id = id;
60 setLayer(layer);
61 }
62
63 @Override
64 public String idAsString() {
65 return id;
66 }
67
68 @Override
69 public String toString() {
70 return id;
71 }
72 }
73
74 private static final List<String> ALL_TAGS = ImmutableList.of(
75 LAYER_OPTICAL, LAYER_PACKET, LAYER_DEFAULT
76 );
77
78 private static final List<String> PKT_DEF_TAGS = ImmutableList.of(
79 LAYER_PACKET, LAYER_DEFAULT
80 );
81
82 private static final List<String> DEF_TAG_ONLY = ImmutableList.of(
83 LAYER_DEFAULT
84 );
85
86 private static final MockNode NODE_A = new MockNode("A-O", LAYER_OPTICAL);
87 private static final MockNode NODE_B = new MockNode("B-P", LAYER_PACKET);
88 private static final MockNode NODE_C = new MockNode("C-O", LAYER_OPTICAL);
89 private static final MockNode NODE_D = new MockNode("D-D", LAYER_DEFAULT);
90 private static final MockNode NODE_E = new MockNode("E-P", LAYER_PACKET);
91 private static final MockNode NODE_F = new MockNode("F-r", "random");
92
93 private static final Set<MockNode> NODES = ImmutableSet.of(
94 NODE_A, NODE_B, NODE_C, NODE_D, NODE_E, NODE_F
95 );
96
97 private Topo2Jsonifier t2 = new Topo2Jsonifier();
98
99 @Test
100 public void threeLayers() {
Simon Huntbf59db22017-05-12 13:26:35 -0700101 title("threeLayers()");
Simon Hunt977aa052016-07-20 17:08:29 -0700102
103 List<Set<UiNode>> result = t2.splitByLayer(ALL_TAGS, NODES);
104 print(result);
105
106 assertEquals("wrong split size", 3, result.size());
107 Set<UiNode> opt = result.get(0);
108 Set<UiNode> pkt = result.get(1);
109 Set<UiNode> def = result.get(2);
110
111 assertEquals("opt bad size", 2, opt.size());
112 assertEquals("missing node A", true, opt.contains(NODE_A));
113 assertEquals("missing node C", true, opt.contains(NODE_C));
114
115 assertEquals("pkt bad size", 2, pkt.size());
116 assertEquals("missing node B", true, pkt.contains(NODE_B));
117 assertEquals("missing node E", true, pkt.contains(NODE_E));
118
119 assertEquals("def bad size", 2, def.size());
120 assertEquals("missing node D", true, def.contains(NODE_D));
121 assertEquals("missing node F", true, def.contains(NODE_F));
122 }
123
124 @Test
125 public void twoLayers() {
Simon Huntbf59db22017-05-12 13:26:35 -0700126 title("twoLayers()");
Simon Hunt977aa052016-07-20 17:08:29 -0700127
128 List<Set<UiNode>> result = t2.splitByLayer(PKT_DEF_TAGS, NODES);
129 print(result);
130
131 assertEquals("wrong split size", 2, result.size());
132 Set<UiNode> pkt = result.get(0);
133 Set<UiNode> def = result.get(1);
134
135 assertEquals("pkt bad size", 2, pkt.size());
136 assertEquals("missing node B", true, pkt.contains(NODE_B));
137 assertEquals("missing node E", true, pkt.contains(NODE_E));
138
139 assertEquals("def bad size", 4, def.size());
140 assertEquals("missing node D", true, def.contains(NODE_D));
141 assertEquals("missing node F", true, def.contains(NODE_F));
142 assertEquals("missing node A", true, def.contains(NODE_A));
143 assertEquals("missing node C", true, def.contains(NODE_C));
144 }
145
146 @Test
147 public void oneLayer() {
Simon Huntbf59db22017-05-12 13:26:35 -0700148 title("oneLayer()");
Simon Hunt977aa052016-07-20 17:08:29 -0700149
150 List<Set<UiNode>> result = t2.splitByLayer(DEF_TAG_ONLY, NODES);
151 print(result);
152
153 assertEquals("wrong split size", 1, result.size());
154 Set<UiNode> def = result.get(0);
155
156 assertEquals("def bad size", 6, def.size());
157 assertEquals("missing node D", true, def.contains(NODE_D));
158 assertEquals("missing node F", true, def.contains(NODE_F));
159 assertEquals("missing node A", true, def.contains(NODE_A));
160 assertEquals("missing node C", true, def.contains(NODE_C));
161 assertEquals("missing node B", true, def.contains(NODE_B));
162 assertEquals("missing node E", true, def.contains(NODE_E));
163 }
Simon Hunt6a8cb4f2016-08-09 15:08:57 -0700164
165 private static final String K1 = "K1";
166 private static final String K2 = "K2";
167 private static final String K3 = "K3";
168 private static final String K4 = "K4";
169
170 private static final String V1 = "V1";
171 private static final String V2 = "V2";
172 private static final String V3 = "V3";
173
174 private static final Annotations ANNOTS = new Annotations() {
175 @Override
176 public Set<String> keys() {
177 return ImmutableSet.of(K1, K2, K3);
178 }
179
180 @Override
181 public String value(String key) {
182 switch (key) {
183 case K1:
184 return V1;
185 case K2:
186 return V2;
187 case K3:
188 return V3;
189 default:
190 return null;
191 }
192 }
193 };
194
195 private static final Annotated THING = () -> ANNOTS;
196
197 private void verifyValues(List<String> vals, String... exp) {
198 print(vals);
199 if (exp.length == 0) {
200 // don't expect any results
201 assertNull("huh?", vals);
202 } else {
203 assertEquals("wrong list len", exp.length, vals.size());
204
205 for (int i = 0; i < exp.length; i++) {
206 assertEquals("wrong value " + i, exp[i], vals.get(i));
207 }
208 }
209 }
210
211 @Test
212 public void annotValues() {
Simon Huntbf59db22017-05-12 13:26:35 -0700213 title("annotValues()");
Simon Hunt6a8cb4f2016-08-09 15:08:57 -0700214 verifyValues(t2.getAnnotValues(THING, K1), V1);
215 verifyValues(t2.getAnnotValues(THING, K3, K1), V3, V1);
216 verifyValues(t2.getAnnotValues(THING, K1, K2, K3), V1, V2, V3);
217 verifyValues(t2.getAnnotValues(THING, K1, K4));
218 }
Simon Huntbf59db22017-05-12 13:26:35 -0700219
220
221 /*
222 * Test collation of region links in the following scenario...
223 *
224 * Region A Region B Region C
225 * +.......+ +.......+ +.......+
226 * : [1] -------- [3] -------- [5] :
227 * : | : : | : : | :
228 * : | : : | : : | :
229 * : [2] -------- [4] -------- [6] :
230 * +.......+ +.......+ +.......+
231 */
232
233 private static PortNumber pn(long i) {
234 return PortNumber.portNumber(i);
235 }
236
237 private static UiLink rrLink(UiLinkId id) {
238 if (!id.isRegionRegion()) {
239 throw new IllegalArgumentException();
240 }
241 return new UiRegionLink(null, id);
242 }
243
244 private static UiLink ddLink(UiLinkId id) {
245 if (!id.isDeviceDevice()) {
246 throw new IllegalArgumentException();
247 }
248 return new UiDeviceLink(null, id);
249 }
250
251 private static final RegionId REGION_ROOT = regionId("root");
252 private static final RegionId REGION_A = regionId("rA");
253 private static final RegionId REGION_B = regionId("rB");
254 private static final RegionId REGION_C = regionId("rC");
255
256 private static final DeviceId DEV_1 = deviceId("d1");
257 private static final DeviceId DEV_2 = deviceId("d2");
258 private static final DeviceId DEV_3 = deviceId("d3");
259 private static final DeviceId DEV_4 = deviceId("d4");
260 private static final DeviceId DEV_5 = deviceId("d5");
261 private static final DeviceId DEV_6 = deviceId("d6");
262
263 private static final UiLinkId D1_D2 = uiLinkId(DEV_1, pn(2), DEV_2, pn(1));
264 private static final UiLinkId D1_D3 = uiLinkId(DEV_1, pn(3), DEV_3, pn(1));
265 private static final UiLinkId D2_D4 = uiLinkId(DEV_2, pn(4), DEV_4, pn(2));
266 private static final UiLinkId D3_D4 = uiLinkId(DEV_3, pn(4), DEV_4, pn(3));
267 private static final UiLinkId D3_D5 = uiLinkId(DEV_3, pn(5), DEV_5, pn(3));
268 private static final UiLinkId D4_D6 = uiLinkId(DEV_4, pn(6), DEV_6, pn(4));
269 private static final UiLinkId D5_D6 = uiLinkId(DEV_5, pn(6), DEV_6, pn(5));
270
271 private static final UiLinkId RA_RB = uiLinkId(REGION_A, REGION_B);
272 private static final UiLinkId RB_RC = uiLinkId(REGION_B, REGION_C);
273
274 private UiSynthLink makeSynth(RegionId container, UiLinkId rr, UiLinkId dd) {
275 return new UiSynthLink(container, rrLink(rr), ddLink(dd));
276 }
277
278 private List<UiSynthLink> createSynthLinks() {
279 List<UiSynthLink> links = new ArrayList<>();
280 links.add(makeSynth(REGION_ROOT, RA_RB, D1_D3));
281 links.add(makeSynth(REGION_ROOT, RA_RB, D2_D4));
282 links.add(makeSynth(REGION_ROOT, RB_RC, D3_D5));
283 links.add(makeSynth(REGION_ROOT, RB_RC, D4_D6));
284 return links;
285 }
286
287 @Test
288 public void encodeSynthLinks() {
289 title("encodeSynthLinks()");
290 JsonNode node = t2.jsonLinks(createSynthLinks());
291 print(node);
292 // TODO: assert structure of JSON created so we know we got it right
293
294 }
Simon Hunt977aa052016-07-20 17:08:29 -0700295}