blob: de4194ed60e56799e4e28803412d2d0f23325af9 [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
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 @Override
102 public Iterable<Link> getOutgoingLinks() {
103 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700104 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 Link link = port.getOutgoingLink();
106 if (link != null) {
107 links.add(link);
108 }
109 }
110 return links;
111 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800112
Ray Milkey269ffb92014-04-03 14:43:30 -0700113 @Override
114 public Iterable<Link> getIncomingLinks() {
115 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700116 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700117 Link link = port.getIncomingLink();
118 if (link != null) {
119 links.add(link);
120 }
121 }
122 return links;
123 }
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700124
125 @Override
126 public String getStringAttribute(String attr) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700127 return this.topology.getSwitchEvent(getDpid()).getStringAttribute(attr);
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700128 }
129
130 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700131 public String getStringAttribute(String attr, String def) {
132 final String v = getStringAttribute(attr);
133 if (v == null) {
134 return def;
135 } else {
136 return v;
137 }
138 }
139
140 @Override
141 public Map<String, String> getAllStringAttributes() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700142 return this.topology.getSwitchEvent(getDpid()).getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700143 }
144
145 @Override
146 public String toString() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700147 return getDpid().toString();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700148 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700149
150 /**
151 * Returns the type of topology object.
152 *
153 * @return the type of the topology object
154 */
155 @Override
156 public String getType() {
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700157 return getStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700158 }
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -0700159
160 /**
161 * Returns the config state of topology element.
162 *
163 * @return ConfigState
164 */
165 @Override
166 public ConfigState getConfigState() {
167 return ConfigState.valueOf(getStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE));
168 }
169
170 /**
171 * Returns the status of topology element.
172 *
173 * @return AdminStatus
174 */
175 @Override
176 public AdminStatus getStatus() {
177 return AdminStatus.valueOf(getStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS));
178 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800179}