blob: 7e03dce13ffe42e9fb7920e1b211043a2ecb07e0 [file] [log] [blame]
shivani vaidya530917c2017-07-11 11:27:48 -07001/*
2* Copyright 2017-present Open Networking Foundation
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.incubator.protobuf.services.nb;
18
shivani vaidya530917c2017-07-11 11:27:48 -070019import com.google.common.collect.ImmutableList;
Jian Lic20272e2017-10-20 17:50:01 +090020import com.google.common.collect.ImmutableSet;
21import io.grpc.BindableService;
shivani vaidya530917c2017-07-11 11:27:48 -070022import io.grpc.ManagedChannel;
23import io.grpc.inprocess.InProcessChannelBuilder;
Jian Lic20272e2017-10-20 17:50:01 +090024import org.junit.AfterClass;
25import org.junit.BeforeClass;
26import org.junit.Test;
27import org.onosproject.cluster.NodeId;
28import org.onosproject.grpc.nb.net.region.RegionServiceGrpc;
29import org.onosproject.grpc.nb.net.region.RegionServiceGrpc.RegionServiceBlockingStub;
30import org.onosproject.grpc.nb.net.region.RegionServiceNb;
31import org.onosproject.grpc.net.models.RegionProtoOuterClass;
32import org.onosproject.incubator.protobuf.models.net.RegionProtoTranslator;
shivani vaidya530917c2017-07-11 11:27:48 -070033import org.onosproject.net.DefaultAnnotations;
shivani vaidya530917c2017-07-11 11:27:48 -070034import org.onosproject.net.DefaultDevice;
35import org.onosproject.net.Device;
36import org.onosproject.net.DeviceId;
shivani vaidya530917c2017-07-11 11:27:48 -070037import org.onosproject.net.HostId;
38import org.onosproject.net.provider.ProviderId;
39import org.onosproject.net.region.DefaultRegion;
Jian Lic20272e2017-10-20 17:50:01 +090040import org.onosproject.net.region.Region;
41import org.onosproject.net.region.RegionId;
shivani vaidya530917c2017-07-11 11:27:48 -070042import org.onosproject.net.region.RegionListener;
43import org.onosproject.net.region.RegionService;
44
45import java.io.IOException;
46import java.util.Collections;
47import java.util.HashMap;
48import java.util.HashSet;
49import java.util.List;
50import java.util.Map;
51import java.util.Set;
52
Jian Lic20272e2017-10-20 17:50:01 +090053import static org.junit.Assert.assertTrue;
54import static org.onosproject.cluster.NodeId.nodeId;
55import static org.onosproject.net.DeviceId.deviceId;
56import static org.onosproject.net.HostId.hostId;
shivani vaidya530917c2017-07-11 11:27:48 -070057
58public class GrpcNbRegionServiceTest {
Jian Lic20272e2017-10-20 17:50:01 +090059
60 private static InProcessServer<BindableService> inprocessServer;
shivani vaidya530917c2017-07-11 11:27:48 -070061 private static RegionServiceBlockingStub blockingStub;
Jian Lic20272e2017-10-20 17:50:01 +090062 private static ManagedChannel channel;
shivani vaidya530917c2017-07-11 11:27:48 -070063
64 private static final String C1 = "C1";
65 private static final String C2 = "C2";
66 private static final String C3 = "C3";
67
68 private static final NodeId CNID_1 = nodeId(C1);
69 private static final NodeId CNID_2 = nodeId(C2);
70 private static final NodeId CNID_3 = nodeId(C3);
71
72 private static final String R1 = "R1";
73 private static final String R2 = "R2";
74 private static final String R3 = "R3";
75
76 private static final Set<NodeId> SET_C1 = ImmutableSet.of(CNID_1);
77 private static final Set<NodeId> SET_C2 = ImmutableSet.of(CNID_2);
78 private static final Set<NodeId> SET_C3 = ImmutableSet.of(CNID_3);
79
80 private static final Region REGION_1 =
81 region(R1, Region.Type.METRO, ImmutableList.of(SET_C1, SET_C2));
82 private static final Region REGION_2 =
83 region(R2, Region.Type.CAMPUS, ImmutableList.of(SET_C2, SET_C1));
84 private static final Region REGION_3 =
85 region(R3, Region.Type.CAMPUS, ImmutableList.of(SET_C3, SET_C1));
86
87 private static final Set<Region> REGION_SET =
88 ImmutableSet.of(REGION_1, REGION_2, REGION_3);
89
90 private static final String D1 = "d1";
91 private static final String D2 = "d2";
92 private static final String D3 = "d3";
93 private static final String D4 = "d4";
94 private static final String D5 = "d5";
95 private static final String D6 = "d6";
96 private static final String D7 = "d7";
97 private static final String D8 = "d8";
98 private static final String D9 = "d9";
99
100 private static final String MFR = "Mfr";
101 private static final String HW = "h/w";
102 private static final String SW = "s/w";
103 private static final String SERIAL = "ser123";
104
105 private static final DeviceId DEVID_1 = deviceId(D1);
106 private static final DeviceId DEVID_2 = deviceId(D2);
107 private static final DeviceId DEVID_3 = deviceId(D3);
108 private static final DeviceId DEVID_4 = deviceId(D4);
109 private static final DeviceId DEVID_5 = deviceId(D5);
110 private static final DeviceId DEVID_6 = deviceId(D6);
111 private static final DeviceId DEVID_7 = deviceId(D7);
112 private static final DeviceId DEVID_8 = deviceId(D8);
113 private static final DeviceId DEVID_9 = deviceId(D9);
114
115 private static final Set<DeviceId> DEVS_TRUNK =
116 ImmutableSet.of(DEVID_1, DEVID_2, DEVID_3);
117
118 private static final Set<DeviceId> DEVS_LEFT =
119 ImmutableSet.of(DEVID_4, DEVID_5, DEVID_6);
120
121 private static final Set<DeviceId> DEVS_RIGHT =
122 ImmutableSet.of(DEVID_7, DEVID_8, DEVID_9);
123
124 private static final String[][] HOST_DATA = {
125 {"AA:00:00:00:00:1A/None", R1},
126 {"AA:00:00:00:00:1B/None", R1},
127 {"AA:00:00:00:00:2A/None", R1},
128 {"AA:00:00:00:00:2B/None", R1},
129 {"AA:00:00:00:00:3A/None", R1},
130 {"AA:00:00:00:00:3B/None", R1},
131 {"AA:00:00:00:00:4A/None", R2},
132 {"AA:00:00:00:00:4B/None", R2},
133 {"AA:00:00:00:00:5A/None", R2},
134 {"AA:00:00:00:00:5B/None", R2},
135 {"AA:00:00:00:00:6A/None", R2},
136 {"AA:00:00:00:00:6B/None", R2},
137 {"AA:00:00:00:00:7A/None", R3},
138 {"AA:00:00:00:00:7B/None", R3},
139 {"AA:00:00:00:00:8A/None", R3},
140 {"AA:00:00:00:00:8B/None", R3},
141 {"AA:00:00:00:00:9A/None", R3},
142 {"AA:00:00:00:00:9B/None", R3},
143 };
144
145 private static final Map<HostId, RegionId> HOSTS = new HashMap<>();
146 private static final RegionService MOCK_REGION = new MockRegionService();
147
148 /**
149 * Returns device with given ID.
150 *
151 * @param id device ID
152 * @return device instance
153 */
154 protected static Device device(String id) {
155 return new DefaultDevice(ProviderId.NONE, deviceId(id),
156 Device.Type.SWITCH, MFR, HW, SW, SERIAL, null);
157 }
158
159 /**
160 * Returns a region instance with specified parameters.
161 *
162 * @param id region id
163 * @param type region type
164 * @param masters ordered list of master sets
165 * @return region instance
166 */
167 private static Region region(String id, Region.Type type,
168 List<Set<NodeId>> masters) {
169 return new DefaultRegion(RegionId.regionId(id), "Region-" + id,
170 type, DefaultAnnotations.EMPTY, masters);
171 }
172
173 /**
174 * Creates a map of hostIds corresponding to their regionIds.
175 *
176 */
177 private static void populateHosts() {
178 for (String[] row : HOST_DATA) {
179 HOSTS.put(hostId(row[0]), RegionId.regionId(row[1]));
180 }
181 }
182
183 public GrpcNbRegionServiceTest() {
184 }
185
186 @Test
187 public void testGetRegion() throws InterruptedException {
188
189 RegionServiceNb.getRegionRequest request = RegionServiceNb.getRegionRequest.newBuilder()
190 .setRegionId(R1).build();
191 RegionServiceNb.getRegionReply response;
192
193 try {
194 response = blockingStub.getRegion(request);
195 Region actualRegion = RegionProtoTranslator.translate(response.getRegion());
196 assertTrue(REGION_1.equals(actualRegion));
197 } catch (Exception e) {
198 e.printStackTrace();
199 }
200 }
201
202 @Test
203 public void testGetRegions() throws InterruptedException {
204
205 RegionServiceNb.getRegionsRequest request = RegionServiceNb.getRegionsRequest.newBuilder()
206 .build();
207 RegionServiceNb.getRegionsReply response;
208
209 try {
210 response = blockingStub.getRegions(request);
211 Set<Region> actualRegions = new HashSet<Region>();
212 for (RegionProtoOuterClass.RegionProto region : response.getRegionList()) {
213 actualRegions.add(RegionProtoTranslator.translate(region));
214 }
215 assertTrue(REGION_SET.equals(actualRegions));
216 } catch (Exception e) {
217 e.printStackTrace();
218 }
219 }
220
221 @Test
222 public void testGetRegionForDevice() throws InterruptedException {
223
224 RegionServiceNb.getRegionForDeviceRequest request = RegionServiceNb.getRegionForDeviceRequest.newBuilder()
225 .setDeviceId(D1).build();
226 RegionServiceNb.getRegionForDeviceReply response;
227
228 try {
229 response = blockingStub.getRegionForDevice(request);
230 Region actualRegion = RegionProtoTranslator.translate(response.getRegion());
231 assertTrue(REGION_1.equals(actualRegion));
232 } catch (Exception e) {
233 e.printStackTrace();
234 }
235 }
236
237 @Test
238 public void testGetRegionDevices() throws InterruptedException {
239
240 RegionServiceNb.getRegionDevicesRequest request = RegionServiceNb.getRegionDevicesRequest.newBuilder()
241 .setRegionId(R1).build();
242 RegionServiceNb.getRegionDevicesReply response;
243
244 try {
245 response = blockingStub.getRegionDevices(request);
246 Set<DeviceId> actualDevices = new HashSet<DeviceId>();
247 for (String deviceId : response.getDeviceIdList()) {
248 actualDevices.add(DeviceId.deviceId(deviceId));
249 }
250 assertTrue(DEVS_TRUNK.equals(actualDevices));
251 } catch (Exception e) {
252 e.printStackTrace();
253 }
254 }
255
256 @Test
257 public void testGetRegionHosts() throws InterruptedException {
258 RegionServiceNb.getRegionHostsRequest request = RegionServiceNb.getRegionHostsRequest.newBuilder()
259 .setRegionId(R1).build();
260 RegionServiceNb.getRegionHostsReply response;
261
262 Set<HostId> expectedHosts = new HashSet<HostId>();
263
264 expectedHosts.add(HostId.hostId(HOST_DATA[0][0]));
265 expectedHosts.add(HostId.hostId(HOST_DATA[1][0]));
266 expectedHosts.add(HostId.hostId(HOST_DATA[2][0]));
267 expectedHosts.add(HostId.hostId(HOST_DATA[3][0]));
268 expectedHosts.add(HostId.hostId(HOST_DATA[4][0]));
269 expectedHosts.add(HostId.hostId(HOST_DATA[5][0]));
270
271 Set<HostId> actualHosts = new HashSet<HostId>();
272
273 try {
274 response = blockingStub.getRegionHosts(request);
275 for (String hostId : response.getHostIdList()) {
276 actualHosts.add(HostId.hostId(hostId));
277 }
278 } catch (Exception e) {
279 e.printStackTrace();
280 }
281 assertTrue(expectedHosts.equals(actualHosts));
282 }
283
284 @BeforeClass
285 public static void beforeClass() throws InstantiationException, IllegalAccessException, IOException {
Jian Lic20272e2017-10-20 17:50:01 +0900286 GrpcNbRegionService regionService = new GrpcNbRegionService();
287 regionService.regionService = MOCK_REGION;
288 inprocessServer = regionService.registerInProcessServer();
shivani vaidya530917c2017-07-11 11:27:48 -0700289 inprocessServer.start();
Jian Lic20272e2017-10-20 17:50:01 +0900290
shivani vaidya530917c2017-07-11 11:27:48 -0700291 channel = InProcessChannelBuilder.forName("test").directExecutor()
292 // Channels are secure by default (via SSL/TLS). For the example we disable TLS to avoid
293 // needing certificates.
294 .usePlaintext(true).build();
295 blockingStub = RegionServiceGrpc.newBlockingStub(channel);
296 populateHosts();
297 }
298
299 @AfterClass
300 public static void afterClass() {
301 channel.shutdownNow();
302
303 inprocessServer.stop();
304 }
305
306 private static class MockRegionService implements RegionService {
307
308 private final Map<RegionId, Region> lookup = new HashMap<>();
309
310 MockRegionService() {
311 lookup.put(REGION_1.id(), REGION_1);
312 lookup.put(REGION_2.id(), REGION_2);
313 lookup.put(REGION_3.id(), REGION_3);
314 }
315
316 @Override
317 public Set<Region> getRegions() {
318 return REGION_SET;
319 }
320
321 @Override
322 public Region getRegion(RegionId regionId) {
323 return lookup.get(regionId);
324 }
325
326 @Override
327 public Region getRegionForDevice(DeviceId deviceId) {
328 if (DEVS_TRUNK.contains(deviceId)) {
329 return REGION_1;
330 }
331 if (DEVS_LEFT.contains(deviceId)) {
332 return REGION_2;
333 }
334 if (DEVS_RIGHT.contains(deviceId)) {
335 return REGION_3;
336 }
337 return null;
338 }
339
340 @Override
341 public Set<DeviceId> getRegionDevices(RegionId regionId) {
342 if (REGION_1.id().equals(regionId)) {
343 return DEVS_TRUNK;
344 }
345 if (REGION_2.id().equals(regionId)) {
346 return DEVS_LEFT;
347 }
348 if (REGION_3.id().equals(regionId)) {
349 return DEVS_RIGHT;
350 }
351 return Collections.emptySet();
352 }
353
354 @Override
355 public Set<HostId> getRegionHosts(RegionId regionId) {
356 Set<HostId> hosts = new HashSet<HostId>();
357 for (HostId hostId : HOSTS.keySet()) {
358 if (HOSTS.get(hostId).equals(regionId)) {
359 hosts.add(hostId);
360 }
361 }
362 return hosts;
363 }
364
365 @Override
366 public void addListener(RegionListener listener) {
367
368 }
369
370 @Override
371 public void removeListener(RegionListener listener) {
372
373 }
374 }
375}