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 | |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 3 | import java.util.ArrayList; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 4 | import java.util.Collection; |
Yuta HIGUCHI | 9b028ca | 2014-02-11 14:07:58 -0800 | [diff] [blame] | 5 | import java.util.HashSet; |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 6 | import java.util.LinkedList; |
Jonathan Hart | 369875b | 2014-02-13 10:00:31 -0800 | [diff] [blame] | 7 | import java.util.List; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 8 | import java.util.Map; |
Yuta HIGUCHI | 9b028ca | 2014-02-11 14:07:58 -0800 | [diff] [blame] | 9 | import java.util.Set; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 10 | |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 11 | import net.onrc.onos.core.util.Dpid; |
| 12 | import net.onrc.onos.core.util.PortNumber; |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 13 | |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 14 | import static com.google.common.base.Preconditions.checkNotNull; |
Yuta HIGUCHI | db1b830 | 2014-06-26 10:50:39 -0700 | [diff] [blame] | 15 | |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 16 | /** |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 17 | * Handler to Switch object stored in In-memory Topology snapshot. |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 18 | * <p/> |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 19 | * |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 20 | */ |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 21 | public class SwitchImpl extends TopologyObject implements Switch { |
Yuta HIGUCHI | 181d34d | 2014-02-05 15:05:46 -0800 | [diff] [blame] | 22 | |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 23 | private final Dpid id; |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 24 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 25 | |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 26 | /** |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 27 | * Creates a Switch handler object. |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 28 | * |
| 29 | * @param topology Topology instance this object belongs to |
| 30 | * @param dpid DPID |
| 31 | */ |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 32 | SwitchImpl(TopologyInternal topology, Dpid dpid) { |
Jonathan Hart | e37e4e2 | 2014-05-13 19:12:02 -0700 | [diff] [blame] | 33 | super(topology); |
Pavlin Radoslavov | a5637c0 | 2014-07-30 15:55:11 -0700 | [diff] [blame] | 34 | this.id = checkNotNull(dpid); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 35 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 36 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 37 | @Override |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 38 | public Dpid getDpid() { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 39 | return this.id; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 40 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 41 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 42 | @Override |
| 43 | public Collection<Port> getPorts() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 44 | topology.acquireReadLock(); |
| 45 | try { |
| 46 | return topology.getPorts(getDpid()); |
| 47 | } finally { |
| 48 | topology.releaseReadLock(); |
| 49 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 50 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 51 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 52 | @Override |
Yuta HIGUCHI | 8f3dfa3 | 2014-06-25 00:14:25 -0700 | [diff] [blame] | 53 | public Port getPort(PortNumber number) { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 54 | topology.acquireReadLock(); |
| 55 | try { |
| 56 | return topology.getPort(getDpid(), number); |
| 57 | } finally { |
| 58 | topology.releaseReadLock(); |
| 59 | } |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 60 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 61 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 62 | @Override |
| 63 | public Iterable<Switch> getNeighbors() { |
| 64 | Set<Switch> neighbors = new HashSet<>(); |
| 65 | for (Link link : getOutgoingLinks()) { |
| 66 | neighbors.add(link.getDstSwitch()); |
| 67 | } |
| 68 | // XXX should incoming considered neighbor? |
| 69 | for (Link link : getIncomingLinks()) { |
| 70 | neighbors.add(link.getSrcSwitch()); |
| 71 | } |
| 72 | return neighbors; |
| 73 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -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 Link getLinkToNeighbor(Dpid neighborDpid) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 77 | for (Link link : getOutgoingLinks()) { |
| 78 | if (link.getDstSwitch().getDpid().equals(neighborDpid)) { |
| 79 | return link; |
| 80 | } |
| 81 | } |
| 82 | return null; |
| 83 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 84 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 85 | @Override |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 86 | public Collection<Host> getHosts() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 87 | // TODO Should switch also store a list of attached devices to avoid |
| 88 | // calculating this every time? |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 89 | List<Host> hosts = new ArrayList<Host>(); |
Yuta HIGUCHI | 2571905 | 2014-02-13 14:42:06 -0800 | [diff] [blame] | 90 | |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 91 | for (Port port : getPorts()) { |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 92 | for (Host host : port.getHosts()) { |
| 93 | hosts.add(host); |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
Yuta HIGUCHI | 2571905 | 2014-02-13 14:42:06 -0800 | [diff] [blame] | 96 | |
Yuta HIGUCHI | bfc77f0 | 2014-07-14 22:50:25 -0700 | [diff] [blame] | 97 | return hosts; |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 98 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 99 | |
Praseed Balakrishnan | fa21be1 | 2014-07-15 14:42:26 -0700 | [diff] [blame] | 100 | /** |
| 101 | * Returns the switch type of this switch. |
| 102 | * |
| 103 | * @return switch type {@link net.onrc.onos.core.topology.SwitchType} of this switch. |
| 104 | */ |
| 105 | @Override |
| 106 | public SwitchType getSwitchType() { |
| 107 | return SwitchType.valueOf(getStringAttribute(TopologyElement.ELEMENT_TYPE, |
| 108 | SwitchType.ETHERNET_SWITCH.toString())); |
| 109 | } |
| 110 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 111 | @Override |
| 112 | public Iterable<Link> getOutgoingLinks() { |
| 113 | LinkedList<Link> links = new LinkedList<Link>(); |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 114 | for (Port port : getPorts()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 115 | Link link = port.getOutgoingLink(); |
| 116 | if (link != null) { |
| 117 | links.add(link); |
| 118 | } |
| 119 | } |
| 120 | return links; |
| 121 | } |
Toshio Koide | 2f570c1 | 2014-02-06 16:55:32 -0800 | [diff] [blame] | 122 | |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 123 | @Override |
| 124 | public Iterable<Link> getIncomingLinks() { |
| 125 | LinkedList<Link> links = new LinkedList<Link>(); |
Yuta HIGUCHI | fa74284 | 2014-07-03 22:35:13 -0700 | [diff] [blame] | 126 | for (Port port : getPorts()) { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 127 | Link link = port.getIncomingLink(); |
| 128 | if (link != null) { |
| 129 | links.add(link); |
| 130 | } |
| 131 | } |
| 132 | return links; |
| 133 | } |
Yuta HIGUCHI | db1b830 | 2014-06-26 10:50:39 -0700 | [diff] [blame] | 134 | |
| 135 | @Override |
| 136 | public String getStringAttribute(String attr) { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 137 | return this.topology.getSwitchEvent(getDpid()).getStringAttribute(attr); |
Yuta HIGUCHI | db1b830 | 2014-06-26 10:50:39 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | @Override |
Yuta HIGUCHI | db1b830 | 2014-06-26 10:50:39 -0700 | [diff] [blame] | 141 | public String getStringAttribute(String attr, String def) { |
| 142 | final String v = getStringAttribute(attr); |
| 143 | if (v == null) { |
| 144 | return def; |
| 145 | } else { |
| 146 | return v; |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public Map<String, String> getAllStringAttributes() { |
Yuta HIGUCHI | 8b389a7 | 2014-07-18 13:50:00 -0700 | [diff] [blame] | 152 | return this.topology.getSwitchEvent(getDpid()).getAllStringAttributes(); |
Yuta HIGUCHI | db1b830 | 2014-06-26 10:50:39 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public String toString() { |
Yuta HIGUCHI | bf0a871 | 2014-06-30 18:59:46 -0700 | [diff] [blame] | 157 | return getDpid().toString(); |
Yuta HIGUCHI | db1b830 | 2014-06-26 10:50:39 -0700 | [diff] [blame] | 158 | } |
Praseed Balakrishnan | 57ed843 | 2014-06-26 11:49:56 -0700 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * Returns the type of topology object. |
| 162 | * |
| 163 | * @return the type of the topology object |
| 164 | */ |
| 165 | @Override |
| 166 | public String getType() { |
Yuta HIGUCHI | dbc3312 | 2014-07-10 13:32:32 -0700 | [diff] [blame] | 167 | return getStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER); |
Praseed Balakrishnan | 57ed843 | 2014-06-26 11:49:56 -0700 | [diff] [blame] | 168 | } |
Praseed Balakrishnan | 2aa6c0b | 2014-07-17 11:42:05 -0700 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * Returns the config state of topology element. |
| 172 | * |
| 173 | * @return ConfigState |
| 174 | */ |
| 175 | @Override |
| 176 | public ConfigState getConfigState() { |
| 177 | return ConfigState.valueOf(getStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE)); |
| 178 | } |
| 179 | |
| 180 | /** |
| 181 | * Returns the status of topology element. |
| 182 | * |
| 183 | * @return AdminStatus |
| 184 | */ |
| 185 | @Override |
| 186 | public AdminStatus getStatus() { |
| 187 | return AdminStatus.valueOf(getStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS)); |
| 188 | } |
Jonathan Hart | 062a2e8 | 2014-02-03 09:41:57 -0800 | [diff] [blame] | 189 | } |