blob: 22058fee573d26379e91c3ba3a6869fe0154608b [file] [log] [blame]
Simon Huntf59d36b2016-10-04 19:05:53 -07001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Simon Huntf59d36b2016-10-04 19:05:53 -07003 *
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.net.config.basics;
18
19import com.fasterxml.jackson.databind.JsonNode;
Simon Hunt4f3a4072016-10-17 17:52:11 -070020import com.fasterxml.jackson.databind.node.ObjectNode;
Simon Huntf59d36b2016-10-04 19:05:53 -070021import com.google.common.collect.ImmutableSet;
22import org.junit.Before;
23import org.junit.Test;
24import org.onosproject.net.DeviceId;
Simon Hunt4f3a4072016-10-17 17:52:11 -070025import org.onosproject.net.config.InvalidFieldException;
Simon Huntf59d36b2016-10-04 19:05:53 -070026import org.onosproject.net.region.Region;
Ray Milkeyd29bc1c2018-03-13 11:57:11 -070027import org.onosproject.ui.topo.LayoutLocation;
Simon Huntf59d36b2016-10-04 19:05:53 -070028
29import java.util.List;
30import java.util.Set;
31
Ray Milkeyd29bc1c2018-03-13 11:57:11 -070032import static org.hamcrest.MatcherAssert.assertThat;
33import static org.hamcrest.Matchers.hasSize;
34import static org.hamcrest.Matchers.is;
35import static org.hamcrest.Matchers.notNullValue;
Simon Huntf59d36b2016-10-04 19:05:53 -070036import static org.junit.Assert.assertEquals;
Simon Hunt4f3a4072016-10-17 17:52:11 -070037import static org.junit.Assert.assertNotNull;
38import static org.junit.Assert.assertNull;
Simon Huntf59d36b2016-10-04 19:05:53 -070039import static org.junit.Assert.assertTrue;
40import static org.onosproject.net.region.RegionId.regionId;
41
42/**
43 * Test class for {@link BasicRegionConfig}.
44 */
45public class BasicRegionConfigTest extends AbstractConfigTest {
46
47 private static final String REGION_JSON = "configs.regions.1.json";
Simon Hunt4f3a4072016-10-17 17:52:11 -070048
49 private static final String NAME = "name";
50 private static final String TYPE = "type";
51 private static final String DEVICES = "devices";
52
Simon Huntf59d36b2016-10-04 19:05:53 -070053 private static final String R1 = "r1";
54 private static final String R2 = "r2";
55 private static final String R3 = "r3";
56
Simon Hunt4f3a4072016-10-17 17:52:11 -070057 private static final String EUROPE = "Europe";
58 private static final String PARIS = "Paris";
59 private static final String AUSTRALIA = "Australia";
60
Simon Huntf59d36b2016-10-04 19:05:53 -070061 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 Hunt4f3a4072016-10-17 17:52:11 -070067 private static final Set<DeviceId> ALT_DEVICES =
68 ImmutableSet.of(dstr("0a"), dstr("0b"), dstr("0c"));
Simon Huntf59d36b2016-10-04 19:05:53 -070069
70 private JsonNode data;
Simon Hunt4f3a4072016-10-17 17:52:11 -070071 private BasicRegionConfig cfg;
Simon Huntf59d36b2016-10-04 19:05:53 -070072
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 Hunt4f3a4072016-10-17 17:52:11 -070082 // 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 Huntf59d36b2016-10-04 19:05:53 -070086
Simon Hunt4f3a4072016-10-17 17:52:11 -070087 cfg = new BasicRegionConfig();
88 cfg.init(regionId(rid), rid, node, mapper, delegate);
89 }
Simon Huntf59d36b2016-10-04 19:05:53 -070090
Simon Hunt4f3a4072016-10-17 17:52:11 -070091 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 Huntf59d36b2016-10-04 19:05:53 -070095
Simon Hunt4f3a4072016-10-17 17:52:11 -070096 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 Huntf59d36b2016-10-04 19:05:53 -0700105 }
106 }
107
108 @Test
109 public void region1Config() {
Simon Hunt4f3a4072016-10-17 17:52:11 -0700110 loadRegion(R1);
111 checkRegion(EUROPE, Region.Type.CONTINENT, R1_DEVS);
Simon Huntf59d36b2016-10-04 19:05:53 -0700112 }
113
114 @Test
115 public void region2Config() {
Simon Hunt4f3a4072016-10-17 17:52:11 -0700116 loadRegion(R2);
117 checkRegion(PARIS, Region.Type.METRO, R2_DEVS);
Simon Huntf59d36b2016-10-04 19:05:53 -0700118 }
119
120 @Test
121 public void region3Config() {
Simon Hunt4f3a4072016-10-17 17:52:11 -0700122 loadRegion(R3);
Simon Hunt53612212016-12-04 17:19:52 -0800123 checkRegion(R3, null, R3_DEVS);
Simon Huntf59d36b2016-10-04 19:05:53 -0700124 }
125
Simon Hunt4f3a4072016-10-17 17:52:11 -0700126 @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 Hunt53612212016-12-04 17:19:52 -0800136 checkRegion(EUROPE, Region.Type.CONTINENT, R1_DEVS);
Simon Hunt4f3a4072016-10-17 17:52:11 -0700137 cfg.name(null);
Simon Hunt53612212016-12-04 17:19:52 -0800138 // if the friendly name is cleared, name() returns the identifier
139 checkRegion(R1, Region.Type.CONTINENT, R1_DEVS);
Simon Hunt4f3a4072016-10-17 17:52:11 -0700140 }
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 Hunt53612212016-12-04 17:19:52 -0800160 checkRegion(R3, null, ALT_DEVICES);
Simon Hunt4f3a4072016-10-17 17:52:11 -0700161 }
162
163 @Test
164 public void clearDevices() {
165 loadRegion(R3);
166 cfg.devices(null);
Simon Hunt53612212016-12-04 17:19:52 -0800167 checkRegion(R3, null, null);
Simon Hunt4f3a4072016-10-17 17:52:11 -0700168 }
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 Milkeyd29bc1c2018-03-13 11:57:11 -0700194
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 Huntf59d36b2016-10-04 19:05:53 -0700227}