blob: 469d33500e20ea67e6fa16047da472ef4c23100b [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Toshio Koide5799b602014-02-10 15:29:06 -08002
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07003import java.util.ArrayList;
4import java.util.List;
5
6import org.apache.commons.lang.Validate;
Toshio Koide5799b602014-02-10 15:29:06 -08007
8import net.floodlightcontroller.util.MACAddress;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07009import net.onrc.onos.core.util.SwitchPort;
Toshio Koide5799b602014-02-10 15:29:06 -080010
11/**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070012 * Host Object stored in In-memory Topology.
Toshio Koide5799b602014-02-10 15:29:06 -080013 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070014public class HostImpl extends TopologyObject implements Host {
Toshio Koide5799b602014-02-10 15:29:06 -080015
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070016 //////////////////////////////////////////////////////
17 /// Topology element attributes
18 /// - any changes made here needs to be replicated.
19 //////////////////////////////////////////////////////
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070020 private HostEvent deviceObj;
Toshio Koide5799b602014-02-10 15:29:06 -080021
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070022
23 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070024 * Creates a Host object based on {@link HostEvent}.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070025 *
26 * @param topology Topology instance this object belongs to
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070027 * @param scHost self contained {@link HostEvent}
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070028 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070029 public HostImpl(Topology topology, HostEvent scHost) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070030 super(topology);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070031 Validate.notNull(scHost);
32
33 // TODO should we assume deviceObj is already frozen before this call
34 // or expect attribute update will happen after .
35 if (scHost.isFrozen()) {
36 this.deviceObj = scHost;
37 } else {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070038 this.deviceObj = new HostEvent(scHost);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070039 this.deviceObj.freeze();
40 }
41 }
42
43 /**
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070044 * Creates a Host object with empty attributes.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070045 *
46 * @param topology Topology instance this object belongs to
47 * @param mac MAC address of the host
48 */
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070049 public HostImpl(Topology topology, MACAddress mac) {
50 this(topology, new HostEvent(mac).freeze());
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080051 }
Toshio Koide5799b602014-02-10 15:29:06 -080052
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080053 @Override
54 public MACAddress getMacAddress() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070055 return this.deviceObj.getMac();
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080056 }
Toshio Koide5799b602014-02-10 15:29:06 -080057
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080058 @Override
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080059 public Iterable<Port> getAttachmentPoints() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070060 List<Port> ports = new ArrayList<>();
61 topology.acquireReadLock();
62 try {
63 for (SwitchPort swp : this.deviceObj.getAttachmentPoints()) {
64 Port p = this.topology.getPort(swp);
65 if (p != null) {
66 ports.add(p);
67 }
68 }
69 } finally {
70 topology.releaseReadLock();
71 }
72 return ports;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080073 }
74
75 @Override
76 public long getLastSeenTime() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070077 return deviceObj.getLastSeenTime();
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080078 }
79
80 @Override
81 public String toString() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070082 return getMacAddress().toString();
TeruUd1c5b652014-03-24 13:58:46 -070083 }
Ray Milkey269ffb92014-04-03 14:43:30 -070084
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070085 // TODO we may no longer need this. confirm and delete later.
TeruUd1c5b652014-03-24 13:58:46 -070086 void setLastSeenTime(long lastSeenTime) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070087 // XXX Following will make this instance thread unsafe. Need to use AtomicRef.
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070088 HostEvent updated = new HostEvent(this.deviceObj);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070089 updated.setLastSeenTime(lastSeenTime);
90 updated.freeze();
91 this.deviceObj = updated;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080092 }
93
94 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -070095 * Only {@link TopologyManager} should use this method.
Ray Milkey269ffb92014-04-03 14:43:30 -070096 *
TeruU5d2c9392014-06-09 20:02:02 -070097 * @param port the port that the device is attached to
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080098 */
TeruU5d2c9392014-06-09 20:02:02 -070099 void addAttachmentPoint(Port port) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700100 // XXX Following will make this instance thread unsafe. Need to use AtomicRef.
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700101 HostEvent updated = new HostEvent(this.deviceObj);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700102 updated.removeAttachmentPoint(port.asSwitchPort());
103 updated.addAttachmentPoint(port.asSwitchPort());
104 updated.freeze();
105 this.deviceObj = updated;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -0800106 }
107
108 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700109 * Only {@link TopologyManager} should use this method.
Ray Milkey269ffb92014-04-03 14:43:30 -0700110 *
TeruU5d2c9392014-06-09 20:02:02 -0700111 * @param port the port that the device is attached to
Yuta HIGUCHIc0366272014-02-10 21:04:57 -0800112 */
TeruU5d2c9392014-06-09 20:02:02 -0700113 boolean removeAttachmentPoint(Port port) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700114 // XXX Following will make this instance thread unsafe. Need to use AtomicRef.
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700115 HostEvent updated = new HostEvent(this.deviceObj);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700116 final boolean result = updated.removeAttachmentPoint(port.asSwitchPort());
117 updated.freeze();
118 this.deviceObj = updated;
119 return result;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -0800120 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700121
122
123 /**
124 * Returns the type of topology object.
125 *
126 * @return the type of the topology object
127 */
128 @Override
129 public String getType() {
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -0700130 // FIXME assuming device is always in packet layer for now.
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700131 return TopologyElement.TYPE_PACKET_LAYER;
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700132 }
Toshio Koide5799b602014-02-10 15:29:06 -0800133}