blob: 7fbc43db6a67ee37c04234fedac338a29f8f833d [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 ///////////////////
31 /// In-memory index
32 ///////////////////
33
34 // none
35
36 // TODO remove when test codes are cleaned up.
Jonathan Harte37e4e22014-05-13 19:12:02 -070037 public SwitchImpl(Topology topology, Long dpid) {
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070038 this(topology, new Dpid(dpid));
39 }
40
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070041 /**
42 * Creates a Switch object with empty attributes.
43 *
44 * @param topology Topology instance this object belongs to
45 * @param dpid DPID
46 */
Sho SHIMIZU83e3c1f2014-06-13 15:57:26 -070047 public SwitchImpl(Topology topology, Dpid dpid) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070048 this(topology, new SwitchEvent(dpid).freeze());
49 }
50
51 /**
52 * Creates a Switch object based on {@link SwitchEvent}.
53 *
54 * @param topology Topology instance this object belongs to
55 * @param scSw self contained {@link SwitchEvent}
56 */
57 public SwitchImpl(Topology topology, SwitchEvent scSw) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070058 super(topology);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070059 Validate.notNull(scSw);
60
61 // TODO should we assume switchObj is already frozen before this call
62 // or expect attribute update will happen after .
63 if (scSw.isFrozen()) {
64 this.switchObj = scSw;
65 } else {
66 this.switchObj = new SwitchEvent(scSw);
67 this.switchObj.freeze();
68 }
Ray Milkey269ffb92014-04-03 14:43:30 -070069 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080070
Ray Milkey269ffb92014-04-03 14:43:30 -070071 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070072 public Dpid getDpid() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070073 return switchObj.getDpid();
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
77 public Collection<Port> getPorts() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070078 topology.acquireReadLock();
79 try {
80 return topology.getPorts(getDpid());
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
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070087 public Port getPort(PortNumber number) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070088 topology.acquireReadLock();
89 try {
90 return topology.getPort(getDpid(), number);
91 } finally {
92 topology.releaseReadLock();
93 }
Ray Milkey269ffb92014-04-03 14:43:30 -070094 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080095
Ray Milkey269ffb92014-04-03 14:43:30 -070096 @Override
97 public Iterable<Switch> getNeighbors() {
98 Set<Switch> neighbors = new HashSet<>();
99 for (Link link : getOutgoingLinks()) {
100 neighbors.add(link.getDstSwitch());
101 }
102 // XXX should incoming considered neighbor?
103 for (Link link : getIncomingLinks()) {
104 neighbors.add(link.getSrcSwitch());
105 }
106 return neighbors;
107 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800108
Ray Milkey269ffb92014-04-03 14:43:30 -0700109 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700110 public Link getLinkToNeighbor(Dpid neighborDpid) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700111 for (Link link : getOutgoingLinks()) {
112 if (link.getDstSwitch().getDpid().equals(neighborDpid)) {
113 return link;
114 }
115 }
116 return null;
117 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800118
Ray Milkey269ffb92014-04-03 14:43:30 -0700119 @Override
120 public Collection<Device> getDevices() {
121 // TODO Should switch also store a list of attached devices to avoid
122 // calculating this every time?
123 List<Device> devices = new ArrayList<Device>();
Yuta HIGUCHI25719052014-02-13 14:42:06 -0800124
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700125 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700126 for (Device device : port.getDevices()) {
127 devices.add(device);
128 }
129 }
Yuta HIGUCHI25719052014-02-13 14:42:06 -0800130
Ray Milkey269ffb92014-04-03 14:43:30 -0700131 return devices;
132 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800133
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700134 // FIXME this should be removed from here and moved to MockTopology
135 Port addPort(Long portNumber) {
136 PortImpl port = new PortImpl(topology, getDpid(),
137 new PortNumber(portNumber.shortValue()));
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700138 ((TopologyImpl) topology).putPort(port);
Ray Milkey269ffb92014-04-03 14:43:30 -0700139 return port;
140 }
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -0800141
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700142 void replaceStringAttributes(SwitchEvent updated) {
143 Validate.isTrue(this.getDpid().equals(updated.getDpid()),
144 "Wrong SwitchEvent given.");
145
146 // XXX simply replacing whole self-contained object for now
147 if (updated.isFrozen()) {
148 this.switchObj = updated;
149 } else {
150 this.switchObj = new SwitchEvent(updated).freeze();
151 }
152 }
153
Ray Milkey269ffb92014-04-03 14:43:30 -0700154 @Override
155 public Iterable<Link> getOutgoingLinks() {
156 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700157 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700158 Link link = port.getOutgoingLink();
159 if (link != null) {
160 links.add(link);
161 }
162 }
163 return links;
164 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800165
Ray Milkey269ffb92014-04-03 14:43:30 -0700166 @Override
167 public Iterable<Link> getIncomingLinks() {
168 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700169 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700170 Link link = port.getIncomingLink();
171 if (link != null) {
172 links.add(link);
173 }
174 }
175 return links;
176 }
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700177
178 @Override
179 public String getStringAttribute(String attr) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700180 return this.switchObj.getStringAttribute(attr);
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700181 }
182
183 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700184 public String getStringAttribute(String attr, String def) {
185 final String v = getStringAttribute(attr);
186 if (v == null) {
187 return def;
188 } else {
189 return v;
190 }
191 }
192
193 @Override
194 public Map<String, String> getAllStringAttributes() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700195 return this.switchObj.getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700196 }
197
198 @Override
199 public String toString() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700200 return getDpid().toString();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700201 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700202
203 /**
204 * Returns the type of topology object.
205 *
206 * @return the type of the topology object
207 */
208 @Override
209 public String getType() {
210 throw new UnsupportedOperationException("Not implemented yet");
211 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800212}