blob: 19abfbaf89b897edf40d27f20d1226fcfde9194e [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.serializers;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070017
Thomas Vachuskad16ce182014-10-29 17:25:29 -070018import com.google.common.collect.ImmutableList;
19import com.google.common.collect.ImmutableMap;
20import com.google.common.collect.ImmutableSet;
21import com.google.common.testing.EqualsTester;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080022
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070023import org.junit.After;
24import org.junit.Before;
25import org.junit.BeforeClass;
26import org.junit.Test;
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -070027import org.onlab.packet.MplsLabel;
Sho SHIMIZU08ec48f2015-07-27 11:27:26 -070028import org.onlab.packet.VlanId;
Sho SHIMIZU63feca72015-05-07 10:44:25 -070029import org.onlab.util.Bandwidth;
Marc De Leenheerbb382352015-04-23 18:20:34 -070030import org.onlab.util.Frequency;
Brian O'Connorabafb502014-12-02 22:26:20 -080031import org.onosproject.cluster.NodeId;
32import org.onosproject.cluster.RoleInfo;
Ray Milkeyd13a37b2015-06-12 11:55:17 -070033import org.onosproject.core.DefaultApplicationId;
Brian O'Connorabafb502014-12-02 22:26:20 -080034import org.onosproject.core.DefaultGroupId;
35import org.onosproject.mastership.MastershipTerm;
36import org.onosproject.net.Annotations;
Sho SHIMIZU014c33a2015-04-30 11:40:37 -070037import org.onosproject.net.ChannelSpacing;
Toru Furusawa72ee30c2016-01-08 13:29:04 -080038import org.onosproject.net.CltSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080039import org.onosproject.net.ConnectPoint;
40import org.onosproject.net.DefaultAnnotations;
41import org.onosproject.net.DefaultDevice;
42import org.onosproject.net.DefaultLink;
43import org.onosproject.net.DefaultPort;
44import org.onosproject.net.Device;
45import org.onosproject.net.DeviceId;
Sho SHIMIZU014c33a2015-04-30 11:40:37 -070046import org.onosproject.net.GridType;
Brian O'Connorabafb502014-12-02 22:26:20 -080047import org.onosproject.net.HostLocation;
48import org.onosproject.net.Link;
Brian O'Connorabafb502014-12-02 22:26:20 -080049import org.onosproject.net.LinkKey;
Marc De Leenheerbb382352015-04-23 18:20:34 -070050import org.onosproject.net.OchPort;
Sho SHIMIZU0abceb82015-05-04 09:20:53 -070051import org.onosproject.net.OchSignal;
Marc De Leenheerbb382352015-04-23 18:20:34 -070052import org.onosproject.net.OduCltPort;
53import org.onosproject.net.OmsPort;
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +020054import org.onosproject.net.OtuPort;
55import org.onosproject.net.OtuSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080056import org.onosproject.net.PortNumber;
Sho SHIMIZU014c33a2015-04-30 11:40:37 -070057import org.onosproject.net.OduSignalType;
Brian O'Connorabafb502014-12-02 22:26:20 -080058import org.onosproject.net.SparseAnnotations;
Sho SHIMIZU166e61e2015-01-21 18:07:12 -080059import org.onosproject.net.flow.DefaultFlowRule;
60import org.onosproject.net.flow.DefaultTrafficSelector;
61import org.onosproject.net.flow.DefaultTrafficTreatment;
Brian O'Connorabafb502014-12-02 22:26:20 -080062import org.onosproject.net.flow.FlowId;
Sho SHIMIZU166e61e2015-01-21 18:07:12 -080063import org.onosproject.net.flow.FlowRule;
64import org.onosproject.net.flow.FlowRuleBatchEntry;
Brian O'Connorabafb502014-12-02 22:26:20 -080065import org.onosproject.net.intent.IntentId;
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -070066import org.onosproject.net.resource.DiscreteResource;
67import org.onosproject.net.resource.DiscreteResourceSet;
68import org.onosproject.net.resource.MplsCodec;
Sho SHIMIZUe18cb122016-02-22 21:04:56 -080069import org.onosproject.net.resource.ResourceAllocation;
70import org.onosproject.net.resource.Resources;
Brian O'Connorabafb502014-12-02 22:26:20 -080071import org.onosproject.net.provider.ProviderId;
Brian O'Connorabafb502014-12-02 22:26:20 -080072import org.onosproject.net.intent.constraint.AnnotationConstraint;
73import org.onosproject.net.intent.constraint.BandwidthConstraint;
Brian O'Connorabafb502014-12-02 22:26:20 -080074import org.onosproject.net.intent.constraint.LatencyConstraint;
75import org.onosproject.net.intent.constraint.LinkTypeConstraint;
76import org.onosproject.net.intent.constraint.ObstacleConstraint;
77import org.onosproject.net.intent.constraint.WaypointConstraint;
alshabib7911a052014-10-16 17:49:37 -070078import org.onlab.packet.ChassisId;
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -070079import org.onlab.packet.IpAddress;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080080import org.onlab.packet.Ip4Address;
81import org.onlab.packet.Ip6Address;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070082import org.onlab.packet.IpPrefix;
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -080083import org.onlab.packet.Ip4Prefix;
84import org.onlab.packet.Ip6Prefix;
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -070085import org.onlab.packet.MacAddress;
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070086import org.onlab.util.KryoNamespace;
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -070087import org.onosproject.net.resource.VlanCodec;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070088
Thomas Vachuskad16ce182014-10-29 17:25:29 -070089import java.nio.ByteBuffer;
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080090import java.util.Arrays;
Yuta HIGUCHIae1d2102014-11-25 19:26:01 -080091import java.util.Collections;
Sho SHIMIZU977cb602014-11-13 08:40:50 -080092import java.time.Duration;
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -070093import java.util.Set;
94import java.util.stream.Collectors;
95import java.util.stream.IntStream;
Thomas Vachuskad16ce182014-10-29 17:25:29 -070096
97import static java.util.Arrays.asList;
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -080098import static org.junit.Assert.*;
Brian O'Connorabafb502014-12-02 22:26:20 -080099import static org.onosproject.net.DeviceId.deviceId;
100import static org.onosproject.net.PortNumber.portNumber;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700101
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700102public class KryoSerializerTest {
103
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700104 private static final ProviderId PID = new ProviderId("of", "foo");
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700105 private static final ProviderId PIDA = new ProviderId("of", "foo", true);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700106 private static final DeviceId DID1 = deviceId("of:foo");
107 private static final DeviceId DID2 = deviceId("of:bar");
108 private static final PortNumber P1 = portNumber(1);
109 private static final PortNumber P2 = portNumber(2);
110 private static final ConnectPoint CP1 = new ConnectPoint(DID1, P1);
111 private static final ConnectPoint CP2 = new ConnectPoint(DID2, P2);
112 private static final String MFR = "whitebox";
113 private static final String HW = "1.1.x";
114 private static final String SW1 = "3.8.1";
115 private static final String SW2 = "3.9.5";
116 private static final String SN = "43311-12345";
alshabib7911a052014-10-16 17:49:37 -0700117 private static final ChassisId CID = new ChassisId();
118 private static final Device DEV1 = new DefaultDevice(PID, DID1, Device.Type.SWITCH, MFR, HW,
119 SW1, SN, CID);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700120 private static final SparseAnnotations A1 = DefaultAnnotations.builder()
121 .set("A1", "a1")
122 .set("B1", "b1")
123 .build();
124 private static final SparseAnnotations A1_2 = DefaultAnnotations.builder()
125 .remove("A1")
126 .set("B3", "b3")
127 .build();
Sho SHIMIZU0abceb82015-05-04 09:20:53 -0700128 private static final OchSignal OCH_SIGNAL1 = (OchSignal) org.onosproject.net.Lambda.ochSignal(
129 GridType.DWDM, ChannelSpacing.CHL_100GHZ, -8, 4);
Sho SHIMIZU08ec48f2015-07-27 11:27:26 -0700130 private static final VlanId VLAN1 = VlanId.vlanId((short) 100);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700131
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700132 private KryoSerializer serializer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700133
134 @BeforeClass
135 public static void setUpBeforeClass() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700136 }
137
138 @Before
139 public void setUp() throws Exception {
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700140 serializer = new KryoSerializer() {
141
142 @Override
143 protected void setupKryoPool() {
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700144 serializerPool = KryoNamespace.newBuilder()
145 .register(KryoNamespaces.API)
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800146 .nextId(KryoNamespaces.BEGIN_USER_CUSTOM_ID)
147 .build();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700148 }
149 };
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700150 }
151
152 @After
153 public void tearDown() throws Exception {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700154 }
155
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800156 private <T> void testSerializedEquals(T original) {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700157 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700158 serializer.encode(original, buffer);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700159 buffer.flip();
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700160 T copy = serializer.decode(buffer);
161
162 T copy2 = serializer.decode(serializer.encode(original));
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700163
164 new EqualsTester()
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700165 .addEqualityGroup(original, copy, copy2)
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700166 .testEquals();
167 }
168
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800169 private <T> void testSerializable(T original) {
170 byte[] bs = serializer.encode(original);
171 T copy = serializer.decode(bs);
172 assertNotNull(copy);
173 }
174
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700175
176 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700177 public void testConnectPoint() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800178 testSerializedEquals(new ConnectPoint(DID1, P1));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700179 }
180
181 @Test
182 public void testDefaultLink() {
Ray Milkey2693bda2016-01-22 16:08:14 -0800183 testSerializedEquals(DefaultLink.builder()
184 .providerId(PID)
185 .src(CP1)
186 .dst(CP2)
187 .type(Link.Type.DIRECT)
188 .build());
189 testSerializedEquals(DefaultLink.builder()
190 .providerId(PID)
191 .src(CP1)
192 .dst(CP2)
193 .type(Link.Type.DIRECT)
194 .annotations(A1)
195 .build());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700196 }
197
198 @Test
199 public void testDefaultPort() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800200 testSerializedEquals(new DefaultPort(DEV1, P1, true));
201 testSerializedEquals(new DefaultPort(DEV1, P1, true, A1_2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700202 }
203
204 @Test
Marc De Leenheerbb382352015-04-23 18:20:34 -0700205 public void testOmsPort() {
206 testSerializedEquals(new OmsPort(DEV1, P1, true, Frequency.ofGHz(190_100), Frequency.ofGHz(197_300),
207 Frequency.ofGHz(100)));
208 testSerializedEquals(new OmsPort(DEV1, P1, true, Frequency.ofGHz(190_100), Frequency.ofGHz(197_300),
209 Frequency.ofGHz(100), A1_2));
210 }
211
212 @Test
213 public void testOchPort() {
Sho SHIMIZU0abceb82015-05-04 09:20:53 -0700214 testSerializedEquals(new OchPort(DEV1, P1, true, OduSignalType.ODU0, false, OCH_SIGNAL1));
215 testSerializedEquals(new OchPort(DEV1, P1, true, OduSignalType.ODU0, false, OCH_SIGNAL1, A1_2));
Marc De Leenheerbb382352015-04-23 18:20:34 -0700216 }
217
218 @Test
219 public void testOduCltPort() {
Toru Furusawa72ee30c2016-01-08 13:29:04 -0800220 testSerializedEquals(new OduCltPort(DEV1, P1, true, CltSignalType.CLT_10GBE));
221 testSerializedEquals(new OduCltPort(DEV1, P1, true, CltSignalType.CLT_10GBE, A1_2));
Marc De Leenheerbb382352015-04-23 18:20:34 -0700222 }
223
224 @Test
Rimon Ashkenazy8ebfff02016-02-01 11:56:36 +0200225 public void testOtuPort() {
226 testSerializedEquals(new OtuPort(DEV1, P1, true, OtuSignalType.OTU2));
227 testSerializedEquals(new OtuPort(DEV1, P1, true, OtuSignalType.OTU2, A1_2));
228 }
229 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700230 public void testDeviceId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800231 testSerializedEquals(DID1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700232 }
233
234 @Test
235 public void testImmutableMap() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800236 testSerializedEquals(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
237 testSerializedEquals(ImmutableMap.of(DID1, DEV1));
238 testSerializedEquals(ImmutableMap.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700239 }
240
241 @Test
242 public void testImmutableSet() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800243 testSerializedEquals(ImmutableSet.of(DID1, DID2));
244 testSerializedEquals(ImmutableSet.of(DID1));
245 testSerializedEquals(ImmutableSet.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700246 }
247
248 @Test
249 public void testImmutableList() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800250 testSerializedEquals(ImmutableList.of(DID1, DID2));
251 testSerializedEquals(ImmutableList.of(DID1));
252 testSerializedEquals(ImmutableList.of());
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700253 }
254
255 @Test
Sho SHIMIZU166e61e2015-01-21 18:07:12 -0800256 public void testFlowRuleBatchEntry() {
257 final FlowRule rule1 =
Ray Milkeyd13a37b2015-06-12 11:55:17 -0700258 DefaultFlowRule.builder()
259 .forDevice(DID1)
260 .withSelector(DefaultTrafficSelector.emptySelector())
261 .withTreatment(DefaultTrafficTreatment.emptyTreatment())
262 .withPriority(0)
263 .fromApp(new DefaultApplicationId(1, "1"))
264 .makeTemporary(1)
265 .build();
266
Sho SHIMIZU166e61e2015-01-21 18:07:12 -0800267 final FlowRuleBatchEntry entry1 =
268 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1);
269 final FlowRuleBatchEntry entry2 =
Sho SHIMIZU7446da92015-01-22 16:27:06 -0800270 new FlowRuleBatchEntry(FlowRuleBatchEntry.FlowRuleOperation.ADD, rule1, 100L);
Sho SHIMIZU166e61e2015-01-21 18:07:12 -0800271
Sho SHIMIZU7446da92015-01-22 16:27:06 -0800272 testSerializedEquals(entry1);
273 testSerializedEquals(entry2);
Sho SHIMIZU166e61e2015-01-21 18:07:12 -0800274 }
275
276 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700277 public void testIpPrefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800278 testSerializedEquals(IpPrefix.valueOf("192.168.0.1/24"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700279 }
280
281 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800282 public void testIp4Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800283 testSerializedEquals(Ip4Prefix.valueOf("192.168.0.1/24"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800284 }
285
286 @Test
287 public void testIp6Prefix() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800288 testSerializedEquals(Ip6Prefix.valueOf("1111:2222::/120"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800289 }
290
291 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700292 public void testIpAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800293 testSerializedEquals(IpAddress.valueOf("192.168.0.1"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700294 }
295
296 @Test
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800297 public void testIp4Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800298 testSerializedEquals(Ip4Address.valueOf("192.168.0.1"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800299 }
300
301 @Test
302 public void testIp6Address() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800303 testSerializedEquals(Ip6Address.valueOf("1111:2222::"));
Pavlin Radoslavov17378ef2014-11-05 16:13:47 -0800304 }
305
306 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700307 public void testMacAddress() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800308 testSerializedEquals(MacAddress.valueOf("12:34:56:78:90:ab"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700309 }
310
311 @Test
312 public void testLinkKey() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800313 testSerializedEquals(LinkKey.linkKey(CP1, CP2));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700314 }
315
316 @Test
317 public void testNodeId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800318 testSerializedEquals(new NodeId("SomeNodeIdentifier"));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700319 }
320
321 @Test
322 public void testPortNumber() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800323 testSerializedEquals(P1);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700324 }
325
326 @Test
327 public void testProviderId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800328 testSerializedEquals(PID);
329 testSerializedEquals(PIDA);
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700330 }
331
332 @Test
333 public void testMastershipTerm() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800334 testSerializedEquals(MastershipTerm.of(new NodeId("foo"), 2));
335 testSerializedEquals(MastershipTerm.of(null, 0));
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700336 }
337
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700338 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700339 public void testHostLocation() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800340 testSerializedEquals(new HostLocation(CP1, 1234L));
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700341 }
342
343 @Test
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700344 public void testFlowId() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800345 testSerializedEquals(FlowId.valueOf(0x12345678L));
Yuta HIGUCHI2fcfde92014-10-20 18:37:14 -0700346 }
347
348 @Test
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700349 public void testRoleInfo() {
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800350 testSerializedEquals(new RoleInfo(new NodeId("master"),
Yuta HIGUCHI40d01772014-10-21 00:08:44 -0700351 asList(new NodeId("stby1"), new NodeId("stby2"))));
352 }
353
354 @Test
Sho SHIMIZU91210a72015-04-29 12:54:28 -0700355 public void testOchSignal() {
356 testSerializedEquals(org.onosproject.net.Lambda.ochSignal(
357 GridType.DWDM, ChannelSpacing.CHL_100GHZ, 1, 1
358 ));
359 }
360
361 @Test
Sho SHIMIZU8fa670a2016-01-14 11:17:18 -0800362 public void testResource() {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800363 testSerializedEquals(Resources.discrete(DID1, P1, VLAN1).resource());
Sho SHIMIZU08ec48f2015-07-27 11:27:26 -0700364 }
365
366 @Test
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -0700367 public void testVlanIdResourceSet() {
368 DiscreteResource port = Resources.discrete(DID1, P1).resource();
369
370 Set<DiscreteResource> vlans = IntStream.range(0, 4096)
371 .mapToObj(x -> VlanId.vlanId((short) x))
372 .map(x -> Resources.discrete(port.id(), x).resource())
373 .collect(Collectors.toSet());
374
375 DiscreteResourceSet sut = DiscreteResourceSet.of(vlans, new VlanCodec());
376 testSerializedEquals(sut);
377 }
378
379 @Test
380 public void testMplsLabelResourceSet() {
381 DiscreteResource port = Resources.discrete(DID1, P1).resource();
382
Jonathan Hart16d6c4c2016-05-09 09:14:39 -0700383 Set<DiscreteResource> labels = IntStream.range(0, 1024)
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -0700384 .mapToObj(MplsLabel::mplsLabel)
385 .map(x -> Resources.discrete(port.id(), x).resource())
386 .collect(Collectors.toSet());
387
388 DiscreteResourceSet sut = DiscreteResourceSet.of(labels, new MplsCodec());
389 testSerializedEquals(sut);
390 }
391
392 @Test
393 public void testEmptyResourceSet() {
394 DiscreteResourceSet sut = DiscreteResourceSet.empty();
395 testSerializedEquals(sut);
396 }
397
398 @Test
Sho SHIMIZU8d5ff162016-01-22 12:56:31 -0800399 public void testResourceId() {
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800400 testSerializedEquals(Resources.discrete(DID1, P1).id());
Sho SHIMIZU7e6d18e2016-01-07 18:44:33 -0800401 }
402
403 @Test
Sho SHIMIZU1f5e5912015-08-10 17:00:00 -0700404 public void testResourceAllocation() {
Sho SHIMIZUe18cb122016-02-22 21:04:56 -0800405 testSerializedEquals(new ResourceAllocation(
Sho SHIMIZU460b9722016-01-28 10:48:26 -0800406 Resources.discrete(DID1, P1, VLAN1).resource(),
Sho SHIMIZU08ec48f2015-07-27 11:27:26 -0700407 IntentId.valueOf(30)));
408 }
Sho SHIMIZUdee78712015-04-30 09:33:52 -0700409
410 @Test
411 public void testFrequency() {
412 testSerializedEquals(Frequency.ofGHz(100));
413 }
414
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800415 @Test
Sho SHIMIZU81697792015-05-08 11:09:38 -0700416 public void testBandwidth() {
Sho SHIMIZU31f37ed2016-01-08 18:45:54 -0800417 testSerializedEquals(Bandwidth.mbps(1000));
Sho SHIMIZU63feca72015-05-07 10:44:25 -0700418 testSerializedEquals(Bandwidth.mbps(1000.0));
Sho SHIMIZU81697792015-05-08 11:09:38 -0700419 }
420
421 @Test
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800422 public void testBandwidthConstraint() {
Sho SHIMIZUa88db492015-11-23 13:21:04 -0800423 testSerializable(new BandwidthConstraint(Bandwidth.bps(1000.0)));
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800424 }
425
426 @Test
427 public void testLinkTypeConstraint() {
428 testSerializable(new LinkTypeConstraint(true, Link.Type.DIRECT));
429 }
430
431 @Test
432 public void testLatencyConstraint() {
433 testSerializable(new LatencyConstraint(Duration.ofSeconds(10)));
434 }
435
436 @Test
437 public void testWaypointConstraint() {
438 testSerializable(new WaypointConstraint(deviceId("of:1"), deviceId("of:2")));
439 }
440
441 @Test
442 public void testObstacleConstraint() {
443 testSerializable(new ObstacleConstraint(deviceId("of:1"), deviceId("of:2")));
444 }
445
446 @Test
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800447 public void testArraysAsList() {
448 testSerializedEquals(Arrays.asList(1, 2, 3));
449 }
450
451 @Test
Sho SHIMIZU977cb602014-11-13 08:40:50 -0800452 public void testAnnotationConstraint() {
453 testSerializable(new AnnotationConstraint("distance", 100.0));
454 }
Yuta HIGUCHIadac04a2014-11-13 00:02:45 -0800455
456 @Test
Yuta HIGUCHIf6171232014-11-25 15:14:44 -0800457 public void testDefaultGroupId() {
458 testSerializedEquals(new DefaultGroupId(99));
459 }
460
Yuta HIGUCHIae1d2102014-11-25 19:26:01 -0800461 @Test
462 public void testEmptySet() {
463 testSerializedEquals(Collections.emptySet());
464 }
Yuta HIGUCHIf6171232014-11-25 15:14:44 -0800465
466 @Test
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700467 public void testAnnotations() {
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700468 // Annotations does not have equals defined, manually test equality
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700469 final byte[] a1Bytes = serializer.encode(A1);
470 SparseAnnotations copiedA1 = serializer.decode(a1Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700471 assertAnnotationsEquals(copiedA1, A1);
472
Yuta HIGUCHIaaac0d72014-10-19 17:57:47 -0700473 final byte[] a12Bytes = serializer.encode(A1_2);
474 SparseAnnotations copiedA12 = serializer.decode(a12Bytes);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700475 assertAnnotationsEquals(copiedA12, A1_2);
476 }
477
478 // code clone
Sho SHIMIZU7d4f2f72016-04-06 16:42:29 -0700479 private static void assertAnnotationsEquals(Annotations actual, SparseAnnotations... annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700480 SparseAnnotations expected = DefaultAnnotations.builder().build();
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700481 for (SparseAnnotations a : annotations) {
Yuta HIGUCHI9ee0d5b2014-10-05 00:03:47 -0700482 expected = DefaultAnnotations.union(expected, a);
Yuta HIGUCHI5bdebe32014-10-04 21:40:41 -0700483 }
484 assertEquals(expected.keys(), actual.keys());
485 for (String key : expected.keys()) {
486 assertEquals(expected.value(key), actual.value(key));
487 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700488 }
489
490}