Sho SHIMIZU
Committed by Sho Shimizu

Add a set of discrete resources that can be encoded as interger

This is for ONOS-4281

Change-Id: I83de45368485a3c4f6c052b64ef82e846c0553e3
(cherry picked from commit 8a703934)
...@@ -125,4 +125,7 @@ public final class DiscreteResourceSet { ...@@ -125,4 +125,7 @@ public final class DiscreteResourceSet {
125 && Objects.equals(this.codec, other.codec); 125 && Objects.equals(this.codec, other.codec);
126 } 126 }
127 127
128 + public boolean contains(DiscreteResource resource) {
129 + return values.contains(resource);
130 + }
128 } 131 }
......
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;
22 +import org.onosproject.net.resource.DiscreteResourceSet;
23 +import org.onosproject.net.resource.Resources;
24 +
25 +import java.util.LinkedHashMap;
26 +import java.util.LinkedHashSet;
27 +import java.util.List;
28 +import java.util.Map;
29 +import java.util.Optional;
30 +import java.util.Set;
31 +import java.util.stream.Collectors;
32 +
33 +/**
34 + * A set of discrete resources that can be encoded as integers.
35 + */
36 +final 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) {
46 + DiscreteResource parent = resources.iterator().next().parent().get();
47 + return of(parent, resources);
48 + }
49 +
50 + static EncodableDiscreteResources of(DiscreteResource parent, Set<DiscreteResource> resources) {
51 + Map<Class<?>, Set<DiscreteResource>> grouped = resources.stream()
52 + .collect(Collectors.groupingBy(x -> getClass(x), Collectors.toCollection(LinkedHashSet::new)));
53 +
54 + Map<Class<?>, DiscreteResourceSet> values = new LinkedHashMap<>();
55 + for (Map.Entry<Class<?>, Set<DiscreteResource>> entry : grouped.entrySet()) {
56 + DiscreteResourceCodec<?> codec = CODECS.getCodec(entry.getKey());
57 + values.put(entry.getKey(), DiscreteResourceSet.of(entry.getValue(), codec));
58 + }
59 +
60 + return new EncodableDiscreteResources(parent, values);
61 + }
62 +
63 + private EncodableDiscreteResources(DiscreteResource parent, Map<Class<?>, DiscreteResourceSet> values) {
64 + this.parent = parent;
65 + this.values = values;
66 + }
67 +
68 + // for serializer
69 + private EncodableDiscreteResources() {
70 + this.parent = null;
71 + this.values = null;
72 + }
73 +
74 + @Override
75 + public Optional<DiscreteResource> lookup(DiscreteResourceId id) {
76 + DiscreteResource resource = Resources.discrete(id).resource();
77 + Class<?> cls = getClass(resource);
78 + return Optional.ofNullable(values.get(cls))
79 + .filter(x -> x.contains(resource))
80 + .map(x -> resource);
81 + }
82 +
83 + @Override
84 + public DiscreteResources difference(DiscreteResources other) {
85 + Set<DiscreteResource> diff = Sets.difference(values(), other.values());
86 +
87 + return of(parent, diff);
88 + }
89 +
90 + @Override
91 + public boolean isEmpty() {
92 + return !values.values().stream()
93 + .flatMap(x -> x.values().stream())
94 + .findAny()
95 + .isPresent();
96 + }
97 +
98 + @Override
99 + public boolean containsAny(List<DiscreteResource> other) {
100 + return other.stream()
101 + .anyMatch(x -> values().contains(x));
102 + }
103 +
104 + @Override
105 + public DiscreteResources add(DiscreteResources other) {
106 + Set<DiscreteResource> union = Sets.union(values(), other.values());
107 +
108 + return of(parent, union);
109 + }
110 +
111 + @Override
112 + public DiscreteResources remove(List<DiscreteResource> removed) {
113 + return of(parent, Sets.difference(values(), new LinkedHashSet<>(removed)));
114 + }
115 +
116 + @Override
117 + public Set<DiscreteResource> values() {
118 + return values.values().stream()
119 + .flatMap(x -> x.values().stream())
120 + .collect(Collectors.toCollection(LinkedHashSet::new));
121 + }
122 +
123 + DiscreteResource parent() {
124 + return parent;
125 + }
126 +
127 + Map<Class<?>, DiscreteResourceSet> rawValues() {
128 + return values;
129 + }
130 +}
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.esotericsoftware.kryo.Kryo;
19 +import com.esotericsoftware.kryo.Serializer;
20 +import com.esotericsoftware.kryo.io.Input;
21 +import com.esotericsoftware.kryo.io.Output;
22 +import org.onosproject.net.resource.DiscreteResource;
23 +import org.onosproject.net.resource.DiscreteResourceSet;
24 +
25 +import java.util.LinkedHashSet;
26 +import java.util.Set;
27 +import java.util.stream.Collectors;
28 +
29 +/**
30 + * Kryo serializer for {@link EncodableDiscreteResources}.
31 + */
32 +class EncodableDiscreteResourcesSerializer extends Serializer<EncodableDiscreteResources> {
33 + @Override
34 + public void write(Kryo kryo, Output output, EncodableDiscreteResources object) {
35 + kryo.writeObject(output, object.parent());
36 + kryo.writeObject(output, new LinkedHashSet<>(object.rawValues().values()));
37 + }
38 +
39 + @Override
40 + public EncodableDiscreteResources read(Kryo kryo, Input input, Class<EncodableDiscreteResources> cls) {
41 + DiscreteResource parent = kryo.readObject(input, DiscreteResource.class);
42 + @SuppressWarnings("unchecked")
43 + Set<DiscreteResourceSet> resources = kryo.readObject(input, LinkedHashSet.class);
44 +
45 + return EncodableDiscreteResources.of(parent,
46 + resources.stream()
47 + .flatMap(x -> x.values().stream())
48 + .collect(Collectors.toCollection(LinkedHashSet::new)));
49 + }
50 +}