blob: e0e02d5c34dde5205e58e14ced82e4497d696476 [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Jonathan Hart062a2e82014-02-03 09:41:57 -08002
Jonathan Hart369875b2014-02-13 10:00:31 -08003import java.util.ArrayList;
Jonathan Hart062a2e82014-02-03 09:41:57 -08004import java.util.Collection;
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -08005import java.util.HashSet;
Toshio Koide2f570c12014-02-06 16:55:32 -08006import java.util.LinkedList;
Jonathan Hart369875b2014-02-13 10:00:31 -08007import java.util.List;
Jonathan Hart062a2e82014-02-03 09:41:57 -08008import java.util.Map;
Yuta HIGUCHI9b028ca2014-02-11 14:07:58 -08009import java.util.Set;
Jonathan Hart062a2e82014-02-03 09:41:57 -080010
Yuta HIGUCHIfa742842014-07-03 22:35:13 -070011import net.onrc.onos.core.util.Dpid;
12import net.onrc.onos.core.util.PortNumber;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070013
14import org.apache.commons.lang.Validate;
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -070015
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080016/**
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070017 * Handler to Switch object stored in In-memory Topology snapshot.
Ray Milkey269ffb92014-04-03 14:43:30 -070018 * <p/>
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070019 *
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080020 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070021public class SwitchImpl extends TopologyObject implements Switch {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080022
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070023 private final Dpid id;
Jonathan Hart062a2e82014-02-03 09:41:57 -080024
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070025
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070026 /**
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070027 * Creates a Switch handler object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070028 *
29 * @param topology Topology instance this object belongs to
30 * @param dpid DPID
31 */
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070032 SwitchImpl(TopologyInternal topology, Dpid dpid) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070033 super(topology);
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070034 Validate.notNull(dpid);
35 this.id = dpid;
Ray Milkey269ffb92014-04-03 14:43:30 -070036 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080037
Ray Milkey269ffb92014-04-03 14:43:30 -070038 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070039 public Dpid getDpid() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070040 return this.id;
Ray Milkey269ffb92014-04-03 14:43:30 -070041 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080042
Ray Milkey269ffb92014-04-03 14:43:30 -070043 @Override
44 public Collection<Port> getPorts() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070045 topology.acquireReadLock();
46 try {
47 return topology.getPorts(getDpid());
48 } finally {
49 topology.releaseReadLock();
50 }
Ray Milkey269ffb92014-04-03 14:43:30 -070051 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080052
Ray Milkey269ffb92014-04-03 14:43:30 -070053 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070054 public Port getPort(PortNumber number) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070055 topology.acquireReadLock();
56 try {
57 return topology.getPort(getDpid(), number);
58 } finally {
59 topology.releaseReadLock();
60 }
Ray Milkey269ffb92014-04-03 14:43:30 -070061 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080062
Ray Milkey269ffb92014-04-03 14:43:30 -070063 @Override
64 public Iterable<Switch> getNeighbors() {
65 Set<Switch> neighbors = new HashSet<>();
66 for (Link link : getOutgoingLinks()) {
67 neighbors.add(link.getDstSwitch());
68 }
69 // XXX should incoming considered neighbor?
70 for (Link link : getIncomingLinks()) {
71 neighbors.add(link.getSrcSwitch());
72 }
73 return neighbors;
74 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080075
Ray Milkey269ffb92014-04-03 14:43:30 -070076 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070077 public Link getLinkToNeighbor(Dpid neighborDpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -070078 for (Link link : getOutgoingLinks()) {
79 if (link.getDstSwitch().getDpid().equals(neighborDpid)) {
80 return link;
81 }
82 }
83 return null;
84 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080085
Ray Milkey269ffb92014-04-03 14:43:30 -070086 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070087 public Collection<Host> getHosts() {
Ray Milkey269ffb92014-04-03 14:43:30 -070088 // TODO Should switch also store a list of attached devices to avoid
89 // calculating this every time?
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070090 List<Host> hosts = new ArrayList<Host>();
Yuta HIGUCHI25719052014-02-13 14:42:06 -080091
Yuta HIGUCHIfa742842014-07-03 22:35:13 -070092 for (Port port : getPorts()) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070093 for (Host host : port.getHosts()) {
94 hosts.add(host);
Ray Milkey269ffb92014-04-03 14:43:30 -070095 }
96 }
Yuta HIGUCHI25719052014-02-13 14:42:06 -080097
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070098 return hosts;
Ray Milkey269ffb92014-04-03 14:43:30 -070099 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800100
Praseed Balakrishnanfa21be12014-07-15 14:42:26 -0700101 /**
102 * Returns the switch type of this switch.
103 *
104 * @return switch type {@link net.onrc.onos.core.topology.SwitchType} of this switch.
105 */
106 @Override
107 public SwitchType getSwitchType() {
108 return SwitchType.valueOf(getStringAttribute(TopologyElement.ELEMENT_TYPE,
109 SwitchType.ETHERNET_SWITCH.toString()));
110 }
111
Ray Milkey269ffb92014-04-03 14:43:30 -0700112 @Override
113 public Iterable<Link> getOutgoingLinks() {
114 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700115 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700116 Link link = port.getOutgoingLink();
117 if (link != null) {
118 links.add(link);
119 }
120 }
121 return links;
122 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800123
Ray Milkey269ffb92014-04-03 14:43:30 -0700124 @Override
125 public Iterable<Link> getIncomingLinks() {
126 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700127 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700128 Link link = port.getIncomingLink();
129 if (link != null) {
130 links.add(link);
131 }
132 }
133 return links;
134 }
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700135
136 @Override
137 public String getStringAttribute(String attr) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700138 return this.topology.getSwitchEvent(getDpid()).getStringAttribute(attr);
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700139 }
140
141 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700142 public String getStringAttribute(String attr, String def) {
143 final String v = getStringAttribute(attr);
144 if (v == null) {
145 return def;
146 } else {
147 return v;
148 }
149 }
150
151 @Override
152 public Map<String, String> getAllStringAttributes() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700153 return this.topology.getSwitchEvent(getDpid()).getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700154 }
155
156 @Override
157 public String toString() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700158 return getDpid().toString();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700159 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700160
161 /**
162 * Returns the type of topology object.
163 *
164 * @return the type of the topology object
165 */
166 @Override
167 public String getType() {
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700168 return getStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700169 }
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -0700170
171 /**
172 * Returns the config state of topology element.
173 *
174 * @return ConfigState
175 */
176 @Override
177 public ConfigState getConfigState() {
178 return ConfigState.valueOf(getStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE));
179 }
180
181 /**
182 * Returns the status of topology element.
183 *
184 * @return AdminStatus
185 */
186 @Override
187 public AdminStatus getStatus() {
188 return AdminStatus.valueOf(getStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS));
189 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800190}