blob: 753563ed99fba38741c0ad7ba9bd8ffd4d81f834 [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
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080017import com.thinkaurelius.titan.core.TitanException;
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;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080021
Naoki Shiota1c393ce2013-06-28 22:55:14 -070022/**
Naoki Shiotab2d17e82013-10-18 18:08:16 -070023 * This is the class for storing the information of links into GraphDB
Naoki Shiota1c393ce2013-06-28 22:55:14 -070024 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080025public class LinkStorageImpl implements ILinkStorage {
Pankaj Berde62016142013-04-09 15:35:50 -070026
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080027 protected static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class);
Toshio Koide7bdea5b2013-06-13 13:57:18 -070028 protected GraphDBOperation dbop;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080029
Naoki Shiotab2d17e82013-10-18 18:08:16 -070030
31 /**
32 * Initialize the object. Open LinkStorage using given configuration file.
33 * @param conf Path (absolute path for now) to configuration file.
34 */
35 @Override
36 public void init(String conf) {
37 this.dbop = new GraphDBOperation(conf);
38 }
39
Naoki Shiota11516712013-06-05 22:36:01 -070040 /**
41 * Update a record in the LinkStorage in a way provided by op.
42 * @param link Record of a link to be updated.
43 * @param op Operation to be done.
44 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080045 @Override
Naoki Shiotab2d17e82013-10-18 18:08:16 -070046 public void update(Link link, LinkInfo linkinfo, DM_OPERATION op) {
47 switch (op) {
48 case CREATE:
49 case INSERT:
50 addLinkImpl(link,op);
51 break;
52 case UPDATE:
53 if (linkinfo != null) {
54 setLinkInfo(link, linkinfo);
55 } else {
56 deleteLinkInfo(link);
57 }
58 break;
59 case DELETE:
60 deleteLink(link);
61 break;
62 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080063 }
64
Naoki Shiotab2d17e82013-10-18 18:08:16 -070065 @Override
66 public void addLink(Link link) {
67 addLinkImpl(link, DM_OPERATION.INSERT);
68 }
69
Naoki Shiota11516712013-06-05 22:36:01 -070070 /**
71 * Update multiple records in the LinkStorage in a way provided by op.
72 * @param links List of records to be updated.
73 * @param op Operation to be done.
74 */
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080075 @Override
Naoki Shiotab2d17e82013-10-18 18:08:16 -070076 public void addLinks(List<Link> links) {
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080077 for (Link lt: links) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -070078 addLinkImpl(lt, DM_OPERATION.INSERT);
79 }
80 }
81
82 /**
83 * Delete a record in the LinkStorage.
84 * @param lt Record to be deleted.
85 */
86 @Override
87 public void deleteLink(Link lt) {
88 IPortObject vportSrc = null, vportDst = null;
89
90 log.debug("deleteLink(): {}", lt);
91
92 try {
93 // get source port vertex
94 String dpid = HexString.toHexString(lt.getSrc());
95 short port = lt.getSrcPort();
96 vportSrc = dbop.searchPort(dpid, port);
97
98 // get dst port vertex
99 dpid = HexString.toHexString(lt.getDst());
100 port = lt.getDstPort();
101 vportDst = dbop.searchPort(dpid, port);
102 // FIXME: This needs to remove all edges
103
104 if (vportSrc != null && vportDst != null) {
105 vportSrc.removeLink(vportDst);
106 dbop.commit();
107 log.debug("deleteLink(): deleted edges src {} dst {}", new Object[]{
108 lt, vportSrc, vportDst});
109
110 // TODO publish DELETE_LINK event here
111 } else {
112 log.error("deleteLink(): failed invalid vertices {} src {} dst {}", new Object[]{lt, vportSrc, vportDst});
113 dbop.rollback();
114 }
115
116 } catch (TitanException e) {
117 /*
118 * retry till we succeed?
119 */
120 log.error("deleteLink(): titan exception {} {}", new Object[]{lt, e.toString()});
121 dbop.rollback();
122 e.printStackTrace();
123 }
124 }
125
126 /**
127 * Delete multiple records in LinkStorage.
128 * @param links List of records to be deleted.
129 */
130 @Override
131 public void deleteLinks(List<Link> links) {
132 for (Link lt : links) {
133 deleteLink(lt);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800134 }
135 }
136
Naoki Shiota11516712013-06-05 22:36:01 -0700137 /**
138 * Update a record of link with meta-information in the LinkStorage in a way provided by op.
139 * @param link Record of a link to update.
140 * @param linkinfo Meta-information of a link to be updated.
141 * @param op Operation to be done.
142 */
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700143 private void setLinkInfo(Link link, LinkInfo linkinfo) {
144 // TODO implement this
145
146 // TODO publish UPDATE_LINK event here
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800147 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800148
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700149 private void deleteLinkInfo(Link link) {
150 // TODO implement this
151
152 // TODO publish UPDATE_LINK event here
153 }
154
Naoki Shiota11516712013-06-05 22:36:01 -0700155 /**
156 * Perform INSERT/CREATE/UPDATE operation to update the LinkStorage.
157 * @param lt Record of a link to be updated.
158 * @param linkinfo Meta-information of a link to be updated.
159 * @param op Operation to be done. (only INSERT/CREATE/UPDATE is acceptable)
160 */
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700161 private void addLinkImpl(Link lt, DM_OPERATION op) {
Pankaj Berde5fb27632013-04-05 08:56:12 -0700162 IPortObject vportSrc = null, vportDst = null;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800163
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700164 log.trace("updateLink(): op {} {}", new Object[]{op, lt});
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800165
166 try {
167 // get source port vertex
168 String dpid = HexString.toHexString(lt.getSrc());
169 short port = lt.getSrcPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700170 vportSrc = dbop.searchPort(dpid, port);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800171
172 // get dest port vertex
173 dpid = HexString.toHexString(lt.getDst());
174 port = lt.getDstPort();
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700175 vportDst = dbop.searchPort(dpid, port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800176
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800177 if (vportSrc != null && vportDst != null) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800178 // check if the link exists
Pankaj Berde77b58512013-04-05 12:06:37 -0700179
Pankaj Berde62016142013-04-09 15:35:50 -0700180 Iterable<IPortObject> currPorts = vportSrc.getLinkedPorts();
181 List<IPortObject> currLinks = new ArrayList<IPortObject>();
Pankaj Berde77b58512013-04-05 12:06:37 -0700182 for (IPortObject V : currPorts) {
Pankaj Berde6debb042013-01-16 18:04:32 -0800183 currLinks.add(V);
184 }
Naoki Shiotacae568a2013-06-05 17:53:41 -0700185
Pankaj Berde6debb042013-01-16 18:04:32 -0800186 if (currLinks.contains(vportDst)) {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800187 if (op.equals(DM_OPERATION.INSERT) || op.equals(DM_OPERATION.CREATE)) {
Umesh Krishnaswamy9306f952013-01-24 20:42:59 -0800188 log.debug("addOrUpdateLink(): failed link exists {} {} src {} dst {}",
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800189 new Object[]{op, lt, vportSrc, vportDst});
190 }
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800191 } else {
Naoki Shiotab0d0c002013-06-05 20:21:20 -0700192 vportSrc.setLinkPort(vportDst);
Pankaj Berde62016142013-04-09 15:35:50 -0700193
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700194 dbop.commit();
Pankaj Berde254abb42013-06-10 21:21:49 -0700195 log.debug("updateLink(): link added {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700196
197 // TODO publish ADD_LINK event here
Pankaj Berde0fc4e432013-01-12 09:47:22 -0800198 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800199 } else {
Pankaj Berde254abb42013-06-10 21:21:49 -0700200 log.error("updateLink(): failed invalid vertices {} {} src {} dst {}", new Object[]{op, lt, vportSrc, vportDst});
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700201 dbop.rollback();
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800202 }
203 } catch (TitanException e) {
204 /*
205 * retry till we succeed?
206 */
Umesh Krishnaswamyc56a8cc2013-01-24 21:47:51 -0800207 e.printStackTrace();
Pankaj Berde254abb42013-06-10 21:21:49 -0700208 log.error("updateLink(): titan exception {} {} {}", new Object[]{op, lt, e.toString()});
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800209 }
210 }
211
Naoki Shiota11516712013-06-05 22:36:01 -0700212 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700213 * Get list of all links connected to the port specified by given DPID and port number.
214 * @param dpid DPID of desired port.
215 * @param port Port number of desired port.
216 * @return List of links. Empty list if no port was found.
217 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800218 @Override
219 public List<Link> getLinks(Long dpid, short port) {
Naoki Shiota93ec1712013-06-13 15:49:36 -0700220 List<Link> links = new ArrayList<Link>();
221
222 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
223 ISwitchObject srcSw = srcPort.getSwitch();
224
Pankaj Berdee7c21522013-08-01 16:52:29 -0700225 if(srcSw != null && srcPort != null) {
Naoki Shiota93ec1712013-06-13 15:49:36 -0700226 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
227 ISwitchObject dstSw = dstPort.getSwitch();
228 Link link = new Link(HexString.toLong(srcSw.getDPID()),
229 srcPort.getNumber(),
230 HexString.toLong(dstSw.getDPID()),
231 dstPort.getNumber());
232
233 links.add(link);
234 }
235 }
236
mininet403d5892013-06-05 03:48:17 -0700237 return links;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800238 }
239
Naoki Shiota11516712013-06-05 22:36:01 -0700240 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700241 * Delete records of the links connected to the port specified by given DPID and port number.
242 * @param dpid DPID of desired port.
243 * @param port Port number of desired port.
244 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800245 @Override
246 public void deleteLinksOnPort(Long dpid, short port) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700247 List<Link> linksToDelete = getLinks(dpid, port);
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800248
mininet403d5892013-06-05 03:48:17 -0700249 for(Link l : linksToDelete) {
250 deleteLink(l);
251 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800252 }
253
Naoki Shiota11516712013-06-05 22:36:01 -0700254 /**
255 * Get list of all links connected to the switch specified by given DPID.
256 * @param dpid DPID of desired switch.
257 * @return List of links. Empty list if no port was found.
258 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800259 @Override
260 public List<Link> getLinks(String dpid) {
mininet403d5892013-06-05 03:48:17 -0700261 List<Link> links = new ArrayList<Link>();
262
Naoki Shiota93ec1712013-06-13 15:49:36 -0700263 ISwitchObject srcSw = dbop.searchSwitch(dpid);
264
265 if(srcSw != null) {
266 for(IPortObject srcPort : srcSw.getPorts()) {
267 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
268 ISwitchObject dstSw = dstPort.getSwitch();
269 if(dstSw != null) {
270 Link link = new Link(HexString.toLong(srcSw.getDPID()),
271 srcPort.getNumber(),
272 HexString.toLong(dstSw.getDPID()),
273 dstPort.getNumber());
274 links.add(link);
275 }
276 }
277 }
278 }
279
mininet403d5892013-06-05 03:48:17 -0700280 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800281 }
282
Naoki Shiota11516712013-06-05 22:36:01 -0700283 /**
284 * Get list of all links whose state is ACTIVE.
285 * @return List of active links. Empty list if no port was found.
286 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800287 public List<Link> getActiveLinks() {
Toshio Koide7bdea5b2013-06-13 13:57:18 -0700288 Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800289
290 List<Link> links = new ArrayList<Link>();
Naoki Shiota826e84a2013-06-18 13:13:25 -0700291
292 for (ISwitchObject srcSw : switches) {
293 for(IPortObject srcPort : srcSw.getPorts()) {
294 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
295 ISwitchObject dstSw = dstPort.getSwitch();
Pankaj Berde5024ec12013-01-31 17:07:29 -0800296
Naoki Shiota826e84a2013-06-18 13:13:25 -0700297 if(dstSw != null && dstSw.getState().equals("ACTIVE")) {
298 links.add(new Link(HexString.toLong(srcSw.getDPID()),
299 srcPort.getNumber(),
300 HexString.toLong(dstSw.getDPID()),
301 dstPort.getNumber()));
302 }
303 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800304 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800305 }
Naoki Shiota826e84a2013-06-18 13:13:25 -0700306
Pankaj Berde5024ec12013-01-31 17:07:29 -0800307 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800308 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800309
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700310 @Override
311 public LinkInfo getLinkInfo(Link link) {
312 // TODO implement this
313 return null;
314 }
315
316 /**
317 * Finalize the object.
318 */
319 public void finalize() {
320 close();
321 }
322
323 /**
324 * Close LinkStorage.
325 */
326 @Override
327 public void close() {
328 // TODO Auto-generated method stub
329// graph.shutdown();
330 }
331
332 // TODO should be moved to TopoLinkServiceImpl (never used in this class)
HIGUCHI Yutaf05c4802013-06-17 11:15:50 -0700333 static class ExtractLink implements PipeFunction<PathPipe<Vertex>, Link> {
Naoki Shiotac57efb82013-06-18 13:40:28 -0700334
Yuta HIGUCHI57e67f22013-10-14 10:21:05 -0700335 @SuppressWarnings("unchecked")
Naoki Shiotac57efb82013-06-18 13:40:28 -0700336 @Override
337 public Link compute(PathPipe<Vertex> pipe ) {
Naoki Shiotac57efb82013-06-18 13:40:28 -0700338 long s_dpid = 0;
339 long d_dpid = 0;
340 short s_port = 0;
341 short d_port = 0;
342 List<Vertex> V = new ArrayList<Vertex>();
Yuta HIGUCHI57e67f22013-10-14 10:21:05 -0700343 V = (List<Vertex>)pipe.next();
Naoki Shiotac57efb82013-06-18 13:40:28 -0700344 Vertex src_sw = V.get(0);
345 Vertex dest_sw = V.get(3);
346 Vertex src_port = V.get(1);
347 Vertex dest_port = V.get(2);
348 s_dpid = HexString.toLong((String) src_sw.getProperty("dpid"));
349 d_dpid = HexString.toLong((String) dest_sw.getProperty("dpid"));
350 s_port = (Short) src_port.getProperty("number");
351 d_port = (Short) dest_port.getProperty("number");
352
353 Link l = new Link(s_dpid,s_port,d_dpid,d_port);
354
355 return l;
356 }
357 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800358
Pankaj Berdeff421802013-01-29 20:28:52 -0800359
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800360}