blob: 1f366150532836c29750a56458f80805edaebec3 [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
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -080018import com.tinkerpop.blueprints.impls.ramcloud.PerfMon;
yoshi2fd4c7e2013-11-22 15:47:55 -080019import net.onrc.onos.graph.GraphDBManager;
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 {
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -080025
yoshi0fee3de2013-11-23 09:13:37 -080026 protected final static Logger log = LoggerFactory.getLogger(LinkStorageImpl.class);
27 protected DBOperation dbop;
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -080028 private static PerfMon pm = PerfMon.getInstance();
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080029
Naoki Shiotab2d17e82013-10-18 18:08:16 -070030 /**
31 * Initialize the object. Open LinkStorage using given configuration file.
32 * @param conf Path (absolute path for now) to configuration file.
33 */
34 @Override
yoshi0fee3de2013-11-23 09:13:37 -080035 public void init(final String dbStore, final String conf) {
yoshid38cd312013-12-02 19:54:44 -080036 //this.dbop = GraphDBManager.getDBOperation(dbStore, conf);
37 this.dbop = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloudconf");
38
Naoki Shiotab2d17e82013-10-18 18:08:16 -070039 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080040
Naoki Shiota987a5722013-10-23 11:59:36 -070041 // Method designing policy:
42 // op.commit() and op.rollback() MUST called in public (first-class) methods.
43 // A first-class method MUST NOT call other first-class method.
44 // Routine process should be implemented in private method.
45 // A private method MUST NOT call commit or rollback.
yoshi2fd4c7e2013-11-22 15:47:55 -080046
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -080047
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080048 /**
Naoki Shiota987a5722013-10-23 11:59:36 -070049 * Update a record in the LinkStorage in a way provided by dmop.
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080050 * @param link Record of a link to be updated.
Naoki Shiota987a5722013-10-23 11:59:36 -070051 * @param linkinfo Meta-information of a link to be updated.
52 * @param dmop Operation to be done.
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080053 */
54 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -070055 public boolean update(Link link, LinkInfo linkinfo, DM_OPERATION dmop) {
56 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -080057
Naoki Shiota987a5722013-10-23 11:59:36 -070058 switch (dmop) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -070059 case CREATE:
60 case INSERT:
Naoki Shiota987a5722013-10-23 11:59:36 -070061 if (link != null) {
62 try {
63 if (addLinkImpl(link)) {
yoshi0fee3de2013-11-23 09:13:37 -080064 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -070065 success = true;
66 }
67 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -080068 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -070069 e.printStackTrace();
70 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
71 }
72 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070073 break;
74 case UPDATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070075 if (link != null && linkinfo != null) {
76 try {
77 if (setLinkInfoImpl(link, linkinfo)) {
yoshi0fee3de2013-11-23 09:13:37 -080078 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -070079 success = true;
80 }
81 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -080082 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -070083 e.printStackTrace();
84 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
85 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070086 }
87 break;
88 case DELETE:
Naoki Shiota987a5722013-10-23 11:59:36 -070089 if (link != null) {
90 try {
91 if (deleteLinkImpl(link)) {
yoshi0fee3de2013-11-23 09:13:37 -080092 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -070093 success = true;
94 log.debug("LinkStorageImpl:update {} link:{} succeeded", dmop, link);
95 } else {
yoshi0fee3de2013-11-23 09:13:37 -080096 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -070097 log.debug("LinkStorageImpl:update {} link:{} failed", dmop, link);
98 }
99 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800100 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700101 e.printStackTrace();
102 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
103 }
104 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700105 break;
106 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800107
Naoki Shiota987a5722013-10-23 11:59:36 -0700108 return success;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800109 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800110
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700111 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700112 public boolean addLink(Link link) {
113 return addLink(link, null);
114 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800115
pingping-lin00926032013-12-18 12:13:08 +0800116 private void deleteDeviceOnPort(Long dpid, Short number)
117 {
Masayoshi Kobayashi1015e362013-12-17 22:54:49 -0800118 IPortObject srcPortObject = dbop.searchPort(HexString.toHexString(dpid), number);
Yuta HIGUCHId489ced2014-01-08 15:46:28 -0800119 if (srcPortObject == null)
120 return;
121 Iterable<IDeviceObject> devices = srcPortObject.getDevices();
122 if (devices == null)
123 return;
124 if (devices.iterator().hasNext()) {
pingping-lin00926032013-12-18 12:13:08 +0800125 for (IDeviceObject deviceObject: srcPortObject.getDevices()) {
126 srcPortObject.removeDevice(deviceObject);
127 log.debug("delete Device "+ deviceObject.getMACAddress() +
128 " from sw: {} port: {} due to a new link added",
129 dpid, number);
130 }
131 }
132 }
133
Naoki Shiota987a5722013-10-23 11:59:36 -0700134 @Override
135 public boolean addLink(Link link, LinkInfo linfo) {
136 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800137
Naoki Shiota987a5722013-10-23 11:59:36 -0700138 try {
pingping-lin00926032013-12-18 12:13:08 +0800139 //delete the Device attachment points for the related switch and port
140 deleteDeviceOnPort(link.getSrc(),link.getSrcPort());
141 deleteDeviceOnPort(link.getDst(),link.getDstPort());
142
Masayoshi Kobayashi1015e362013-12-17 22:54:49 -0800143 long startLinkTime = System.nanoTime();
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800144 pm.addlink_start();
Naoki Shiota987a5722013-10-23 11:59:36 -0700145 if (addLinkImpl(link)) {
146 // Set LinkInfo only if linfo is non-null.
147 if (linfo != null && (! setLinkInfoImpl(link, linfo))) {
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700148 log.debug("Adding linkinfo failed: {}", link);
yoshi0fee3de2013-11-23 09:13:37 -0800149 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700150 }
yoshi0fee3de2013-11-23 09:13:37 -0800151 dbop.commit();
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800152 pm.addlink_end();
Masayoshi Kobayashi1015e362013-12-17 22:54:49 -0800153 long endLinkTime = System.nanoTime();
154 log.error("Performance ##add link total time {}", endLinkTime - startLinkTime);
Naoki Shiota987a5722013-10-23 11:59:36 -0700155 success = true;
156 } else {
Yuta HIGUCHI1df38152014-01-08 13:26:29 -0800157 pm.addlink_end();
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700158 // If we fail here that's because the ports aren't added
159 // before we try to add the link
160 log.debug("Adding link failed: {}", link);
yoshi0fee3de2013-11-23 09:13:37 -0800161 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700162 }
163 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800164 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700165 e.printStackTrace();
166 log.error("LinkStorageImpl:addLink link:{} linfo:{} failed", link, linfo);
167 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800168
Naoki Shiota987a5722013-10-23 11:59:36 -0700169 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700170 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800171
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800172 /**
173 * Update multiple records in the LinkStorage in a way provided by op.
174 * @param links List of records to be updated.
175 * @param op Operation to be done.
176 */
177 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700178 public boolean addLinks(List<Link> links) {
179 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800180
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800181 for (Link lt: links) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700182 if (! addLinkImpl(lt)) {
183 return false;
184 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700185 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800186
Naoki Shiota987a5722013-10-23 11:59:36 -0700187 try {
yoshi0fee3de2013-11-23 09:13:37 -0800188 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700189 success = true;
190 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800191 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700192 e.printStackTrace();
193 log.error("LinkStorageImpl:addLinks link:s{} failed", links);
194 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800195
Naoki Shiota987a5722013-10-23 11:59:36 -0700196 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700197 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800198
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700199 /**
Naoki Shiota987a5722013-10-23 11:59:36 -0700200 * Delete a record in the LinkStorage.
201 * @param lt Record to be deleted.
202 */
203 @Override
204 public boolean deleteLink(Link lt) {
205 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800206
Naoki Shiota987a5722013-10-23 11:59:36 -0700207 log.debug("LinkStorageImpl:deleteLink(): {}", lt);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800208
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800209 try {
Naoki Shiota987a5722013-10-23 11:59:36 -0700210 if (deleteLinkImpl(lt)) {
yoshi0fee3de2013-11-23 09:13:37 -0800211 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700212 success = true;
213 log.debug("LinkStorageImpl:deleteLink(): deleted edges {}", lt);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800214 } else {
yoshi0fee3de2013-11-23 09:13:37 -0800215 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700216 log.error("LinkStorageImpl:deleteLink(): failed invalid vertices {}", lt);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800217 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700218 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800219 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700220 log.error("LinkStorageImpl:deleteLink(): failed {} {}",
221 new Object[]{lt, e.toString()});
222 e.printStackTrace();
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800223 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800224
Naoki Shiota987a5722013-10-23 11:59:36 -0700225 return success;
226 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800227
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700228 /**
229 * Delete multiple records in LinkStorage.
230 * @param links List of records to be deleted.
231 */
232 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700233 public boolean deleteLinks(List<Link> links) {
234 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800235
Naoki Shiota987a5722013-10-23 11:59:36 -0700236 try {
237 for (Link lt : links) {
238 if (! deleteLinkImpl(lt)) {
yoshi0fee3de2013-11-23 09:13:37 -0800239 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700240 return false;
241 }
242 }
yoshi0fee3de2013-11-23 09:13:37 -0800243 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700244 success = true;
245 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800246 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700247 e.printStackTrace();
248 log.error("LinkStorageImpl:deleteLinks failed invalid vertices {}", links);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800249 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800250
Naoki Shiota987a5722013-10-23 11:59:36 -0700251 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700252 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800253
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800254 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700255 * Get list of all links connected to the port specified by given DPID and port number.
256 * @param dpid DPID of desired port.
257 * @param port Port number of desired port.
258 * @return List of links. Empty list if no port was found.
259 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800260 @Override
261 public List<Link> getLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800262 List<Link> links = new ArrayList<Link>();
yoshi0fee3de2013-11-23 09:13:37 -0800263 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800264 if (srcPort == null)
265 return links;
266 ISwitchObject srcSw = srcPort.getSwitch();
267 if (srcSw == null)
268 return links;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800269
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800270 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
271 ISwitchObject dstSw = dstPort.getSwitch();
272 if (dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800273 Link link = new Link(dpid, port,
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800274 HexString.toLong(dstSw.getDPID()),
275 dstPort.getNumber());
276 links.add(link);
277 }
278 }
279 return links;
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800280 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800281
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800282 /**
283 * Get list of all reverse links connected to the port specified by given DPID and port number.
284 * @param dpid DPID of desired port.
285 * @param port Port number of desired port.
286 * @return List of reverse links. Empty list if no port was found.
287 */
288 @Override
289 public List<Link> getReverseLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800290 List<Link> links = new ArrayList<Link>();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800291
yoshi0fee3de2013-11-23 09:13:37 -0800292 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800293 if (srcPort == null)
294 return links;
295 ISwitchObject srcSw = srcPort.getSwitch();
296 if (srcSw == null)
297 return links;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800298
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800299 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
300 ISwitchObject dstSw = dstPort.getSwitch();
301 if (dstSw != null) {
302 Link link = new Link(HexString.toLong(dstSw.getDPID()),
303 dstPort.getNumber(),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800304 dpid, port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800305 links.add(link);
306 }
307 }
308 return links;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800309 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800310
Naoki Shiota11516712013-06-05 22:36:01 -0700311 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700312 * Delete records of the links connected to the port specified by given DPID and port number.
313 * @param dpid DPID of desired port.
314 * @param port Port number of desired port.
315 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800316 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700317 public boolean deleteLinksOnPort(Long dpid, short port) {
318 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800319
Naoki Shiota987a5722013-10-23 11:59:36 -0700320 List<Link> linksToDelete = getLinks(dpid, port);
321 try {
322 for(Link l : linksToDelete) {
323 if (! deleteLinkImpl(l)) {
yoshi0fee3de2013-11-23 09:13:37 -0800324 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700325 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
326 return false;
327 }
328 }
yoshi0fee3de2013-11-23 09:13:37 -0800329 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700330 success = true;
331 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800332 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700333 e.printStackTrace();
334 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
mininet403d5892013-06-05 03:48:17 -0700335 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800336
Naoki Shiota987a5722013-10-23 11:59:36 -0700337 return success;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800338 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800339
Naoki Shiota11516712013-06-05 22:36:01 -0700340 /**
341 * Get list of all links connected to the switch specified by given DPID.
342 * @param dpid DPID of desired switch.
343 * @return List of links. Empty list if no port was found.
344 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800345 @Override
346 public List<Link> getLinks(String dpid) {
mininet403d5892013-06-05 03:48:17 -0700347 List<Link> links = new ArrayList<Link>();
yoshi0fee3de2013-11-23 09:13:37 -0800348 ISwitchObject srcSw = dbop.searchSwitch(dpid);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800349
Naoki Shiota93ec1712013-06-13 15:49:36 -0700350 if(srcSw != null) {
351 for(IPortObject srcPort : srcSw.getPorts()) {
352 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
353 ISwitchObject dstSw = dstPort.getSwitch();
354 if(dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800355 Link link = new Link(HexString.toLong(dpid),
Naoki Shiota93ec1712013-06-13 15:49:36 -0700356 srcPort.getNumber(),
357 HexString.toLong(dstSw.getDPID()),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800358 dstPort.getNumber());
Naoki Shiota93ec1712013-06-13 15:49:36 -0700359 links.add(link);
360 }
361 }
362 }
363 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800364
mininet403d5892013-06-05 03:48:17 -0700365 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800366 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800367
Naoki Shiota11516712013-06-05 22:36:01 -0700368 /**
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700369 * Get list of all reverse links connected to the switch specified by
370 * given DPID.
371 * @param dpid DPID of desired switch.
372 * @return List of reverse links. Empty list if no port was found.
373 */
374 @Override
375 public List<Link> getReverseLinks(String dpid) {
376 List<Link> links = new ArrayList<Link>();
yoshi2fd4c7e2013-11-22 15:47:55 -0800377
yoshi0fee3de2013-11-23 09:13:37 -0800378 ISwitchObject srcSw = dbop.searchSwitch(dpid);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800379
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700380 if(srcSw != null) {
381 for(IPortObject srcPort : srcSw.getPorts()) {
382 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
383 ISwitchObject dstSw = dstPort.getSwitch();
384 if(dstSw != null) {
385 Link link = new Link(
386 HexString.toLong(dstSw.getDPID()),
387 dstPort.getNumber(),
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800388
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800389 HexString.toLong(dpid),
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700390 srcPort.getNumber());
391 links.add(link);
392 }
393 }
394 }
395 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800396
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700397 return links;
398 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800399
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700400 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700401 * Get list of all links whose state is ACTIVE.
402 * @return List of active links. Empty list if no port was found.
403 */
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800404 @Override
Pankaj Berdeff421802013-01-29 20:28:52 -0800405 public List<Link> getActiveLinks() {
yoshi0fee3de2013-11-23 09:13:37 -0800406 Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
yoshi2fd4c7e2013-11-22 15:47:55 -0800407
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800408 List<Link> links = new ArrayList<Link>();
409
Naoki Shiota826e84a2013-06-18 13:13:25 -0700410 for (ISwitchObject srcSw : switches) {
411 for(IPortObject srcPort : srcSw.getPorts()) {
412 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
413 ISwitchObject dstSw = dstPort.getSwitch();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800414
Naoki Shiota826e84a2013-06-18 13:13:25 -0700415 if(dstSw != null && dstSw.getState().equals("ACTIVE")) {
416 links.add(new Link(HexString.toLong(srcSw.getDPID()),
417 srcPort.getNumber(),
418 HexString.toLong(dstSw.getDPID()),
419 dstPort.getNumber()));
420 }
421 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800422 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800423 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800424
Pankaj Berde5024ec12013-01-31 17:07:29 -0800425 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800426 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800427
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700428 @Override
429 public LinkInfo getLinkInfo(Link link) {
430 // TODO implement this
431 return null;
432 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800433
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700434 /**
435 * Finalize the object.
436 */
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800437 @Override
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700438 public void finalize() {
439 close();
440 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800441
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700442 /**
443 * Close LinkStorage.
444 */
445 @Override
446 public void close() {
447 // TODO Auto-generated method stub
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800448// graph.shutdown();
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700449 }
450
Naoki Shiota987a5722013-10-23 11:59:36 -0700451 /**
452 * Update a record of link with meta-information in the LinkStorage.
453 * @param link Record of a link to update.
454 * @param linkinfo Meta-information of a link to be updated.
455 */
456 private boolean setLinkInfoImpl(Link link, LinkInfo linkinfo) {
457 // TODO implement this
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800458
Naoki Shiota987a5722013-10-23 11:59:36 -0700459 return false;
460 }
461
462 private boolean addLinkImpl(Link lt) {
463 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800464
Naoki Shiota987a5722013-10-23 11:59:36 -0700465 IPortObject vportSrc = null, vportDst = null;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800466
Naoki Shiota987a5722013-10-23 11:59:36 -0700467 // get source port vertex
468 String dpid = HexString.toHexString(lt.getSrc());
469 short port = lt.getSrcPort();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800470 log.debug("addLinkImpl Src dpid : {} port : {}", dpid, port);
yoshi0fee3de2013-11-23 09:13:37 -0800471 vportSrc = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800472
Naoki Shiota987a5722013-10-23 11:59:36 -0700473 // get dest port vertex
474 dpid = HexString.toHexString(lt.getDst());
475 port = lt.getDstPort();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800476 log.debug("addLinkImpl Dst dpid : {} port : {}", dpid, port);
yoshi0fee3de2013-11-23 09:13:37 -0800477 vportDst = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800478
479 log.debug("addLinkImpl vportSrc : {} vportDst : {}", vportSrc, vportDst);
480
Naoki Shiota987a5722013-10-23 11:59:36 -0700481 if (vportSrc != null && vportDst != null) {
482 IPortObject portExist = null;
483 // check if the link exists
484 for (IPortObject V : vportSrc.getLinkedPorts()) {
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800485 log.debug("vportSrc.getLinkedPorts() :{}", V);
Naoki Shiota987a5722013-10-23 11:59:36 -0700486 if (V.equals(vportDst)) {
487 portExist = V;
488 break;
489 }
490 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800491
Naoki Shiota987a5722013-10-23 11:59:36 -0700492 if (portExist == null) {
493 vportSrc.setLinkPort(vportDst);
494 success = true;
495 } else {
Yuta HIGUCHI1df38152014-01-08 13:26:29 -0800496 log.error("LinkStorageImpl:addLinkImpl failed link exists {} {} src {} dst {}",
yoshi0fee3de2013-11-23 09:13:37 -0800497 new Object[]{dbop, lt, vportSrc, vportDst});
Naoki Shiota987a5722013-10-23 11:59:36 -0700498 }
499 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800500
Naoki Shiota987a5722013-10-23 11:59:36 -0700501 return success;
502 }
503
504 private boolean deleteLinkImpl(Link lt) {
505 boolean success = false;
506 IPortObject vportSrc = null, vportDst = null;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800507
Naoki Shiota987a5722013-10-23 11:59:36 -0700508 // get source port vertex
509 String dpid = HexString.toHexString(lt.getSrc());
510 short port = lt.getSrcPort();
yoshi0fee3de2013-11-23 09:13:37 -0800511 vportSrc = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800512
Naoki Shiota987a5722013-10-23 11:59:36 -0700513 // get dst port vertex
514 dpid = HexString.toHexString(lt.getDst());
515 port = lt.getDstPort();
yoshi0fee3de2013-11-23 09:13:37 -0800516 vportDst = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800517
Naoki Shiota987a5722013-10-23 11:59:36 -0700518 // FIXME: This needs to remove all edges
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800519 if (vportSrc != null && vportDst != null) {
520 vportSrc.removeLink(vportDst);
521 log.debug("deleteLinkImpl(): deleted edge {} src {} dst {}", new Object[]{
522 lt, vportSrc, vportDst});
523 success = true;
524 }
525
526 return success;
Naoki Shiota987a5722013-10-23 11:59:36 -0700527 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800528}