Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
Toshio Koide | 5799b60 | 2014-02-10 15:29:06 -0800 | [diff] [blame] | 2 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import org.apache.commons.lang.Validate; |
Toshio Koide | 5799b60 | 2014-02-10 15:29:06 -0800 | [diff] [blame] | 7 | |
| 8 | import net.floodlightcontroller.util.MACAddress; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 9 | import net.onrc.onos.core.util.SwitchPort; |
Toshio Koide | 5799b60 | 2014-02-10 15:29:06 -0800 | [diff] [blame] | 10 | |
| 11 | /** |
Yuta HIGUCHI | d4acc80 | 2014-06-19 22:30:31 -0700 | [diff] [blame] | 12 | * Device Object stored in In-memory Topology. |
Toshio Koide | 5799b60 | 2014-02-10 15:29:06 -0800 | [diff] [blame] | 13 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 14 | public class DeviceImpl extends TopologyObject implements Device { |
Toshio Koide | 5799b60 | 2014-02-10 15:29:06 -0800 | [diff] [blame] | 15 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 16 | ////////////////////////////////////////////////////// |
| 17 | /// Topology element attributes |
| 18 | /// - any changes made here needs to be replicated. |
| 19 | ////////////////////////////////////////////////////// |
| 20 | private DeviceEvent deviceObj; |
Toshio Koide | 5799b60 | 2014-02-10 15:29:06 -0800 | [diff] [blame] | 21 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 22 | /////////////////// |
| 23 | /// In-memory index |
| 24 | /////////////////// |
| 25 | |
| 26 | // none |
| 27 | |
| 28 | /** |
| 29 | * Creates a Device object based on {@link DeviceEvent}. |
| 30 | * |
| 31 | * @param topology Topology instance this object belongs to |
| 32 | * @param scHost self contained {@link DeviceEvent} |
| 33 | */ |
| 34 | public DeviceImpl(Topology topology, DeviceEvent scHost) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 35 | super(topology); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 36 | Validate.notNull(scHost); |
| 37 | |
| 38 | // TODO should we assume deviceObj is already frozen before this call |
| 39 | // or expect attribute update will happen after . |
| 40 | if (scHost.isFrozen()) { |
| 41 | this.deviceObj = scHost; |
| 42 | } else { |
| 43 | this.deviceObj = new DeviceEvent(scHost); |
| 44 | this.deviceObj.freeze(); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Creates a Device object with empty attributes. |
| 50 | * |
| 51 | * @param topology Topology instance this object belongs to |
| 52 | * @param mac MAC address of the host |
| 53 | */ |
| 54 | public DeviceImpl(Topology topology, MACAddress mac) { |
| 55 | this(topology, new DeviceEvent(mac).freeze()); |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 56 | } |
Toshio Koide | 5799b60 | 2014-02-10 15:29:06 -0800 | [diff] [blame] | 57 | |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 58 | @Override |
| 59 | public MACAddress getMacAddress() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 60 | return this.deviceObj.getMac(); |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 61 | } |
Toshio Koide | 5799b60 | 2014-02-10 15:29:06 -0800 | [diff] [blame] | 62 | |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 63 | @Override |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 64 | public Iterable<Port> getAttachmentPoints() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 65 | List<Port> ports = new ArrayList<>(); |
| 66 | topology.acquireReadLock(); |
| 67 | try { |
| 68 | for (SwitchPort swp : this.deviceObj.getAttachmentPoints()) { |
| 69 | Port p = this.topology.getPort(swp); |
| 70 | if (p != null) { |
| 71 | ports.add(p); |
| 72 | } |
| 73 | } |
| 74 | } finally { |
| 75 | topology.releaseReadLock(); |
| 76 | } |
| 77 | return ports; |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public long getLastSeenTime() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 82 | return deviceObj.getLastSeenTime(); |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public String toString() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 87 | return getMacAddress().toString(); |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 88 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 89 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 90 | // TODO we may no longer need this. confirm and delete later. |
TeruU | d1c5b65 | 2014-03-24 13:58:46 -0700 | [diff] [blame] | 91 | void setLastSeenTime(long lastSeenTime) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 92 | // XXX Following will make this instance thread unsafe. Need to use AtomicRef. |
| 93 | DeviceEvent updated = new DeviceEvent(this.deviceObj); |
| 94 | updated.setLastSeenTime(lastSeenTime); |
| 95 | updated.freeze(); |
| 96 | this.deviceObj = updated; |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 100 | * Only {@link TopologyManager} should use this method. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 101 | * |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 102 | * @param port the port that the device is attached to |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 103 | */ |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 104 | void addAttachmentPoint(Port port) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 105 | // XXX Following will make this instance thread unsafe. Need to use AtomicRef. |
| 106 | DeviceEvent updated = new DeviceEvent(this.deviceObj); |
| 107 | updated.removeAttachmentPoint(port.asSwitchPort()); |
| 108 | updated.addAttachmentPoint(port.asSwitchPort()); |
| 109 | updated.freeze(); |
| 110 | this.deviceObj = updated; |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
Ray Milkey | b41100a | 2014-04-10 10:42:15 -0700 | [diff] [blame] | 114 | * Only {@link TopologyManager} should use this method. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 115 | * |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 116 | * @param port the port that the device is attached to |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 117 | */ |
TeruU | 5d2c939 | 2014-06-09 20:02:02 -0700 | [diff] [blame] | 118 | boolean removeAttachmentPoint(Port port) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 119 | // XXX Following will make this instance thread unsafe. Need to use AtomicRef. |
| 120 | DeviceEvent updated = new DeviceEvent(this.deviceObj); |
| 121 | final boolean result = updated.removeAttachmentPoint(port.asSwitchPort()); |
| 122 | updated.freeze(); |
| 123 | this.deviceObj = updated; |
| 124 | return result; |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 125 | } |
Praseed Balakrishnan | 57ed843 | 2014-06-26 11:49:56 -0700 | [diff] [blame] | 126 | |
| 127 | |
| 128 | /** |
| 129 | * Returns the type of topology object. |
| 130 | * |
| 131 | * @return the type of the topology object |
| 132 | */ |
| 133 | @Override |
| 134 | public String getType() { |
Yuta HIGUCHI | 1222ac5 | 2014-07-09 16:50:28 -0700 | [diff] [blame] | 135 | // FIXME assuming device is always in packet layer for now. |
Yuta HIGUCHI | dbc3312 | 2014-07-10 13:32:32 -0700 | [diff] [blame] | 136 | return TopologyElement.TYPE_PACKET_LAYER; |
Praseed Balakrishnan | 57ed843 | 2014-06-26 11:49:56 -0700 | [diff] [blame] | 137 | } |
Toshio Koide | 5799b60 | 2014-02-10 15:29:06 -0800 | [diff] [blame] | 138 | } |