Yuta HIGUCHI

Support LinkResource related serialization

- DefaultLinkResourceRequest
- DefaultLinkResourceAllocations

Change-Id: I8ebb32d5cb96c8465a23a9a512d2e8a56b6fce56
...@@ -13,20 +13,21 @@ ...@@ -13,20 +13,21 @@
13 * See the License for the specific language governing permissions and 13 * See the License for the specific language governing permissions and
14 * limitations under the License. 14 * limitations under the License.
15 */ 15 */
16 -package org.onlab.onos.net.resource.impl; 16 +package org.onlab.onos.net.resource;
17 +
18 +import static com.google.common.base.Preconditions.checkNotNull;
17 19
18 import com.google.common.base.MoreObjects; 20 import com.google.common.base.MoreObjects;
21 +import com.google.common.collect.ImmutableMap;
22 +import com.google.common.collect.ImmutableSet;
23 +
19 import org.onlab.onos.net.Link; 24 import org.onlab.onos.net.Link;
20 import org.onlab.onos.net.intent.IntentId; 25 import org.onlab.onos.net.intent.IntentId;
21 -import org.onlab.onos.net.resource.LinkResourceAllocations;
22 -import org.onlab.onos.net.resource.LinkResourceRequest;
23 -import org.onlab.onos.net.resource.ResourceAllocation;
24 -import org.onlab.onos.net.resource.ResourceRequest;
25 -import org.onlab.onos.net.resource.ResourceType;
26 26
27 import java.util.Collection; 27 import java.util.Collection;
28 import java.util.Collections; 28 import java.util.Collections;
29 import java.util.Map; 29 import java.util.Map;
30 +import java.util.Map.Entry;
30 import java.util.Set; 31 import java.util.Set;
31 32
32 /** 33 /**
...@@ -34,6 +35,7 @@ import java.util.Set; ...@@ -34,6 +35,7 @@ import java.util.Set;
34 */ 35 */
35 public class DefaultLinkResourceAllocations implements LinkResourceAllocations { 36 public class DefaultLinkResourceAllocations implements LinkResourceAllocations {
36 private final LinkResourceRequest request; 37 private final LinkResourceRequest request;
38 + // TODO: probably should be using LinkKey instead
37 private final Map<Link, Set<ResourceAllocation>> allocations; 39 private final Map<Link, Set<ResourceAllocation>> allocations;
38 40
39 /** 41 /**
...@@ -42,10 +44,15 @@ public class DefaultLinkResourceAllocations implements LinkResourceAllocations { ...@@ -42,10 +44,15 @@ public class DefaultLinkResourceAllocations implements LinkResourceAllocations {
42 * @param request requested resources 44 * @param request requested resources
43 * @param allocations allocated resources 45 * @param allocations allocated resources
44 */ 46 */
45 - DefaultLinkResourceAllocations(LinkResourceRequest request, 47 + public DefaultLinkResourceAllocations(LinkResourceRequest request,
46 Map<Link, Set<ResourceAllocation>> allocations) { 48 Map<Link, Set<ResourceAllocation>> allocations) {
47 - this.request = request; 49 + this.request = checkNotNull(request);
48 - this.allocations = allocations; 50 + ImmutableMap.Builder<Link, Set<ResourceAllocation>> builder
51 + = ImmutableMap.builder();
52 + for (Entry<Link, Set<ResourceAllocation>> e : allocations.entrySet()) {
53 + builder.put(e.getKey(), ImmutableSet.copyOf(e.getValue()));
54 + }
55 + this.allocations = builder.build();
49 } 56 }
50 57
51 @Override 58 @Override
......
...@@ -36,6 +36,7 @@ import org.onlab.onos.net.Link; ...@@ -36,6 +36,7 @@ import org.onlab.onos.net.Link;
36 import org.onlab.onos.net.intent.IntentId; 36 import org.onlab.onos.net.intent.IntentId;
37 import org.onlab.onos.net.resource.BandwidthResourceAllocation; 37 import org.onlab.onos.net.resource.BandwidthResourceAllocation;
38 import org.onlab.onos.net.resource.BandwidthResourceRequest; 38 import org.onlab.onos.net.resource.BandwidthResourceRequest;
39 +import org.onlab.onos.net.resource.DefaultLinkResourceAllocations;
39 import org.onlab.onos.net.resource.Lambda; 40 import org.onlab.onos.net.resource.Lambda;
40 import org.onlab.onos.net.resource.LambdaResourceAllocation; 41 import org.onlab.onos.net.resource.LambdaResourceAllocation;
41 import org.onlab.onos.net.resource.LambdaResourceRequest; 42 import org.onlab.onos.net.resource.LambdaResourceRequest;
......
...@@ -87,7 +87,13 @@ import org.onlab.onos.net.link.DefaultLinkDescription; ...@@ -87,7 +87,13 @@ import org.onlab.onos.net.link.DefaultLinkDescription;
87 import org.onlab.onos.net.packet.DefaultOutboundPacket; 87 import org.onlab.onos.net.packet.DefaultOutboundPacket;
88 import org.onlab.onos.net.provider.ProviderId; 88 import org.onlab.onos.net.provider.ProviderId;
89 import org.onlab.onos.net.resource.Bandwidth; 89 import org.onlab.onos.net.resource.Bandwidth;
90 +import org.onlab.onos.net.resource.BandwidthResourceAllocation;
91 +import org.onlab.onos.net.resource.BandwidthResourceRequest;
92 +import org.onlab.onos.net.resource.DefaultLinkResourceAllocations;
93 +import org.onlab.onos.net.resource.DefaultLinkResourceRequest;
90 import org.onlab.onos.net.resource.Lambda; 94 import org.onlab.onos.net.resource.Lambda;
95 +import org.onlab.onos.net.resource.LambdaResourceAllocation;
96 +import org.onlab.onos.net.resource.LambdaResourceRequest;
91 import org.onlab.onos.net.resource.LinkResourceRequest; 97 import org.onlab.onos.net.resource.LinkResourceRequest;
92 import org.onlab.onos.store.Timestamp; 98 import org.onlab.onos.store.Timestamp;
93 import org.onlab.packet.ChassisId; 99 import org.onlab.packet.ChassisId;
...@@ -210,8 +216,14 @@ public final class KryoNamespaces { ...@@ -210,8 +216,14 @@ public final class KryoNamespaces {
210 OpticalConnectivityIntent.class, 216 OpticalConnectivityIntent.class,
211 OpticalPathIntent.class, 217 OpticalPathIntent.class,
212 LinkResourceRequest.class, 218 LinkResourceRequest.class,
219 + DefaultLinkResourceRequest.class,
220 + BandwidthResourceRequest.class,
221 + LambdaResourceRequest.class,
213 Lambda.class, 222 Lambda.class,
214 Bandwidth.class, 223 Bandwidth.class,
224 + DefaultLinkResourceAllocations.class,
225 + BandwidthResourceAllocation.class,
226 + LambdaResourceAllocation.class,
215 // Constraints 227 // Constraints
216 LambdaConstraint.class, 228 LambdaConstraint.class,
217 BandwidthConstraint.class, 229 BandwidthConstraint.class,
......
...@@ -19,6 +19,7 @@ import com.google.common.collect.ImmutableList; ...@@ -19,6 +19,7 @@ import com.google.common.collect.ImmutableList;
19 import com.google.common.collect.ImmutableMap; 19 import com.google.common.collect.ImmutableMap;
20 import com.google.common.collect.ImmutableSet; 20 import com.google.common.collect.ImmutableSet;
21 import com.google.common.testing.EqualsTester; 21 import com.google.common.testing.EqualsTester;
22 +
22 import org.junit.After; 23 import org.junit.After;
23 import org.junit.Before; 24 import org.junit.Before;
24 import org.junit.BeforeClass; 25 import org.junit.BeforeClass;
...@@ -36,11 +37,21 @@ import org.onlab.onos.net.Device; ...@@ -36,11 +37,21 @@ import org.onlab.onos.net.Device;
36 import org.onlab.onos.net.DeviceId; 37 import org.onlab.onos.net.DeviceId;
37 import org.onlab.onos.net.HostLocation; 38 import org.onlab.onos.net.HostLocation;
38 import org.onlab.onos.net.Link; 39 import org.onlab.onos.net.Link;
40 +import org.onlab.onos.net.Link.Type;
39 import org.onlab.onos.net.LinkKey; 41 import org.onlab.onos.net.LinkKey;
40 import org.onlab.onos.net.PortNumber; 42 import org.onlab.onos.net.PortNumber;
41 import org.onlab.onos.net.SparseAnnotations; 43 import org.onlab.onos.net.SparseAnnotations;
42 import org.onlab.onos.net.flow.FlowId; 44 import org.onlab.onos.net.flow.FlowId;
45 +import org.onlab.onos.net.intent.IntentId;
43 import org.onlab.onos.net.provider.ProviderId; 46 import org.onlab.onos.net.provider.ProviderId;
47 +import org.onlab.onos.net.resource.Bandwidth;
48 +import org.onlab.onos.net.resource.BandwidthResourceAllocation;
49 +import org.onlab.onos.net.resource.DefaultLinkResourceAllocations;
50 +import org.onlab.onos.net.resource.DefaultLinkResourceRequest;
51 +import org.onlab.onos.net.resource.Lambda;
52 +import org.onlab.onos.net.resource.LambdaResourceAllocation;
53 +import org.onlab.onos.net.resource.LinkResourceRequest;
54 +import org.onlab.onos.net.resource.ResourceAllocation;
44 import org.onlab.packet.ChassisId; 55 import org.onlab.packet.ChassisId;
45 import org.onlab.packet.IpAddress; 56 import org.onlab.packet.IpAddress;
46 import org.onlab.packet.Ip4Address; 57 import org.onlab.packet.Ip4Address;
...@@ -52,9 +63,12 @@ import org.onlab.packet.MacAddress; ...@@ -52,9 +63,12 @@ import org.onlab.packet.MacAddress;
52 import org.onlab.util.KryoNamespace; 63 import org.onlab.util.KryoNamespace;
53 64
54 import java.nio.ByteBuffer; 65 import java.nio.ByteBuffer;
66 +import java.util.HashMap;
67 +import java.util.Map;
68 +import java.util.Set;
55 69
56 import static java.util.Arrays.asList; 70 import static java.util.Arrays.asList;
57 -import static org.junit.Assert.assertEquals; 71 +import static org.junit.Assert.*;
58 import static org.onlab.onos.net.DeviceId.deviceId; 72 import static org.onlab.onos.net.DeviceId.deviceId;
59 import static org.onlab.onos.net.PortNumber.portNumber; 73 import static org.onlab.onos.net.PortNumber.portNumber;
60 74
...@@ -109,7 +123,7 @@ public class KryoSerializerTest { ...@@ -109,7 +123,7 @@ public class KryoSerializerTest {
109 public void tearDown() throws Exception { 123 public void tearDown() throws Exception {
110 } 124 }
111 125
112 - private <T> void testSerialized(T original) { 126 + private <T> void testSerializedEquals(T original) {
113 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024); 127 ByteBuffer buffer = ByteBuffer.allocate(1 * 1024 * 1024);
114 serializer.encode(original, buffer); 128 serializer.encode(original, buffer);
115 buffer.flip(); 129 buffer.flip();
...@@ -122,129 +136,159 @@ public class KryoSerializerTest { ...@@ -122,129 +136,159 @@ public class KryoSerializerTest {
122 .testEquals(); 136 .testEquals();
123 } 137 }
124 138
139 + private <T> void testSerializable(T original) {
140 + byte[] bs = serializer.encode(original);
141 + T copy = serializer.decode(bs);
142 + assertNotNull(copy);
143 + }
144 +
125 145
126 @Test 146 @Test
127 public void testConnectPoint() { 147 public void testConnectPoint() {
128 - testSerialized(new ConnectPoint(DID1, P1)); 148 + testSerializedEquals(new ConnectPoint(DID1, P1));
129 } 149 }
130 150
131 @Test 151 @Test
132 public void testDefaultLink() { 152 public void testDefaultLink() {
133 - testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT)); 153 + testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT));
134 - testSerialized(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1)); 154 + testSerializedEquals(new DefaultLink(PID, CP1, CP2, Link.Type.DIRECT, A1));
135 } 155 }
136 156
137 @Test 157 @Test
138 public void testDefaultPort() { 158 public void testDefaultPort() {
139 - testSerialized(new DefaultPort(DEV1, P1, true)); 159 + testSerializedEquals(new DefaultPort(DEV1, P1, true));
140 - testSerialized(new DefaultPort(DEV1, P1, true, A1_2)); 160 + testSerializedEquals(new DefaultPort(DEV1, P1, true, A1_2));
141 } 161 }
142 162
143 @Test 163 @Test
144 public void testDeviceId() { 164 public void testDeviceId() {
145 - testSerialized(DID1); 165 + testSerializedEquals(DID1);
146 } 166 }
147 167
148 @Test 168 @Test
149 public void testImmutableMap() { 169 public void testImmutableMap() {
150 - testSerialized(ImmutableMap.of(DID1, DEV1, DID2, DEV1)); 170 + testSerializedEquals(ImmutableMap.of(DID1, DEV1, DID2, DEV1));
151 - testSerialized(ImmutableMap.of(DID1, DEV1)); 171 + testSerializedEquals(ImmutableMap.of(DID1, DEV1));
152 - testSerialized(ImmutableMap.of()); 172 + testSerializedEquals(ImmutableMap.of());
153 } 173 }
154 174
155 @Test 175 @Test
156 public void testImmutableSet() { 176 public void testImmutableSet() {
157 - testSerialized(ImmutableSet.of(DID1, DID2)); 177 + testSerializedEquals(ImmutableSet.of(DID1, DID2));
158 - testSerialized(ImmutableSet.of(DID1)); 178 + testSerializedEquals(ImmutableSet.of(DID1));
159 - testSerialized(ImmutableSet.of()); 179 + testSerializedEquals(ImmutableSet.of());
160 } 180 }
161 181
162 @Test 182 @Test
163 public void testImmutableList() { 183 public void testImmutableList() {
164 - testSerialized(ImmutableList.of(DID1, DID2)); 184 + testSerializedEquals(ImmutableList.of(DID1, DID2));
165 - testSerialized(ImmutableList.of(DID1)); 185 + testSerializedEquals(ImmutableList.of(DID1));
166 - testSerialized(ImmutableList.of()); 186 + testSerializedEquals(ImmutableList.of());
167 } 187 }
168 188
169 @Test 189 @Test
170 public void testIpPrefix() { 190 public void testIpPrefix() {
171 - testSerialized(IpPrefix.valueOf("192.168.0.1/24")); 191 + testSerializedEquals(IpPrefix.valueOf("192.168.0.1/24"));
172 } 192 }
173 193
174 @Test 194 @Test
175 public void testIp4Prefix() { 195 public void testIp4Prefix() {
176 - testSerialized(Ip4Prefix.valueOf("192.168.0.1/24")); 196 + testSerializedEquals(Ip4Prefix.valueOf("192.168.0.1/24"));
177 } 197 }
178 198
179 @Test 199 @Test
180 public void testIp6Prefix() { 200 public void testIp6Prefix() {
181 - testSerialized(Ip6Prefix.valueOf("1111:2222::/120")); 201 + testSerializedEquals(Ip6Prefix.valueOf("1111:2222::/120"));
182 } 202 }
183 203
184 @Test 204 @Test
185 public void testIpAddress() { 205 public void testIpAddress() {
186 - testSerialized(IpAddress.valueOf("192.168.0.1")); 206 + testSerializedEquals(IpAddress.valueOf("192.168.0.1"));
187 } 207 }
188 208
189 @Test 209 @Test
190 public void testIp4Address() { 210 public void testIp4Address() {
191 - testSerialized(Ip4Address.valueOf("192.168.0.1")); 211 + testSerializedEquals(Ip4Address.valueOf("192.168.0.1"));
192 } 212 }
193 213
194 @Test 214 @Test
195 public void testIp6Address() { 215 public void testIp6Address() {
196 - testSerialized(Ip6Address.valueOf("1111:2222::")); 216 + testSerializedEquals(Ip6Address.valueOf("1111:2222::"));
197 } 217 }
198 218
199 @Test 219 @Test
200 public void testMacAddress() { 220 public void testMacAddress() {
201 - testSerialized(MacAddress.valueOf("12:34:56:78:90:ab")); 221 + testSerializedEquals(MacAddress.valueOf("12:34:56:78:90:ab"));
202 } 222 }
203 223
204 @Test 224 @Test
205 public void testLinkKey() { 225 public void testLinkKey() {
206 - testSerialized(LinkKey.linkKey(CP1, CP2)); 226 + testSerializedEquals(LinkKey.linkKey(CP1, CP2));
207 } 227 }
208 228
209 @Test 229 @Test
210 public void testNodeId() { 230 public void testNodeId() {
211 - testSerialized(new NodeId("SomeNodeIdentifier")); 231 + testSerializedEquals(new NodeId("SomeNodeIdentifier"));
212 } 232 }
213 233
214 @Test 234 @Test
215 public void testPortNumber() { 235 public void testPortNumber() {
216 - testSerialized(P1); 236 + testSerializedEquals(P1);
217 } 237 }
218 238
219 @Test 239 @Test
220 public void testProviderId() { 240 public void testProviderId() {
221 - testSerialized(PID); 241 + testSerializedEquals(PID);
222 - testSerialized(PIDA); 242 + testSerializedEquals(PIDA);
223 } 243 }
224 244
225 @Test 245 @Test
226 public void testMastershipTerm() { 246 public void testMastershipTerm() {
227 - testSerialized(MastershipTerm.of(new NodeId("foo"), 2)); 247 + testSerializedEquals(MastershipTerm.of(new NodeId("foo"), 2));
228 - testSerialized(MastershipTerm.of(null, 0)); 248 + testSerializedEquals(MastershipTerm.of(null, 0));
229 } 249 }
230 250
231 @Test 251 @Test
232 public void testHostLocation() { 252 public void testHostLocation() {
233 - testSerialized(new HostLocation(CP1, 1234L)); 253 + testSerializedEquals(new HostLocation(CP1, 1234L));
234 } 254 }
235 255
236 @Test 256 @Test
237 public void testFlowId() { 257 public void testFlowId() {
238 - testSerialized(FlowId.valueOf(0x12345678L)); 258 + testSerializedEquals(FlowId.valueOf(0x12345678L));
239 } 259 }
240 260
241 @Test 261 @Test
242 public void testRoleInfo() { 262 public void testRoleInfo() {
243 - testSerialized(new RoleInfo(new NodeId("master"), 263 + testSerializedEquals(new RoleInfo(new NodeId("master"),
244 asList(new NodeId("stby1"), new NodeId("stby2")))); 264 asList(new NodeId("stby1"), new NodeId("stby2"))));
245 } 265 }
246 266
247 @Test 267 @Test
268 + public void testDefaultLinkResourceRequest() {
269 + testSerializable(DefaultLinkResourceRequest.builder(IntentId.valueOf(2501), ImmutableList.of())
270 + .addLambdaRequest()
271 + .addBandwidthRequest(32.195)
272 + .build()
273 + );
274 + }
275 +
276 + @Test
277 + public void testDefaultLinkResourceAllocations() {
278 + LinkResourceRequest request = DefaultLinkResourceRequest
279 + .builder(IntentId.valueOf(2501), ImmutableList.of())
280 + .addLambdaRequest()
281 + .addBandwidthRequest(32.195)
282 + .build();
283 + Map<Link, Set<ResourceAllocation>> allocations = new HashMap<>();
284 + allocations.put(new DefaultLink(PID, CP1, CP2, Type.DIRECT),
285 + ImmutableSet.of(new BandwidthResourceAllocation(Bandwidth.valueOf(10.0)),
286 + new LambdaResourceAllocation(Lambda.valueOf(1))));
287 + testSerializable(new DefaultLinkResourceAllocations(request, allocations));
288 + }
289 +
290 +
291 + @Test
248 public void testAnnotations() { 292 public void testAnnotations() {
249 // Annotations does not have equals defined, manually test equality 293 // Annotations does not have equals defined, manually test equality
250 final byte[] a1Bytes = serializer.encode(A1); 294 final byte[] a1Bytes = serializer.encode(A1);
......