blob: 56fcb91cdd0406066c4bfa3b5a1f5dd3d27196ee [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 {
Masayoshi Kobayashi1015e362013-12-17 22:54:49 -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
Masayoshi Kobayashi1015e362013-12-17 22:54:49 -0800139 long startLinkTime = System.nanoTime();
Naoki Shiota987a5722013-10-23 11:59:36 -0700140 if (addLinkImpl(link)) {
141 // Set LinkInfo only if linfo is non-null.
142 if (linfo != null && (! setLinkInfoImpl(link, linfo))) {
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700143 log.debug("Adding linkinfo failed: {}", link);
yoshi0fee3de2013-11-23 09:13:37 -0800144 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700145 }
yoshi0fee3de2013-11-23 09:13:37 -0800146 dbop.commit();
Masayoshi Kobayashi1015e362013-12-17 22:54:49 -0800147 long endLinkTime = System.nanoTime();
148 log.error("Performance ##add link total time {}", endLinkTime - startLinkTime);
Naoki Shiota987a5722013-10-23 11:59:36 -0700149 success = true;
150 } else {
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700151 // If we fail here that's because the ports aren't added
152 // before we try to add the link
153 log.debug("Adding link failed: {}", link);
yoshi0fee3de2013-11-23 09:13:37 -0800154 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700155 }
156 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800157 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700158 e.printStackTrace();
159 log.error("LinkStorageImpl:addLink link:{} linfo:{} failed", link, linfo);
160 }
161
162 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700163 }
164
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800165 /**
166 * Update multiple records in the LinkStorage in a way provided by op.
167 * @param links List of records to be updated.
168 * @param op Operation to be done.
169 */
170 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700171 public boolean addLinks(List<Link> links) {
172 boolean success = false;
173
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800174 for (Link lt: links) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700175 if (! addLinkImpl(lt)) {
176 return false;
177 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700178 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700179
180 try {
yoshi0fee3de2013-11-23 09:13:37 -0800181 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700182 success = true;
183 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800184 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700185 e.printStackTrace();
186 log.error("LinkStorageImpl:addLinks link:s{} failed", links);
187 }
188
189 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700190 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800191
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700192 /**
Naoki Shiota987a5722013-10-23 11:59:36 -0700193 * Delete a record in the LinkStorage.
194 * @param lt Record to be deleted.
195 */
196 @Override
197 public boolean deleteLink(Link lt) {
198 boolean success = false;
199
200 log.debug("LinkStorageImpl:deleteLink(): {}", lt);
201
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800202 try {
Naoki Shiota987a5722013-10-23 11:59:36 -0700203 if (deleteLinkImpl(lt)) {
yoshi0fee3de2013-11-23 09:13:37 -0800204 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700205 success = true;
206 log.debug("LinkStorageImpl:deleteLink(): deleted edges {}", lt);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800207 } else {
yoshi0fee3de2013-11-23 09:13:37 -0800208 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700209 log.error("LinkStorageImpl:deleteLink(): failed invalid vertices {}", lt);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800210 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700211 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800212 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700213 log.error("LinkStorageImpl:deleteLink(): failed {} {}",
214 new Object[]{lt, e.toString()});
215 e.printStackTrace();
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800216 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700217
218 return success;
219 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800220
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700221 /**
222 * Delete multiple records in LinkStorage.
223 * @param links List of records to be deleted.
224 */
225 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700226 public boolean deleteLinks(List<Link> links) {
227 boolean success = false;
228
229 try {
230 for (Link lt : links) {
231 if (! deleteLinkImpl(lt)) {
yoshi0fee3de2013-11-23 09:13:37 -0800232 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700233 return false;
234 }
235 }
yoshi0fee3de2013-11-23 09:13:37 -0800236 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700237 success = true;
238 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800239 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700240 e.printStackTrace();
241 log.error("LinkStorageImpl:deleteLinks failed invalid vertices {}", links);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800242 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700243
Naoki Shiota987a5722013-10-23 11:59:36 -0700244 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700245 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800246
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800247 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700248 * Get list of all links connected to the port specified by given DPID and port number.
249 * @param dpid DPID of desired port.
250 * @param port Port number of desired port.
251 * @return List of links. Empty list if no port was found.
252 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800253 @Override
254 public List<Link> getLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800255 List<Link> links = new ArrayList<Link>();
yoshi0fee3de2013-11-23 09:13:37 -0800256 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800257 if (srcPort == null)
258 return links;
259 ISwitchObject srcSw = srcPort.getSwitch();
260 if (srcSw == null)
261 return links;
Naoki Shiota93ec1712013-06-13 15:49:36 -0700262
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800263 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
264 ISwitchObject dstSw = dstPort.getSwitch();
265 if (dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800266 Link link = new Link(dpid, port,
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800267 HexString.toLong(dstSw.getDPID()),
268 dstPort.getNumber());
269 links.add(link);
270 }
271 }
272 return links;
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800273 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800274
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800275 /**
276 * Get list of all reverse links connected to the port specified by given DPID and port number.
277 * @param dpid DPID of desired port.
278 * @param port Port number of desired port.
279 * @return List of reverse links. Empty list if no port was found.
280 */
281 @Override
282 public List<Link> getReverseLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800283 List<Link> links = new ArrayList<Link>();
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800284
yoshi0fee3de2013-11-23 09:13:37 -0800285 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800286 if (srcPort == null)
287 return links;
288 ISwitchObject srcSw = srcPort.getSwitch();
289 if (srcSw == null)
290 return links;
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800291
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800292 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
293 ISwitchObject dstSw = dstPort.getSwitch();
294 if (dstSw != null) {
295 Link link = new Link(HexString.toLong(dstSw.getDPID()),
296 dstPort.getNumber(),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800297 dpid, port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800298 links.add(link);
299 }
300 }
301 return links;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800302 }
303
Naoki Shiota11516712013-06-05 22:36:01 -0700304 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700305 * Delete records of the links connected to the port specified by given DPID and port number.
306 * @param dpid DPID of desired port.
307 * @param port Port number of desired port.
308 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800309 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700310 public boolean deleteLinksOnPort(Long dpid, short port) {
311 boolean success = false;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800312
Naoki Shiota987a5722013-10-23 11:59:36 -0700313 List<Link> linksToDelete = getLinks(dpid, port);
314 try {
315 for(Link l : linksToDelete) {
316 if (! deleteLinkImpl(l)) {
yoshi0fee3de2013-11-23 09:13:37 -0800317 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700318 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
319 return false;
320 }
321 }
yoshi0fee3de2013-11-23 09:13:37 -0800322 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700323 success = true;
324 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800325 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700326 e.printStackTrace();
327 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
mininet403d5892013-06-05 03:48:17 -0700328 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700329
330 return success;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800331 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800332
Naoki Shiota11516712013-06-05 22:36:01 -0700333 /**
334 * Get list of all links connected to the switch specified by given DPID.
335 * @param dpid DPID of desired switch.
336 * @return List of links. Empty list if no port was found.
337 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800338 @Override
339 public List<Link> getLinks(String dpid) {
mininet403d5892013-06-05 03:48:17 -0700340 List<Link> links = new ArrayList<Link>();
yoshi0fee3de2013-11-23 09:13:37 -0800341 ISwitchObject srcSw = dbop.searchSwitch(dpid);
Naoki Shiota93ec1712013-06-13 15:49:36 -0700342
343 if(srcSw != null) {
344 for(IPortObject srcPort : srcSw.getPorts()) {
345 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
346 ISwitchObject dstSw = dstPort.getSwitch();
347 if(dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800348 Link link = new Link(HexString.toLong(dpid),
Naoki Shiota93ec1712013-06-13 15:49:36 -0700349 srcPort.getNumber(),
350 HexString.toLong(dstSw.getDPID()),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800351 dstPort.getNumber());
Naoki Shiota93ec1712013-06-13 15:49:36 -0700352 links.add(link);
353 }
354 }
355 }
356 }
357
mininet403d5892013-06-05 03:48:17 -0700358 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800359 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800360
Naoki Shiota11516712013-06-05 22:36:01 -0700361 /**
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700362 * Get list of all reverse links connected to the switch specified by
363 * given DPID.
364 * @param dpid DPID of desired switch.
365 * @return List of reverse links. Empty list if no port was found.
366 */
367 @Override
368 public List<Link> getReverseLinks(String dpid) {
369 List<Link> links = new ArrayList<Link>();
yoshi2fd4c7e2013-11-22 15:47:55 -0800370
yoshi0fee3de2013-11-23 09:13:37 -0800371 ISwitchObject srcSw = dbop.searchSwitch(dpid);
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700372
373 if(srcSw != null) {
374 for(IPortObject srcPort : srcSw.getPorts()) {
375 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
376 ISwitchObject dstSw = dstPort.getSwitch();
377 if(dstSw != null) {
378 Link link = new Link(
379 HexString.toLong(dstSw.getDPID()),
380 dstPort.getNumber(),
381
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800382 HexString.toLong(dpid),
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700383 srcPort.getNumber());
384 links.add(link);
385 }
386 }
387 }
388 }
389
390 return links;
391 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800392
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700393 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700394 * Get list of all links whose state is ACTIVE.
395 * @return List of active links. Empty list if no port was found.
396 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800397 public List<Link> getActiveLinks() {
yoshi0fee3de2013-11-23 09:13:37 -0800398 Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
yoshi2fd4c7e2013-11-22 15:47:55 -0800399
Pankaj Berde5024ec12013-01-31 17:07:29 -0800400 List<Link> links = new ArrayList<Link>();
Naoki Shiota826e84a2013-06-18 13:13:25 -0700401
402 for (ISwitchObject srcSw : switches) {
403 for(IPortObject srcPort : srcSw.getPorts()) {
404 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
405 ISwitchObject dstSw = dstPort.getSwitch();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800406
Naoki Shiota826e84a2013-06-18 13:13:25 -0700407 if(dstSw != null && dstSw.getState().equals("ACTIVE")) {
408 links.add(new Link(HexString.toLong(srcSw.getDPID()),
409 srcPort.getNumber(),
410 HexString.toLong(dstSw.getDPID()),
411 dstPort.getNumber()));
412 }
413 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800414 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800415 }
Naoki Shiota826e84a2013-06-18 13:13:25 -0700416
Pankaj Berde5024ec12013-01-31 17:07:29 -0800417 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800418 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800419
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700420 @Override
421 public LinkInfo getLinkInfo(Link link) {
422 // TODO implement this
423 return null;
424 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800425
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700426 /**
427 * Finalize the object.
428 */
429 public void finalize() {
430 close();
431 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800432
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700433 /**
434 * Close LinkStorage.
435 */
436 @Override
437 public void close() {
438 // TODO Auto-generated method stub
Pankaj Berde62016142013-04-09 15:35:50 -0700439// graph.shutdown();
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700440 }
441
Naoki Shiota987a5722013-10-23 11:59:36 -0700442 /**
443 * Update a record of link with meta-information in the LinkStorage.
444 * @param link Record of a link to update.
445 * @param linkinfo Meta-information of a link to be updated.
446 */
447 private boolean setLinkInfoImpl(Link link, LinkInfo linkinfo) {
448 // TODO implement this
449
450 return false;
451 }
452
453 private boolean addLinkImpl(Link lt) {
454 boolean success = false;
455
456 IPortObject vportSrc = null, vportDst = null;
457
458 // get source port vertex
459 String dpid = HexString.toHexString(lt.getSrc());
460 short port = lt.getSrcPort();
yoshia97632b2013-12-17 15:46:08 -0800461 log.debug("addLinkImpl Src dpid : " + dpid + " port :" + port);
yoshi0fee3de2013-11-23 09:13:37 -0800462 vportSrc = dbop.searchPort(dpid, port);
Naoki Shiota987a5722013-10-23 11:59:36 -0700463
464 // get dest port vertex
465 dpid = HexString.toHexString(lt.getDst());
466 port = lt.getDstPort();
yoshia97632b2013-12-17 15:46:08 -0800467 log.debug("addLinkImpl Dst dpid : " + dpid + " port :" + port);
yoshi0fee3de2013-11-23 09:13:37 -0800468 vportDst = dbop.searchPort(dpid, port);
yoshia97632b2013-12-17 15:46:08 -0800469
470 log.debug("addLinkImpl vportSrc : " + vportSrc + " vportDst : " + vportDst);
471
Naoki Shiota987a5722013-10-23 11:59:36 -0700472 if (vportSrc != null && vportDst != null) {
473 IPortObject portExist = null;
474 // check if the link exists
475 for (IPortObject V : vportSrc.getLinkedPorts()) {
yoshia97632b2013-12-17 15:46:08 -0800476 log.debug("vportSrc.getLinkedPorts() :" + V);
Naoki Shiota987a5722013-10-23 11:59:36 -0700477 if (V.equals(vportDst)) {
478 portExist = V;
479 break;
480 }
481 }
482
483 if (portExist == null) {
484 vportSrc.setLinkPort(vportDst);
485 success = true;
486 } else {
487 log.debug("LinkStorageImpl:addLinkImpl failed link exists {} {} src {} dst {}",
yoshi0fee3de2013-11-23 09:13:37 -0800488 new Object[]{dbop, lt, vportSrc, vportDst});
Naoki Shiota987a5722013-10-23 11:59:36 -0700489 }
490 }
491
492 return success;
493 }
494
495 private boolean deleteLinkImpl(Link lt) {
496 boolean success = false;
497 IPortObject vportSrc = null, vportDst = null;
498
499 // get source port vertex
500 String dpid = HexString.toHexString(lt.getSrc());
501 short port = lt.getSrcPort();
yoshi0fee3de2013-11-23 09:13:37 -0800502 vportSrc = dbop.searchPort(dpid, port);
Naoki Shiota987a5722013-10-23 11:59:36 -0700503
504 // get dst port vertex
505 dpid = HexString.toHexString(lt.getDst());
506 port = lt.getDstPort();
yoshi0fee3de2013-11-23 09:13:37 -0800507 vportDst = dbop.searchPort(dpid, port);
Naoki Shiota987a5722013-10-23 11:59:36 -0700508
509 // FIXME: This needs to remove all edges
510 if (vportSrc != null && vportDst != null) {
511 vportSrc.removeLink(vportDst);
512 log.debug("deleteLinkImpl(): deleted edges src {} dst {}", new Object[]{
513 lt, vportSrc, vportDst});
514 success = true;
515 }
516
517 return success;
518 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800519}