blob: 2d6446af22d0b56ab5e78e81d500895b2e0779de [file] [log] [blame]
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -07001/*
2 * Copyright 2016-present Open Networking Laboratory
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 */
16package org.onosproject.net.resource;
17
18import com.esotericsoftware.kryo.Kryo;
19import com.esotericsoftware.kryo.Serializer;
20import com.esotericsoftware.kryo.io.Input;
21import com.esotericsoftware.kryo.io.Output;
22import com.google.common.annotations.Beta;
23import com.google.common.collect.DiscreteDomain;
24import com.google.common.collect.Range;
25import com.google.common.collect.TreeRangeSet;
26import org.onlab.util.ClosedOpenRange;
Sho SHIMIZUcf98b1a2016-05-18 15:11:31 -070027import org.onlab.util.Tools;
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -070028
29import java.util.ArrayList;
30import java.util.List;
31import java.util.Set;
32import java.util.stream.Collectors;
33import java.util.stream.IntStream;
34
35/**
36 * Kryo serializer for {@link DiscreteResourceSet}.
37 */
38@Beta
39public final class DiscreteResourceSetSerializer extends Serializer<DiscreteResourceSet> {
40
41 public DiscreteResourceSetSerializer() {
42 super(false, true);
43 }
44
45 @Override
46 public void write(Kryo kryo, Output output, DiscreteResourceSet object) {
47 TreeRangeSet<Integer> rangeSet = TreeRangeSet.create();
48 object.values().stream()
Sho SHIMIZUcf98b1a2016-05-18 15:11:31 -070049 .map(x -> x.valueAs(Object.class))
50 .flatMap(Tools::stream)
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -070051 .map(x -> object.codec().encode(x))
52 .map(Range::singleton)
53 .map(x -> x.canonical(DiscreteDomain.integers()))
54 .forEach(rangeSet::add);
55 List<ClosedOpenRange> ranges = rangeSet.asRanges().stream()
56 .map(ClosedOpenRange::of)
57 .collect(Collectors.toList());
58 kryo.writeObject(output, ranges);
59 kryo.writeClassAndObject(output, object.codec());
60 kryo.writeObject(output, object.parent());
61 }
62
63 @Override
64 public DiscreteResourceSet read(Kryo kryo, Input input, Class<DiscreteResourceSet> type) {
65 @SuppressWarnings("unchecked")
66 List<ClosedOpenRange> ranges = kryo.readObject(input, ArrayList.class);
67 DiscreteResourceCodec codec = (DiscreteResourceCodec) kryo.readClassAndObject(input);
68 DiscreteResourceId parent = kryo.readObject(input, DiscreteResourceId.class);
69
70 if (ranges.isEmpty()) {
71 return DiscreteResourceSet.empty();
72 }
73
74 Set<DiscreteResource> resources = ranges.stream()
75 .flatMapToInt(x -> IntStream.range(x.lowerBound(), x.upperBound()))
Sho SHIMIZUcf98b1a2016-05-18 15:11:31 -070076 .mapToObj(x -> codec.decode(x))
77 .map(x -> Resources.discrete(parent, x).resource())
Sho SHIMIZUc0e010dd2016-05-02 14:46:22 -070078 .collect(Collectors.toSet());
79
80 return DiscreteResourceSet.of(resources, codec);
81 }
82}