blob: 92e28314b9de0035b94ab27b6db3072217972448 [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;
HIGUCHI Yuta20514902013-06-12 11:24:16 -07009import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject;
10import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject;
HIGUCHI Yutaa56fbde2013-06-17 14:26:05 -070011import net.onrc.onos.ofcontroller.linkdiscovery.LinkInfo;
Jonathan Hartb7e3d2c2013-01-15 18:45:19 -080012
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080013import org.openflow.util.HexString;
14import org.slf4j.Logger;
15import org.slf4j.LoggerFactory;
16
Naoki Shiotac57efb82013-06-18 13:40:28 -070017import com.tinkerpop.blueprints.Vertex;
Naoki Shiotac57efb82013-06-18 13:40:28 -070018import com.tinkerpop.pipes.PipeFunction;
19import com.tinkerpop.pipes.transform.PathPipe;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080020
Naoki Shiota1c393ce2013-06-28 22:55:14 -070021/**
Naoki Shiotab2d17e82013-10-18 18:08:16 -070022 * This is the class for storing the information of links into GraphDB
Naoki Shiota1c393ce2013-06-28 22:55:14 -070023 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080024public class LinkStorageImpl implements ILinkStorage {
Pankaj Berde62016142013-04-09 15:35:50 -070025
Yuta HIGUCHI6ac8d182013-10-22 15:24:56 -070026 protected final static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class);
Naoki Shiota987a5722013-10-23 11:59:36 -070027 protected GraphDBOperation op;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080028
Naoki Shiotab2d17e82013-10-18 18:08:16 -070029
30 /**
31 * Initialize the object. Open LinkStorage using given configuration file.
32 * @param conf Path (absolute path for now) to configuration file.
33 */
34 @Override
35 public void init(String conf) {
Naoki Shiota987a5722013-10-23 11:59:36 -070036 this.op = new GraphDBOperation(conf);
Naoki Shiotab2d17e82013-10-18 18:08:16 -070037 }
38
Naoki Shiota987a5722013-10-23 11:59:36 -070039 // Method designing policy:
40 // op.commit() and op.rollback() MUST called in public (first-class) methods.
41 // A first-class method MUST NOT call other first-class method.
42 // Routine process should be implemented in private method.
43 // A private method MUST NOT call commit or rollback.
44
45
Naoki Shiota11516712013-06-05 22:36:01 -070046 /**
Naoki Shiota987a5722013-10-23 11:59:36 -070047 * Update a record in the LinkStorage in a way provided by dmop.
Naoki Shiota11516712013-06-05 22:36:01 -070048 * @param link Record of a link to be updated.
Naoki Shiota987a5722013-10-23 11:59:36 -070049 * @param linkinfo Meta-information of a link to be updated.
50 * @param dmop Operation to be done.
Naoki Shiota11516712013-06-05 22:36:01 -070051 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080052 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -070053 public boolean update(Link link, LinkInfo linkinfo, DM_OPERATION dmop) {
54 boolean success = false;
55
56 switch (dmop) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -070057 case CREATE:
58 case INSERT:
Naoki Shiota987a5722013-10-23 11:59:36 -070059 if (link != null) {
60 try {
61 if (addLinkImpl(link)) {
62 op.commit();
63 success = true;
64 }
65 } catch (Exception e) {
66 op.rollback();
67 e.printStackTrace();
68 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
69 }
70 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070071 break;
72 case UPDATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070073 if (link != null && linkinfo != null) {
74 try {
75 if (setLinkInfoImpl(link, linkinfo)) {
76 op.commit();
77 success = true;
78 }
79 } catch (Exception e) {
80 op.rollback();
81 e.printStackTrace();
82 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
83 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070084 }
85 break;
86 case DELETE:
Naoki Shiota987a5722013-10-23 11:59:36 -070087 if (link != null) {
88 try {
89 if (deleteLinkImpl(link)) {
90 op.commit();
91 success = true;
92 log.debug("LinkStorageImpl:update {} link:{} succeeded", dmop, link);
93 } else {
94 op.rollback();
95 log.debug("LinkStorageImpl:update {} link:{} failed", dmop, link);
96 }
97 } catch (Exception e) {
98 op.rollback();
99 e.printStackTrace();
100 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
101 }
102 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700103 break;
104 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700105
106 return success;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800107 }
108
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700109 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700110 public boolean addLink(Link link) {
111 return addLink(link, null);
112 }
113
114 @Override
115 public boolean addLink(Link link, LinkInfo linfo) {
116 boolean success = false;
117
118 try {
119 if (addLinkImpl(link)) {
120 // Set LinkInfo only if linfo is non-null.
121 if (linfo != null && (! setLinkInfoImpl(link, linfo))) {
122 op.rollback();
123 }
124 op.commit();
125 success = true;
126 } else {
127 op.rollback();
128 }
129 } catch (Exception e) {
130 e.printStackTrace();
131 log.error("LinkStorageImpl:addLink link:{} linfo:{} failed", link, linfo);
132 }
133
134 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700135 }
136
Naoki Shiota11516712013-06-05 22:36:01 -0700137 /**
138 * Update multiple records in the LinkStorage in a way provided by op.
139 * @param links List of records to be updated.
140 * @param op Operation to be done.
141 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800142 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700143 public boolean addLinks(List<Link> links) {
144 boolean success = false;
145
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800146 for (Link lt: links) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700147 if (! addLinkImpl(lt)) {
148 return false;
149 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700150 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700151
152 try {
153 op.commit();
154 success = true;
155 } catch (Exception e) {
156 e.printStackTrace();
157 log.error("LinkStorageImpl:addLinks link:s{} failed", links);
158 }
159
160 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700161 }
162
163 /**
Naoki Shiota987a5722013-10-23 11:59:36 -0700164 * Delete a record in the LinkStorage.
165 * @param lt Record to be deleted.
166 */
167 @Override
168 public boolean deleteLink(Link lt) {
169 boolean success = false;
170
171 log.debug("LinkStorageImpl:deleteLink(): {}", lt);
172
173 try {
174 if (deleteLinkImpl(lt)) {
175 op.commit();
176 success = true;
177 log.debug("LinkStorageImpl:deleteLink(): deleted edges {}", lt);
178 } else {
179 op.rollback();
180 log.error("LinkStorageImpl:deleteLink(): failed invalid vertices {}", lt);
181 }
182 } catch (Exception e) {
183 op.rollback();
184 log.error("LinkStorageImpl:deleteLink(): failed {} {}",
185 new Object[]{lt, e.toString()});
186 e.printStackTrace();
187 }
188
189 return success;
190 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700191
192 /**
193 * Delete multiple records in LinkStorage.
194 * @param links List of records to be deleted.
195 */
196 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700197 public boolean deleteLinks(List<Link> links) {
198 boolean success = false;
199
200 try {
201 for (Link lt : links) {
202 if (! deleteLinkImpl(lt)) {
203 op.rollback();
204 return false;
205 }
206 }
207 op.commit();
208 success = true;
209 } catch (Exception e) {
210 op.rollback();
211 e.printStackTrace();
212 log.error("LinkStorageImpl:deleteLinks failed invalid vertices {}", links);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800213 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700214
Naoki Shiota987a5722013-10-23 11:59:36 -0700215 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700216 }
217
Naoki Shiota11516712013-06-05 22:36:01 -0700218 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700219 * Get list of all links connected to the port specified by given DPID and port number.
220 * @param dpid DPID of desired port.
221 * @param port Port number of desired port.
222 * @return List of links. Empty list if no port was found.
223 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800224 @Override
225 public List<Link> getLinks(Long dpid, short port) {
Naoki Shiota93ec1712013-06-13 15:49:36 -0700226 List<Link> links = new ArrayList<Link>();
227
Naoki Shiota987a5722013-10-23 11:59:36 -0700228 IPortObject srcPort = op.searchPort(HexString.toHexString(dpid), port);
Naoki Shiota93ec1712013-06-13 15:49:36 -0700229 ISwitchObject srcSw = srcPort.getSwitch();
230
Pankaj Berdee7c21522013-08-01 16:52:29 -0700231 if(srcSw != null && srcPort != null) {
Naoki Shiota93ec1712013-06-13 15:49:36 -0700232 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
233 ISwitchObject dstSw = dstPort.getSwitch();
234 Link link = new Link(HexString.toLong(srcSw.getDPID()),
235 srcPort.getNumber(),
236 HexString.toLong(dstSw.getDPID()),
237 dstPort.getNumber());
238
239 links.add(link);
240 }
241 }
242
mininet403d5892013-06-05 03:48:17 -0700243 return links;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800244 }
245
Naoki Shiota11516712013-06-05 22:36:01 -0700246 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700247 * Delete records of the links connected to the port specified by given DPID and port number.
248 * @param dpid DPID of desired port.
249 * @param port Port number of desired port.
250 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800251 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700252 public boolean deleteLinksOnPort(Long dpid, short port) {
253 boolean success = false;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800254
Naoki Shiota987a5722013-10-23 11:59:36 -0700255 List<Link> linksToDelete = getLinks(dpid, port);
256 try {
257 for(Link l : linksToDelete) {
258 if (! deleteLinkImpl(l)) {
259 op.rollback();
260 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
261 return false;
262 }
263 }
264 op.commit();
265 success = true;
266 } catch (Exception e) {
267 op.rollback();
268 e.printStackTrace();
269 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
mininet403d5892013-06-05 03:48:17 -0700270 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700271
272 return success;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800273 }
274
Naoki Shiota11516712013-06-05 22:36:01 -0700275 /**
276 * Get list of all links connected to the switch specified by given DPID.
277 * @param dpid DPID of desired switch.
278 * @return List of links. Empty list if no port was found.
279 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800280 @Override
281 public List<Link> getLinks(String dpid) {
mininet403d5892013-06-05 03:48:17 -0700282 List<Link> links = new ArrayList<Link>();
283
Naoki Shiota987a5722013-10-23 11:59:36 -0700284 ISwitchObject srcSw = op.searchSwitch(dpid);
Naoki Shiota93ec1712013-06-13 15:49:36 -0700285
286 if(srcSw != null) {
287 for(IPortObject srcPort : srcSw.getPorts()) {
288 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
289 ISwitchObject dstSw = dstPort.getSwitch();
290 if(dstSw != null) {
291 Link link = new Link(HexString.toLong(srcSw.getDPID()),
292 srcPort.getNumber(),
293 HexString.toLong(dstSw.getDPID()),
294 dstPort.getNumber());
295 links.add(link);
296 }
297 }
298 }
299 }
300
mininet403d5892013-06-05 03:48:17 -0700301 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800302 }
303
Naoki Shiota11516712013-06-05 22:36:01 -0700304 /**
305 * Get list of all links whose state is ACTIVE.
306 * @return List of active links. Empty list if no port was found.
307 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800308 public List<Link> getActiveLinks() {
Naoki Shiota987a5722013-10-23 11:59:36 -0700309 Iterable<ISwitchObject> switches = op.getActiveSwitches();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800310
311 List<Link> links = new ArrayList<Link>();
Naoki Shiota826e84a2013-06-18 13:13:25 -0700312
313 for (ISwitchObject srcSw : switches) {
314 for(IPortObject srcPort : srcSw.getPorts()) {
315 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
316 ISwitchObject dstSw = dstPort.getSwitch();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800317
Naoki Shiota826e84a2013-06-18 13:13:25 -0700318 if(dstSw != null && dstSw.getState().equals("ACTIVE")) {
319 links.add(new Link(HexString.toLong(srcSw.getDPID()),
320 srcPort.getNumber(),
321 HexString.toLong(dstSw.getDPID()),
322 dstPort.getNumber()));
323 }
324 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800325 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800326 }
Naoki Shiota826e84a2013-06-18 13:13:25 -0700327
Pankaj Berde5024ec12013-01-31 17:07:29 -0800328 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800329 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800330
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700331 @Override
332 public LinkInfo getLinkInfo(Link link) {
333 // TODO implement this
334 return null;
335 }
336
337 /**
338 * Finalize the object.
339 */
340 public void finalize() {
341 close();
342 }
343
344 /**
345 * Close LinkStorage.
346 */
347 @Override
348 public void close() {
349 // TODO Auto-generated method stub
350// graph.shutdown();
351 }
352
Naoki Shiota987a5722013-10-23 11:59:36 -0700353 /**
354 * Update a record of link with meta-information in the LinkStorage.
355 * @param link Record of a link to update.
356 * @param linkinfo Meta-information of a link to be updated.
357 */
358 private boolean setLinkInfoImpl(Link link, LinkInfo linkinfo) {
359 // TODO implement this
360
361 return false;
362 }
363
364 private boolean addLinkImpl(Link lt) {
365 boolean success = false;
366
367 IPortObject vportSrc = null, vportDst = null;
368
369 // get source port vertex
370 String dpid = HexString.toHexString(lt.getSrc());
371 short port = lt.getSrcPort();
372 vportSrc = op.searchPort(dpid, port);
373
374 // get dest port vertex
375 dpid = HexString.toHexString(lt.getDst());
376 port = lt.getDstPort();
377 vportDst = op.searchPort(dpid, port);
378
379 if (vportSrc != null && vportDst != null) {
380 IPortObject portExist = null;
381 // check if the link exists
382 for (IPortObject V : vportSrc.getLinkedPorts()) {
383 if (V.equals(vportDst)) {
384 portExist = V;
385 break;
386 }
387 }
388
389 if (portExist == null) {
390 vportSrc.setLinkPort(vportDst);
391 success = true;
392 } else {
393 log.debug("LinkStorageImpl:addLinkImpl failed link exists {} {} src {} dst {}",
394 new Object[]{op, lt, vportSrc, vportDst});
395 }
396 }
397
398 return success;
399 }
400
401 private boolean deleteLinkImpl(Link lt) {
402 boolean success = false;
403 IPortObject vportSrc = null, vportDst = null;
404
405 // get source port vertex
406 String dpid = HexString.toHexString(lt.getSrc());
407 short port = lt.getSrcPort();
408 vportSrc = op.searchPort(dpid, port);
409
410 // get dst port vertex
411 dpid = HexString.toHexString(lt.getDst());
412 port = lt.getDstPort();
413 vportDst = op.searchPort(dpid, port);
414
415 // FIXME: This needs to remove all edges
416 if (vportSrc != null && vportDst != null) {
417 vportSrc.removeLink(vportDst);
418 log.debug("deleteLinkImpl(): deleted edges src {} dst {}", new Object[]{
419 lt, vportSrc, vportDst});
420 success = true;
421 }
422
423 return success;
424 }
425
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700426 // TODO should be moved to TopoLinkServiceImpl (never used in this class)
HIGUCHI Yutaf05c4802013-06-17 11:15:50 -0700427 static class ExtractLink implements PipeFunction<PathPipe<Vertex>, Link> {
Naoki Shiotac57efb82013-06-18 13:40:28 -0700428
Yuta HIGUCHI57e67f22013-10-14 10:21:05 -0700429 @SuppressWarnings("unchecked")
Naoki Shiotac57efb82013-06-18 13:40:28 -0700430 @Override
431 public Link compute(PathPipe<Vertex> pipe ) {
Naoki Shiotac57efb82013-06-18 13:40:28 -0700432 long s_dpid = 0;
433 long d_dpid = 0;
434 short s_port = 0;
435 short d_port = 0;
436 List<Vertex> V = new ArrayList<Vertex>();
Yuta HIGUCHI57e67f22013-10-14 10:21:05 -0700437 V = (List<Vertex>)pipe.next();
Naoki Shiotac57efb82013-06-18 13:40:28 -0700438 Vertex src_sw = V.get(0);
439 Vertex dest_sw = V.get(3);
440 Vertex src_port = V.get(1);
441 Vertex dest_port = V.get(2);
442 s_dpid = HexString.toLong((String) src_sw.getProperty("dpid"));
443 d_dpid = HexString.toLong((String) dest_sw.getProperty("dpid"));
444 s_port = (Short) src_port.getProperty("number");
445 d_port = (Short) dest_port.getProperty("number");
446
447 Link l = new Link(s_dpid,s_port,d_dpid,d_port);
448
449 return l;
450 }
451 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800452
Pankaj Berdeff421802013-01-29 20:28:52 -0800453
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800454}