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