Yuta HIGUCHI | 9e6223d | 2014-08-26 00:01:32 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.topology; |
| 2 | |
| 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | |
| 5 | import java.util.ArrayList; |
| 6 | import java.util.Collection; |
| 7 | import java.util.Collections; |
| 8 | import java.util.List; |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 9 | |
Yuta HIGUCHI | 9e6223d | 2014-08-26 00:01:32 -0700 | [diff] [blame] | 10 | import net.floodlightcontroller.util.MACAddress; |
| 11 | import net.onrc.onos.core.util.Dpid; |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 12 | import net.onrc.onos.core.util.OnosInstanceId; |
Yuta HIGUCHI | 9e6223d | 2014-08-26 00:01:32 -0700 | [diff] [blame] | 13 | import net.onrc.onos.core.util.PortNumber; |
| 14 | import net.onrc.onos.core.util.SwitchPort; |
| 15 | |
| 16 | /** |
| 17 | * Adaptor to access {@link BaseInternalTopology} as {@link BaseTopology}. |
| 18 | */ |
| 19 | public class BaseTopologyAdaptor implements BaseTopology { |
| 20 | |
| 21 | private final BaseInternalTopology internal; |
| 22 | |
| 23 | /** |
| 24 | * Constructor. |
| 25 | * |
| 26 | * @param internal {@link BaseInternalTopology} to use internally |
| 27 | */ |
| 28 | public BaseTopologyAdaptor(BaseInternalTopology internal) { |
| 29 | this.internal = checkNotNull(internal); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | @Override |
| 34 | public Switch getSwitch(Dpid dpid) { |
| 35 | final SwitchEvent sw = internal.getSwitchEvent(dpid); |
| 36 | if (sw != null) { |
| 37 | return new SwitchImpl(internal, dpid); |
| 38 | } |
| 39 | return null; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public Iterable<Switch> getSwitches() { |
| 44 | final Collection<SwitchEvent> switches = internal.getAllSwitchEvents(); |
| 45 | List<Switch> list = new ArrayList<>(switches.size()); |
| 46 | for (SwitchEvent elm : switches) { |
| 47 | list.add(new SwitchImpl(internal, elm.getDpid())); |
| 48 | } |
| 49 | return list; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Port getPort(Dpid dpid, PortNumber portNumber) { |
| 54 | final PortEvent port = internal.getPortEvent(dpid, portNumber); |
| 55 | if (port != null) { |
| 56 | return new PortImpl(internal, port.getSwitchPort()); |
| 57 | } |
| 58 | return null; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public Port getPort(SwitchPort port) { |
| 63 | return getPort(port.getDpid(), port.getPortNumber()); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public Collection<Port> getPorts(Dpid dpid) { |
| 68 | final Collection<PortEvent> ports = internal.getPortEvents(dpid); |
| 69 | List<Port> list = new ArrayList<>(ports.size()); |
| 70 | for (PortEvent elm : ports) { |
| 71 | list.add(new PortImpl(internal, elm.getSwitchPort())); |
| 72 | } |
| 73 | return list; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public Link getOutgoingLink(Dpid dpid, PortNumber number) { |
| 78 | return getOutgoingLink(new SwitchPort(dpid, number)); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public Link getIncomingLink(Dpid dpid, PortNumber number) { |
| 83 | return getIncomingLink(new SwitchPort(dpid, number)); |
| 84 | } |
| 85 | |
| 86 | |
| 87 | @Override |
| 88 | public Link getOutgoingLink(SwitchPort port) { |
| 89 | final Collection<LinkEvent> links = internal.getLinkEventsFrom(port); |
| 90 | LinkEvent link = getPacketLinkEventIfExists(links); |
| 91 | if (link != null) { |
| 92 | return new LinkImpl(internal, link.getLinkTuple()); |
| 93 | } |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | @Override |
| 98 | public Link getIncomingLink(SwitchPort port) { |
| 99 | final Collection<LinkEvent> links = internal.getLinkEventsTo(port); |
| 100 | LinkEvent link = getPacketLinkEventIfExists(links); |
| 101 | if (link != null) { |
| 102 | return new LinkImpl(internal, link.getLinkTuple()); |
| 103 | } |
| 104 | return null; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Gets the "packet" link if such exists, |
| 109 | * otherwise return whichever link is found first. |
| 110 | * |
| 111 | * @param links Collection of links to search from |
| 112 | * @return Link instance found or null if no link exists |
| 113 | */ |
| 114 | private static LinkEvent getPacketLinkEventIfExists(Collection<LinkEvent> links) { |
| 115 | for (LinkEvent link : links) { |
| 116 | if (TopologyElement.TYPE_PACKET_LAYER.equals(link.getType())) { |
| 117 | return link; |
| 118 | } |
| 119 | } |
| 120 | if (!links.isEmpty()) { |
| 121 | return links.iterator().next(); |
| 122 | } |
| 123 | return null; |
| 124 | } |
| 125 | |
| 126 | @Override |
| 127 | public Link getOutgoingLink(Dpid dpid, PortNumber number, String type) { |
| 128 | return getOutgoingLink(new SwitchPort(dpid, number), type); |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public Link getIncomingLink(Dpid dpid, PortNumber number, String type) { |
| 133 | return getIncomingLink(new SwitchPort(dpid, number), type); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | @Override |
| 138 | public Link getOutgoingLink(SwitchPort port, String type) { |
| 139 | final Collection<LinkEvent> links = internal.getLinkEventsFrom(port); |
| 140 | for (LinkEvent link : links) { |
| 141 | if (link.getType().equals(type)) { |
| 142 | return new LinkImpl(internal, link.getLinkTuple()); |
| 143 | } |
| 144 | } |
| 145 | return null; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | public Link getIncomingLink(SwitchPort port, String type) { |
| 150 | final Collection<LinkEvent> links = internal.getLinkEventsTo(port); |
| 151 | for (LinkEvent link : links) { |
| 152 | if (link.getType().equals(type)) { |
| 153 | return new LinkImpl(internal, link.getLinkTuple()); |
| 154 | } |
| 155 | } |
| 156 | return null; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | @Override |
| 161 | public Collection<Link> getOutgoingLinks(SwitchPort port) { |
| 162 | final Collection<LinkEvent> links = internal.getLinkEventsFrom(port); |
| 163 | return toLinkImpls(internal, links); |
| 164 | } |
| 165 | |
| 166 | @Override |
| 167 | public Collection<Link> getIncomingLinks(SwitchPort port) { |
| 168 | final Collection<LinkEvent> links = internal.getLinkEventsTo(port); |
| 169 | return toLinkImpls(internal, links); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /** |
| 174 | * Converts collection of LinkEvent to collection of LinkImpls. |
| 175 | * |
| 176 | * @param internalTopology topology {@code links} resides |
| 177 | * @param links collection of LinkEvent |
| 178 | * @return collection of {@link LinkImpl}s |
| 179 | */ |
| 180 | private static Collection<Link> toLinkImpls( |
| 181 | final BaseInternalTopology internalTopology, |
| 182 | final Collection<LinkEvent> links) { |
| 183 | |
| 184 | if (links == null) { |
| 185 | return Collections.emptyList(); |
| 186 | } |
| 187 | List<Link> list = new ArrayList<>(links.size()); |
| 188 | for (LinkEvent elm : links) { |
| 189 | list.add(new LinkImpl(internalTopology, elm.getLinkTuple())); |
| 190 | } |
| 191 | return list; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public Link getLink(Dpid srcDpid, PortNumber srcNumber, |
| 196 | Dpid dstDpid, PortNumber dstNumber) { |
| 197 | |
| 198 | final SwitchPort dstSwitchPort = new SwitchPort(dstDpid, dstNumber); |
| 199 | Collection<Link> links = getOutgoingLinks(new SwitchPort(srcDpid, srcNumber)); |
| 200 | for (Link link : links) { |
| 201 | if (link == null) { |
| 202 | continue; |
| 203 | } |
| 204 | if (link.getDstPort().getSwitchPort().equals(dstSwitchPort)) { |
| 205 | return link; |
| 206 | } |
| 207 | } |
| 208 | return null; |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public Link getLink(Dpid srcDpid, PortNumber srcNumber, |
| 213 | Dpid dstDpid, PortNumber dstNumber, |
| 214 | String type) { |
| 215 | |
| 216 | Link link = getOutgoingLink(srcDpid, srcNumber, type); |
| 217 | if (link == null) { |
| 218 | return null; |
| 219 | } |
| 220 | if (!link.getDstSwitch().getDpid().equals(dstDpid)) { |
| 221 | return null; |
| 222 | } |
| 223 | if (!link.getDstPort().getNumber().equals(dstNumber)) { |
| 224 | return null; |
| 225 | } |
| 226 | return link; |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | public Iterable<Link> getLinks() { |
| 231 | final Collection<LinkEvent> links = internal.getAllLinkEvents(); |
| 232 | return toLinkImpls(internal, links); |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public Host getHostByMac(MACAddress address) { |
| 237 | |
| 238 | HostEvent host = internal.getHostEvent(address); |
| 239 | if (host != null) { |
| 240 | return new HostImpl(internal, address); |
| 241 | } |
| 242 | return null; |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | public Iterable<Host> getHosts() { |
| 247 | return toHostImpls(internal, internal.getAllHostEvents()); |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Converts collection of HostEvent to collection of HostImpl. |
| 252 | * |
| 253 | * @param internalTopology topology {@code hosts} resides |
| 254 | * @param hosts collection of HostEvent |
| 255 | * @return collection of HostImpl |
| 256 | */ |
| 257 | private static List<Host> toHostImpls(BaseInternalTopology internalTopology, |
| 258 | Collection<HostEvent> hosts) { |
| 259 | if (hosts == null) { |
| 260 | return Collections.emptyList(); |
| 261 | } |
| 262 | List<Host> list = new ArrayList<>(hosts.size()); |
| 263 | for (HostEvent elm : hosts) { |
| 264 | list.add(new HostImpl(internalTopology, elm.getMac())); |
| 265 | } |
| 266 | return list; |
| 267 | } |
| 268 | |
| 269 | @Override |
| 270 | public Collection<Host> getHosts(SwitchPort port) { |
| 271 | return toHostImpls(internal, internal.getHostEvents(port)); |
| 272 | } |
Yuta HIGUCHI | ab9dc7b | 2014-08-26 22:53:13 -0700 | [diff] [blame] | 273 | |
| 274 | |
| 275 | @Override |
| 276 | public OnosInstanceId getSwitchMaster(Dpid dpid) { |
| 277 | return internal.getSwitchMaster(dpid); |
| 278 | } |
Yuta HIGUCHI | 9e6223d | 2014-08-26 00:01:32 -0700 | [diff] [blame] | 279 | } |