blob: 61de0d2213b3c4a4c67310b58f5f5b7255bcadc8 [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 HIGUCHId4acc802014-06-19 22:30:31 -070012 * Device Object stored in In-memory Topology.
Toshio Koide5799b602014-02-10 15:29:06 -080013 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070014public class DeviceImpl extends TopologyObject implements Device {
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 //////////////////////////////////////////////////////
20 private DeviceEvent deviceObj;
Toshio Koide5799b602014-02-10 15:29:06 -080021
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070022 ///////////////////
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 Harte37e4e22014-05-13 19:12:02 -070035 super(topology);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070036 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 HIGUCHIc0366272014-02-10 21:04:57 -080056 }
Toshio Koide5799b602014-02-10 15:29:06 -080057
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080058 @Override
59 public MACAddress getMacAddress() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070060 return this.deviceObj.getMac();
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080061 }
Toshio Koide5799b602014-02-10 15:29:06 -080062
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080063 @Override
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080064 public Iterable<Port> getAttachmentPoints() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070065 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 HIGUCHIc0366272014-02-10 21:04:57 -080078 }
79
80 @Override
81 public long getLastSeenTime() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070082 return deviceObj.getLastSeenTime();
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080083 }
84
85 @Override
86 public String toString() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070087 return getMacAddress().toString();
TeruUd1c5b652014-03-24 13:58:46 -070088 }
Ray Milkey269ffb92014-04-03 14:43:30 -070089
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070090 // TODO we may no longer need this. confirm and delete later.
TeruUd1c5b652014-03-24 13:58:46 -070091 void setLastSeenTime(long lastSeenTime) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070092 // 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 HIGUCHIc0366272014-02-10 21:04:57 -080097 }
98
99 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700100 * Only {@link TopologyManager} should use this method.
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 *
TeruU5d2c9392014-06-09 20:02:02 -0700102 * @param port the port that the device is attached to
Yuta HIGUCHIc0366272014-02-10 21:04:57 -0800103 */
TeruU5d2c9392014-06-09 20:02:02 -0700104 void addAttachmentPoint(Port port) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700105 // 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 HIGUCHIc0366272014-02-10 21:04:57 -0800111 }
112
113 /**
Ray Milkeyb41100a2014-04-10 10:42:15 -0700114 * Only {@link TopologyManager} should use this method.
Ray Milkey269ffb92014-04-03 14:43:30 -0700115 *
TeruU5d2c9392014-06-09 20:02:02 -0700116 * @param port the port that the device is attached to
Yuta HIGUCHIc0366272014-02-10 21:04:57 -0800117 */
TeruU5d2c9392014-06-09 20:02:02 -0700118 boolean removeAttachmentPoint(Port port) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700119 // 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 HIGUCHIc0366272014-02-10 21:04:57 -0800125 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700126
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() {
135 throw new UnsupportedOperationException("Not implemented yet");
136 }
Toshio Koide5799b602014-02-10 15:29:06 -0800137}