blob: 60f8e1000548c4fe3b22e12d7385322a260b2ba0 [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;
Pankaj Berde38646d62013-06-21 11:34:04 -07007import net.onrc.onos.graph.GraphDBOperation;
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 Shiota1c393ce2013-06-28 22:55:14 -070018/**
Naoki Shiotab2d17e82013-10-18 18:08:16 -070019 * This is the class for storing the information of links into GraphDB
Naoki Shiota1c393ce2013-06-28 22:55:14 -070020 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080021public class LinkStorageImpl implements ILinkStorage {
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080022
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070023 protected final static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class);
Naoki Shiota987a5722013-10-23 11:59:36 -070024 protected GraphDBOperation op;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080025
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080026
Naoki Shiotab2d17e82013-10-18 18:08:16 -070027 /**
28 * Initialize the object. Open LinkStorage using given configuration file.
29 * @param conf Path (absolute path for now) to configuration file.
30 */
31 @Override
32 public void init(String conf) {
Naoki Shiota987a5722013-10-23 11:59:36 -070033 this.op = new GraphDBOperation(conf);
Naoki Shiotab2d17e82013-10-18 18:08:16 -070034 }
35
Naoki Shiota987a5722013-10-23 11:59:36 -070036 // Method designing policy:
37 // op.commit() and op.rollback() MUST called in public (first-class) methods.
38 // A first-class method MUST NOT call other first-class method.
39 // Routine process should be implemented in private method.
40 // A private method MUST NOT call commit or rollback.
41
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080042
Naoki Shiota11516712013-06-05 22:36:01 -070043 /**
Naoki Shiota987a5722013-10-23 11:59:36 -070044 * Update a record in the LinkStorage in a way provided by dmop.
Naoki Shiota11516712013-06-05 22:36:01 -070045 * @param link Record of a link to be updated.
Naoki Shiota987a5722013-10-23 11:59:36 -070046 * @param linkinfo Meta-information of a link to be updated.
47 * @param dmop Operation to be done.
Naoki Shiota11516712013-06-05 22:36:01 -070048 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080049 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -070050 public boolean update(Link link, LinkInfo linkinfo, DM_OPERATION dmop) {
51 boolean success = false;
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -080052
Naoki Shiota987a5722013-10-23 11:59:36 -070053 switch (dmop) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -070054 case CREATE:
55 case INSERT:
Naoki Shiota987a5722013-10-23 11:59:36 -070056 if (link != null) {
57 try {
58 if (addLinkImpl(link)) {
59 op.commit();
60 success = true;
61 }
62 } catch (Exception e) {
63 op.rollback();
64 e.printStackTrace();
65 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
66 }
67 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070068 break;
69 case UPDATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070070 if (link != null && linkinfo != null) {
71 try {
72 if (setLinkInfoImpl(link, linkinfo)) {
73 op.commit();
74 success = true;
75 }
76 } catch (Exception e) {
77 op.rollback();
78 e.printStackTrace();
79 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
80 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070081 }
82 break;
83 case DELETE:
Naoki Shiota987a5722013-10-23 11:59:36 -070084 if (link != null) {
85 try {
86 if (deleteLinkImpl(link)) {
87 op.commit();
88 success = true;
89 log.debug("LinkStorageImpl:update {} link:{} succeeded", dmop, link);
90 } else {
91 op.rollback();
92 log.debug("LinkStorageImpl:update {} link:{} failed", dmop, link);
93 }
94 } catch (Exception e) {
95 op.rollback();
96 e.printStackTrace();
97 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
98 }
99 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700100 break;
101 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800102
Naoki Shiota987a5722013-10-23 11:59:36 -0700103 return success;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800104 }
105
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700106 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700107 public boolean addLink(Link link) {
108 return addLink(link, null);
109 }
110
pingping-lin00926032013-12-18 12:13:08 +0800111 private void deleteDeviceOnPort(Long dpid, Short number)
112 {
113 IPortObject srcPortObject = op.searchPort(HexString.toHexString(dpid), number);
Pavlin Radoslavov78073282013-12-19 17:01:23 -0800114 if (srcPortObject == null)
115 return;
116 Iterable<IDeviceObject> devices = srcPortObject.getDevices();
117 if (devices == null)
118 return;
119 if (devices.iterator().hasNext()) {
pingping-lin00926032013-12-18 12:13:08 +0800120 for (IDeviceObject deviceObject: srcPortObject.getDevices()) {
121 srcPortObject.removeDevice(deviceObject);
122 log.debug("delete Device "+ deviceObject.getMACAddress() +
123 " from sw: {} port: {} due to a new link added",
124 dpid, number);
125 }
126 }
127 }
128
Naoki Shiota987a5722013-10-23 11:59:36 -0700129 @Override
130 public boolean addLink(Link link, LinkInfo linfo) {
131 boolean success = false;
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800132
Naoki Shiota987a5722013-10-23 11:59:36 -0700133 try {
pingping-lin00926032013-12-18 12:13:08 +0800134 //delete the Device attachment points for the related switch and port
135 deleteDeviceOnPort(link.getSrc(),link.getSrcPort());
136 deleteDeviceOnPort(link.getDst(),link.getDstPort());
137
Naoki Shiota987a5722013-10-23 11:59:36 -0700138 if (addLinkImpl(link)) {
139 // Set LinkInfo only if linfo is non-null.
140 if (linfo != null && (! setLinkInfoImpl(link, linfo))) {
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700141 log.debug("Adding linkinfo failed: {}", link);
Naoki Shiota987a5722013-10-23 11:59:36 -0700142 op.rollback();
143 }
144 op.commit();
145 success = true;
146 } else {
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700147 // If we fail here that's because the ports aren't added
148 // before we try to add the link
149 log.debug("Adding link failed: {}", link);
Naoki Shiota987a5722013-10-23 11:59:36 -0700150 op.rollback();
151 }
152 } catch (Exception e) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800153 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700154 e.printStackTrace();
155 log.error("LinkStorageImpl:addLink link:{} linfo:{} failed", link, linfo);
156 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800157
Naoki Shiota987a5722013-10-23 11:59:36 -0700158 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700159 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800160
Naoki Shiota11516712013-06-05 22:36:01 -0700161 /**
162 * Update multiple records in the LinkStorage in a way provided by op.
163 * @param links List of records to be updated.
164 * @param op Operation to be done.
165 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800166 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700167 public boolean addLinks(List<Link> links) {
168 boolean success = false;
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800169
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800170 for (Link lt: links) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700171 if (! addLinkImpl(lt)) {
172 return false;
173 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700174 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800175
Naoki Shiota987a5722013-10-23 11:59:36 -0700176 try {
177 op.commit();
178 success = true;
179 } catch (Exception e) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800180 op.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700181 e.printStackTrace();
182 log.error("LinkStorageImpl:addLinks link:s{} failed", links);
183 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800184
Naoki Shiota987a5722013-10-23 11:59:36 -0700185 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700186 }
187
188 /**
Naoki Shiota987a5722013-10-23 11:59:36 -0700189 * Delete a record in the LinkStorage.
190 * @param lt Record to be deleted.
191 */
192 @Override
193 public boolean deleteLink(Link lt) {
194 boolean success = false;
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800195
Naoki Shiota987a5722013-10-23 11:59:36 -0700196 log.debug("LinkStorageImpl:deleteLink(): {}", lt);
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800197
Naoki Shiota987a5722013-10-23 11:59:36 -0700198 try {
199 if (deleteLinkImpl(lt)) {
200 op.commit();
201 success = true;
202 log.debug("LinkStorageImpl:deleteLink(): deleted edges {}", lt);
203 } else {
204 op.rollback();
205 log.error("LinkStorageImpl:deleteLink(): failed invalid vertices {}", lt);
206 }
207 } catch (Exception e) {
208 op.rollback();
209 log.error("LinkStorageImpl:deleteLink(): failed {} {}",
210 new Object[]{lt, e.toString()});
211 e.printStackTrace();
212 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800213
Naoki Shiota987a5722013-10-23 11:59:36 -0700214 return success;
215 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700216
217 /**
218 * Delete multiple records in LinkStorage.
219 * @param links List of records to be deleted.
220 */
221 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700222 public boolean deleteLinks(List<Link> links) {
223 boolean success = false;
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800224
Naoki Shiota987a5722013-10-23 11:59:36 -0700225 try {
226 for (Link lt : links) {
227 if (! deleteLinkImpl(lt)) {
228 op.rollback();
229 return false;
230 }
231 }
232 op.commit();
233 success = true;
234 } catch (Exception e) {
235 op.rollback();
236 e.printStackTrace();
237 log.error("LinkStorageImpl:deleteLinks failed invalid vertices {}", links);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800238 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800239
Naoki Shiota987a5722013-10-23 11:59:36 -0700240 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700241 }
242
Naoki Shiota11516712013-06-05 22:36:01 -0700243 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700244 * Get list of all links connected to the port specified by given DPID and port number.
245 * @param dpid DPID of desired port.
246 * @param port Port number of desired port.
247 * @return List of links. Empty list if no port was found.
248 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800249 @Override
250 public List<Link> getLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800251 List<Link> links = new ArrayList<Link>();
252
253 IPortObject srcPort = op.searchPort(HexString.toHexString(dpid), port);
254 if (srcPort == null)
255 return links;
256 ISwitchObject srcSw = srcPort.getSwitch();
257 if (srcSw == null)
258 return links;
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800259
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 }
271
272 /**
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>();
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800281
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800282 IPortObject srcPort = op.searchPort(HexString.toHexString(dpid), port);
283 if (srcPort == null)
284 return links;
285 ISwitchObject srcSw = srcPort.getSwitch();
286 if (srcSw == null)
287 return links;
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -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 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800300
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;
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -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)) {
314 op.rollback();
315 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
316 return false;
317 }
318 }
319 op.commit();
320 success = true;
321 } catch (Exception e) {
322 op.rollback();
323 e.printStackTrace();
324 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
mininet403d5892013-06-05 03:48:17 -0700325 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800326
Naoki Shiota987a5722013-10-23 11:59:36 -0700327 return success;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800328 }
329
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>();
338
Naoki Shiota987a5722013-10-23 11:59:36 -0700339 ISwitchObject srcSw = op.searchSwitch(dpid);
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800340
Naoki Shiota93ec1712013-06-13 15:49:36 -0700341 if(srcSw != null) {
342 for(IPortObject srcPort : srcSw.getPorts()) {
343 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
344 ISwitchObject dstSw = dstPort.getSwitch();
345 if(dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800346 Link link = new Link(HexString.toLong(dpid),
Naoki Shiota93ec1712013-06-13 15:49:36 -0700347 srcPort.getNumber(),
348 HexString.toLong(dstSw.getDPID()),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800349 dstPort.getNumber());
Naoki Shiota93ec1712013-06-13 15:49:36 -0700350 links.add(link);
351 }
352 }
353 }
354 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800355
mininet403d5892013-06-05 03:48:17 -0700356 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800357 }
358
Naoki Shiota11516712013-06-05 22:36:01 -0700359 /**
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700360 * Get list of all reverse links connected to the switch specified by
361 * given DPID.
362 * @param dpid DPID of desired switch.
363 * @return List of reverse links. Empty list if no port was found.
364 */
365 @Override
366 public List<Link> getReverseLinks(String dpid) {
367 List<Link> links = new ArrayList<Link>();
368
369 ISwitchObject srcSw = op.searchSwitch(dpid);
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800370
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700371 if(srcSw != null) {
372 for(IPortObject srcPort : srcSw.getPorts()) {
373 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
374 ISwitchObject dstSw = dstPort.getSwitch();
375 if(dstSw != null) {
376 Link link = new Link(
377 HexString.toLong(dstSw.getDPID()),
378 dstPort.getNumber(),
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800379
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800380 HexString.toLong(dpid),
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700381 srcPort.getNumber());
382 links.add(link);
383 }
384 }
385 }
386 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800387
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700388 return links;
389 }
390
391 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700392 * Get list of all links whose state is ACTIVE.
393 * @return List of active links. Empty list if no port was found.
394 */
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800395 @Override
Pankaj Berdeff421802013-01-29 20:28:52 -0800396 public List<Link> getActiveLinks() {
Naoki Shiota987a5722013-10-23 11:59:36 -0700397 Iterable<ISwitchObject> switches = op.getActiveSwitches();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800398
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800399 List<Link> links = new ArrayList<Link>();
400
Naoki Shiota826e84a2013-06-18 13:13:25 -0700401 for (ISwitchObject srcSw : switches) {
402 for(IPortObject srcPort : srcSw.getPorts()) {
403 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
404 ISwitchObject dstSw = dstPort.getSwitch();
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800405
Naoki Shiota826e84a2013-06-18 13:13:25 -0700406 if(dstSw != null && dstSw.getState().equals("ACTIVE")) {
407 links.add(new Link(HexString.toLong(srcSw.getDPID()),
408 srcPort.getNumber(),
409 HexString.toLong(dstSw.getDPID()),
410 dstPort.getNumber()));
411 }
412 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800413 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800414 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800415
Pankaj Berde5024ec12013-01-31 17:07:29 -0800416 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800417 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800418
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700419 @Override
420 public LinkInfo getLinkInfo(Link link) {
421 // TODO implement this
422 return null;
423 }
424
425 /**
426 * Finalize the object.
427 */
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800428 @Override
429 protected void finalize() {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700430 close();
431 }
432
433 /**
434 * Close LinkStorage.
435 */
436 @Override
437 public void close() {
438 // TODO Auto-generated method stub
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800439// 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
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800449
Naoki Shiota987a5722013-10-23 11:59:36 -0700450 return false;
451 }
452
453 private boolean addLinkImpl(Link lt) {
454 boolean success = false;
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800455
Naoki Shiota987a5722013-10-23 11:59:36 -0700456 IPortObject vportSrc = null, vportDst = null;
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800457
Naoki Shiota987a5722013-10-23 11:59:36 -0700458 // get source port vertex
459 String dpid = HexString.toHexString(lt.getSrc());
460 short port = lt.getSrcPort();
461 vportSrc = op.searchPort(dpid, port);
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800462
Naoki Shiota987a5722013-10-23 11:59:36 -0700463 // get dest port vertex
464 dpid = HexString.toHexString(lt.getDst());
465 port = lt.getDstPort();
466 vportDst = op.searchPort(dpid, port);
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800467
Naoki Shiota987a5722013-10-23 11:59:36 -0700468 if (vportSrc != null && vportDst != null) {
469 IPortObject portExist = null;
470 // check if the link exists
471 for (IPortObject V : vportSrc.getLinkedPorts()) {
472 if (V.equals(vportDst)) {
473 portExist = V;
474 break;
475 }
476 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800477
Naoki Shiota987a5722013-10-23 11:59:36 -0700478 if (portExist == null) {
479 vportSrc.setLinkPort(vportDst);
480 success = true;
481 } else {
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800482 log.debug("LinkStorageImpl:addLinkImpl failed link exists {} {} src {} dst {}",
Naoki Shiota987a5722013-10-23 11:59:36 -0700483 new Object[]{op, lt, vportSrc, vportDst});
484 }
485 }
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800486
Naoki Shiota987a5722013-10-23 11:59:36 -0700487 return success;
488 }
489
490 private boolean deleteLinkImpl(Link lt) {
491 boolean success = false;
492 IPortObject vportSrc = null, vportDst = null;
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800493
Naoki Shiota987a5722013-10-23 11:59:36 -0700494 // get source port vertex
495 String dpid = HexString.toHexString(lt.getSrc());
496 short port = lt.getSrcPort();
497 vportSrc = op.searchPort(dpid, port);
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800498
Naoki Shiota987a5722013-10-23 11:59:36 -0700499 // get dst port vertex
500 dpid = HexString.toHexString(lt.getDst());
501 port = lt.getDstPort();
502 vportDst = op.searchPort(dpid, port);
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800503
Naoki Shiota987a5722013-10-23 11:59:36 -0700504 // FIXME: This needs to remove all edges
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800505 if (vportSrc != null && vportDst != null) {
506 vportSrc.removeLink(vportDst);
507 log.debug("deleteLinkImpl(): deleted edge {} src {} dst {}", new Object[]{
508 lt, vportSrc, vportDst});
509 success = true;
510 }
511
512 return success;
Naoki Shiota987a5722013-10-23 11:59:36 -0700513 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800514}