blob: 66ad0e9a71365f6715f885a706a12fbc028d5c3e [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);
yoshib6a66eb2014-01-21 10:11:49 -080037 this.dbop = GraphDBManager.getDBOperation("ramcloud", "/tmp/ramcloud.conf");
yoshid38cd312013-12-02 19:54:44 -080038
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 Kobayashi536d72e2014-01-06 13:15:52 -0800143 pm.addlink_start();
Naoki Shiota987a5722013-10-23 11:59:36 -0700144 if (addLinkImpl(link)) {
145 // Set LinkInfo only if linfo is non-null.
146 if (linfo != null && (! setLinkInfoImpl(link, linfo))) {
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700147 log.debug("Adding linkinfo failed: {}", link);
yoshi0fee3de2013-11-23 09:13:37 -0800148 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700149 }
yoshi0fee3de2013-11-23 09:13:37 -0800150 dbop.commit();
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800151 pm.addlink_end();
Naoki Shiota987a5722013-10-23 11:59:36 -0700152 success = true;
153 } else {
Yuta HIGUCHI1df38152014-01-08 13:26:29 -0800154 pm.addlink_end();
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700155 // If we fail here that's because the ports aren't added
156 // before we try to add the link
157 log.debug("Adding link failed: {}", link);
yoshi0fee3de2013-11-23 09:13:37 -0800158 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700159 }
160 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800161 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700162 e.printStackTrace();
163 log.error("LinkStorageImpl:addLink link:{} linfo:{} failed", link, linfo);
164 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800165
Naoki Shiota987a5722013-10-23 11:59:36 -0700166 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700167 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800168
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800169 /**
170 * Update multiple records in the LinkStorage in a way provided by op.
171 * @param links List of records to be updated.
172 * @param op Operation to be done.
173 */
174 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700175 public boolean addLinks(List<Link> links) {
176 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800177
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800178 for (Link lt: links) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700179 if (! addLinkImpl(lt)) {
180 return false;
181 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700182 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800183
Naoki Shiota987a5722013-10-23 11:59:36 -0700184 try {
yoshi0fee3de2013-11-23 09:13:37 -0800185 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700186 success = true;
187 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800188 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700189 e.printStackTrace();
190 log.error("LinkStorageImpl:addLinks link:s{} failed", links);
191 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800192
Naoki Shiota987a5722013-10-23 11:59:36 -0700193 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700194 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800195
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700196 /**
Naoki Shiota987a5722013-10-23 11:59:36 -0700197 * Delete a record in the LinkStorage.
198 * @param lt Record to be deleted.
199 */
200 @Override
201 public boolean deleteLink(Link lt) {
202 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800203
Naoki Shiota987a5722013-10-23 11:59:36 -0700204 log.debug("LinkStorageImpl:deleteLink(): {}", lt);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800205
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800206 try {
Naoki Shiota987a5722013-10-23 11:59:36 -0700207 if (deleteLinkImpl(lt)) {
yoshi0fee3de2013-11-23 09:13:37 -0800208 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700209 success = true;
210 log.debug("LinkStorageImpl:deleteLink(): deleted edges {}", lt);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800211 } else {
yoshi0fee3de2013-11-23 09:13:37 -0800212 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700213 log.error("LinkStorageImpl:deleteLink(): failed invalid vertices {}", lt);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800214 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700215 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800216 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700217 log.error("LinkStorageImpl:deleteLink(): failed {} {}",
218 new Object[]{lt, e.toString()});
219 e.printStackTrace();
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800220 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800221
Naoki Shiota987a5722013-10-23 11:59:36 -0700222 return success;
223 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800224
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700225 /**
226 * Delete multiple records in LinkStorage.
227 * @param links List of records to be deleted.
228 */
229 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700230 public boolean deleteLinks(List<Link> links) {
231 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800232
Naoki Shiota987a5722013-10-23 11:59:36 -0700233 try {
234 for (Link lt : links) {
235 if (! deleteLinkImpl(lt)) {
yoshi0fee3de2013-11-23 09:13:37 -0800236 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700237 return false;
238 }
239 }
yoshi0fee3de2013-11-23 09:13:37 -0800240 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700241 success = true;
242 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800243 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700244 e.printStackTrace();
245 log.error("LinkStorageImpl:deleteLinks failed invalid vertices {}", links);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800246 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800247
Naoki Shiota987a5722013-10-23 11:59:36 -0700248 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700249 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800250
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800251 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700252 * Get list of all links connected to the port specified by given DPID and port number.
253 * @param dpid DPID of desired port.
254 * @param port Port number of desired port.
255 * @return List of links. Empty list if no port was found.
256 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800257 @Override
258 public List<Link> getLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800259 List<Link> links = new ArrayList<Link>();
yoshi0fee3de2013-11-23 09:13:37 -0800260 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800261 if (srcPort == null)
262 return links;
263 ISwitchObject srcSw = srcPort.getSwitch();
264 if (srcSw == null)
265 return links;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800266
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800267 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
268 ISwitchObject dstSw = dstPort.getSwitch();
269 if (dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800270 Link link = new Link(dpid, port,
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800271 HexString.toLong(dstSw.getDPID()),
272 dstPort.getNumber());
273 links.add(link);
274 }
275 }
276 return links;
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800277 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800278
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800279 /**
280 * Get list of all reverse links connected to the port specified by given DPID and port number.
281 * @param dpid DPID of desired port.
282 * @param port Port number of desired port.
283 * @return List of reverse links. Empty list if no port was found.
284 */
285 @Override
286 public List<Link> getReverseLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800287 List<Link> links = new ArrayList<Link>();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800288
yoshi0fee3de2013-11-23 09:13:37 -0800289 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800290 if (srcPort == null)
291 return links;
292 ISwitchObject srcSw = srcPort.getSwitch();
293 if (srcSw == null)
294 return links;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800295
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800296 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
297 ISwitchObject dstSw = dstPort.getSwitch();
298 if (dstSw != null) {
299 Link link = new Link(HexString.toLong(dstSw.getDPID()),
300 dstPort.getNumber(),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800301 dpid, port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800302 links.add(link);
303 }
304 }
305 return links;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800306 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800307
Naoki Shiota11516712013-06-05 22:36:01 -0700308 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700309 * Delete records of the links connected to the port specified by given DPID and port number.
310 * @param dpid DPID of desired port.
311 * @param port Port number of desired port.
312 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800313 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700314 public boolean deleteLinksOnPort(Long dpid, short port) {
315 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800316
Naoki Shiota987a5722013-10-23 11:59:36 -0700317 List<Link> linksToDelete = getLinks(dpid, port);
318 try {
319 for(Link l : linksToDelete) {
320 if (! deleteLinkImpl(l)) {
yoshi0fee3de2013-11-23 09:13:37 -0800321 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700322 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
323 return false;
324 }
325 }
yoshi0fee3de2013-11-23 09:13:37 -0800326 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700327 success = true;
328 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800329 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700330 e.printStackTrace();
331 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
mininet403d5892013-06-05 03:48:17 -0700332 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800333
Naoki Shiota987a5722013-10-23 11:59:36 -0700334 return success;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800335 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800336
Naoki Shiota11516712013-06-05 22:36:01 -0700337 /**
338 * Get list of all links connected to the switch specified by given DPID.
339 * @param dpid DPID of desired switch.
340 * @return List of links. Empty list if no port was found.
341 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800342 @Override
343 public List<Link> getLinks(String dpid) {
mininet403d5892013-06-05 03:48:17 -0700344 List<Link> links = new ArrayList<Link>();
yoshi0fee3de2013-11-23 09:13:37 -0800345 ISwitchObject srcSw = dbop.searchSwitch(dpid);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800346
Naoki Shiota93ec1712013-06-13 15:49:36 -0700347 if(srcSw != null) {
348 for(IPortObject srcPort : srcSw.getPorts()) {
349 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
350 ISwitchObject dstSw = dstPort.getSwitch();
351 if(dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800352 Link link = new Link(HexString.toLong(dpid),
Naoki Shiota93ec1712013-06-13 15:49:36 -0700353 srcPort.getNumber(),
354 HexString.toLong(dstSw.getDPID()),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800355 dstPort.getNumber());
Naoki Shiota93ec1712013-06-13 15:49:36 -0700356 links.add(link);
357 }
358 }
359 }
360 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800361
mininet403d5892013-06-05 03:48:17 -0700362 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800363 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800364
Naoki Shiota11516712013-06-05 22:36:01 -0700365 /**
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700366 * Get list of all reverse links connected to the switch specified by
367 * given DPID.
368 * @param dpid DPID of desired switch.
369 * @return List of reverse links. Empty list if no port was found.
370 */
371 @Override
372 public List<Link> getReverseLinks(String dpid) {
373 List<Link> links = new ArrayList<Link>();
yoshi2fd4c7e2013-11-22 15:47:55 -0800374
yoshi0fee3de2013-11-23 09:13:37 -0800375 ISwitchObject srcSw = dbop.searchSwitch(dpid);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800376
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700377 if(srcSw != null) {
378 for(IPortObject srcPort : srcSw.getPorts()) {
379 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
380 ISwitchObject dstSw = dstPort.getSwitch();
381 if(dstSw != null) {
382 Link link = new Link(
383 HexString.toLong(dstSw.getDPID()),
384 dstPort.getNumber(),
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800385
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800386 HexString.toLong(dpid),
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700387 srcPort.getNumber());
388 links.add(link);
389 }
390 }
391 }
392 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800393
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700394 return links;
395 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800396
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700397 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700398 * Get list of all links whose state is ACTIVE.
399 * @return List of active links. Empty list if no port was found.
400 */
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800401 @Override
Pankaj Berdeff421802013-01-29 20:28:52 -0800402 public List<Link> getActiveLinks() {
yoshi0fee3de2013-11-23 09:13:37 -0800403 Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
yoshi2fd4c7e2013-11-22 15:47:55 -0800404
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800405 List<Link> links = new ArrayList<Link>();
406
Naoki Shiota826e84a2013-06-18 13:13:25 -0700407 for (ISwitchObject srcSw : switches) {
408 for(IPortObject srcPort : srcSw.getPorts()) {
409 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
410 ISwitchObject dstSw = dstPort.getSwitch();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800411
Naoki Shiota826e84a2013-06-18 13:13:25 -0700412 if(dstSw != null && dstSw.getState().equals("ACTIVE")) {
413 links.add(new Link(HexString.toLong(srcSw.getDPID()),
414 srcPort.getNumber(),
415 HexString.toLong(dstSw.getDPID()),
416 dstPort.getNumber()));
417 }
418 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800419 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800420 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800421
Pankaj Berde5024ec12013-01-31 17:07:29 -0800422 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800423 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800424
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700425 @Override
426 public LinkInfo getLinkInfo(Link link) {
427 // TODO implement this
428 return null;
429 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800430
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700431 /**
432 * Finalize the object.
433 */
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800434 @Override
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800435 protected void finalize() {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700436 close();
437 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800438
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700439 /**
440 * Close LinkStorage.
441 */
442 @Override
443 public void close() {
444 // TODO Auto-generated method stub
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800445// graph.shutdown();
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700446 }
447
Naoki Shiota987a5722013-10-23 11:59:36 -0700448 /**
449 * Update a record of link with meta-information in the LinkStorage.
450 * @param link Record of a link to update.
451 * @param linkinfo Meta-information of a link to be updated.
452 */
453 private boolean setLinkInfoImpl(Link link, LinkInfo linkinfo) {
454 // TODO implement this
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800455
Naoki Shiota987a5722013-10-23 11:59:36 -0700456 return false;
457 }
458
459 private boolean addLinkImpl(Link lt) {
460 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800461
Naoki Shiota987a5722013-10-23 11:59:36 -0700462 IPortObject vportSrc = null, vportDst = null;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800463
Naoki Shiota987a5722013-10-23 11:59:36 -0700464 // get source port vertex
465 String dpid = HexString.toHexString(lt.getSrc());
466 short port = lt.getSrcPort();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800467 log.debug("addLinkImpl Src dpid : {} port : {}", dpid, port);
yoshi0fee3de2013-11-23 09:13:37 -0800468 vportSrc = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800469
Naoki Shiota987a5722013-10-23 11:59:36 -0700470 // get dest port vertex
471 dpid = HexString.toHexString(lt.getDst());
472 port = lt.getDstPort();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800473 log.debug("addLinkImpl Dst dpid : {} port : {}", dpid, port);
yoshi0fee3de2013-11-23 09:13:37 -0800474 vportDst = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800475
476 log.debug("addLinkImpl vportSrc : {} vportDst : {}", vportSrc, vportDst);
477
Naoki Shiota987a5722013-10-23 11:59:36 -0700478 if (vportSrc != null && vportDst != null) {
479 IPortObject portExist = null;
480 // check if the link exists
481 for (IPortObject V : vportSrc.getLinkedPorts()) {
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800482 log.debug("vportSrc.getLinkedPorts() :{}", V);
Naoki Shiota987a5722013-10-23 11:59:36 -0700483 if (V.equals(vportDst)) {
484 portExist = V;
485 break;
486 }
487 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800488
Naoki Shiota987a5722013-10-23 11:59:36 -0700489 if (portExist == null) {
490 vportSrc.setLinkPort(vportDst);
491 success = true;
492 } else {
Yuta HIGUCHI1df38152014-01-08 13:26:29 -0800493 log.error("LinkStorageImpl:addLinkImpl failed link exists {} {} src {} dst {}",
yoshi0fee3de2013-11-23 09:13:37 -0800494 new Object[]{dbop, lt, vportSrc, vportDst});
Naoki Shiota987a5722013-10-23 11:59:36 -0700495 }
496 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800497
Naoki Shiota987a5722013-10-23 11:59:36 -0700498 return success;
499 }
500
501 private boolean deleteLinkImpl(Link lt) {
502 boolean success = false;
503 IPortObject vportSrc = null, vportDst = null;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800504
Naoki Shiota987a5722013-10-23 11:59:36 -0700505 // get source port vertex
506 String dpid = HexString.toHexString(lt.getSrc());
507 short port = lt.getSrcPort();
yoshi0fee3de2013-11-23 09:13:37 -0800508 vportSrc = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800509
Naoki Shiota987a5722013-10-23 11:59:36 -0700510 // get dst port vertex
511 dpid = HexString.toHexString(lt.getDst());
512 port = lt.getDstPort();
yoshi0fee3de2013-11-23 09:13:37 -0800513 vportDst = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800514
Naoki Shiota987a5722013-10-23 11:59:36 -0700515 // FIXME: This needs to remove all edges
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800516 if (vportSrc != null && vportDst != null) {
517 vportSrc.removeLink(vportDst);
518 log.debug("deleteLinkImpl(): deleted edge {} src {} dst {}", new Object[]{
519 lt, vportSrc, vportDst});
520 success = true;
521 }
522
523 return success;
Naoki Shiota987a5722013-10-23 11:59:36 -0700524 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800525}