blob: 52dbe478c5b9192b707dbc5efd3fb61b0b3b0d35 [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 HIGUCHIdb1b8302014-06-26 10:50:39 -07003import java.util.Map;
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -07004
5import org.apache.commons.lang.Validate;
6
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -07007import net.onrc.onos.core.util.Dpid;
8import net.onrc.onos.core.util.PortNumber;
Jonathan Hart25bd53e2014-04-30 23:44:09 -07009import net.onrc.onos.core.util.SwitchPort;
10
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080011/**
12 * Port Object stored in In-memory Topology.
Ray Milkey269ffb92014-04-03 14:43:30 -070013 * <p/>
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080014 * TODO REMOVE following design memo: This object itself may hold the DBObject,
15 * but this Object itself will not issue any read/write to the DataStore.
16 */
Jonathan Harte37e4e22014-05-13 19:12:02 -070017public class PortImpl extends TopologyObject implements Port {
Yuta HIGUCHI181d34d2014-02-05 15:05:46 -080018
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070019 //////////////////////////////////////////////////////
20 /// Topology element attributes
21 /// - any changes made here needs to be replicated.
22 //////////////////////////////////////////////////////
23 private PortEvent portObj;
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080024
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070025 ///////////////////
26 /// In-memory index
27 ///////////////////
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080028
Jonathan Hart25bd53e2014-04-30 23:44:09 -070029
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070030 /**
31 * Creates a Port object based on {@link PortEvent}.
32 *
33 * @param topology Topology instance this object belongs to
34 * @param scPort self contained {@link PortEvent}
35 */
36 public PortImpl(Topology topology, PortEvent scPort) {
Jonathan Harte37e4e22014-05-13 19:12:02 -070037 super(topology);
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070038 Validate.notNull(scPort);
Jonathan Hart25bd53e2014-04-30 23:44:09 -070039
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070040 // TODO should we assume portObj is already frozen before this call
41 // or expect attribute update will happen after .
42 if (scPort.isFrozen()) {
43 this.portObj = scPort;
44 } else {
45 this.portObj = new PortEvent(scPort);
46 this.portObj.freeze();
47 }
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070048 }
49
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070050 /**
51 * Creates a Port object with empty attributes.
52 *
53 * @param topology Topology instance this object belongs to
54 * @param switchPort SwitchPort
55 */
56 public PortImpl(Topology topology, SwitchPort switchPort) {
57 this(topology, new PortEvent(switchPort).freeze());
58 }
59
60 /**
61 * Creates a Port object with empty attributes.
62 *
63 * @param topology Topology instance this object belongs to
64 * @param dpid DPID
65 * @param number PortNumber
66 */
67 public PortImpl(Topology topology, Dpid dpid, PortNumber number) {
68 this(topology, new SwitchPort(dpid, number));
69 Validate.notNull(dpid);
70 Validate.notNull(number);
71 }
72
73 public PortImpl(Topology topology, Long dpid, Long number) {
74 this(topology, new SwitchPort(dpid, number));
75 Validate.notNull(dpid);
76 Validate.notNull(number);
77 }
78
79 @Deprecated
80 public PortImpl(Topology topology, Switch parentSwitch, PortNumber number) {
81 this(topology, new SwitchPort(parentSwitch.getDpid(), number));
82 }
83
84 @Deprecated
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070085 public PortImpl(Topology topology, Switch parentSwitch, Long number) {
86 this(topology, parentSwitch, new PortNumber(number.shortValue()));
Ray Milkey269ffb92014-04-03 14:43:30 -070087 }
Jonathan Hart062a2e82014-02-03 09:41:57 -080088
Ray Milkey269ffb92014-04-03 14:43:30 -070089 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070090 public Dpid getDpid() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070091 return asSwitchPort().getDpid();
Ray Milkey269ffb92014-04-03 14:43:30 -070092 }
Yuta HIGUCHIea516d62014-02-13 15:59:32 -080093
Ray Milkey269ffb92014-04-03 14:43:30 -070094 @Override
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -070095 public PortNumber getNumber() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -070096 return asSwitchPort().getPortNumber();
Ray Milkey269ffb92014-04-03 14:43:30 -070097 }
Yuta HIGUCHIc0366272014-02-10 21:04:57 -080098
Ray Milkey269ffb92014-04-03 14:43:30 -070099 @Override
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700100 public SwitchPort asSwitchPort() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700101 return portObj.getSwitchPort();
Jonathan Hart25bd53e2014-04-30 23:44:09 -0700102 }
103
104 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700105 public String getDescription() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700106 return getStringAttribute(PortEvent.DESCRIPTION, "");
Ray Milkey269ffb92014-04-03 14:43:30 -0700107 }
Yuta HIGUCHIc0366272014-02-10 21:04:57 -0800108
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700109 void setDescription(String description) {
110// portObj.createStringAttribute(attr, value);
111 // TODO implement using attributes
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700112 throw new UnsupportedOperationException("Not implemented yet");
113 }
114
115 @Override
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700116 public Long getHardwareAddress() {
117 // TODO implement using attributes?
118 throw new UnsupportedOperationException("Not implemented yet");
119 }
120
121 @Override
122 public Switch getSwitch() {
123 topology.acquireReadLock();
124 try {
125 return topology.getSwitch(getDpid());
126 } finally {
127 topology.releaseReadLock();
128 }
129 }
130
131 @Override
132 public Link getOutgoingLink() {
133 topology.acquireReadLock();
134 try {
135 return topology.getOutgoingLink(asSwitchPort());
136 } finally {
137 topology.releaseReadLock();
138 }
139 }
140
141 @Override
142 public Link getIncomingLink() {
143 topology.acquireReadLock();
144 try {
145 return topology.getIncomingLink(asSwitchPort());
146 } finally {
147 topology.releaseReadLock();
148 }
149 }
150
151 @Override
152 public Iterable<Device> getDevices() {
153 topology.acquireReadLock();
154 try {
155 return topology.getDevices(this.asSwitchPort());
156 } finally {
157 topology.releaseReadLock();
158 }
159 }
160
161 void replaceStringAttributes(PortEvent updated) {
162 Validate.isTrue(this.asSwitchPort().equals(updated.getSwitchPort()),
163 "Wrong PortEvent given.");
164
165 // XXX simply replacing whole self-contained object for now
166 if (updated.isFrozen()) {
167 this.portObj = updated;
168 } else {
169 this.portObj = new PortEvent(updated).freeze();
170 }
171 }
172
173 @Override
174 public String getStringAttribute(String attr) {
175 return portObj.getStringAttribute(attr);
176 }
177
178 @Override
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700179 public String getStringAttribute(String attr, String def) {
180 final String v = getStringAttribute(attr);
181 if (v == null) {
182 return def;
183 } else {
184 return v;
185 }
186 }
187
188 @Override
189 public Map<String, String> getAllStringAttributes() {
Yuta HIGUCHIbf0a8712014-06-30 18:59:46 -0700190 return portObj.getAllStringAttributes();
Yuta HIGUCHIdb1b8302014-06-26 10:50:39 -0700191 }
192
193 @Override
Ray Milkey269ffb92014-04-03 14:43:30 -0700194 public String toString() {
Yuta HIGUCHI8f3dfa32014-06-25 00:14:25 -0700195 return String.format("%s:%s",
Ray Milkey269ffb92014-04-03 14:43:30 -0700196 getSwitch().getDpid(),
197 getNumber());
198 }
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700199
200
201 /**
202 * Returns the type of topology object.
203 *
204 * @return the type of the topology object
205 */
206 @Override
207 public String getType() {
Yuta HIGUCHI1222ac52014-07-09 16:50:28 -0700208 return getStringAttribute(TopologyElement.TYPE, TopologyElement.TYPE_PACKET);
Praseed Balakrishnan57ed8432014-06-26 11:49:56 -0700209 }
Jonathan Hart062a2e82014-02-03 09:41:57 -0800210}