tom

Formalized HostStore in preparation for separating managers and stores.

1 +package org.onlab.onos.net.host;
2 +
3 +import org.onlab.onos.net.ConnectPoint;
4 +import org.onlab.onos.net.DeviceId;
5 +import org.onlab.onos.net.Host;
6 +import org.onlab.onos.net.HostId;
7 +import org.onlab.onos.net.provider.ProviderId;
8 +import org.onlab.packet.IpAddress;
9 +import org.onlab.packet.MacAddress;
10 +import org.onlab.packet.VlanId;
11 +
12 +import java.util.Set;
13 +
14 +/**
15 + * Manages inventory of end-station hosts. It may do so using whatever
16 + * means are appropriate.
17 + */
18 +public interface HostStore {
19 +
20 + /**
21 + * Creates a new host or updates the existing one based on the specified
22 + * description.
23 + *
24 + * @param providerId provider identification
25 + * @param hostId host identification
26 + * @param hostDescription host description data
27 + * @return appropriate event or null if no change resulted
28 + */
29 + HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId,
30 + HostDescription hostDescription);
31 +
32 + /**
33 + * Removes the specified host from the inventory.
34 + *
35 + * @param hostId host identification
36 + * @return remove event or null if host was not found
37 + */
38 + HostEvent removeHost(HostId hostId);
39 +
40 + /**
41 + * Returns the number of hosts in the store.
42 + *
43 + * @return host count
44 + */
45 + int getHostCount();
46 +
47 + /**
48 + * Returns a collection of all hosts in the store.
49 + *
50 + * @return iterable collection of all hosts
51 + */
52 + Iterable<Host> getHosts();
53 +
54 + /**
55 + * Returns the host with the specified identifer.
56 + *
57 + * @param hostId host identification
58 + * @return host or null if not found
59 + */
60 + Host getHost(HostId hostId);
61 +
62 + /**
63 + * Returns the set of all hosts within the specified VLAN.
64 + *
65 + * @param vlanId vlan id
66 + * @return set of hosts in the vlan
67 + */
68 + Set<Host> getHosts(VlanId vlanId);
69 +
70 + /**
71 + * Returns the set of hosts with the specified MAC address.
72 + *
73 + * @param mac mac address
74 + * @return set of hosts with the given mac
75 + */
76 + Set<Host> getHosts(MacAddress mac);
77 +
78 + /**
79 + * Returns the set of hosts with the specified IP address.
80 + *
81 + * @param ip ip address
82 + * @return set of hosts with the given IP
83 + */
84 + Set<Host> getHosts(IpAddress ip);
85 +
86 + /**
87 + * Returns the set of hosts whose location falls on the given connection point.
88 + *
89 + * @param connectPoint connection point
90 + * @return set of hosts
91 + */
92 + Set<Host> getConnectedHosts(ConnectPoint connectPoint);
93 +
94 + /**
95 + * Returns the set of hosts whose location falls on the given device.
96 + *
97 + * @param deviceId infrastructure device identifier
98 + * @return set of hosts
99 + */
100 + Set<Host> getConnectedHosts(DeviceId deviceId);
101 +
102 +}
...@@ -8,7 +8,8 @@ import org.onlab.onos.net.provider.ProviderId; ...@@ -8,7 +8,8 @@ import org.onlab.onos.net.provider.ProviderId;
8 import java.util.Set; 8 import java.util.Set;
9 9
10 /** 10 /**
11 - * Manages inventory of infrastructure links using whatever means are appropriate. 11 + * Manages inventory of infrastructure links. It may do so using whatever
12 + * means are appropriate.
12 */ 13 */
13 public interface LinkStore { 14 public interface LinkStore {
14 15
......
...@@ -60,6 +60,7 @@ public class SimpleDeviceStore implements DeviceStore { ...@@ -60,6 +60,7 @@ public class SimpleDeviceStore implements DeviceStore {
60 public void deactivate() { 60 public void deactivate() {
61 log.info("Stopped"); 61 log.info("Stopped");
62 } 62 }
63 +
63 @Override 64 @Override
64 public int getDeviceCount() { 65 public int getDeviceCount() {
65 return devices.size(); 66 return devices.size();
......
...@@ -20,6 +20,7 @@ import org.onlab.onos.net.host.HostProvider; ...@@ -20,6 +20,7 @@ import org.onlab.onos.net.host.HostProvider;
20 import org.onlab.onos.net.host.HostProviderRegistry; 20 import org.onlab.onos.net.host.HostProviderRegistry;
21 import org.onlab.onos.net.host.HostProviderService; 21 import org.onlab.onos.net.host.HostProviderService;
22 import org.onlab.onos.net.host.HostService; 22 import org.onlab.onos.net.host.HostService;
23 +import org.onlab.onos.net.host.HostStore;
23 import org.onlab.onos.net.provider.AbstractProviderRegistry; 24 import org.onlab.onos.net.provider.AbstractProviderRegistry;
24 import org.onlab.onos.net.provider.AbstractProviderService; 25 import org.onlab.onos.net.provider.AbstractProviderService;
25 import org.onlab.packet.IpAddress; 26 import org.onlab.packet.IpAddress;
...@@ -47,7 +48,8 @@ public class SimpleHostManager ...@@ -47,7 +48,8 @@ public class SimpleHostManager
47 private final AbstractListenerRegistry<HostEvent, HostListener> 48 private final AbstractListenerRegistry<HostEvent, HostListener>
48 listenerRegistry = new AbstractListenerRegistry<>(); 49 listenerRegistry = new AbstractListenerRegistry<>();
49 50
50 - private final SimpleHostStore store = new SimpleHostStore(); 51 + @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
52 + protected HostStore store;
51 53
52 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) 54 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
53 protected EventDeliveryService eventDispatcher; 55 protected EventDeliveryService eventDispatcher;
......
...@@ -4,6 +4,7 @@ import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED; ...@@ -4,6 +4,7 @@ import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED;
4 import static org.onlab.onos.net.host.HostEvent.Type.HOST_MOVED; 4 import static org.onlab.onos.net.host.HostEvent.Type.HOST_MOVED;
5 import static org.onlab.onos.net.host.HostEvent.Type.HOST_REMOVED; 5 import static org.onlab.onos.net.host.HostEvent.Type.HOST_REMOVED;
6 import static org.onlab.onos.net.host.HostEvent.Type.HOST_UPDATED; 6 import static org.onlab.onos.net.host.HostEvent.Type.HOST_UPDATED;
7 +import static org.slf4j.LoggerFactory.getLogger;
7 8
8 import java.util.Collections; 9 import java.util.Collections;
9 import java.util.HashSet; 10 import java.util.HashSet;
...@@ -11,6 +12,10 @@ import java.util.Map; ...@@ -11,6 +12,10 @@ import java.util.Map;
11 import java.util.Set; 12 import java.util.Set;
12 import java.util.concurrent.ConcurrentHashMap; 13 import java.util.concurrent.ConcurrentHashMap;
13 14
15 +import org.apache.felix.scr.annotations.Activate;
16 +import org.apache.felix.scr.annotations.Component;
17 +import org.apache.felix.scr.annotations.Deactivate;
18 +import org.apache.felix.scr.annotations.Service;
14 import org.onlab.onos.net.ConnectPoint; 19 import org.onlab.onos.net.ConnectPoint;
15 import org.onlab.onos.net.DefaultHost; 20 import org.onlab.onos.net.DefaultHost;
16 import org.onlab.onos.net.DeviceId; 21 import org.onlab.onos.net.DeviceId;
...@@ -18,6 +23,7 @@ import org.onlab.onos.net.Host; ...@@ -18,6 +23,7 @@ import org.onlab.onos.net.Host;
18 import org.onlab.onos.net.HostId; 23 import org.onlab.onos.net.HostId;
19 import org.onlab.onos.net.host.HostDescription; 24 import org.onlab.onos.net.host.HostDescription;
20 import org.onlab.onos.net.host.HostEvent; 25 import org.onlab.onos.net.host.HostEvent;
26 +import org.onlab.onos.net.host.HostStore;
21 import org.onlab.onos.net.provider.ProviderId; 27 import org.onlab.onos.net.provider.ProviderId;
22 import org.onlab.packet.IpAddress; 28 import org.onlab.packet.IpAddress;
23 import org.onlab.packet.MacAddress; 29 import org.onlab.packet.MacAddress;
...@@ -26,29 +32,37 @@ import org.onlab.packet.VlanId; ...@@ -26,29 +32,37 @@ import org.onlab.packet.VlanId;
26 import com.google.common.collect.HashMultimap; 32 import com.google.common.collect.HashMultimap;
27 import com.google.common.collect.ImmutableSet; 33 import com.google.common.collect.ImmutableSet;
28 import com.google.common.collect.Multimap; 34 import com.google.common.collect.Multimap;
35 +import org.slf4j.Logger;
29 36
30 /** 37 /**
31 * Manages inventory of end-station hosts using trivial in-memory 38 * Manages inventory of end-station hosts using trivial in-memory
32 * implementation. 39 * implementation.
33 */ 40 */
34 -public class SimpleHostStore { 41 +@Component(immediate = true)
42 +@Service
43 +public class SimpleHostStore implements HostStore {
35 44
45 + private final Logger log = getLogger(getClass());
46 +
47 + // Host inventory
36 private final Map<HostId, Host> hosts = new ConcurrentHashMap<>(); 48 private final Map<HostId, Host> hosts = new ConcurrentHashMap<>();
37 49
38 - // hosts sorted based on their location 50 + // Hosts tracked by their location
39 private final Multimap<ConnectPoint, Host> locations = HashMultimap.create(); 51 private final Multimap<ConnectPoint, Host> locations = HashMultimap.create();
40 52
41 - /** 53 + @Activate
42 - * Creates a new host or updates the existing one based on the specified 54 + public void activate() {
43 - * description. 55 + log.info("Started");
44 - * 56 + }
45 - * @param providerId provider identification 57 +
46 - * @param hostId host identification 58 + @Deactivate
47 - * @param hostDescription host description data 59 + public void deactivate() {
48 - * @return appropriate event or null if no change resulted 60 + log.info("Stopped");
49 - */ 61 + }
50 - HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId, 62 +
51 - HostDescription hostDescription) { 63 + @Override
64 + public HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId,
65 + HostDescription hostDescription) {
52 Host host = hosts.get(hostId); 66 Host host = hosts.get(hostId);
53 if (host == null) { 67 if (host == null) {
54 return createHost(providerId, hostId, hostDescription); 68 return createHost(providerId, hostId, hostDescription);
...@@ -102,13 +116,8 @@ public class SimpleHostStore { ...@@ -102,13 +116,8 @@ public class SimpleHostStore {
102 return event; 116 return event;
103 } 117 }
104 118
105 - /** 119 + @Override
106 - * Removes the specified host from the inventory. 120 + public HostEvent removeHost(HostId hostId) {
107 - *
108 - * @param hostId host identification
109 - * @return remove event or null if host was not found
110 - */
111 - HostEvent removeHost(HostId hostId) {
112 synchronized (this) { 121 synchronized (this) {
113 Host host = hosts.remove(hostId); 122 Host host = hosts.remove(hostId);
114 if (host != null) { 123 if (host != null) {
...@@ -119,41 +128,23 @@ public class SimpleHostStore { ...@@ -119,41 +128,23 @@ public class SimpleHostStore {
119 } 128 }
120 } 129 }
121 130
122 - /** 131 + @Override
123 - * Returns the number of hosts in the store. 132 + public int getHostCount() {
124 - *
125 - * @return host count
126 - */
127 - int getHostCount() {
128 return hosts.size(); 133 return hosts.size();
129 } 134 }
130 135
131 - /** 136 + @Override
132 - * Returns a collection of all hosts in the store. 137 + public Iterable<Host> getHosts() {
133 - *
134 - * @return iterable collection of all hosts
135 - */
136 - Iterable<Host> getHosts() {
137 return Collections.unmodifiableSet(new HashSet<>(hosts.values())); 138 return Collections.unmodifiableSet(new HashSet<>(hosts.values()));
138 } 139 }
139 140
140 - /** 141 + @Override
141 - * Returns the host with the specified identifer. 142 + public Host getHost(HostId hostId) {
142 - *
143 - * @param hostId host identification
144 - * @return host or null if not found
145 - */
146 - Host getHost(HostId hostId) {
147 return hosts.get(hostId); 143 return hosts.get(hostId);
148 } 144 }
149 145
150 - /** 146 + @Override
151 - * Returns the set of all hosts within the specified VLAN. 147 + public Set<Host> getHosts(VlanId vlanId) {
152 - *
153 - * @param vlanId vlan id
154 - * @return set of hosts in the vlan
155 - */
156 - Set<Host> getHosts(VlanId vlanId) {
157 Set<Host> vlanset = new HashSet<>(); 148 Set<Host> vlanset = new HashSet<>();
158 for (Host h : hosts.values()) { 149 for (Host h : hosts.values()) {
159 if (h.vlan().equals(vlanId)) { 150 if (h.vlan().equals(vlanId)) {
...@@ -163,13 +154,8 @@ public class SimpleHostStore { ...@@ -163,13 +154,8 @@ public class SimpleHostStore {
163 return vlanset; 154 return vlanset;
164 } 155 }
165 156
166 - /** 157 + @Override
167 - * Returns the set of hosts with the specified MAC address. 158 + public Set<Host> getHosts(MacAddress mac) {
168 - *
169 - * @param mac mac address
170 - * @return set of hosts with the given mac
171 - */
172 - Set<Host> getHosts(MacAddress mac) {
173 Set<Host> macset = new HashSet<>(); 159 Set<Host> macset = new HashSet<>();
174 for (Host h : hosts.values()) { 160 for (Host h : hosts.values()) {
175 if (h.mac().equals(mac)) { 161 if (h.mac().equals(mac)) {
...@@ -179,13 +165,8 @@ public class SimpleHostStore { ...@@ -179,13 +165,8 @@ public class SimpleHostStore {
179 return macset; 165 return macset;
180 } 166 }
181 167
182 - /** 168 + @Override
183 - * Returns the set of hosts with the specified IP address. 169 + public Set<Host> getHosts(IpAddress ip) {
184 - *
185 - * @param ip ip address
186 - * @return set of hosts with the given IP
187 - */
188 - Set<Host> getHosts(IpAddress ip) {
189 Set<Host> ipset = new HashSet<>(); 170 Set<Host> ipset = new HashSet<>();
190 for (Host h : hosts.values()) { 171 for (Host h : hosts.values()) {
191 if (h.ipAddresses().contains(ip)) { 172 if (h.ipAddresses().contains(ip)) {
...@@ -195,22 +176,12 @@ public class SimpleHostStore { ...@@ -195,22 +176,12 @@ public class SimpleHostStore {
195 return ipset; 176 return ipset;
196 } 177 }
197 178
198 - /** 179 + @Override
199 - * Returns the set of hosts whose location falls on the given connection point. 180 + public Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
200 - *
201 - * @param connectPoint connection point
202 - * @return set of hosts
203 - */
204 - Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
205 return ImmutableSet.copyOf(locations.get(connectPoint)); 181 return ImmutableSet.copyOf(locations.get(connectPoint));
206 } 182 }
207 183
208 - /** 184 + @Override
209 - * Returns the set of hosts whose location falls on the given device.
210 - *
211 - * @param deviceId infrastructure device identifier
212 - * @return set of hosts
213 - */
214 public Set<Host> getConnectedHosts(DeviceId deviceId) { 185 public Set<Host> getConnectedHosts(DeviceId deviceId) {
215 Set<Host> hostset = new HashSet<>(); 186 Set<Host> hostset = new HashSet<>();
216 for (ConnectPoint p : locations.keySet()) { 187 for (ConnectPoint p : locations.keySet()) {
......
...@@ -3,7 +3,9 @@ package org.onlab.onos.net.trivial.link.impl; ...@@ -3,7 +3,9 @@ package org.onlab.onos.net.trivial.link.impl;
3 import com.google.common.collect.HashMultimap; 3 import com.google.common.collect.HashMultimap;
4 import com.google.common.collect.ImmutableSet; 4 import com.google.common.collect.ImmutableSet;
5 import com.google.common.collect.Multimap; 5 import com.google.common.collect.Multimap;
6 +import org.apache.felix.scr.annotations.Activate;
6 import org.apache.felix.scr.annotations.Component; 7 import org.apache.felix.scr.annotations.Component;
8 +import org.apache.felix.scr.annotations.Deactivate;
7 import org.apache.felix.scr.annotations.Service; 9 import org.apache.felix.scr.annotations.Service;
8 import org.onlab.onos.net.ConnectPoint; 10 import org.onlab.onos.net.ConnectPoint;
9 import org.onlab.onos.net.DefaultLink; 11 import org.onlab.onos.net.DefaultLink;
...@@ -13,6 +15,7 @@ import org.onlab.onos.net.link.LinkDescription; ...@@ -13,6 +15,7 @@ import org.onlab.onos.net.link.LinkDescription;
13 import org.onlab.onos.net.link.LinkEvent; 15 import org.onlab.onos.net.link.LinkEvent;
14 import org.onlab.onos.net.link.LinkStore; 16 import org.onlab.onos.net.link.LinkStore;
15 import org.onlab.onos.net.provider.ProviderId; 17 import org.onlab.onos.net.provider.ProviderId;
18 +import org.slf4j.Logger;
16 19
17 import java.util.Collections; 20 import java.util.Collections;
18 import java.util.HashSet; 21 import java.util.HashSet;
...@@ -24,6 +27,7 @@ import java.util.concurrent.ConcurrentHashMap; ...@@ -24,6 +27,7 @@ import java.util.concurrent.ConcurrentHashMap;
24 import static org.onlab.onos.net.Link.Type.DIRECT; 27 import static org.onlab.onos.net.Link.Type.DIRECT;
25 import static org.onlab.onos.net.Link.Type.INDIRECT; 28 import static org.onlab.onos.net.Link.Type.INDIRECT;
26 import static org.onlab.onos.net.link.LinkEvent.Type.*; 29 import static org.onlab.onos.net.link.LinkEvent.Type.*;
30 +import static org.slf4j.LoggerFactory.getLogger;
27 31
28 /** 32 /**
29 * Manages inventory of infrastructure links using trivial in-memory structures 33 * Manages inventory of infrastructure links using trivial in-memory structures
...@@ -33,6 +37,8 @@ import static org.onlab.onos.net.link.LinkEvent.Type.*; ...@@ -33,6 +37,8 @@ import static org.onlab.onos.net.link.LinkEvent.Type.*;
33 @Service 37 @Service
34 public class SimpleLinkStore implements LinkStore { 38 public class SimpleLinkStore implements LinkStore {
35 39
40 + private final Logger log = getLogger(getClass());
41 +
36 // Link inventory 42 // Link inventory
37 private final Map<LinkKey, DefaultLink> links = new ConcurrentHashMap<>(); 43 private final Map<LinkKey, DefaultLink> links = new ConcurrentHashMap<>();
38 44
...@@ -40,7 +46,17 @@ public class SimpleLinkStore implements LinkStore { ...@@ -40,7 +46,17 @@ public class SimpleLinkStore implements LinkStore {
40 private final Multimap<DeviceId, Link> srcLinks = HashMultimap.create(); 46 private final Multimap<DeviceId, Link> srcLinks = HashMultimap.create();
41 private final Multimap<DeviceId, Link> dstLinks = HashMultimap.create(); 47 private final Multimap<DeviceId, Link> dstLinks = HashMultimap.create();
42 48
43 - private static final Set<Link> EMPTY = ImmutableSet.copyOf(new Link[]{}); 49 + private static final Set<Link> EMPTY = ImmutableSet.of();
50 +
51 + @Activate
52 + public void activate() {
53 + log.info("Started");
54 + }
55 +
56 + @Deactivate
57 + public void deactivate() {
58 + log.info("Stopped");
59 + }
44 60
45 @Override 61 @Override
46 public int getLinkCount() { 62 public int getLinkCount() {
......
...@@ -73,6 +73,7 @@ public class SimpleHostManagerTest { ...@@ -73,6 +73,7 @@ public class SimpleHostManagerTest {
73 @Before 73 @Before
74 public void setUp() { 74 public void setUp() {
75 mgr = new SimpleHostManager(); 75 mgr = new SimpleHostManager();
76 + mgr.store = new SimpleHostStore();
76 mgr.eventDispatcher = new TestEventDispatcher(); 77 mgr.eventDispatcher = new TestEventDispatcher();
77 registry = mgr; 78 registry = mgr;
78 mgr.activate(); 79 mgr.activate();
......