Simon Hunt | f59d36b | 2016-10-04 19:05:53 -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 | f59d36b | 2016-10-04 19:05:53 -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.net.config.basics; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableSet; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onosproject.net.DeviceId; |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 25 | import org.onosproject.net.config.InvalidFieldException; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 26 | import org.onosproject.net.region.Region; |
Ray Milkey | d29bc1c | 2018-03-13 11:57:11 -0700 | [diff] [blame] | 27 | import org.onosproject.ui.topo.LayoutLocation; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 28 | |
| 29 | import java.util.List; |
| 30 | import java.util.Set; |
| 31 | |
Ray Milkey | d29bc1c | 2018-03-13 11:57:11 -0700 | [diff] [blame] | 32 | import static org.hamcrest.MatcherAssert.assertThat; |
| 33 | import static org.hamcrest.Matchers.hasSize; |
| 34 | import static org.hamcrest.Matchers.is; |
| 35 | import static org.hamcrest.Matchers.notNullValue; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 36 | import static org.junit.Assert.assertEquals; |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 37 | import static org.junit.Assert.assertNotNull; |
| 38 | import static org.junit.Assert.assertNull; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 39 | import static org.junit.Assert.assertTrue; |
| 40 | import static org.onosproject.net.region.RegionId.regionId; |
| 41 | |
| 42 | /** |
| 43 | * Test class for {@link BasicRegionConfig}. |
| 44 | */ |
| 45 | public class BasicRegionConfigTest extends AbstractConfigTest { |
| 46 | |
| 47 | private static final String REGION_JSON = "configs.regions.1.json"; |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 48 | |
| 49 | private static final String NAME = "name"; |
| 50 | private static final String TYPE = "type"; |
| 51 | private static final String DEVICES = "devices"; |
| 52 | |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 53 | private static final String R1 = "r1"; |
| 54 | private static final String R2 = "r2"; |
| 55 | private static final String R3 = "r3"; |
| 56 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 57 | private static final String EUROPE = "Europe"; |
| 58 | private static final String PARIS = "Paris"; |
| 59 | private static final String AUSTRALIA = "Australia"; |
| 60 | |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 61 | private static final Set<DeviceId> R1_DEVS = |
| 62 | ImmutableSet.of(dstr("01"), dstr("02"), dstr("03")); |
| 63 | private static final Set<DeviceId> R2_DEVS = |
| 64 | ImmutableSet.of(dstr("04"), dstr("05"), dstr("06")); |
| 65 | private static final Set<DeviceId> R3_DEVS = |
| 66 | ImmutableSet.of(dstr("07"), dstr("08"), dstr("09")); |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 67 | private static final Set<DeviceId> ALT_DEVICES = |
| 68 | ImmutableSet.of(dstr("0a"), dstr("0b"), dstr("0c")); |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 69 | |
| 70 | private JsonNode data; |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 71 | private BasicRegionConfig cfg; |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 72 | |
| 73 | @Before |
| 74 | public void setUp() { |
| 75 | data = getTestJson(REGION_JSON); |
| 76 | } |
| 77 | |
| 78 | private JsonNode getR(String key) { |
| 79 | return data.get("regions").get(key).get("basic"); |
| 80 | } |
| 81 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 82 | // loads a region config from the test resource file |
| 83 | private void loadRegion(String rid) { |
| 84 | JsonNode node = getR(rid); |
| 85 | print(JSON_LOADED, node); |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 86 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 87 | cfg = new BasicRegionConfig(); |
| 88 | cfg.init(regionId(rid), rid, node, mapper, delegate); |
| 89 | } |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 90 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 91 | private void checkRegion(String expN, Region.Type expT, Set<DeviceId> expD) { |
| 92 | print(CHECKING_S, cfg); |
| 93 | assertEquals("wrong name", expN, cfg.name()); |
| 94 | assertEquals("wrong type", expT, cfg.type()); |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 95 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 96 | List<DeviceId> devs = cfg.devices(); |
| 97 | if (expD == null) { |
| 98 | assertNull("unexp device list", devs); |
| 99 | } else { |
| 100 | assertNotNull(devs); |
| 101 | assertEquals("wr.size", expD.size(), devs.size()); |
| 102 | for (DeviceId d : expD) { |
| 103 | assertTrue("missing dev: " + d, devs.contains(d)); |
| 104 | } |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | public void region1Config() { |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 110 | loadRegion(R1); |
| 111 | checkRegion(EUROPE, Region.Type.CONTINENT, R1_DEVS); |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void region2Config() { |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 116 | loadRegion(R2); |
| 117 | checkRegion(PARIS, Region.Type.METRO, R2_DEVS); |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void region3Config() { |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 122 | loadRegion(R3); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 123 | checkRegion(R3, null, R3_DEVS); |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 126 | @Test |
| 127 | public void modifyName() { |
| 128 | loadRegion(R1); |
| 129 | cfg.name(AUSTRALIA); |
| 130 | checkRegion(AUSTRALIA, Region.Type.CONTINENT, R1_DEVS); |
| 131 | } |
| 132 | |
| 133 | @Test |
| 134 | public void clearName() { |
| 135 | loadRegion(R1); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 136 | checkRegion(EUROPE, Region.Type.CONTINENT, R1_DEVS); |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 137 | cfg.name(null); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 138 | // if the friendly name is cleared, name() returns the identifier |
| 139 | checkRegion(R1, Region.Type.CONTINENT, R1_DEVS); |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Test |
| 143 | public void modifyType() { |
| 144 | loadRegion(R2); |
| 145 | cfg.type(Region.Type.CAMPUS); |
| 146 | checkRegion(PARIS, Region.Type.CAMPUS, R2_DEVS); |
| 147 | } |
| 148 | |
| 149 | @Test |
| 150 | public void clearType() { |
| 151 | loadRegion(R2); |
| 152 | cfg.type(null); |
| 153 | checkRegion(PARIS, null, R2_DEVS); |
| 154 | } |
| 155 | |
| 156 | @Test |
| 157 | public void modifyDevices() { |
| 158 | loadRegion(R3); |
| 159 | cfg.devices(ALT_DEVICES); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 160 | checkRegion(R3, null, ALT_DEVICES); |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | @Test |
| 164 | public void clearDevices() { |
| 165 | loadRegion(R3); |
| 166 | cfg.devices(null); |
Simon Hunt | 5361221 | 2016-12-04 17:19:52 -0800 | [diff] [blame] | 167 | checkRegion(R3, null, null); |
Simon Hunt | 4f3a407 | 2016-10-17 17:52:11 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | |
| 171 | @Test |
| 172 | public void sampleValidConfig() { |
| 173 | ObjectNode node = new TmpJson() |
| 174 | .props(NAME, TYPE) |
| 175 | .arrays(DEVICES) |
| 176 | .node(); |
| 177 | cfg = new BasicRegionConfig(); |
| 178 | cfg.init(regionId(R1), BASIC, node, mapper, delegate); |
| 179 | |
| 180 | assertTrue("not valid: " + cfg, cfg.isValid()); |
| 181 | } |
| 182 | |
| 183 | @Test(expected = InvalidFieldException.class) |
| 184 | public void sampleInvalidConfig() { |
| 185 | ObjectNode node = new TmpJson() |
| 186 | .props(NAME, TYPE, "foo") |
| 187 | .arrays(DEVICES) |
| 188 | .node(); |
| 189 | cfg = new BasicRegionConfig(); |
| 190 | cfg.init(regionId(R1), BASIC, node, mapper, delegate); |
| 191 | |
| 192 | cfg.isValid(); |
| 193 | } |
Ray Milkey | d29bc1c | 2018-03-13 11:57:11 -0700 | [diff] [blame] | 194 | |
| 195 | @Test |
| 196 | public void testPeerLocMapping() { |
| 197 | String peer1 = "peer1"; |
| 198 | String loc1 = LayoutLocation.Type.GRID.toString(); |
| 199 | double loc1Y = 22.0; |
| 200 | double loc1X = 33.0; |
| 201 | String peer2 = "peer2"; |
| 202 | String loc2 = LayoutLocation.Type.GEO.toString(); |
| 203 | double loc2Y = 222.0; |
| 204 | double loc2X = 333.0; |
| 205 | loadRegion(R2); |
| 206 | cfg.addPeerLocMapping(peer1, loc1, loc1Y, loc1X); |
| 207 | cfg.addPeerLocMapping(peer2, loc2, loc2Y, loc2X); |
| 208 | |
| 209 | List<LayoutLocation> locs = cfg.getMappings(); |
| 210 | |
| 211 | assertThat(locs, hasSize(2)); |
| 212 | |
| 213 | LayoutLocation createdLoc1 = locs.stream().filter(loc -> loc.id().equals(peer1)).findFirst().orElse(null); |
| 214 | LayoutLocation createdLoc2 = locs.stream().filter(loc -> loc.id().equals(peer2)).findFirst().orElse(null); |
| 215 | |
| 216 | assertThat(createdLoc1, notNullValue()); |
| 217 | assertThat(createdLoc2, notNullValue()); |
| 218 | |
| 219 | assertThat(createdLoc1.locType().toString(), is(loc1)); |
| 220 | assertThat(createdLoc1.longOrX(), is(loc1X)); |
| 221 | assertThat(createdLoc1.latOrY(), is(loc1Y)); |
| 222 | |
| 223 | assertThat(createdLoc2.locType().toString(), is(loc2)); |
| 224 | assertThat(createdLoc2.longOrX(), is(loc2X)); |
| 225 | assertThat(createdLoc2.latOrY(), is(loc2Y)); |
| 226 | } |
Simon Hunt | f59d36b | 2016-10-04 19:05:53 -0700 | [diff] [blame] | 227 | } |