blob: e10c705416bcda781648a414d0c072348625836f [file] [log] [blame]
HIGUCHI Yuta2d011582013-06-15 01:47:11 -07001package net.onrc.onos.ofcontroller.core.internal;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -08002
Pankaj Berde6debb042013-01-16 18:04:32 -08003import java.util.ArrayList;
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -08004import java.util.List;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -08005
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -08006import net.floodlightcontroller.routing.Link;
yoshi2fd4c7e2013-11-22 15:47:55 -08007import net.onrc.onos.graph.DBOperation;
HIGUCHI Yuta2d011582013-06-15 01:47:11 -07008import net.onrc.onos.ofcontroller.core.ILinkStorage;
pingping-lin00926032013-12-18 12:13:08 +08009import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IDeviceObject;
HIGUCHI Yuta20514902013-06-12 11:24:16 -070010import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
11import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
HIGUCHI Yutaa56fbde2013-06-17 14:26:05 -070012import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo;
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080013
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080014import org.openflow.util.HexString;
15import org.slf4j.Logger;
16import org.slf4j.LoggerFactory;
17
Naoki Shiotac57efb82013-06-18 13:40:28 -070018import com.tinkerpop.blueprints.Vertex;
Naoki Shiotac57efb82013-06-18 13:40:28 -070019import com.tinkerpop.pipes.PipeFunction;
20import com.tinkerpop.pipes.transform.PathPipe;
yoshi2fd4c7e2013-11-22 15:47:55 -080021import net.onrc.onos.graph.GraphDBManager;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080022
Naoki Shiota1c393ce2013-06-28 22:55:14 -070023/**
Naoki Shiotab2d17e82013-10-18 18:08:16 -070024 * This is the class for storing the information of links into GraphDB
Naoki Shiota1c393ce2013-06-28 22:55:14 -070025 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080026public class LinkStorageImpl implements ILinkStorage {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080027
yoshi0fee3de2013-11-23 09:13:37 -080028 protected final static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class);
29 protected DBOperation dbop;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080030
Naoki Shiotab2d17e82013-10-18 18:08:16 -070031 /**
32 * Initialize the object. Open LinkStorage using given configuration file.
33 * @param conf Path (absolute path for now) to configuration file.
34 */
35 @Override
yoshi0fee3de2013-11-23 09:13:37 -080036 public void init(final String dbStore, final String conf) {
yoshid38cd312013-12-02 19:54:44 -080037 //this.dbop = GraphDBManager.getDBOperation(dbStore, conf);
38 this.dbop = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf");
39
Naoki Shiotab2d17e82013-10-18 18:08:16 -070040 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080041
Naoki Shiota987a5722013-10-23 11:59:36 -070042 // Method designing policy:
43 // op.commit() and op.rollback() MUST called in public (first-class) methods.
44 // A first-class method MUST NOT call other first-class method.
45 // Routine process should be implemented in private method.
46 // A private method MUST NOT call commit or rollback.
yoshi2fd4c7e2013-11-22 15:47:55 -080047
Naoki Shiota987a5722013-10-23 11:59:36 -070048
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080049 /**
Naoki Shiota987a5722013-10-23 11:59:36 -070050 * Update a record in the LinkStorage in a way provided by dmop.
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080051 * @param link Record of a link to be updated.
Naoki Shiota987a5722013-10-23 11:59:36 -070052 * @param linkinfo Meta-information of a link to be updated.
53 * @param dmop Operation to be done.
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080054 */
55 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -070056 public boolean update(Link link, LinkInfo linkinfo, DM_OPERATION dmop) {
57 boolean success = false;
58
59 switch (dmop) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -070060 case CREATE:
61 case INSERT:
Naoki Shiota987a5722013-10-23 11:59:36 -070062 if (link != null) {
63 try {
64 if (addLinkImpl(link)) {
yoshi0fee3de2013-11-23 09:13:37 -080065 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -070066 success = true;
67 }
68 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -080069 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -070070 e.printStackTrace();
71 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
72 }
73 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070074 break;
75 case UPDATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070076 if (link != null && linkinfo != null) {
77 try {
78 if (setLinkInfoImpl(link, linkinfo)) {
yoshi0fee3de2013-11-23 09:13:37 -080079 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -070080 success = true;
81 }
82 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -080083 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -070084 e.printStackTrace();
85 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
86 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070087 }
88 break;
89 case DELETE:
Naoki Shiota987a5722013-10-23 11:59:36 -070090 if (link != null) {
91 try {
92 if (deleteLinkImpl(link)) {
yoshi0fee3de2013-11-23 09:13:37 -080093 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -070094 success = true;
95 log.debug("LinkStorageImpl:update {} link:{} succeeded", dmop, link);
96 } else {
yoshi0fee3de2013-11-23 09:13:37 -080097 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -070098 log.debug("LinkStorageImpl:update {} link:{} failed", dmop, link);
99 }
100 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800101 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700102 e.printStackTrace();
103 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
104 }
105 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700106 break;
107 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700108
109 return success;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800110 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800111
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700112 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700113 public boolean addLink(Link link) {
114 return addLink(link, null);
115 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800116
pingping-lin00926032013-12-18 12:13:08 +0800117 private void deleteDeviceOnPort(Long dpid, Short number)
118 {
Yuta HIGUCHI0c20db82013-12-17 23:06:36 -0800119 IPortObject srcPortObject = dbop.searchPort(HexString.toHexString(dpid), number);
pingping-lin00926032013-12-18 12:13:08 +0800120 if (srcPortObject.getDevices().iterator().hasNext()) {
121 for (IDeviceObject deviceObject: srcPortObject.getDevices()) {
122 srcPortObject.removeDevice(deviceObject);
123 log.debug("delete Device "+ deviceObject.getMACAddress() +
124 " from sw: {} port: {} due to a new link added",
125 dpid, number);
126 }
127 }
128 }
129
Naoki Shiota987a5722013-10-23 11:59:36 -0700130 @Override
131 public boolean addLink(Link link, LinkInfo linfo) {
132 boolean success = false;
133
134 try {
pingping-lin00926032013-12-18 12:13:08 +0800135 //delete the Device attachment points for the related switch and port
136 deleteDeviceOnPort(link.getSrc(),link.getSrcPort());
137 deleteDeviceOnPort(link.getDst(),link.getDstPort());
138
Naoki Shiota987a5722013-10-23 11:59:36 -0700139 if (addLinkImpl(link)) {
140 // Set LinkInfo only if linfo is non-null.
141 if (linfo != null && (! setLinkInfoImpl(link, linfo))) {
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700142 log.debug("Adding linkinfo failed: {}", link);
yoshi0fee3de2013-11-23 09:13:37 -0800143 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700144 }
yoshi0fee3de2013-11-23 09:13:37 -0800145 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700146 success = true;
147 } else {
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700148 // If we fail here that's because the ports aren't added
149 // before we try to add the link
150 log.debug("Adding link failed: {}", link);
yoshi0fee3de2013-11-23 09:13:37 -0800151 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700152 }
153 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800154 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700155 e.printStackTrace();
156 log.error("LinkStorageImpl:addLink link:{} linfo:{} failed", link, linfo);
157 }
158
159 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700160 }
161
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800162 /**
163 * Update multiple records in the LinkStorage in a way provided by op.
164 * @param links List of records to be updated.
165 * @param op Operation to be done.
166 */
167 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700168 public boolean addLinks(List<Link> links) {
169 boolean success = false;
170
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800171 for (Link lt: links) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700172 if (! addLinkImpl(lt)) {
173 return false;
174 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700175 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700176
177 try {
yoshi0fee3de2013-11-23 09:13:37 -0800178 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700179 success = true;
180 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800181 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700182 e.printStackTrace();
183 log.error("LinkStorageImpl:addLinks link:s{} failed", links);
184 }
185
186 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700187 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800188
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700189 /**
Naoki Shiota987a5722013-10-23 11:59:36 -0700190 * Delete a record in the LinkStorage.
191 * @param lt Record to be deleted.
192 */
193 @Override
194 public boolean deleteLink(Link lt) {
195 boolean success = false;
196
197 log.debug("LinkStorageImpl:deleteLink(): {}", lt);
198
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800199 try {
Naoki Shiota987a5722013-10-23 11:59:36 -0700200 if (deleteLinkImpl(lt)) {
yoshi0fee3de2013-11-23 09:13:37 -0800201 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700202 success = true;
203 log.debug("LinkStorageImpl:deleteLink(): deleted edges {}", lt);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800204 } else {
yoshi0fee3de2013-11-23 09:13:37 -0800205 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700206 log.error("LinkStorageImpl:deleteLink(): failed invalid vertices {}", lt);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800207 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700208 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800209 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700210 log.error("LinkStorageImpl:deleteLink(): failed {} {}",
211 new Object[]{lt, e.toString()});
212 e.printStackTrace();
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800213 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700214
215 return success;
216 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800217
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700218 /**
219 * Delete multiple records in LinkStorage.
220 * @param links List of records to be deleted.
221 */
222 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700223 public boolean deleteLinks(List<Link> links) {
224 boolean success = false;
225
226 try {
227 for (Link lt : links) {
228 if (! deleteLinkImpl(lt)) {
yoshi0fee3de2013-11-23 09:13:37 -0800229 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700230 return false;
231 }
232 }
yoshi0fee3de2013-11-23 09:13:37 -0800233 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700234 success = true;
235 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800236 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700237 e.printStackTrace();
238 log.error("LinkStorageImpl:deleteLinks failed invalid vertices {}", links);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800239 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700240
Naoki Shiota987a5722013-10-23 11:59:36 -0700241 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700242 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800243
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800244 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700245 * Get list of all links connected to the port specified by given DPID and port number.
246 * @param dpid DPID of desired port.
247 * @param port Port number of desired port.
248 * @return List of links. Empty list if no port was found.
249 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800250 @Override
251 public List<Link> getLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800252 List<Link> links = new ArrayList<Link>();
yoshi0fee3de2013-11-23 09:13:37 -0800253 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800254 if (srcPort == null)
255 return links;
256 ISwitchObject srcSw = srcPort.getSwitch();
257 if (srcSw == null)
258 return links;
Naoki Shiota93ec1712013-06-13 15:49:36 -0700259
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800260 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
261 ISwitchObject dstSw = dstPort.getSwitch();
262 if (dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800263 Link link = new Link(dpid, port,
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800264 HexString.toLong(dstSw.getDPID()),
265 dstPort.getNumber());
266 links.add(link);
267 }
268 }
269 return links;
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800270 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800271
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800272 /**
273 * Get list of all reverse links connected to the port specified by given DPID and port number.
274 * @param dpid DPID of desired port.
275 * @param port Port number of desired port.
276 * @return List of reverse links. Empty list if no port was found.
277 */
278 @Override
279 public List<Link> getReverseLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800280 List<Link> links = new ArrayList<Link>();
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800281
yoshi0fee3de2013-11-23 09:13:37 -0800282 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800283 if (srcPort == null)
284 return links;
285 ISwitchObject srcSw = srcPort.getSwitch();
286 if (srcSw == null)
287 return links;
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800288
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800289 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
290 ISwitchObject dstSw = dstPort.getSwitch();
291 if (dstSw != null) {
292 Link link = new Link(HexString.toLong(dstSw.getDPID()),
293 dstPort.getNumber(),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800294 dpid, port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800295 links.add(link);
296 }
297 }
298 return links;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800299 }
300
Naoki Shiota11516712013-06-05 22:36:01 -0700301 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700302 * Delete records of the links connected to the port specified by given DPID and port number.
303 * @param dpid DPID of desired port.
304 * @param port Port number of desired port.
305 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800306 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700307 public boolean deleteLinksOnPort(Long dpid, short port) {
308 boolean success = false;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800309
Naoki Shiota987a5722013-10-23 11:59:36 -0700310 List<Link> linksToDelete = getLinks(dpid, port);
311 try {
312 for(Link l : linksToDelete) {
313 if (! deleteLinkImpl(l)) {
yoshi0fee3de2013-11-23 09:13:37 -0800314 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700315 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
316 return false;
317 }
318 }
yoshi0fee3de2013-11-23 09:13:37 -0800319 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700320 success = true;
321 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800322 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700323 e.printStackTrace();
324 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
mininet403d5892013-06-05 03:48:17 -0700325 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700326
327 return success;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800328 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800329
Naoki Shiota11516712013-06-05 22:36:01 -0700330 /**
331 * Get list of all links connected to the switch specified by given DPID.
332 * @param dpid DPID of desired switch.
333 * @return List of links. Empty list if no port was found.
334 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800335 @Override
336 public List<Link> getLinks(String dpid) {
mininet403d5892013-06-05 03:48:17 -0700337 List<Link> links = new ArrayList<Link>();
yoshi0fee3de2013-11-23 09:13:37 -0800338 ISwitchObject srcSw = dbop.searchSwitch(dpid);
Naoki Shiota93ec1712013-06-13 15:49:36 -0700339
340 if(srcSw != null) {
341 for(IPortObject srcPort : srcSw.getPorts()) {
342 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
343 ISwitchObject dstSw = dstPort.getSwitch();
344 if(dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800345 Link link = new Link(HexString.toLong(dpid),
Naoki Shiota93ec1712013-06-13 15:49:36 -0700346 srcPort.getNumber(),
347 HexString.toLong(dstSw.getDPID()),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800348 dstPort.getNumber());
Naoki Shiota93ec1712013-06-13 15:49:36 -0700349 links.add(link);
350 }
351 }
352 }
353 }
354
mininet403d5892013-06-05 03:48:17 -0700355 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800356 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800357
Naoki Shiota11516712013-06-05 22:36:01 -0700358 /**
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700359 * Get list of all reverse links connected to the switch specified by
360 * given DPID.
361 * @param dpid DPID of desired switch.
362 * @return List of reverse links. Empty list if no port was found.
363 */
364 @Override
365 public List<Link> getReverseLinks(String dpid) {
366 List<Link> links = new ArrayList<Link>();
yoshi2fd4c7e2013-11-22 15:47:55 -0800367
yoshi0fee3de2013-11-23 09:13:37 -0800368 ISwitchObject srcSw = dbop.searchSwitch(dpid);
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700369
370 if(srcSw != null) {
371 for(IPortObject srcPort : srcSw.getPorts()) {
372 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
373 ISwitchObject dstSw = dstPort.getSwitch();
374 if(dstSw != null) {
375 Link link = new Link(
376 HexString.toLong(dstSw.getDPID()),
377 dstPort.getNumber(),
378
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800379 HexString.toLong(dpid),
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700380 srcPort.getNumber());
381 links.add(link);
382 }
383 }
384 }
385 }
386
387 return links;
388 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800389
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700390 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700391 * Get list of all links whose state is ACTIVE.
392 * @return List of active links. Empty list if no port was found.
393 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800394 public List<Link> getActiveLinks() {
yoshi0fee3de2013-11-23 09:13:37 -0800395 Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
yoshi2fd4c7e2013-11-22 15:47:55 -0800396
Pankaj Berde5024ec12013-01-31 17:07:29 -0800397 List<Link> links = new ArrayList<Link>();
Naoki Shiota826e84a2013-06-18 13:13:25 -0700398
399 for (ISwitchObject srcSw : switches) {
400 for(IPortObject srcPort : srcSw.getPorts()) {
401 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
402 ISwitchObject dstSw = dstPort.getSwitch();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800403
Naoki Shiota826e84a2013-06-18 13:13:25 -0700404 if(dstSw != null && dstSw.getState().equals("ACTIVE")) {
405 links.add(new Link(HexString.toLong(srcSw.getDPID()),
406 srcPort.getNumber(),
407 HexString.toLong(dstSw.getDPID()),
408 dstPort.getNumber()));
409 }
410 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800411 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800412 }
Naoki Shiota826e84a2013-06-18 13:13:25 -0700413
Pankaj Berde5024ec12013-01-31 17:07:29 -0800414 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800415 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800416
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700417 @Override
418 public LinkInfo getLinkInfo(Link link) {
419 // TODO implement this
420 return null;
421 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800422
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700423 /**
424 * Finalize the object.
425 */
426 public void finalize() {
427 close();
428 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800429
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700430 /**
431 * Close LinkStorage.
432 */
433 @Override
434 public void close() {
435 // TODO Auto-generated method stub
Pankaj Berde62016142013-04-09 15:35:50 -0700436// graph.shutdown();
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700437 }
438
Naoki Shiota987a5722013-10-23 11:59:36 -0700439 /**
440 * Update a record of link with meta-information in the LinkStorage.
441 * @param link Record of a link to update.
442 * @param linkinfo Meta-information of a link to be updated.
443 */
444 private boolean setLinkInfoImpl(Link link, LinkInfo linkinfo) {
445 // TODO implement this
446
447 return false;
448 }
449
450 private boolean addLinkImpl(Link lt) {
451 boolean success = false;
452
453 IPortObject vportSrc = null, vportDst = null;
454
455 // get source port vertex
456 String dpid = HexString.toHexString(lt.getSrc());
457 short port = lt.getSrcPort();
yoshia97632b2013-12-17 15:46:08 -0800458 log.debug("addLinkImpl Src dpid : " + dpid + " port :" + port);
yoshi0fee3de2013-11-23 09:13:37 -0800459 vportSrc = dbop.searchPort(dpid, port);
Naoki Shiota987a5722013-10-23 11:59:36 -0700460
461 // get dest port vertex
462 dpid = HexString.toHexString(lt.getDst());
463 port = lt.getDstPort();
yoshia97632b2013-12-17 15:46:08 -0800464 log.debug("addLinkImpl Dst dpid : " + dpid + " port :" + port);
yoshi0fee3de2013-11-23 09:13:37 -0800465 vportDst = dbop.searchPort(dpid, port);
yoshia97632b2013-12-17 15:46:08 -0800466
467 log.debug("addLinkImpl vportSrc : " + vportSrc + " vportDst : " + vportDst);
468
Naoki Shiota987a5722013-10-23 11:59:36 -0700469 if (vportSrc != null && vportDst != null) {
470 IPortObject portExist = null;
471 // check if the link exists
472 for (IPortObject V : vportSrc.getLinkedPorts()) {
yoshia97632b2013-12-17 15:46:08 -0800473 log.debug("vportSrc.getLinkedPorts() :" + V);
Naoki Shiota987a5722013-10-23 11:59:36 -0700474 if (V.equals(vportDst)) {
475 portExist = V;
476 break;
477 }
478 }
479
480 if (portExist == null) {
481 vportSrc.setLinkPort(vportDst);
482 success = true;
483 } else {
484 log.debug("LinkStorageImpl:addLinkImpl failed link exists {} {} src {} dst {}",
yoshi0fee3de2013-11-23 09:13:37 -0800485 new Object[]{dbop, lt, vportSrc, vportDst});
Naoki Shiota987a5722013-10-23 11:59:36 -0700486 }
487 }
488
489 return success;
490 }
491
492 private boolean deleteLinkImpl(Link lt) {
493 boolean success = false;
494 IPortObject vportSrc = null, vportDst = null;
495
496 // get source port vertex
497 String dpid = HexString.toHexString(lt.getSrc());
498 short port = lt.getSrcPort();
yoshi0fee3de2013-11-23 09:13:37 -0800499 vportSrc = dbop.searchPort(dpid, port);
Naoki Shiota987a5722013-10-23 11:59:36 -0700500
501 // get dst port vertex
502 dpid = HexString.toHexString(lt.getDst());
503 port = lt.getDstPort();
yoshi0fee3de2013-11-23 09:13:37 -0800504 vportDst = dbop.searchPort(dpid, port);
Naoki Shiota987a5722013-10-23 11:59:36 -0700505
506 // FIXME: This needs to remove all edges
507 if (vportSrc != null && vportDst != null) {
508 vportSrc.removeLink(vportDst);
509 log.debug("deleteLinkImpl(): deleted edges src {} dst {}", new Object[]{
510 lt, vportSrc, vportDst});
511 success = true;
512 }
513
514 return success;
515 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800516}