blob: 27fcbcea88fabf5b193529ef44060383504de7ca [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.
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080018 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070019public class SwitchImpl extends TopologyObject implements Switch {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080020
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070021 private final Dpid id;
Jonathan Hart062a2e82014-02-03 09:41:57 -080022
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070023
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070024 /**
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070025 * Creates a Switch handler object.
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070026 *
27 * @param topology Topology instance this object belongs to
28 * @param dpid DPID
29 */
Yuta HIGUCHI9e6223d2014-08-26 00:01:32 -070030 SwitchImpl(BaseInternalTopology topology, Dpid dpid) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070031 super(topology);
Pavlin Radoslavova5637c02014-07-30 15:55:11 -070032 this.id = checkNotNull(dpid);
Ray Milkey269ffb92014-04-03 14:43:30 -070033 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080034
Ray Milkey269ffb92014-04-03 14:43:30 -070035 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070036 public Dpid getDpid() {
Yuta HIGUCHI8b389a72014-07-18 13:50:00 -070037 return this.id;
Ray Milkey269ffb92014-04-03 14:43:30 -070038 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080039
Ray Milkey269ffb92014-04-03 14:43:30 -070040 @Override
41 public Collection<Port> getPorts() {
Yuta HIGUCHI9e6223d2014-08-26 00:01:32 -070042 return new BaseTopologyAdaptor(topology).getPorts(getDpid());
Ray Milkey269ffb92014-04-03 14:43:30 -070043 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080044
Ray Milkey269ffb92014-04-03 14:43:30 -070045 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070046 public Port getPort(PortNumber number) {
Yuta HIGUCHI9e6223d2014-08-26 00:01:32 -070047 return new BaseTopologyAdaptor(topology).getPort(getDpid(), number);
Ray Milkey269ffb92014-04-03 14:43:30 -070048 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080049
Ray Milkey269ffb92014-04-03 14:43:30 -070050 @Override
51 public Iterable<Switch> getNeighbors() {
52 Set<Switch> neighbors = new HashSet<>();
53 for (Link link : getOutgoingLinks()) {
54 neighbors.add(link.getDstSwitch());
55 }
56 // XXX should incoming considered neighbor?
57 for (Link link : getIncomingLinks()) {
58 neighbors.add(link.getSrcSwitch());
59 }
60 return neighbors;
61 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080062
Ray Milkey269ffb92014-04-03 14:43:30 -070063 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070064 public Link getLinkToNeighbor(Dpid neighborDpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -070065 for (Link link : getOutgoingLinks()) {
66 if (link.getDstSwitch().getDpid().equals(neighborDpid)) {
67 return link;
68 }
69 }
70 return null;
71 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080072
Ray Milkey269ffb92014-04-03 14:43:30 -070073 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070074 public Collection<Host> getHosts() {
Ray Milkey269ffb92014-04-03 14:43:30 -070075 // TODO Should switch also store a list of attached devices to avoid
76 // calculating this every time?
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070077 List<Host> hosts = new ArrayList<Host>();
Yuta HIGUCHI25719052014-02-13 14:42:06 -080078
Yuta HIGUCHIfa742842014-07-03 22:35:13 -070079 for (Port port : getPorts()) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070080 for (Host host : port.getHosts()) {
81 hosts.add(host);
Ray Milkey269ffb92014-04-03 14:43:30 -070082 }
83 }
Yuta HIGUCHI25719052014-02-13 14:42:06 -080084
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -070085 return hosts;
Ray Milkey269ffb92014-04-03 14:43:30 -070086 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080087
Praseed Balakrishnanfa21be12014-07-15 14:42:26 -070088 /**
89 * Returns the switch type of this switch.
90 *
91 * @return switch type {@link net.onrc.onos.core.topology.SwitchType} of this switch.
92 */
93 @Override
94 public SwitchType getSwitchType() {
95 return SwitchType.valueOf(getStringAttribute(TopologyElement.ELEMENT_TYPE,
96 SwitchType.ETHERNET_SWITCH.toString()));
97 }
98
Ray Milkey269ffb92014-04-03 14:43:30 -070099 @Override
100 public Iterable<Link> getOutgoingLinks() {
101 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700102 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700103 Link link = port.getOutgoingLink();
104 if (link != null) {
105 links.add(link);
106 }
107 }
108 return links;
109 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800110
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 @Override
112 public Iterable<Link> getIncomingLinks() {
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.getIncomingLink();
116 if (link != null) {
117 links.add(link);
118 }
119 }
120 return links;
121 }
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700122
123 @Override
124 public String getStringAttribute(String attr) {
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700125 return this.topology.getSwitchData(getDpid()).getStringAttribute(attr);
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700126 }
127
128 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700129 public String getStringAttribute(String attr, String def) {
130 final String v = getStringAttribute(attr);
131 if (v == null) {
132 return def;
133 } else {
134 return v;
135 }
136 }
137
138 @Override
139 public Map<String, String> getAllStringAttributes() {
Yuta HIGUCHI93d35ea2014-08-31 23:26:13 -0700140 return this.topology.getSwitchData(getDpid()).getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700141 }
142
143 @Override
144 public String toString() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700145 return getDpid().toString();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700146 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700147
148 /**
149 * Returns the type of topology object.
150 *
151 * @return the type of the topology object
152 */
153 @Override
154 public String getType() {
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700155 return getStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700156 }
Praseed Balakrishnan2aa6c0b2014-07-17 11:42:05 -0700157
158 /**
159 * Returns the config state of topology element.
160 *
161 * @return ConfigState
162 */
163 @Override
164 public ConfigState getConfigState() {
165 return ConfigState.valueOf(getStringAttribute(TopologyElement.ELEMENT_CONFIG_STATE));
166 }
167
168 /**
169 * Returns the status of topology element.
170 *
171 * @return AdminStatus
172 */
173 @Override
174 public AdminStatus getStatus() {
175 return AdminStatus.valueOf(getStringAttribute(TopologyElement.ELEMENT_ADMIN_STATUS));
176 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800177}