Sho SHIMIZU | e4f76ed | 2016-05-19 11:40:31 -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 | */ |
| 16 | package org.onosproject.store.resource.impl; |
| 17 | |
| 18 | import com.google.common.collect.Sets; |
| 19 | import org.onosproject.net.resource.DiscreteResource; |
| 20 | import org.onosproject.net.resource.DiscreteResourceCodec; |
| 21 | import org.onosproject.net.resource.DiscreteResourceId; |
Sho SHIMIZU | e4f76ed | 2016-05-19 11:40:31 -0700 | [diff] [blame] | 22 | import org.onosproject.net.resource.Resources; |
| 23 | |
| 24 | import java.util.LinkedHashMap; |
| 25 | import java.util.LinkedHashSet; |
| 26 | import java.util.List; |
| 27 | import java.util.Map; |
| 28 | import java.util.Optional; |
| 29 | import java.util.Set; |
| 30 | import java.util.stream.Collectors; |
| 31 | |
| 32 | /** |
| 33 | * A set of discrete resources that can be encoded as integers. |
| 34 | */ |
| 35 | final class EncodableDiscreteResources implements DiscreteResources { |
| 36 | private static final Codecs CODECS = Codecs.getInstance(); |
| 37 | private final DiscreteResource parent; |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 38 | private final Map<Class<?>, EncodedDiscreteResources> values; |
Sho SHIMIZU | e4f76ed | 2016-05-19 11:40:31 -0700 | [diff] [blame] | 39 | |
| 40 | private static Class<?> getClass(DiscreteResource resource) { |
| 41 | return resource.valueAs(Object.class).map(Object::getClass).get(); |
| 42 | } |
| 43 | |
| 44 | static DiscreteResources of(Set<DiscreteResource> resources) { |
Sho SHIMIZU | 65de961 | 2016-05-19 12:24:20 -0700 | [diff] [blame] | 45 | if (resources.isEmpty()) { |
| 46 | return DiscreteResources.empty(); |
| 47 | } |
| 48 | |
Sho SHIMIZU | e4f76ed | 2016-05-19 11:40:31 -0700 | [diff] [blame] | 49 | DiscreteResource parent = resources.iterator().next().parent().get(); |
| 50 | return of(parent, resources); |
| 51 | } |
| 52 | |
| 53 | static EncodableDiscreteResources of(DiscreteResource parent, Set<DiscreteResource> resources) { |
| 54 | Map<Class<?>, Set<DiscreteResource>> grouped = resources.stream() |
| 55 | .collect(Collectors.groupingBy(x -> getClass(x), Collectors.toCollection(LinkedHashSet::new))); |
| 56 | |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 57 | Map<Class<?>, EncodedDiscreteResources> values = new LinkedHashMap<>(); |
Sho SHIMIZU | e4f76ed | 2016-05-19 11:40:31 -0700 | [diff] [blame] | 58 | for (Map.Entry<Class<?>, Set<DiscreteResource>> entry : grouped.entrySet()) { |
| 59 | DiscreteResourceCodec<?> codec = CODECS.getCodec(entry.getKey()); |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 60 | values.put(entry.getKey(), EncodedDiscreteResources.of(entry.getValue(), codec)); |
Sho SHIMIZU | e4f76ed | 2016-05-19 11:40:31 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | return new EncodableDiscreteResources(parent, values); |
| 64 | } |
| 65 | |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 66 | private EncodableDiscreteResources(DiscreteResource parent, Map<Class<?>, EncodedDiscreteResources> values) { |
Sho SHIMIZU | e4f76ed | 2016-05-19 11:40:31 -0700 | [diff] [blame] | 67 | this.parent = parent; |
| 68 | this.values = values; |
| 69 | } |
| 70 | |
| 71 | // for serializer |
| 72 | private EncodableDiscreteResources() { |
| 73 | this.parent = null; |
| 74 | this.values = null; |
| 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public Optional<DiscreteResource> lookup(DiscreteResourceId id) { |
| 79 | DiscreteResource resource = Resources.discrete(id).resource(); |
| 80 | Class<?> cls = getClass(resource); |
| 81 | return Optional.ofNullable(values.get(cls)) |
| 82 | .filter(x -> x.contains(resource)) |
| 83 | .map(x -> resource); |
| 84 | } |
| 85 | |
| 86 | @Override |
| 87 | public DiscreteResources difference(DiscreteResources other) { |
| 88 | Set<DiscreteResource> diff = Sets.difference(values(), other.values()); |
| 89 | |
| 90 | return of(parent, diff); |
| 91 | } |
| 92 | |
| 93 | @Override |
| 94 | public boolean isEmpty() { |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 95 | return values.values().stream() |
| 96 | .allMatch(x -> x.isEmpty()); |
Sho SHIMIZU | e4f76ed | 2016-05-19 11:40:31 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public boolean containsAny(List<DiscreteResource> other) { |
| 101 | return other.stream() |
| 102 | .anyMatch(x -> values().contains(x)); |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public DiscreteResources add(DiscreteResources other) { |
| 107 | Set<DiscreteResource> union = Sets.union(values(), other.values()); |
| 108 | |
| 109 | return of(parent, union); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public DiscreteResources remove(List<DiscreteResource> removed) { |
| 114 | return of(parent, Sets.difference(values(), new LinkedHashSet<>(removed))); |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public Set<DiscreteResource> values() { |
| 119 | return values.values().stream() |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 120 | .flatMap(x -> x.resources(parent.id()).stream()) |
Sho SHIMIZU | e4f76ed | 2016-05-19 11:40:31 -0700 | [diff] [blame] | 121 | .collect(Collectors.toCollection(LinkedHashSet::new)); |
| 122 | } |
| 123 | |
| 124 | DiscreteResource parent() { |
| 125 | return parent; |
| 126 | } |
| 127 | |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame^] | 128 | Map<Class<?>, EncodedDiscreteResources> rawValues() { |
Sho SHIMIZU | e4f76ed | 2016-05-19 11:40:31 -0700 | [diff] [blame] | 129 | return values; |
| 130 | } |
| 131 | } |