Ayaka Koshibe

added functionality to SimpleHostStore

Change-Id: I2e73e026149ecb547b320d321cb86c48d051037c
...@@ -18,7 +18,7 @@ public class DefaultHost extends AbstractElement implements Host { ...@@ -18,7 +18,7 @@ public class DefaultHost extends AbstractElement implements Host {
18 private final HostLocation location; 18 private final HostLocation location;
19 private final Set<IPv4> ips; 19 private final Set<IPv4> ips;
20 20
21 - protected DefaultHost(ProviderId providerId, ElementId id, MACAddress mac, 21 + public DefaultHost(ProviderId providerId, ElementId id, MACAddress mac,
22 short vlan, HostLocation loc, Set<IPv4> ips) { 22 short vlan, HostLocation loc, Set<IPv4> ips) {
23 super(providerId, id); 23 super(providerId, id);
24 this.mac = mac; 24 this.mac = mac;
......
...@@ -2,7 +2,6 @@ package org.onlab.onos.net.host; ...@@ -2,7 +2,6 @@ package org.onlab.onos.net.host;
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.Collections;
6 import java.util.HashSet; 5 import java.util.HashSet;
7 import java.util.Set; 6 import java.util.Set;
8 7
...@@ -10,6 +9,8 @@ import org.onlab.onos.net.HostLocation; ...@@ -10,6 +9,8 @@ import org.onlab.onos.net.HostLocation;
10 import org.onlab.packet.IPv4; 9 import org.onlab.packet.IPv4;
11 import org.onlab.packet.MACAddress; 10 import org.onlab.packet.MACAddress;
12 11
12 +import com.google.common.collect.ImmutableSet;
13 +
13 public class DefaultHostDescription implements HostDescription { 14 public class DefaultHostDescription implements HostDescription {
14 15
15 private final MACAddress mac; 16 private final MACAddress mac;
...@@ -42,7 +43,7 @@ public class DefaultHostDescription implements HostDescription { ...@@ -42,7 +43,7 @@ public class DefaultHostDescription implements HostDescription {
42 43
43 @Override 44 @Override
44 public Set<IPv4> ipAddresses() { 45 public Set<IPv4> ipAddresses() {
45 - return Collections.unmodifiableSet(ips); 46 + return ImmutableSet.copyOf(ips);
46 } 47 }
47 48
48 @Override 49 @Override
......
1 package org.onlab.onos.net.trivial.impl; 1 package org.onlab.onos.net.trivial.impl;
2 2
3 import org.onlab.onos.net.ConnectPoint; 3 import org.onlab.onos.net.ConnectPoint;
4 +import org.onlab.onos.net.DefaultHost;
4 import org.onlab.onos.net.DeviceId; 5 import org.onlab.onos.net.DeviceId;
5 import org.onlab.onos.net.Host; 6 import org.onlab.onos.net.Host;
6 import org.onlab.onos.net.HostId; 7 import org.onlab.onos.net.HostId;
...@@ -10,10 +11,21 @@ import org.onlab.onos.net.provider.ProviderId; ...@@ -10,10 +11,21 @@ import org.onlab.onos.net.provider.ProviderId;
10 import org.onlab.packet.IPv4; 11 import org.onlab.packet.IPv4;
11 import org.onlab.packet.MACAddress; 12 import org.onlab.packet.MACAddress;
12 13
14 +import com.google.common.collect.HashMultimap;
15 +import com.google.common.collect.ImmutableSet;
16 +import com.google.common.collect.Multimap;
17 +
18 +import java.util.Collections;
19 +import java.util.HashSet;
13 import java.util.Map; 20 import java.util.Map;
14 import java.util.Set; 21 import java.util.Set;
15 import java.util.concurrent.ConcurrentHashMap; 22 import java.util.concurrent.ConcurrentHashMap;
16 23
24 +import static org.onlab.onos.net.host.HostEvent.Type.HOST_REMOVED;
25 +import static org.onlab.onos.net.host.HostEvent.Type.HOST_ADDED;
26 +import static org.onlab.onos.net.host.HostEvent.Type.HOST_UPDATED;
27 +import static org.onlab.onos.net.host.HostEvent.Type.HOST_MOVED;
28 +
17 /** 29 /**
18 * Manages inventory of end-station hosts using trivial in-memory 30 * Manages inventory of end-station hosts using trivial in-memory
19 * implementation. 31 * implementation.
...@@ -22,6 +34,8 @@ public class SimpleHostStore { ...@@ -22,6 +34,8 @@ public class SimpleHostStore {
22 34
23 private final Map<HostId, Host> hosts = new ConcurrentHashMap<>(); 35 private final Map<HostId, Host> hosts = new ConcurrentHashMap<>();
24 36
37 + // hosts sorted based on their location
38 + private final Multimap<ConnectPoint, Host> locations = HashMultimap.create();
25 /** 39 /**
26 * Creates a new host or updates the existing one based on the specified 40 * Creates a new host or updates the existing one based on the specified
27 * description. 41 * description.
...@@ -33,7 +47,54 @@ public class SimpleHostStore { ...@@ -33,7 +47,54 @@ public class SimpleHostStore {
33 */ 47 */
34 HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId, 48 HostEvent createOrUpdateHost(ProviderId providerId, HostId hostId,
35 HostDescription hostDescription) { 49 HostDescription hostDescription) {
36 - return null; 50 + Host host = hosts.get(hostId);
51 + if (host == null) {
52 + return createHost(providerId, hostId, hostDescription);
53 + }
54 + return updateHost(providerId, host, hostDescription);
55 + }
56 +
57 + // creates a new host and sends HOST_ADDED
58 + private HostEvent createHost(ProviderId providerId, HostId hostId,
59 + HostDescription descr) {
60 + DefaultHost newhost = new DefaultHost(providerId, hostId,
61 + descr.hwAddress(),
62 + descr.vlan(),
63 + descr.location(),
64 + descr.ipAddresses());
65 + synchronized(this) {
66 + hosts.put(hostId, newhost);
67 + locations.put(descr.location(), newhost);
68 + }
69 + return new HostEvent(HOST_ADDED, newhost);
70 + }
71 +
72 + // checks for type of update to host, sends appropriate event
73 + private HostEvent updateHost(ProviderId providerId, Host host,
74 + HostDescription descr) {
75 + DefaultHost updated;
76 + HostEvent event;
77 + if (host.location().equals(descr.location())) {
78 + updated = new DefaultHost(providerId, host.id(),
79 + host.mac(),
80 + host.vlan(),
81 + host.location(),
82 + descr.ipAddresses());
83 + event = new HostEvent(HOST_UPDATED, updated);
84 + } else {
85 + updated = new DefaultHost(providerId, host.id(),
86 + host.mac(),
87 + host.vlan(),
88 + descr.location(),
89 + host.ipAddresses());
90 + event = new HostEvent(HOST_MOVED, updated);
91 + }
92 + synchronized (this) {
93 + hosts.put(host.id(), updated);
94 + locations.remove(host.location(), host);
95 + locations.put(updated.location(), updated);
96 + }
97 + return event;
37 } 98 }
38 99
39 /** 100 /**
...@@ -43,7 +104,14 @@ public class SimpleHostStore { ...@@ -43,7 +104,14 @@ public class SimpleHostStore {
43 * @return remove even or null if host was not found 104 * @return remove even or null if host was not found
44 */ 105 */
45 HostEvent removeHost(HostId hostId) { 106 HostEvent removeHost(HostId hostId) {
46 - return null; 107 + synchronized(this) {
108 + Host host = hosts.remove(hostId);
109 + if (host != null) {
110 + locations.remove((host.location()), host);
111 + return new HostEvent(HOST_REMOVED, host);
112 + }
113 + return null;
114 + }
47 } 115 }
48 116
49 /** 117 /**
...@@ -61,7 +129,7 @@ public class SimpleHostStore { ...@@ -61,7 +129,7 @@ public class SimpleHostStore {
61 * @return iterable collection of all hosts 129 * @return iterable collection of all hosts
62 */ 130 */
63 Iterable<Host> getHosts() { 131 Iterable<Host> getHosts() {
64 - return null; 132 + return Collections.unmodifiableSet(new HashSet<Host>(hosts.values()));
65 } 133 }
66 134
67 /** 135 /**
...@@ -71,7 +139,7 @@ public class SimpleHostStore { ...@@ -71,7 +139,7 @@ public class SimpleHostStore {
71 * @return host or null if not found 139 * @return host or null if not found
72 */ 140 */
73 Host getHost(HostId hostId) { 141 Host getHost(HostId hostId) {
74 - return null; 142 + return hosts.get(hostId);
75 } 143 }
76 144
77 /** 145 /**
...@@ -81,7 +149,13 @@ public class SimpleHostStore { ...@@ -81,7 +149,13 @@ public class SimpleHostStore {
81 * @return set of hosts in the vlan 149 * @return set of hosts in the vlan
82 */ 150 */
83 Set<Host> getHosts(long vlanId) { 151 Set<Host> getHosts(long vlanId) {
84 - return null; 152 + Set<Host> vlanset = new HashSet<Host>();
153 + for (Host h : hosts.values()) {
154 + if (h.vlan() == vlanId) {
155 + vlanset.add(h);
156 + }
157 + }
158 + return vlanset;
85 } 159 }
86 160
87 /** 161 /**
...@@ -91,7 +165,13 @@ public class SimpleHostStore { ...@@ -91,7 +165,13 @@ public class SimpleHostStore {
91 * @return set of hosts with the given mac 165 * @return set of hosts with the given mac
92 */ 166 */
93 Set<Host> getHosts(MACAddress mac) { 167 Set<Host> getHosts(MACAddress mac) {
94 - return null; 168 + Set<Host> macset = new HashSet<>();
169 + for (Host h : hosts.values()) {
170 + if (h.mac().equals(mac)) {
171 + macset.add(h);
172 + }
173 + }
174 + return macset;
95 } 175 }
96 176
97 /** 177 /**
...@@ -101,7 +181,13 @@ public class SimpleHostStore { ...@@ -101,7 +181,13 @@ public class SimpleHostStore {
101 * @return set of hosts with the given IP 181 * @return set of hosts with the given IP
102 */ 182 */
103 Set<Host> getHosts(IPv4 ip) { 183 Set<Host> getHosts(IPv4 ip) {
104 - return null; 184 + Set<Host> ipset = new HashSet<>();
185 + for (Host h : hosts.values()) {
186 + if (h.ipAddresses().contains(ip)) {
187 + ipset.add(h);
188 + }
189 + }
190 + return ipset;
105 } 191 }
106 192
107 /** 193 /**
...@@ -111,7 +197,7 @@ public class SimpleHostStore { ...@@ -111,7 +197,7 @@ public class SimpleHostStore {
111 * @return set of hosts 197 * @return set of hosts
112 */ 198 */
113 Set<Host> getConnectedHosts(ConnectPoint connectPoint) { 199 Set<Host> getConnectedHosts(ConnectPoint connectPoint) {
114 - return null; 200 + return ImmutableSet.copyOf(locations.get(connectPoint));
115 } 201 }
116 202
117 /** 203 /**
...@@ -121,7 +207,13 @@ public class SimpleHostStore { ...@@ -121,7 +207,13 @@ public class SimpleHostStore {
121 * @return set of hosts 207 * @return set of hosts
122 */ 208 */
123 public Set<Host> getConnectedHosts(DeviceId deviceId) { 209 public Set<Host> getConnectedHosts(DeviceId deviceId) {
124 - return null; 210 + Set<Host> hostset = new HashSet<>();
211 + for (ConnectPoint p : locations.keySet()) {
212 + if (p.deviceId().equals(deviceId)) {
213 + hostset.addAll(locations.get(p));
214 + }
215 + }
216 + return hostset;
125 } 217 }
126 218
127 -} 219 +}
...\ No newline at end of file ...\ No newline at end of file
......