Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -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 | package org.onosproject.ui.impl.topo; |
| 18 | |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 19 | import com.fasterxml.jackson.databind.JsonNode; |
Simon Hunt | cf76a65 | 2017-05-12 18:28:24 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ArrayNode; |
| 21 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableList; |
| 23 | import com.google.common.collect.ImmutableSet; |
| 24 | import org.junit.Test; |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 25 | import org.onosproject.net.Annotated; |
| 26 | import org.onosproject.net.Annotations; |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
| 28 | import org.onosproject.net.PortNumber; |
| 29 | import org.onosproject.net.region.RegionId; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 30 | import org.onosproject.ui.impl.AbstractUiImplTest; |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 31 | import org.onosproject.ui.model.topo.UiDeviceLink; |
| 32 | import org.onosproject.ui.model.topo.UiLink; |
| 33 | import org.onosproject.ui.model.topo.UiLinkId; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 34 | import org.onosproject.ui.model.topo.UiNode; |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 35 | import org.onosproject.ui.model.topo.UiRegionLink; |
| 36 | import org.onosproject.ui.model.topo.UiSynthLink; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 37 | |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 38 | import java.util.ArrayList; |
Simon Hunt | cf76a65 | 2017-05-12 18:28:24 -0700 | [diff] [blame] | 39 | import java.util.HashSet; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 40 | import java.util.List; |
| 41 | import java.util.Set; |
| 42 | |
| 43 | import static org.junit.Assert.assertEquals; |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 44 | import static org.junit.Assert.assertNull; |
Simon Hunt | cf76a65 | 2017-05-12 18:28:24 -0700 | [diff] [blame] | 45 | import static org.junit.Assert.assertTrue; |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 46 | import static org.onosproject.net.DeviceId.deviceId; |
| 47 | import static org.onosproject.net.region.RegionId.regionId; |
| 48 | import static org.onosproject.ui.model.topo.UiLinkId.uiLinkId; |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 49 | import static org.onosproject.ui.model.topo.UiNode.LAYER_DEFAULT; |
| 50 | import static org.onosproject.ui.model.topo.UiNode.LAYER_OPTICAL; |
| 51 | import static org.onosproject.ui.model.topo.UiNode.LAYER_PACKET; |
| 52 | |
| 53 | /** |
| 54 | * Unit tests for {@link Topo2ViewMessageHandler}. |
| 55 | */ |
| 56 | public class Topo2JsonifierTest extends AbstractUiImplTest { |
| 57 | |
| 58 | // mock node class for testing |
| 59 | private static class MockNode extends UiNode { |
| 60 | private final String id; |
| 61 | |
| 62 | MockNode(String id, String layer) { |
| 63 | this.id = id; |
| 64 | setLayer(layer); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public String idAsString() { |
| 69 | return id; |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public String toString() { |
| 74 | return id; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | private static final List<String> ALL_TAGS = ImmutableList.of( |
| 79 | LAYER_OPTICAL, LAYER_PACKET, LAYER_DEFAULT |
| 80 | ); |
| 81 | |
| 82 | private static final List<String> PKT_DEF_TAGS = ImmutableList.of( |
| 83 | LAYER_PACKET, LAYER_DEFAULT |
| 84 | ); |
| 85 | |
| 86 | private static final List<String> DEF_TAG_ONLY = ImmutableList.of( |
| 87 | LAYER_DEFAULT |
| 88 | ); |
| 89 | |
| 90 | private static final MockNode NODE_A = new MockNode("A-O", LAYER_OPTICAL); |
| 91 | private static final MockNode NODE_B = new MockNode("B-P", LAYER_PACKET); |
| 92 | private static final MockNode NODE_C = new MockNode("C-O", LAYER_OPTICAL); |
| 93 | private static final MockNode NODE_D = new MockNode("D-D", LAYER_DEFAULT); |
| 94 | private static final MockNode NODE_E = new MockNode("E-P", LAYER_PACKET); |
| 95 | private static final MockNode NODE_F = new MockNode("F-r", "random"); |
| 96 | |
| 97 | private static final Set<MockNode> NODES = ImmutableSet.of( |
| 98 | NODE_A, NODE_B, NODE_C, NODE_D, NODE_E, NODE_F |
| 99 | ); |
| 100 | |
| 101 | private Topo2Jsonifier t2 = new Topo2Jsonifier(); |
| 102 | |
| 103 | @Test |
| 104 | public void threeLayers() { |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 105 | title("threeLayers()"); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 106 | |
| 107 | List<Set<UiNode>> result = t2.splitByLayer(ALL_TAGS, NODES); |
| 108 | print(result); |
| 109 | |
| 110 | assertEquals("wrong split size", 3, result.size()); |
| 111 | Set<UiNode> opt = result.get(0); |
| 112 | Set<UiNode> pkt = result.get(1); |
| 113 | Set<UiNode> def = result.get(2); |
| 114 | |
| 115 | assertEquals("opt bad size", 2, opt.size()); |
| 116 | assertEquals("missing node A", true, opt.contains(NODE_A)); |
| 117 | assertEquals("missing node C", true, opt.contains(NODE_C)); |
| 118 | |
| 119 | assertEquals("pkt bad size", 2, pkt.size()); |
| 120 | assertEquals("missing node B", true, pkt.contains(NODE_B)); |
| 121 | assertEquals("missing node E", true, pkt.contains(NODE_E)); |
| 122 | |
| 123 | assertEquals("def bad size", 2, def.size()); |
| 124 | assertEquals("missing node D", true, def.contains(NODE_D)); |
| 125 | assertEquals("missing node F", true, def.contains(NODE_F)); |
| 126 | } |
| 127 | |
| 128 | @Test |
| 129 | public void twoLayers() { |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 130 | title("twoLayers()"); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 131 | |
| 132 | List<Set<UiNode>> result = t2.splitByLayer(PKT_DEF_TAGS, NODES); |
| 133 | print(result); |
| 134 | |
| 135 | assertEquals("wrong split size", 2, result.size()); |
| 136 | Set<UiNode> pkt = result.get(0); |
| 137 | Set<UiNode> def = result.get(1); |
| 138 | |
| 139 | assertEquals("pkt bad size", 2, pkt.size()); |
| 140 | assertEquals("missing node B", true, pkt.contains(NODE_B)); |
| 141 | assertEquals("missing node E", true, pkt.contains(NODE_E)); |
| 142 | |
| 143 | assertEquals("def bad size", 4, def.size()); |
| 144 | assertEquals("missing node D", true, def.contains(NODE_D)); |
| 145 | assertEquals("missing node F", true, def.contains(NODE_F)); |
| 146 | assertEquals("missing node A", true, def.contains(NODE_A)); |
| 147 | assertEquals("missing node C", true, def.contains(NODE_C)); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void oneLayer() { |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 152 | title("oneLayer()"); |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 153 | |
| 154 | List<Set<UiNode>> result = t2.splitByLayer(DEF_TAG_ONLY, NODES); |
| 155 | print(result); |
| 156 | |
| 157 | assertEquals("wrong split size", 1, result.size()); |
| 158 | Set<UiNode> def = result.get(0); |
| 159 | |
| 160 | assertEquals("def bad size", 6, def.size()); |
| 161 | assertEquals("missing node D", true, def.contains(NODE_D)); |
| 162 | assertEquals("missing node F", true, def.contains(NODE_F)); |
| 163 | assertEquals("missing node A", true, def.contains(NODE_A)); |
| 164 | assertEquals("missing node C", true, def.contains(NODE_C)); |
| 165 | assertEquals("missing node B", true, def.contains(NODE_B)); |
| 166 | assertEquals("missing node E", true, def.contains(NODE_E)); |
| 167 | } |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 168 | |
| 169 | private static final String K1 = "K1"; |
| 170 | private static final String K2 = "K2"; |
| 171 | private static final String K3 = "K3"; |
| 172 | private static final String K4 = "K4"; |
| 173 | |
| 174 | private static final String V1 = "V1"; |
| 175 | private static final String V2 = "V2"; |
| 176 | private static final String V3 = "V3"; |
| 177 | |
| 178 | private static final Annotations ANNOTS = new Annotations() { |
| 179 | @Override |
| 180 | public Set<String> keys() { |
| 181 | return ImmutableSet.of(K1, K2, K3); |
| 182 | } |
| 183 | |
| 184 | @Override |
| 185 | public String value(String key) { |
| 186 | switch (key) { |
| 187 | case K1: |
| 188 | return V1; |
| 189 | case K2: |
| 190 | return V2; |
| 191 | case K3: |
| 192 | return V3; |
| 193 | default: |
| 194 | return null; |
| 195 | } |
| 196 | } |
| 197 | }; |
| 198 | |
| 199 | private static final Annotated THING = () -> ANNOTS; |
| 200 | |
| 201 | private void verifyValues(List<String> vals, String... exp) { |
| 202 | print(vals); |
| 203 | if (exp.length == 0) { |
| 204 | // don't expect any results |
| 205 | assertNull("huh?", vals); |
| 206 | } else { |
| 207 | assertEquals("wrong list len", exp.length, vals.size()); |
| 208 | |
| 209 | for (int i = 0; i < exp.length; i++) { |
| 210 | assertEquals("wrong value " + i, exp[i], vals.get(i)); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | @Test |
| 216 | public void annotValues() { |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 217 | title("annotValues()"); |
Simon Hunt | 6a8cb4f | 2016-08-09 15:08:57 -0700 | [diff] [blame] | 218 | verifyValues(t2.getAnnotValues(THING, K1), V1); |
| 219 | verifyValues(t2.getAnnotValues(THING, K3, K1), V3, V1); |
| 220 | verifyValues(t2.getAnnotValues(THING, K1, K2, K3), V1, V2, V3); |
| 221 | verifyValues(t2.getAnnotValues(THING, K1, K4)); |
| 222 | } |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 223 | |
| 224 | |
| 225 | /* |
| 226 | * Test collation of region links in the following scenario... |
| 227 | * |
| 228 | * Region A Region B Region C |
| 229 | * +.......+ +.......+ +.......+ |
| 230 | * : [1] -------- [3] -------- [5] : |
| 231 | * : | : : | : : | : |
| 232 | * : | : : | : : | : |
| 233 | * : [2] -------- [4] -------- [6] : |
| 234 | * +.......+ +.......+ +.......+ |
| 235 | */ |
| 236 | |
| 237 | private static PortNumber pn(long i) { |
| 238 | return PortNumber.portNumber(i); |
| 239 | } |
| 240 | |
| 241 | private static UiLink rrLink(UiLinkId id) { |
| 242 | if (!id.isRegionRegion()) { |
| 243 | throw new IllegalArgumentException(); |
| 244 | } |
| 245 | return new UiRegionLink(null, id); |
| 246 | } |
| 247 | |
| 248 | private static UiLink ddLink(UiLinkId id) { |
| 249 | if (!id.isDeviceDevice()) { |
| 250 | throw new IllegalArgumentException(); |
| 251 | } |
| 252 | return new UiDeviceLink(null, id); |
| 253 | } |
| 254 | |
| 255 | private static final RegionId REGION_ROOT = regionId("root"); |
| 256 | private static final RegionId REGION_A = regionId("rA"); |
| 257 | private static final RegionId REGION_B = regionId("rB"); |
| 258 | private static final RegionId REGION_C = regionId("rC"); |
| 259 | |
| 260 | private static final DeviceId DEV_1 = deviceId("d1"); |
| 261 | private static final DeviceId DEV_2 = deviceId("d2"); |
| 262 | private static final DeviceId DEV_3 = deviceId("d3"); |
| 263 | private static final DeviceId DEV_4 = deviceId("d4"); |
| 264 | private static final DeviceId DEV_5 = deviceId("d5"); |
| 265 | private static final DeviceId DEV_6 = deviceId("d6"); |
| 266 | |
| 267 | private static final UiLinkId D1_D2 = uiLinkId(DEV_1, pn(2), DEV_2, pn(1)); |
| 268 | private static final UiLinkId D1_D3 = uiLinkId(DEV_1, pn(3), DEV_3, pn(1)); |
| 269 | private static final UiLinkId D2_D4 = uiLinkId(DEV_2, pn(4), DEV_4, pn(2)); |
| 270 | private static final UiLinkId D3_D4 = uiLinkId(DEV_3, pn(4), DEV_4, pn(3)); |
| 271 | private static final UiLinkId D3_D5 = uiLinkId(DEV_3, pn(5), DEV_5, pn(3)); |
| 272 | private static final UiLinkId D4_D6 = uiLinkId(DEV_4, pn(6), DEV_6, pn(4)); |
| 273 | private static final UiLinkId D5_D6 = uiLinkId(DEV_5, pn(6), DEV_6, pn(5)); |
| 274 | |
| 275 | private static final UiLinkId RA_RB = uiLinkId(REGION_A, REGION_B); |
| 276 | private static final UiLinkId RB_RC = uiLinkId(REGION_B, REGION_C); |
| 277 | |
| 278 | private UiSynthLink makeSynth(RegionId container, UiLinkId rr, UiLinkId dd) { |
| 279 | return new UiSynthLink(container, rrLink(rr), ddLink(dd)); |
| 280 | } |
| 281 | |
| 282 | private List<UiSynthLink> createSynthLinks() { |
| 283 | List<UiSynthLink> links = new ArrayList<>(); |
| 284 | links.add(makeSynth(REGION_ROOT, RA_RB, D1_D3)); |
| 285 | links.add(makeSynth(REGION_ROOT, RA_RB, D2_D4)); |
| 286 | links.add(makeSynth(REGION_ROOT, RB_RC, D3_D5)); |
| 287 | links.add(makeSynth(REGION_ROOT, RB_RC, D4_D6)); |
| 288 | return links; |
| 289 | } |
| 290 | |
| 291 | @Test |
| 292 | public void encodeSynthLinks() { |
| 293 | title("encodeSynthLinks()"); |
Simon Hunt | cf76a65 | 2017-05-12 18:28:24 -0700 | [diff] [blame] | 294 | ArrayNode array = (ArrayNode) t2.jsonLinks(createSynthLinks()); |
| 295 | print(array); |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 296 | |
Simon Hunt | cf76a65 | 2017-05-12 18:28:24 -0700 | [diff] [blame] | 297 | assertEquals("wrong size", 2, array.size()); |
| 298 | ObjectNode first = (ObjectNode) array.get(0); |
| 299 | ObjectNode second = (ObjectNode) array.get(1); |
| 300 | |
| 301 | boolean firstIsAB = first.get("id").asText().equals("rA~rB"); |
| 302 | if (firstIsAB) { |
| 303 | validateSynthLinks(first, second); |
| 304 | } else { |
| 305 | validateSynthLinks(second, first); |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | private void validateSynthLinks(ObjectNode ab, ObjectNode bc) { |
| 310 | validateLinkRollup(ab, RA_RB, D1_D3, D2_D4); |
| 311 | validateLinkRollup(bc, RB_RC, D3_D5, D4_D6); |
| 312 | } |
| 313 | |
| 314 | private void validateLinkRollup(ObjectNode link, UiLinkId id, |
| 315 | UiLinkId... expInRollup) { |
| 316 | String actId = link.get("id").asText(); |
| 317 | assertEquals("unexp id", id.toString(), actId); |
| 318 | |
| 319 | Set<String> rollupIds = new HashSet<>(); |
| 320 | ArrayNode rollupArray = (ArrayNode) link.get("rollup"); |
| 321 | |
| 322 | for (JsonNode n : rollupArray) { |
| 323 | ObjectNode o = (ObjectNode) n; |
| 324 | rollupIds.add(o.get("id").asText()); |
| 325 | } |
| 326 | |
| 327 | for (UiLinkId expId : expInRollup) { |
| 328 | assertTrue("missing exp id: " + expId, rollupIds.contains(expId.toString())); |
| 329 | } |
Simon Hunt | bf59db2 | 2017-05-12 13:26:35 -0700 | [diff] [blame] | 330 | } |
Simon Hunt | 977aa05 | 2016-07-20 17:08:29 -0700 | [diff] [blame] | 331 | } |