blob: 29df7b8524059ef6e1ddda27136c3ba69e2a0e9a [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
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070014import static com.google.common.base.Preconditions.checkNotNull;
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);
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070034 this.id = checkNotNull(dpid);
Ray Milkey269ffb92014-04-03 14:43:30 -070035 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080036
Ray Milkey269ffb92014-04-03 14:43:30 -070037 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070038 public Dpid getDpid() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070039 return this.id;
Ray Milkey269ffb92014-04-03 14:43:30 -070040 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080041
Ray Milkey269ffb92014-04-03 14:43:30 -070042 @Override
43 public Collection<Port> getPorts() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070044 topology.acquireReadLock();
45 try {
46 return topology.getPorts(getDpid());
47 } finally {
48 topology.releaseReadLock();
49 }
Ray Milkey269ffb92014-04-03 14:43:30 -070050 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080051
Ray Milkey269ffb92014-04-03 14:43:30 -070052 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070053 public Port getPort(PortNumber number) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070054 topology.acquireReadLock();
55 try {
56 return topology.getPort(getDpid(), number);
57 } finally {
58 topology.releaseReadLock();
59 }
Ray Milkey269ffb92014-04-03 14:43:30 -070060 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080061
Ray Milkey269ffb92014-04-03 14:43:30 -070062 @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 Hart062a2e82014-02-03 09:41:57 -080074
Ray Milkey269ffb92014-04-03 14:43:30 -070075 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070076 public Link getLinkToNeighbor(Dpid neighborDpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -070077 for (Link link : getOutgoingLinks()) {
78 if (link.getDstSwitch().getDpid().equals(neighborDpid)) {
79 return link;
80 }
81 }
82 return null;
83 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080084
Ray Milkey269ffb92014-04-03 14:43:30 -070085 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070086 public Collection<Host> getHosts() {
Ray Milkey269ffb92014-04-03 14:43:30 -070087 // TODO Should switch also store a list of attached devices to avoid
88 // calculating this every time?
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070089 List<Host> hosts = new ArrayList<Host>();
Yuta HIGUCHI25719052014-02-13 14:42:06 -080090
Yuta HIGUCHIfa742842014-07-03 22:35:13 -070091 for (Port port : getPorts()) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070092 for (Host host : port.getHosts()) {
93 hosts.add(host);
Ray Milkey269ffb92014-04-03 14:43:30 -070094 }
95 }
Yuta HIGUCHI25719052014-02-13 14:42:06 -080096
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070097 return hosts;
Ray Milkey269ffb92014-04-03 14:43:30 -070098 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080099
Praseed Balakrishnanfa21be12014-07-15 14:42:26 -0700100 /**
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 Milkey269ffb92014-04-03 14:43:30 -0700111 @Override
112 public Iterable<Link> getOutgoingLinks() {
113 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700114 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700115 Link link = port.getOutgoingLink();
116 if (link != null) {
117 links.add(link);
118 }
119 }
120 return links;
121 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800122
Ray Milkey269ffb92014-04-03 14:43:30 -0700123 @Override
124 public Iterable<Link> getIncomingLinks() {
125 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700126 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700127 Link link = port.getIncomingLink();
128 if (link != null) {
129 links.add(link);
130 }
131 }
132 return links;
133 }
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700134
135 @Override
136 public String getStringAttribute(String attr) {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -0700137 return this.topology.getSwitchEvent(getDpid()).getStringAttribute(attr);
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700138 }
139
140 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700141 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 HIGUCHI8b389a72014-07-18 13:50:00 -0700152 return this.topology.getSwitchEvent(getDpid()).getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700153 }
154
155 @Override
156 public String toString() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700157 return getDpid().toString();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700158 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700159
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 HIGUCHIdbc33122014-07-10 13:32:32 -0700167 return getStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700168 }
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -0700169
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 Hart062a2e82014-02-03 09:41:57 -0800189}