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