Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 16 | package org.onosproject.store.resource.impl; |
Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 17 | |
| 18 | import com.esotericsoftware.kryo.Kryo; |
| 19 | import com.esotericsoftware.kryo.Serializer; |
| 20 | import com.esotericsoftware.kryo.io.Input; |
| 21 | import com.esotericsoftware.kryo.io.Output; |
Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 22 | import com.google.common.collect.DiscreteDomain; |
| 23 | import com.google.common.collect.Range; |
| 24 | import com.google.common.collect.TreeRangeSet; |
| 25 | import org.onlab.util.ClosedOpenRange; |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 26 | import org.onosproject.net.resource.DiscreteResourceCodec; |
Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 27 | |
| 28 | import java.util.ArrayList; |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 29 | import java.util.HashSet; |
| 30 | import java.util.LinkedHashSet; |
Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 31 | import java.util.List; |
Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 32 | import java.util.stream.Collectors; |
| 33 | import java.util.stream.IntStream; |
| 34 | |
| 35 | /** |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 36 | * Kryo Serializer for {@link EncodedDiscreteResources}. |
Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 37 | */ |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 38 | final class EncodedResourcesSerializer extends Serializer<EncodedDiscreteResources> { |
Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 39 | @Override |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 40 | public void write(Kryo kryo, Output output, EncodedDiscreteResources object) { |
Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 41 | TreeRangeSet<Integer> rangeSet = TreeRangeSet.create(); |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 42 | object.rawValues().stream() |
Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 43 | .map(Range::singleton) |
| 44 | .map(x -> x.canonical(DiscreteDomain.integers())) |
| 45 | .forEach(rangeSet::add); |
| 46 | List<ClosedOpenRange> ranges = rangeSet.asRanges().stream() |
| 47 | .map(ClosedOpenRange::of) |
| 48 | .collect(Collectors.toList()); |
| 49 | kryo.writeObject(output, ranges); |
| 50 | kryo.writeClassAndObject(output, object.codec()); |
Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | @Override |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 54 | public EncodedDiscreteResources read(Kryo kryo, Input input, Class<EncodedDiscreteResources> cls) { |
Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 55 | @SuppressWarnings("unchecked") |
| 56 | List<ClosedOpenRange> ranges = kryo.readObject(input, ArrayList.class); |
| 57 | DiscreteResourceCodec codec = (DiscreteResourceCodec) kryo.readClassAndObject(input); |
Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 58 | |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 59 | HashSet<Integer> rawValues = ranges.stream() |
Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 60 | .flatMapToInt(x -> IntStream.range(x.lowerBound(), x.upperBound())) |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 61 | .boxed() |
| 62 | .collect(Collectors.toCollection(LinkedHashSet::new)); |
| 63 | return new EncodedDiscreteResources(rawValues, codec); |
Sho SHIMIZU | c0e010dd | 2016-05-02 14:46:22 -0700 | [diff] [blame] | 64 | } |
| 65 | } |