Showing
5 changed files
with
123 additions
and
5 deletions
1 | +package org.onlab.onos.net; | ||
2 | + | ||
3 | +import com.google.common.collect.ImmutableMap; | ||
4 | + | ||
5 | +import java.util.Map; | ||
6 | +import java.util.Objects; | ||
7 | +import java.util.Set; | ||
8 | + | ||
9 | +/** | ||
10 | + * Represents a set of simple annotations that can be used to add arbitrary | ||
11 | + * attributes to various parts of the data model. | ||
12 | + */ | ||
13 | +public final class Annotations { | ||
14 | + | ||
15 | + private final Map<String, String> map; | ||
16 | + | ||
17 | + /** | ||
18 | + * Creates a new set of annotations using the specified immutable map. | ||
19 | + * | ||
20 | + * @param map immutable map of key/value pairs | ||
21 | + */ | ||
22 | + private Annotations(ImmutableMap<String, String> map) { | ||
23 | + this.map = map; | ||
24 | + } | ||
25 | + | ||
26 | + /** | ||
27 | + * Creates a new annotations builder. | ||
28 | + * | ||
29 | + * @return new annotations builder | ||
30 | + */ | ||
31 | + public static Builder builder() { | ||
32 | + return new Builder(); | ||
33 | + } | ||
34 | + | ||
35 | + /** | ||
36 | + * Returns the set of keys for available annotations. Note that this set | ||
37 | + * includes keys for any attributes tagged for removal. | ||
38 | + * | ||
39 | + * @return annotation keys | ||
40 | + */ | ||
41 | + public Set<String> keys() { | ||
42 | + return map.keySet(); | ||
43 | + } | ||
44 | + | ||
45 | + /** | ||
46 | + * Returns the value of the specified annotation. | ||
47 | + * | ||
48 | + * @param key annotation key | ||
49 | + * @return annotation value | ||
50 | + */ | ||
51 | + public String value(String key) { | ||
52 | + String value = map.get(key); | ||
53 | + return Objects.equals(Builder.REMOVED, value) ? null : value; | ||
54 | + } | ||
55 | + | ||
56 | + /** | ||
57 | + * Indicates whether the specified key has been tagged as removed. This is | ||
58 | + * used to for merging sparse annotation sets. | ||
59 | + * | ||
60 | + * @param key annotation key | ||
61 | + * @return true if the previous annotation has been tagged for removal | ||
62 | + */ | ||
63 | + public boolean isRemoved(String key) { | ||
64 | + return Objects.equals(Builder.REMOVED, map.get(key)); | ||
65 | + } | ||
66 | + | ||
67 | + /** | ||
68 | + * Facility for gradually building model annotations. | ||
69 | + */ | ||
70 | + public static final class Builder { | ||
71 | + | ||
72 | + private static final String REMOVED = "~rEmOvEd~"; | ||
73 | + private final ImmutableMap.Builder<String, String> builder = ImmutableMap.builder(); | ||
74 | + | ||
75 | + // Private construction is forbidden. | ||
76 | + private Builder() { | ||
77 | + } | ||
78 | + | ||
79 | + /** | ||
80 | + * Adds the specified annotation. Any previous value associated with | ||
81 | + * the given annotation key will be overwritten. | ||
82 | + * | ||
83 | + * @param key annotation key | ||
84 | + * @param value annotation value | ||
85 | + * @return self | ||
86 | + */ | ||
87 | + public Builder set(String key, String value) { | ||
88 | + builder.put(key, value); | ||
89 | + return this; | ||
90 | + } | ||
91 | + | ||
92 | + /** | ||
93 | + * Adds the specified annotation. Any previous value associated with | ||
94 | + * the given annotation key will be tagged for removal. | ||
95 | + * | ||
96 | + * @param key annotation key | ||
97 | + * @return self | ||
98 | + */ | ||
99 | + public Builder remove(String key) { | ||
100 | + builder.put(key, REMOVED); | ||
101 | + return this; | ||
102 | + } | ||
103 | + | ||
104 | + /** | ||
105 | + * Returns immutable annotations built from the accrued key/values pairs. | ||
106 | + * | ||
107 | + * @return annotations | ||
108 | + */ | ||
109 | + public Annotations build() { | ||
110 | + return new Annotations(builder.build()); | ||
111 | + } | ||
112 | + } | ||
113 | +} |
... | @@ -2,12 +2,13 @@ package org.onlab.onos.net; | ... | @@ -2,12 +2,13 @@ package org.onlab.onos.net; |
2 | 2 | ||
3 | import static com.google.common.base.MoreObjects.toStringHelper; | 3 | import static com.google.common.base.MoreObjects.toStringHelper; |
4 | 4 | ||
5 | +import java.util.Map; | ||
5 | import java.util.Objects; | 6 | import java.util.Objects; |
6 | 7 | ||
7 | /** | 8 | /** |
8 | * Default port implementation. | 9 | * Default port implementation. |
9 | */ | 10 | */ |
10 | -public class DefaultPort implements Port { | 11 | +public class DefaultPort extends AbstractAnnotated implements Port { |
11 | 12 | ||
12 | private final Element element; | 13 | private final Element element; |
13 | private final PortNumber number; | 14 | private final PortNumber number; |
... | @@ -19,9 +20,13 @@ public class DefaultPort implements Port { | ... | @@ -19,9 +20,13 @@ public class DefaultPort implements Port { |
19 | * @param element parent network element | 20 | * @param element parent network element |
20 | * @param number port number | 21 | * @param number port number |
21 | * @param isEnabled indicator whether the port is up and active | 22 | * @param isEnabled indicator whether the port is up and active |
23 | + * @param annotations optional key/value annotations | ||
22 | */ | 24 | */ |
25 | + @SafeVarargs | ||
23 | public DefaultPort(Element element, PortNumber number, | 26 | public DefaultPort(Element element, PortNumber number, |
24 | - boolean isEnabled) { | 27 | + boolean isEnabled, |
28 | + Map<String, String>... annotations) { | ||
29 | + super(annotations); | ||
25 | this.element = element; | 30 | this.element = element; |
26 | this.number = number; | 31 | this.number = number; |
27 | this.isEnabled = isEnabled; | 32 | this.isEnabled = isEnabled; | ... | ... |
... | @@ -3,7 +3,7 @@ package org.onlab.onos.net; | ... | @@ -3,7 +3,7 @@ package org.onlab.onos.net; |
3 | /** | 3 | /** |
4 | * Base abstraction of a network element, i.e. an infrastructure device or an end-station host. | 4 | * Base abstraction of a network element, i.e. an infrastructure device or an end-station host. |
5 | */ | 5 | */ |
6 | -public interface Element extends Provided { | 6 | +public interface Element extends Annotated, Provided { |
7 | 7 | ||
8 | /** | 8 | /** |
9 | * Returns the network element identifier. | 9 | * Returns the network element identifier. | ... | ... |
... | @@ -3,7 +3,7 @@ package org.onlab.onos.net; | ... | @@ -3,7 +3,7 @@ package org.onlab.onos.net; |
3 | /** | 3 | /** |
4 | * Abstraction of a network infrastructure link. | 4 | * Abstraction of a network infrastructure link. |
5 | */ | 5 | */ |
6 | -public interface Link extends Provided { | 6 | +public interface Link extends Annotated, Provided { |
7 | 7 | ||
8 | /** | 8 | /** |
9 | * Coarse representation of the link type. | 9 | * Coarse representation of the link type. | ... | ... |
... | @@ -4,7 +4,7 @@ package org.onlab.onos.net; | ... | @@ -4,7 +4,7 @@ package org.onlab.onos.net; |
4 | /** | 4 | /** |
5 | * Abstraction of a network port. | 5 | * Abstraction of a network port. |
6 | */ | 6 | */ |
7 | -public interface Port { | 7 | +public interface Port extends Annotated { |
8 | 8 | ||
9 | /** | 9 | /** |
10 | * Returns the port number. | 10 | * Returns the port number. | ... | ... |
-
Please register or login to post a comment