blob: c1bb75c453e5a04457ab302ffcc70796d12aa1bd [file] [log] [blame]
Jonathan Hart472062d2014-04-03 10:56:48 -07001package net.onrc.onos.core.topology;
Jonathan Hart062a2e82014-02-03 09:41:57 -08002
Yuta HIGUCHI8313f0b2014-07-09 16:36:03 -07003import java.util.Collection;
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -07004import java.util.Map;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07005
6import org.apache.commons.lang.Validate;
7
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07008import net.onrc.onos.core.util.Dpid;
9import net.onrc.onos.core.util.PortNumber;
Jonathan Hart25bd53e2014-04-30 23:44:09 -070010import net.onrc.onos.core.util.SwitchPort;
11
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080012/**
13 * Port Object stored in In-memory Topology.
Ray Milkey269ffb92014-04-03 14:43:30 -070014 * <p/>
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080015 * TODO REMOVE following design memo: This object itself may hold the DBObject,
16 * but this Object itself will not issue any read/write to the DataStore.
17 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070018public class PortImpl extends TopologyObject implements Port {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080019
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070020 //////////////////////////////////////////////////////
21 /// Topology element attributes
22 /// - any changes made here needs to be replicated.
23 //////////////////////////////////////////////////////
24 private PortEvent portObj;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080025
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070026 ///////////////////
27 /// In-memory index
28 ///////////////////
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080029
Jonathan Hart25bd53e2014-04-30 23:44:09 -070030
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070031 /**
32 * Creates a Port object based on {@link PortEvent}.
33 *
34 * @param topology Topology instance this object belongs to
35 * @param scPort self contained {@link PortEvent}
36 */
37 public PortImpl(Topology topology, PortEvent scPort) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070038 super(topology);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070039 Validate.notNull(scPort);
Jonathan Hart25bd53e2014-04-30 23:44:09 -070040
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070041 // TODO should we assume portObj is already frozen before this call
42 // or expect attribute update will happen after .
43 if (scPort.isFrozen()) {
44 this.portObj = scPort;
45 } else {
46 this.portObj = new PortEvent(scPort);
47 this.portObj.freeze();
48 }
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070049 }
50
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070051 /**
52 * Creates a Port object with empty attributes.
53 *
54 * @param topology Topology instance this object belongs to
55 * @param switchPort SwitchPort
56 */
57 public PortImpl(Topology topology, SwitchPort switchPort) {
58 this(topology, new PortEvent(switchPort).freeze());
59 }
60
61 /**
62 * Creates a Port object with empty attributes.
63 *
64 * @param topology Topology instance this object belongs to
65 * @param dpid DPID
66 * @param number PortNumber
67 */
68 public PortImpl(Topology topology, Dpid dpid, PortNumber number) {
69 this(topology, new SwitchPort(dpid, number));
70 Validate.notNull(dpid);
71 Validate.notNull(number);
72 }
73
74 public PortImpl(Topology topology, Long dpid, Long number) {
75 this(topology, new SwitchPort(dpid, number));
76 Validate.notNull(dpid);
77 Validate.notNull(number);
78 }
79
80 @Deprecated
81 public PortImpl(Topology topology, Switch parentSwitch, PortNumber number) {
82 this(topology, new SwitchPort(parentSwitch.getDpid(), number));
83 }
84
85 @Deprecated
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070086 public PortImpl(Topology topology, Switch parentSwitch, Long number) {
87 this(topology, parentSwitch, new PortNumber(number.shortValue()));
Ray Milkey269ffb92014-04-03 14:43:30 -070088 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080089
Ray Milkey269ffb92014-04-03 14:43:30 -070090 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070091 public Dpid getDpid() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070092 return asSwitchPort().getDpid();
Ray Milkey269ffb92014-04-03 14:43:30 -070093 }
Yuta HIGUCHIea516d62014-02-13 15:59:32 -080094
Ray Milkey269ffb92014-04-03 14:43:30 -070095 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070096 public PortNumber getNumber() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070097 return asSwitchPort().getPortNumber();
Ray Milkey269ffb92014-04-03 14:43:30 -070098 }
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080099
Ray Milkey269ffb92014-04-03 14:43:30 -0700100 @Override
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700101 public SwitchPort asSwitchPort() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700102 return portObj.getSwitchPort();
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700103 }
104
105 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700106 public String getDescription() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700107 return getStringAttribute(PortEvent.DESCRIPTION, "");
Ray Milkey269ffb92014-04-03 14:43:30 -0700108 }
Yuta HIGUCHIc0366272014-02-10 21:04:57 -0800109
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700110 void setDescription(String description) {
111// portObj.createStringAttribute(attr, value);
112 // TODO implement using attributes
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700113 throw new UnsupportedOperationException("Not implemented yet");
114 }
115
116 @Override
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700117 public Long getHardwareAddress() {
118 // TODO implement using attributes?
119 throw new UnsupportedOperationException("Not implemented yet");
120 }
121
122 @Override
123 public Switch getSwitch() {
124 topology.acquireReadLock();
125 try {
126 return topology.getSwitch(getDpid());
127 } finally {
128 topology.releaseReadLock();
129 }
130 }
131
132 @Override
133 public Link getOutgoingLink() {
134 topology.acquireReadLock();
135 try {
136 return topology.getOutgoingLink(asSwitchPort());
137 } finally {
138 topology.releaseReadLock();
139 }
140 }
141
142 @Override
Yuta HIGUCHI8313f0b2014-07-09 16:36:03 -0700143 public Link getOutgoingLink(String type) {
144 topology.acquireReadLock();
145 try {
146 return topology.getOutgoingLink(asSwitchPort(), type);
147 } finally {
148 topology.releaseReadLock();
149 }
150 }
151
152 @Override
153 public Collection<Link> getOutgoingLinks() {
154 topology.acquireReadLock();
155 try {
156 return topology.getOutgoingLinks(asSwitchPort());
157 } finally {
158 topology.releaseReadLock();
159 }
160 }
161
162 @Override
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700163 public Link getIncomingLink() {
164 topology.acquireReadLock();
165 try {
166 return topology.getIncomingLink(asSwitchPort());
167 } finally {
168 topology.releaseReadLock();
169 }
170 }
171
172 @Override
Yuta HIGUCHI8313f0b2014-07-09 16:36:03 -0700173 public Link getIncomingLink(String type) {
174 topology.acquireReadLock();
175 try {
176 return topology.getIncomingLink(asSwitchPort(), type);
177 } finally {
178 topology.releaseReadLock();
179 }
180 }
181
182 @Override
183 public Collection<Link> getIncomingLinks() {
184 topology.acquireReadLock();
185 try {
186 return topology.getIncomingLinks(asSwitchPort());
187 } finally {
188 topology.releaseReadLock();
189 }
190 }
191
192 @Override
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700193 public Iterable<Device> getDevices() {
194 topology.acquireReadLock();
195 try {
196 return topology.getDevices(this.asSwitchPort());
197 } finally {
198 topology.releaseReadLock();
199 }
200 }
201
202 void replaceStringAttributes(PortEvent updated) {
203 Validate.isTrue(this.asSwitchPort().equals(updated.getSwitchPort()),
204 "Wrong PortEvent given.");
205
206 // XXX simply replacing whole self-contained object for now
207 if (updated.isFrozen()) {
208 this.portObj = updated;
209 } else {
210 this.portObj = new PortEvent(updated).freeze();
211 }
212 }
213
214 @Override
215 public String getStringAttribute(String attr) {
216 return portObj.getStringAttribute(attr);
217 }
218
219 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700220 public String getStringAttribute(String attr, String def) {
221 final String v = getStringAttribute(attr);
222 if (v == null) {
223 return def;
224 } else {
225 return v;
226 }
227 }
228
229 @Override
230 public Map<String, String> getAllStringAttributes() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700231 return portObj.getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700232 }
233
234 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700235 public String toString() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700236 return String.format("%s:%s",
Ray Milkey269ffb92014-04-03 14:43:30 -0700237 getSwitch().getDpid(),
238 getNumber());
239 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700240
241
242 /**
243 * Returns the type of topology object.
244 *
245 * @return the type of the topology object
246 */
247 @Override
248 public String getType() {
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700249 return getStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700250 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800251}