blob: b00409ea6d28c10543fb58b5943237163369ac3b [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 void replaceStringAttributes(SwitchEvent updated) {
135 Validate.isTrue(this.getDpid().equals(updated.getDpid()),
136 "Wrong SwitchEvent given.");
137
138 // XXX simply replacing whole self-contained object for now
139 if (updated.isFrozen()) {
140 this.switchObj = updated;
141 } else {
142 this.switchObj = new SwitchEvent(updated).freeze();
143 }
144 }
145
Ray Milkey269ffb92014-04-03 14:43:30 -0700146 @Override
147 public Iterable<Link> getOutgoingLinks() {
148 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700149 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700150 Link link = port.getOutgoingLink();
151 if (link != null) {
152 links.add(link);
153 }
154 }
155 return links;
156 }
Toshio Koide2f570c12014-02-06 16:55:32 -0800157
Ray Milkey269ffb92014-04-03 14:43:30 -0700158 @Override
159 public Iterable<Link> getIncomingLinks() {
160 LinkedList<Link> links = new LinkedList<Link>();
Yuta HIGUCHIfa742842014-07-03 22:35:13 -0700161 for (Port port : getPorts()) {
Ray Milkey269ffb92014-04-03 14:43:30 -0700162 Link link = port.getIncomingLink();
163 if (link != null) {
164 links.add(link);
165 }
166 }
167 return links;
168 }
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700169
170 @Override
171 public String getStringAttribute(String attr) {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700172 return this.switchObj.getStringAttribute(attr);
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700173 }
174
175 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700176 public String getStringAttribute(String attr, String def) {
177 final String v = getStringAttribute(attr);
178 if (v == null) {
179 return def;
180 } else {
181 return v;
182 }
183 }
184
185 @Override
186 public Map<String, String> getAllStringAttributes() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700187 return this.switchObj.getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700188 }
189
190 @Override
191 public String toString() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700192 return getDpid().toString();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700193 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700194
195 /**
196 * Returns the type of topology object.
197 *
198 * @return the type of the topology object
199 */
200 @Override
201 public String getType() {
202 throw new UnsupportedOperationException("Not implemented yet");
203 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800204}