blob: b0486b0bce1abfc4666f2c9fe630a8001c654bc6 [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/**
17 * Switch Object stored in In-memory Topology.
Ray Milkey269ffb92014-04-03 14:43:30 -070018 * <p/>
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080019 * TODO REMOVE following design memo: This object itself may hold the DBObject,
20 * but this Object itself will not issue any read/write to the DataStore.
21 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070022public class SwitchImpl extends TopologyObject implements Switch {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080023
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070024 //////////////////////////////////////////////////////
25 /// Topology element attributes
26 /// - any changes made here needs to be replicated.
27 //////////////////////////////////////////////////////
28 private SwitchEvent switchObj;
Jonathan Hart062a2e82014-02-03 09:41:57 -080029
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070030
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070031 /**
32 * Creates a Switch object with empty attributes.
33 *
34 * @param topology Topology instance this object belongs to
35 * @param dpid DPID
36 */
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070037 public SwitchImpl(Topology topology, Dpid dpid) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070038 this(topology, new SwitchEvent(dpid).freeze());
39 }
40
41 /**
42 * Creates a Switch object based on {@link SwitchEvent}.
43 *
44 * @param topology Topology instance this object belongs to
45 * @param scSw self contained {@link SwitchEvent}
46 */
47 public SwitchImpl(Topology topology, SwitchEvent scSw) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070048 super(topology);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070049 Validate.notNull(scSw);
50
51 // TODO should we assume switchObj is already frozen before this call
52 // or expect attribute update will happen after .
53 if (scSw.isFrozen()) {
54 this.switchObj = scSw;
55 } else {
56 this.switchObj = new SwitchEvent(scSw);
57 this.switchObj.freeze();
58 }
Ray Milkey269ffb92014-04-03 14:43:30 -070059 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080060
Ray Milkey269ffb92014-04-03 14:43:30 -070061 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070062 public Dpid getDpid() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070063 return switchObj.getDpid();
Ray Milkey269ffb92014-04-03 14:43:30 -070064 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080065
Ray Milkey269ffb92014-04-03 14:43:30 -070066 @Override
67 public Collection<Port> getPorts() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070068 topology.acquireReadLock();
69 try {
70 return topology.getPorts(getDpid());
71 } finally {
72 topology.releaseReadLock();
73 }
Ray Milkey269ffb92014-04-03 14:43:30 -070074 }
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 Port getPort(PortNumber number) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070078 topology.acquireReadLock();
79 try {
80 return topology.getPort(getDpid(), number);
81 } finally {
82 topology.releaseReadLock();
83 }
Ray Milkey269ffb92014-04-03 14:43:30 -070084 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080085
Ray Milkey269ffb92014-04-03 14:43:30 -070086 @Override
87 public Iterable<Switch> getNeighbors() {
88 Set<Switch> neighbors = new HashSet<>();
89 for (Link link : getOutgoingLinks()) {
90 neighbors.add(link.getDstSwitch());
91 }
92 // XXX should incoming considered neighbor?
93 for (Link link : getIncomingLinks()) {
94 neighbors.add(link.getSrcSwitch());
95 }
96 return neighbors;
97 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080098
Ray Milkey269ffb92014-04-03 14:43:30 -070099 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700100 public Link getLinkToNeighbor(Dpid neighborDpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700101 for (Link link : getOutgoingLinks()) {
102 if (link.getDstSwitch().getDpid().equals(neighborDpid)) {
103 return link;
104 }
105 }
106 return null;
107 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800108
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700110 public Collection<Host> getHosts() {
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 // TODO Should switch also store a list of attached devices to avoid
112 // calculating this every time?
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700113 List<Host> hosts = new ArrayList<Host>();
Yuta HIGUCHI25719052014-02-13 14:42:06 -0800114
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700115 for (Port port : getPorts()) {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700116 for (Host host : port.getHosts()) {
117 hosts.add(host);
Ray Milkey269ffb92014-04-03 14:43:30 -0700118 }
119 }
Yuta HIGUCHI25719052014-02-13 14:42:06 -0800120
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700121 return hosts;
Ray Milkey269ffb92014-04-03 14:43:30 -0700122 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800123
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700124 void replaceStringAttributes(SwitchEvent updated) {
125 Validate.isTrue(this.getDpid().equals(updated.getDpid()),
126 "Wrong SwitchEvent given.");
127
128 // XXX simply replacing whole self-contained object for now
129 if (updated.isFrozen()) {
130 this.switchObj = updated;
131 } else {
132 this.switchObj = new SwitchEvent(updated).freeze();
133 }
134 }
135
Ray Milkey269ffb92014-04-03 14:43:30 -0700136 @Override
137 public Iterable<Link> getOutgoingLinks() {
138 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700139 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700140 Link link = port.getOutgoingLink();
141 if (link != null) {
142 links.add(link);
143 }
144 }
145 return links;
146 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800147
Ray Milkey269ffb92014-04-03 14:43:30 -0700148 @Override
149 public Iterable<Link> getIncomingLinks() {
150 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700151 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700152 Link link = port.getIncomingLink();
153 if (link != null) {
154 links.add(link);
155 }
156 }
157 return links;
158 }
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700159
160 @Override
161 public String getStringAttribute(String attr) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700162 return this.switchObj.getStringAttribute(attr);
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700163 }
164
165 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700166 public String getStringAttribute(String attr, String def) {
167 final String v = getStringAttribute(attr);
168 if (v == null) {
169 return def;
170 } else {
171 return v;
172 }
173 }
174
175 @Override
176 public Map<String, String> getAllStringAttributes() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700177 return this.switchObj.getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700178 }
179
180 @Override
181 public String toString() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700182 return getDpid().toString();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700183 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700184
185 /**
186 * Returns the type of topology object.
187 *
188 * @return the type of the topology object
189 */
190 @Override
191 public String getType() {
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700192 return getStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700193 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800194}