Jonathan Hart | 472062d | 2014-04-03 10:56:48 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 2 | |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 3 | import java.util.Collection; |
Yuta HIGUCHI | db1b830 | 2014-06-26 10:50:39 -0700 | [diff] [blame] | 4 | import java.util.Map; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 5 | |
| 6 | import org.apache.commons.lang.Validate; |
| 7 | |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 8 | import net.onrc.onos.core.util.Dpid; |
| 9 | import net.onrc.onos.core.util.PortNumber; |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 10 | import net.onrc.onos.core.util.SwitchPort; |
| 11 | |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 12 | /** |
| 13 | * Port Object stored in In-memory Topology. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 14 | * <p/> |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 15 | * TODO REMOVE following design memo: This object itself may hold the DBObject, |
| 16 | * but this Object itself will not issue any read/write to the DataStore. |
| 17 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 18 | public class PortImpl extends TopologyObject implements Port { |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 19 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 20 | ////////////////////////////////////////////////////// |
| 21 | /// Topology element attributes |
| 22 | /// - any changes made here needs to be replicated. |
| 23 | ////////////////////////////////////////////////////// |
| 24 | private PortEvent portObj; |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 25 | |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 26 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 27 | /** |
| 28 | * Creates a Port object based on {@link PortEvent}. |
| 29 | * |
| 30 | * @param topology Topology instance this object belongs to |
| 31 | * @param scPort self contained {@link PortEvent} |
| 32 | */ |
| 33 | public PortImpl(Topology topology, PortEvent scPort) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 34 | super(topology); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 35 | Validate.notNull(scPort); |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 36 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 37 | // TODO should we assume portObj is already frozen before this call |
| 38 | // or expect attribute update will happen after . |
| 39 | if (scPort.isFrozen()) { |
| 40 | this.portObj = scPort; |
| 41 | } else { |
| 42 | this.portObj = new PortEvent(scPort); |
| 43 | this.portObj.freeze(); |
| 44 | } |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 47 | /** |
| 48 | * Creates a Port object with empty attributes. |
| 49 | * |
| 50 | * @param topology Topology instance this object belongs to |
| 51 | * @param switchPort SwitchPort |
| 52 | */ |
| 53 | public PortImpl(Topology topology, SwitchPort switchPort) { |
| 54 | this(topology, new PortEvent(switchPort).freeze()); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Creates a Port object with empty attributes. |
| 59 | * |
| 60 | * @param topology Topology instance this object belongs to |
| 61 | * @param dpid DPID |
| 62 | * @param number PortNumber |
| 63 | */ |
| 64 | public PortImpl(Topology topology, Dpid dpid, PortNumber number) { |
| 65 | this(topology, new SwitchPort(dpid, number)); |
| 66 | Validate.notNull(dpid); |
| 67 | Validate.notNull(number); |
| 68 | } |
| 69 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 70 | @Override |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 71 | public Dpid getDpid() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 72 | return asSwitchPort().getDpid(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 73 | } |
Yuta HIGUCHI | ea516d6 | 2014-02-13 15:59:32 -0800 | [diff] [blame] | 74 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 75 | @Override |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 76 | public PortNumber getNumber() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 77 | return asSwitchPort().getPortNumber(); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 78 | } |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 79 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 80 | @Override |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 81 | public SwitchPort asSwitchPort() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 82 | return portObj.getSwitchPort(); |
Jonathan Hart | 25bd53e | 2014-04-30 23:44:09 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 86 | public String getDescription() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 87 | return getStringAttribute(PortEvent.DESCRIPTION, ""); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 88 | } |
Yuta HIGUCHI | c036627 | 2014-02-10 21:04:57 -0800 | [diff] [blame] | 89 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 90 | void setDescription(String description) { |
| 91 | // portObj.createStringAttribute(attr, value); |
| 92 | // TODO implement using attributes |
Yuta HIGUCHI | db1b830 | 2014-06-26 10:50:39 -0700 | [diff] [blame] | 93 | throw new UnsupportedOperationException("Not implemented yet"); |
| 94 | } |
| 95 | |
| 96 | @Override |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 97 | public Long getHardwareAddress() { |
| 98 | // TODO implement using attributes? |
| 99 | throw new UnsupportedOperationException("Not implemented yet"); |
| 100 | } |
| 101 | |
| 102 | @Override |
| 103 | public Switch getSwitch() { |
| 104 | topology.acquireReadLock(); |
| 105 | try { |
| 106 | return topology.getSwitch(getDpid()); |
| 107 | } finally { |
| 108 | topology.releaseReadLock(); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public Link getOutgoingLink() { |
| 114 | topology.acquireReadLock(); |
| 115 | try { |
| 116 | return topology.getOutgoingLink(asSwitchPort()); |
| 117 | } finally { |
| 118 | topology.releaseReadLock(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | @Override |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 123 | public Link getOutgoingLink(String type) { |
| 124 | topology.acquireReadLock(); |
| 125 | try { |
| 126 | return topology.getOutgoingLink(asSwitchPort(), type); |
| 127 | } finally { |
| 128 | topology.releaseReadLock(); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | @Override |
| 133 | public Collection<Link> getOutgoingLinks() { |
| 134 | topology.acquireReadLock(); |
| 135 | try { |
| 136 | return topology.getOutgoingLinks(asSwitchPort()); |
| 137 | } finally { |
| 138 | topology.releaseReadLock(); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 143 | public Link getIncomingLink() { |
| 144 | topology.acquireReadLock(); |
| 145 | try { |
| 146 | return topology.getIncomingLink(asSwitchPort()); |
| 147 | } finally { |
| 148 | topology.releaseReadLock(); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | @Override |
Yuta HIGUCHI | 8313f0b | 2014-07-09 16:36:03 -0700 | [diff] [blame] | 153 | public Link getIncomingLink(String type) { |
| 154 | topology.acquireReadLock(); |
| 155 | try { |
| 156 | return topology.getIncomingLink(asSwitchPort(), type); |
| 157 | } finally { |
| 158 | topology.releaseReadLock(); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | @Override |
| 163 | public Collection<Link> getIncomingLinks() { |
| 164 | topology.acquireReadLock(); |
| 165 | try { |
| 166 | return topology.getIncomingLinks(asSwitchPort()); |
| 167 | } finally { |
| 168 | topology.releaseReadLock(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | @Override |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 173 | public Iterable<Host> getHosts() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 174 | topology.acquireReadLock(); |
| 175 | try { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 176 | return topology.getHosts(this.asSwitchPort()); |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 177 | } finally { |
| 178 | topology.releaseReadLock(); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | void replaceStringAttributes(PortEvent updated) { |
| 183 | Validate.isTrue(this.asSwitchPort().equals(updated.getSwitchPort()), |
| 184 | "Wrong PortEvent given."); |
| 185 | |
| 186 | // XXX simply replacing whole self-contained object for now |
| 187 | if (updated.isFrozen()) { |
| 188 | this.portObj = updated; |
| 189 | } else { |
| 190 | this.portObj = new PortEvent(updated).freeze(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public String getStringAttribute(String attr) { |
| 196 | return portObj.getStringAttribute(attr); |
| 197 | } |
| 198 | |
| 199 | @Override |
Yuta HIGUCHI | db1b830 | 2014-06-26 10:50:39 -0700 | [diff] [blame] | 200 | public String getStringAttribute(String attr, String def) { |
| 201 | final String v = getStringAttribute(attr); |
| 202 | if (v == null) { |
| 203 | return def; |
| 204 | } else { |
| 205 | return v; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | @Override |
| 210 | public Map<String, String> getAllStringAttributes() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 211 | return portObj.getAllStringAttributes(); |
Yuta HIGUCHI | db1b830 | 2014-06-26 10:50:39 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | @Override |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 215 | public String toString() { |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 216 | return String.format("%s:%s", |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 217 | getSwitch().getDpid(), |
| 218 | getNumber()); |
| 219 | } |
Praseed Balakrishnan | 57ed843 | 2014-06-26 11:49:56 -0700 | [diff] [blame] | 220 | |
| 221 | |
| 222 | /** |
| 223 | * Returns the type of topology object. |
| 224 | * |
| 225 | * @return the type of the topology object |
| 226 | */ |
| 227 | @Override |
| 228 | public String getType() { |
Yuta HIGUCHI | dbc3312 | 2014-07-10 13:32:32 -0700 | [diff] [blame] | 229 | return getStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER); |
Praseed Balakrishnan | 57ed843 | 2014-06-26 11:49:56 -0700 | [diff] [blame] | 230 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 231 | } |