blob: 935b3c5a32ae728d11131bd1089eefad4539a9c2 [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
Jonathan Hart25bd53e2014-04-30 23:44:09 -070026
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070027 /**
28 * Creates a Port object based on {@link PortEvent}.
29 *
30 * @param topology Topology instance this object belongs to
31 * @param scPort self contained {@link PortEvent}
32 */
33 public PortImpl(Topology topology, PortEvent scPort) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070034 super(topology);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070035 Validate.notNull(scPort);
Jonathan Hart25bd53e2014-04-30 23:44:09 -070036
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070037 // TODO should we assume portObj is already frozen before this call
38 // or expect attribute update will happen after .
39 if (scPort.isFrozen()) {
40 this.portObj = scPort;
41 } else {
42 this.portObj = new PortEvent(scPort);
43 this.portObj.freeze();
44 }
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070045 }
46
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070047 /**
48 * Creates a Port object with empty attributes.
49 *
50 * @param topology Topology instance this object belongs to
51 * @param switchPort SwitchPort
52 */
53 public PortImpl(Topology topology, SwitchPort switchPort) {
54 this(topology, new PortEvent(switchPort).freeze());
55 }
56
57 /**
58 * Creates a Port object with empty attributes.
59 *
60 * @param topology Topology instance this object belongs to
61 * @param dpid DPID
62 * @param number PortNumber
63 */
64 public PortImpl(Topology topology, Dpid dpid, PortNumber number) {
65 this(topology, new SwitchPort(dpid, number));
66 Validate.notNull(dpid);
67 Validate.notNull(number);
68 }
69
Ray Milkey269ffb92014-04-03 14:43:30 -070070 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070071 public Dpid getDpid() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070072 return asSwitchPort().getDpid();
Ray Milkey269ffb92014-04-03 14:43:30 -070073 }
Yuta HIGUCHIea516d62014-02-13 15:59:32 -080074
Ray Milkey269ffb92014-04-03 14:43:30 -070075 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070076 public PortNumber getNumber() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070077 return asSwitchPort().getPortNumber();
Ray Milkey269ffb92014-04-03 14:43:30 -070078 }
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080079
Ray Milkey269ffb92014-04-03 14:43:30 -070080 @Override
Jonathan Hart25bd53e2014-04-30 23:44:09 -070081 public SwitchPort asSwitchPort() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070082 return portObj.getSwitchPort();
Jonathan Hart25bd53e2014-04-30 23:44:09 -070083 }
84
85 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -070086 public String getDescription() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070087 return getStringAttribute(PortEvent.DESCRIPTION, "");
Ray Milkey269ffb92014-04-03 14:43:30 -070088 }
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080089
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070090 void setDescription(String description) {
91// portObj.createStringAttribute(attr, value);
92 // TODO implement using attributes
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -070093 throw new UnsupportedOperationException("Not implemented yet");
94 }
95
96 @Override
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070097 public Long getHardwareAddress() {
98 // TODO implement using attributes?
99 throw new UnsupportedOperationException("Not implemented yet");
100 }
101
102 @Override
103 public Switch getSwitch() {
104 topology.acquireReadLock();
105 try {
106 return topology.getSwitch(getDpid());
107 } finally {
108 topology.releaseReadLock();
109 }
110 }
111
112 @Override
113 public Link getOutgoingLink() {
114 topology.acquireReadLock();
115 try {
116 return topology.getOutgoingLink(asSwitchPort());
117 } finally {
118 topology.releaseReadLock();
119 }
120 }
121
122 @Override
Yuta HIGUCHI8313f0b2014-07-09 16:36:03 -0700123 public Link getOutgoingLink(String type) {
124 topology.acquireReadLock();
125 try {
126 return topology.getOutgoingLink(asSwitchPort(), type);
127 } finally {
128 topology.releaseReadLock();
129 }
130 }
131
132 @Override
133 public Collection<Link> getOutgoingLinks() {
134 topology.acquireReadLock();
135 try {
136 return topology.getOutgoingLinks(asSwitchPort());
137 } finally {
138 topology.releaseReadLock();
139 }
140 }
141
142 @Override
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700143 public Link getIncomingLink() {
144 topology.acquireReadLock();
145 try {
146 return topology.getIncomingLink(asSwitchPort());
147 } finally {
148 topology.releaseReadLock();
149 }
150 }
151
152 @Override
Yuta HIGUCHI8313f0b2014-07-09 16:36:03 -0700153 public Link getIncomingLink(String type) {
154 topology.acquireReadLock();
155 try {
156 return topology.getIncomingLink(asSwitchPort(), type);
157 } finally {
158 topology.releaseReadLock();
159 }
160 }
161
162 @Override
163 public Collection<Link> getIncomingLinks() {
164 topology.acquireReadLock();
165 try {
166 return topology.getIncomingLinks(asSwitchPort());
167 } finally {
168 topology.releaseReadLock();
169 }
170 }
171
172 @Override
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700173 public Iterable<Host> getHosts() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700174 topology.acquireReadLock();
175 try {
Yuta HIGUCHIbfc77f02014-07-14 22:50:25 -0700176 return topology.getHosts(this.asSwitchPort());
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700177 } finally {
178 topology.releaseReadLock();
179 }
180 }
181
182 void replaceStringAttributes(PortEvent updated) {
183 Validate.isTrue(this.asSwitchPort().equals(updated.getSwitchPort()),
184 "Wrong PortEvent given.");
185
186 // XXX simply replacing whole self-contained object for now
187 if (updated.isFrozen()) {
188 this.portObj = updated;
189 } else {
190 this.portObj = new PortEvent(updated).freeze();
191 }
192 }
193
194 @Override
195 public String getStringAttribute(String attr) {
196 return portObj.getStringAttribute(attr);
197 }
198
199 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700200 public String getStringAttribute(String attr, String def) {
201 final String v = getStringAttribute(attr);
202 if (v == null) {
203 return def;
204 } else {
205 return v;
206 }
207 }
208
209 @Override
210 public Map<String, String> getAllStringAttributes() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700211 return portObj.getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700212 }
213
214 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700215 public String toString() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700216 return String.format("%s:%s",
Ray Milkey269ffb92014-04-03 14:43:30 -0700217 getSwitch().getDpid(),
218 getNumber());
219 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700220
221
222 /**
223 * Returns the type of topology object.
224 *
225 * @return the type of the topology object
226 */
227 @Override
228 public String getType() {
Yuta HIGUCHIdbc33122014-07-10 13:32:32 -0700229 return getStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET_LAYER);
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700230 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800231}