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