tom

Working on model annotations; still in progress.

1 +package org.onlab.onos.net;
2 +
3 +import com.google.common.collect.ImmutableSet;
4 +
5 +import java.util.HashMap;
6 +import java.util.Map;
7 +import java.util.Set;
8 +
9 +import static com.google.common.base.Preconditions.checkArgument;
10 +
11 +/**
12 + * Base abstraction of an annotated entity.
13 + */
14 +public class AbstractAnnotated implements Annotated {
15 +
16 + private static final Map<String, String> EMPTY = new HashMap<>();
17 +
18 + private final Map<String, String> annotations;
19 +
20 + // For serialization
21 + protected AbstractAnnotated() {
22 + this.annotations = EMPTY;
23 + }
24 +
25 + /**
26 + * Creates a new entity, annotated with the specified annotations.
27 + *
28 + * @param annotations optional key/value annotations map
29 + */
30 + protected AbstractAnnotated(Map<String, String>[] annotations) {
31 + checkArgument(annotations.length <= 1, "Only one set of annotations is expected");
32 + this.annotations = annotations.length == 1 ? annotations[0] : EMPTY;
33 + }
34 +
35 + @Override
36 + public Set<String> annotationKeys() {
37 + return ImmutableSet.copyOf(annotations.keySet());
38 + }
39 +
40 + @Override
41 + public String annotation(String key) {
42 + return annotations.get(key);
43 + }
44 +
45 +}
1 package org.onlab.onos.net; 1 package org.onlab.onos.net;
2 2
3 -import com.google.common.collect.ImmutableSet;
4 -import com.google.common.collect.Maps;
5 import org.onlab.onos.net.provider.ProviderId; 3 import org.onlab.onos.net.provider.ProviderId;
6 4
7 import java.util.Map; 5 import java.util.Map;
8 -import java.util.Set;
9 6
10 /** 7 /**
11 * Base implementation of a network model entity. 8 * Base implementation of a network model entity.
12 */ 9 */
13 -public class AbstractModel implements Provided, Annotated { 10 +public class AbstractModel extends AbstractAnnotated implements Provided {
14 11
15 private final ProviderId providerId; 12 private final ProviderId providerId;
16 13
17 - // FIXME: figure out whether to make this concurrent or immutable
18 - private final Map<String, String> annotations = Maps.newHashMap();
19 -
20 // For serialization 14 // For serialization
21 public AbstractModel() { 15 public AbstractModel() {
22 providerId = null; 16 providerId = null;
23 } 17 }
24 18
25 /** 19 /**
26 - * Creates a model entity attributed to the specified provider. 20 + * Creates a model entity attributed to the specified provider and
21 + * optionally annotated.
27 * 22 *
28 - * @param providerId identity of the provider 23 + * @param providerId identity of the provider
24 + * @param annotations optional key/value annotations
29 */ 25 */
30 - protected AbstractModel(ProviderId providerId) { 26 + @SafeVarargs
27 + protected AbstractModel(ProviderId providerId,
28 + Map<String, String>... annotations) {
29 + super(annotations);
31 this.providerId = providerId; 30 this.providerId = providerId;
32 } 31 }
33 32
...@@ -36,13 +35,4 @@ public class AbstractModel implements Provided, Annotated { ...@@ -36,13 +35,4 @@ public class AbstractModel implements Provided, Annotated {
36 return providerId; 35 return providerId;
37 } 36 }
38 37
39 - @Override
40 - public Set<String> annotationKeys() {
41 - return ImmutableSet.copyOf(annotations.keySet());
42 - }
43 -
44 - @Override
45 - public String annotation(String key) {
46 - return annotations.get(key);
47 - }
48 } 38 }
......
...@@ -3,5 +3,5 @@ package org.onlab.onos.net; ...@@ -3,5 +3,5 @@ package org.onlab.onos.net;
3 /** 3 /**
4 * Base abstraction of a piece of information about network elements. 4 * Base abstraction of a piece of information about network elements.
5 */ 5 */
6 -public interface Description { 6 +public interface Description extends Annotated {
7 } 7 }
......
1 package org.onlab.onos.net.device; 1 package org.onlab.onos.net.device;
2 2
3 +import org.onlab.onos.net.AbstractAnnotated;
4 +
3 import java.net.URI; 5 import java.net.URI;
6 +import java.util.Map;
4 7
5 import static com.google.common.base.MoreObjects.toStringHelper; 8 import static com.google.common.base.MoreObjects.toStringHelper;
6 import static com.google.common.base.Preconditions.checkNotNull; 9 import static com.google.common.base.Preconditions.checkNotNull;
...@@ -9,7 +12,8 @@ import static org.onlab.onos.net.Device.Type; ...@@ -9,7 +12,8 @@ import static org.onlab.onos.net.Device.Type;
9 /** 12 /**
10 * Default implementation of immutable device description entity. 13 * Default implementation of immutable device description entity.
11 */ 14 */
12 -public class DefaultDeviceDescription implements DeviceDescription { 15 +public class DefaultDeviceDescription extends AbstractAnnotated
16 + implements DeviceDescription {
13 private final URI uri; 17 private final URI uri;
14 private final Type type; 18 private final Type type;
15 private final String manufacturer; 19 private final String manufacturer;
...@@ -26,10 +30,14 @@ public class DefaultDeviceDescription implements DeviceDescription { ...@@ -26,10 +30,14 @@ public class DefaultDeviceDescription implements DeviceDescription {
26 * @param hwVersion device HW version 30 * @param hwVersion device HW version
27 * @param swVersion device SW version 31 * @param swVersion device SW version
28 * @param serialNumber device serial number 32 * @param serialNumber device serial number
33 + * @param annotations optional key/value annotations map
29 */ 34 */
35 + @SafeVarargs
30 public DefaultDeviceDescription(URI uri, Type type, String manufacturer, 36 public DefaultDeviceDescription(URI uri, Type type, String manufacturer,
31 String hwVersion, String swVersion, 37 String hwVersion, String swVersion,
32 - String serialNumber) { 38 + String serialNumber,
39 + Map<String, String>... annotations) {
40 + super(annotations);
33 this.uri = checkNotNull(uri, "Device URI cannot be null"); 41 this.uri = checkNotNull(uri, "Device URI cannot be null");
34 this.type = checkNotNull(type, "Device type cannot be null"); 42 this.type = checkNotNull(type, "Device type cannot be null");
35 this.manufacturer = manufacturer; 43 this.manufacturer = manufacturer;
......
1 package org.onlab.onos.net.host; 1 package org.onlab.onos.net.host;
2 2
3 -import static com.google.common.base.MoreObjects.toStringHelper; 3 +import com.google.common.collect.ImmutableSet;
4 - 4 +import org.onlab.onos.net.AbstractAnnotated;
5 -import java.util.HashSet;
6 -import java.util.Set;
7 -
8 import org.onlab.onos.net.HostLocation; 5 import org.onlab.onos.net.HostLocation;
9 import org.onlab.packet.IpPrefix; 6 import org.onlab.packet.IpPrefix;
10 import org.onlab.packet.MacAddress; 7 import org.onlab.packet.MacAddress;
11 import org.onlab.packet.VlanId; 8 import org.onlab.packet.VlanId;
12 9
13 -import com.google.common.collect.ImmutableSet; 10 +import java.util.HashSet;
11 +import java.util.Map;
12 +import java.util.Set;
13 +
14 +import static com.google.common.base.MoreObjects.toStringHelper;
14 15
15 -public class DefaultHostDescription implements HostDescription { 16 +/**
17 + * Default implementation of an immutable host description.
18 + */
19 +public class DefaultHostDescription extends AbstractAnnotated
20 + implements HostDescription {
16 21
17 private final MacAddress mac; 22 private final MacAddress mac;
18 private final VlanId vlan; 23 private final VlanId vlan;
19 private final HostLocation location; 24 private final HostLocation location;
20 private final Set<IpPrefix> ips; 25 private final Set<IpPrefix> ips;
21 26
27 + /**
28 + * Creates a host description using the supplied information.
29 + *
30 + * @param mac host MAC address
31 + * @param vlan host VLAN identifier
32 + * @param location host location
33 + * @param annotations optional key/value annotations map
34 + */
35 + @SafeVarargs
22 public DefaultHostDescription(MacAddress mac, VlanId vlan, 36 public DefaultHostDescription(MacAddress mac, VlanId vlan,
23 - HostLocation loc) { 37 + HostLocation location,
24 - this.mac = mac; 38 + Map<String, String>... annotations) {
25 - this.vlan = vlan; 39 + this(mac, vlan, location, new HashSet<IpPrefix>(), annotations);
26 - this.location = loc;
27 - this.ips = new HashSet<IpPrefix>();
28 } 40 }
29 41
42 + /**
43 + * Creates a host description using the supplied information.
44 + *
45 + * @param mac host MAC address
46 + * @param vlan host VLAN identifier
47 + * @param location host location
48 + * @param ips of host IP addresses
49 + * @param annotations optional key/value annotations map
50 + */
51 + @SafeVarargs
30 public DefaultHostDescription(MacAddress mac, VlanId vlan, 52 public DefaultHostDescription(MacAddress mac, VlanId vlan,
31 - HostLocation loc, Set<IpPrefix> ips) { 53 + HostLocation location, Set<IpPrefix> ips,
54 + Map<String, String>... annotations) {
55 + super(annotations);
32 this.mac = mac; 56 this.mac = mac;
33 this.vlan = vlan; 57 this.vlan = vlan;
34 - this.location = loc; 58 + this.location = location;
35 - this.ips = new HashSet<IpPrefix>(ips); 59 + this.ips = new HashSet<>(ips);
36 } 60 }
37 61
38 @Override 62 @Override
......
...@@ -2,6 +2,7 @@ package org.onlab.onos.net.topology; ...@@ -2,6 +2,7 @@ package org.onlab.onos.net.topology;
2 2
3 import com.google.common.collect.ImmutableSet; 3 import com.google.common.collect.ImmutableSet;
4 import com.google.common.collect.Maps; 4 import com.google.common.collect.Maps;
5 +import org.onlab.onos.net.AbstractAnnotated;
5 import org.onlab.onos.net.ConnectPoint; 6 import org.onlab.onos.net.ConnectPoint;
6 import org.onlab.onos.net.Device; 7 import org.onlab.onos.net.Device;
7 import org.onlab.onos.net.DeviceId; 8 import org.onlab.onos.net.DeviceId;
...@@ -12,7 +13,8 @@ import java.util.Map; ...@@ -12,7 +13,8 @@ import java.util.Map;
12 /** 13 /**
13 * Default implementation of an immutable topology graph data carrier. 14 * Default implementation of an immutable topology graph data carrier.
14 */ 15 */
15 -public class DefaultGraphDescription implements GraphDescription { 16 +public class DefaultGraphDescription extends AbstractAnnotated
17 + implements GraphDescription {
16 18
17 private final long nanos; 19 private final long nanos;
18 private final ImmutableSet<TopologyVertex> vertexes; 20 private final ImmutableSet<TopologyVertex> vertexes;
...@@ -25,11 +27,16 @@ public class DefaultGraphDescription implements GraphDescription { ...@@ -25,11 +27,16 @@ public class DefaultGraphDescription implements GraphDescription {
25 * Creates a minimal topology graph description to allow core to construct 27 * Creates a minimal topology graph description to allow core to construct
26 * and process the topology graph. 28 * and process the topology graph.
27 * 29 *
28 - * @param nanos time in nanos of when the topology description was created 30 + * @param nanos time in nanos of when the topology description was created
29 - * @param devices collection of infrastructure devices 31 + * @param devices collection of infrastructure devices
30 - * @param links collection of infrastructure links 32 + * @param links collection of infrastructure links
33 + * @param annotations optional key/value annotations map
31 */ 34 */
32 - public DefaultGraphDescription(long nanos, Iterable<Device> devices, Iterable<Link> links) { 35 + @SafeVarargs
36 + public DefaultGraphDescription(long nanos, Iterable<Device> devices,
37 + Iterable<Link> links,
38 + Map<String, String>... annotations) {
39 + super(annotations);
33 this.nanos = nanos; 40 this.nanos = nanos;
34 this.vertexes = buildVertexes(devices); 41 this.vertexes = buildVertexes(devices);
35 this.edges = buildEdges(links); 42 this.edges = buildEdges(links);
......