blob: 639c8fdb0fcc2ae409c5de94095b3a7746bb0549 [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);
pingping-lin00926032013-12-18 12:13:08 +0800119 if (srcPortObject.getDevices().iterator().hasNext()) {
120 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 HIGUCHIaa5e4a72014-01-06 21:55:25 -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
Masayoshi Kobayashi1015e362013-12-17 22:54:49 -0800138 long startLinkTime = System.nanoTime();
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800139 pm.addlink_start();
Naoki Shiota987a5722013-10-23 11:59:36 -0700140 if (addLinkImpl(link)) {
141 // Set LinkInfo only if linfo is non-null.
142 if (linfo != null && (! setLinkInfoImpl(link, linfo))) {
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700143 log.debug("Adding linkinfo failed: {}", link);
yoshi0fee3de2013-11-23 09:13:37 -0800144 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700145 }
yoshi0fee3de2013-11-23 09:13:37 -0800146 dbop.commit();
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800147 pm.addlink_end();
Masayoshi Kobayashi1015e362013-12-17 22:54:49 -0800148 long endLinkTime = System.nanoTime();
149 log.error("Performance ##add link total time {}", endLinkTime - startLinkTime);
Naoki Shiota987a5722013-10-23 11:59:36 -0700150 success = true;
151 } else {
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700152 // If we fail here that's because the ports aren't added
153 // before we try to add the link
154 log.debug("Adding link failed: {}", link);
yoshi0fee3de2013-11-23 09:13:37 -0800155 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700156 }
157 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800158 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700159 e.printStackTrace();
160 log.error("LinkStorageImpl:addLink link:{} linfo:{} failed", link, linfo);
161 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800162
Naoki Shiota987a5722013-10-23 11:59:36 -0700163 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700164 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800165
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800166 /**
167 * Update multiple records in the LinkStorage in a way provided by op.
168 * @param links List of records to be updated.
169 * @param op Operation to be done.
170 */
171 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700172 public boolean addLinks(List<Link> links) {
173 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800174
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800175 for (Link lt: links) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700176 if (! addLinkImpl(lt)) {
177 return false;
178 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700179 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800180
Naoki Shiota987a5722013-10-23 11:59:36 -0700181 try {
yoshi0fee3de2013-11-23 09:13:37 -0800182 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700183 success = true;
184 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800185 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700186 e.printStackTrace();
187 log.error("LinkStorageImpl:addLinks link:s{} failed", links);
188 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800189
Naoki Shiota987a5722013-10-23 11:59:36 -0700190 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700191 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800192
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700193 /**
Naoki Shiota987a5722013-10-23 11:59:36 -0700194 * Delete a record in the LinkStorage.
195 * @param lt Record to be deleted.
196 */
197 @Override
198 public boolean deleteLink(Link lt) {
199 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800200
Naoki Shiota987a5722013-10-23 11:59:36 -0700201 log.debug("LinkStorageImpl:deleteLink(): {}", lt);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800202
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800203 try {
Naoki Shiota987a5722013-10-23 11:59:36 -0700204 if (deleteLinkImpl(lt)) {
yoshi0fee3de2013-11-23 09:13:37 -0800205 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700206 success = true;
207 log.debug("LinkStorageImpl:deleteLink(): deleted edges {}", lt);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800208 } else {
yoshi0fee3de2013-11-23 09:13:37 -0800209 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700210 log.error("LinkStorageImpl:deleteLink(): failed invalid vertices {}", lt);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800211 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700212 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800213 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700214 log.error("LinkStorageImpl:deleteLink(): failed {} {}",
215 new Object[]{lt, e.toString()});
216 e.printStackTrace();
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800217 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800218
Naoki Shiota987a5722013-10-23 11:59:36 -0700219 return success;
220 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800221
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700222 /**
223 * Delete multiple records in LinkStorage.
224 * @param links List of records to be deleted.
225 */
226 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700227 public boolean deleteLinks(List<Link> links) {
228 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800229
Naoki Shiota987a5722013-10-23 11:59:36 -0700230 try {
231 for (Link lt : links) {
232 if (! deleteLinkImpl(lt)) {
yoshi0fee3de2013-11-23 09:13:37 -0800233 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700234 return false;
235 }
236 }
yoshi0fee3de2013-11-23 09:13:37 -0800237 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700238 success = true;
239 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800240 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700241 e.printStackTrace();
242 log.error("LinkStorageImpl:deleteLinks failed invalid vertices {}", links);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800243 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800244
Naoki Shiota987a5722013-10-23 11:59:36 -0700245 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700246 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800247
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800248 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700249 * Get list of all links connected to the port specified by given DPID and port number.
250 * @param dpid DPID of desired port.
251 * @param port Port number of desired port.
252 * @return List of links. Empty list if no port was found.
253 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800254 @Override
255 public List<Link> getLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800256 List<Link> links = new ArrayList<Link>();
yoshi0fee3de2013-11-23 09:13:37 -0800257 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800258 if (srcPort == null)
259 return links;
260 ISwitchObject srcSw = srcPort.getSwitch();
261 if (srcSw == null)
262 return links;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800263
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800264 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
265 ISwitchObject dstSw = dstPort.getSwitch();
266 if (dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800267 Link link = new Link(dpid, port,
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800268 HexString.toLong(dstSw.getDPID()),
269 dstPort.getNumber());
270 links.add(link);
271 }
272 }
273 return links;
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800274 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800275
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800276 /**
277 * Get list of all reverse links connected to the port specified by given DPID and port number.
278 * @param dpid DPID of desired port.
279 * @param port Port number of desired port.
280 * @return List of reverse links. Empty list if no port was found.
281 */
282 @Override
283 public List<Link> getReverseLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800284 List<Link> links = new ArrayList<Link>();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800285
yoshi0fee3de2013-11-23 09:13:37 -0800286 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800287 if (srcPort == null)
288 return links;
289 ISwitchObject srcSw = srcPort.getSwitch();
290 if (srcSw == null)
291 return links;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800292
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800293 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
294 ISwitchObject dstSw = dstPort.getSwitch();
295 if (dstSw != null) {
296 Link link = new Link(HexString.toLong(dstSw.getDPID()),
297 dstPort.getNumber(),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800298 dpid, port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800299 links.add(link);
300 }
301 }
302 return links;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800303 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800304
Naoki Shiota11516712013-06-05 22:36:01 -0700305 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700306 * Delete records of the links connected to the port specified by given DPID and port number.
307 * @param dpid DPID of desired port.
308 * @param port Port number of desired port.
309 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800310 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700311 public boolean deleteLinksOnPort(Long dpid, short port) {
312 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800313
Naoki Shiota987a5722013-10-23 11:59:36 -0700314 List<Link> linksToDelete = getLinks(dpid, port);
315 try {
316 for(Link l : linksToDelete) {
317 if (! deleteLinkImpl(l)) {
yoshi0fee3de2013-11-23 09:13:37 -0800318 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700319 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
320 return false;
321 }
322 }
yoshi0fee3de2013-11-23 09:13:37 -0800323 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700324 success = true;
325 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800326 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700327 e.printStackTrace();
328 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
mininet403d5892013-06-05 03:48:17 -0700329 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800330
Naoki Shiota987a5722013-10-23 11:59:36 -0700331 return success;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800332 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800333
Naoki Shiota11516712013-06-05 22:36:01 -0700334 /**
335 * Get list of all links connected to the switch specified by given DPID.
336 * @param dpid DPID of desired switch.
337 * @return List of links. Empty list if no port was found.
338 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800339 @Override
340 public List<Link> getLinks(String dpid) {
mininet403d5892013-06-05 03:48:17 -0700341 List<Link> links = new ArrayList<Link>();
yoshi0fee3de2013-11-23 09:13:37 -0800342 ISwitchObject srcSw = dbop.searchSwitch(dpid);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800343
Naoki Shiota93ec1712013-06-13 15:49:36 -0700344 if(srcSw != null) {
345 for(IPortObject srcPort : srcSw.getPorts()) {
346 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
347 ISwitchObject dstSw = dstPort.getSwitch();
348 if(dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800349 Link link = new Link(HexString.toLong(dpid),
Naoki Shiota93ec1712013-06-13 15:49:36 -0700350 srcPort.getNumber(),
351 HexString.toLong(dstSw.getDPID()),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800352 dstPort.getNumber());
Naoki Shiota93ec1712013-06-13 15:49:36 -0700353 links.add(link);
354 }
355 }
356 }
357 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800358
mininet403d5892013-06-05 03:48:17 -0700359 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800360 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800361
Naoki Shiota11516712013-06-05 22:36:01 -0700362 /**
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700363 * Get list of all reverse links connected to the switch specified by
364 * given DPID.
365 * @param dpid DPID of desired switch.
366 * @return List of reverse links. Empty list if no port was found.
367 */
368 @Override
369 public List<Link> getReverseLinks(String dpid) {
370 List<Link> links = new ArrayList<Link>();
yoshi2fd4c7e2013-11-22 15:47:55 -0800371
yoshi0fee3de2013-11-23 09:13:37 -0800372 ISwitchObject srcSw = dbop.searchSwitch(dpid);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800373
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700374 if(srcSw != null) {
375 for(IPortObject srcPort : srcSw.getPorts()) {
376 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
377 ISwitchObject dstSw = dstPort.getSwitch();
378 if(dstSw != null) {
379 Link link = new Link(
380 HexString.toLong(dstSw.getDPID()),
381 dstPort.getNumber(),
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800382
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800383 HexString.toLong(dpid),
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700384 srcPort.getNumber());
385 links.add(link);
386 }
387 }
388 }
389 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800390
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700391 return links;
392 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800393
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700394 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700395 * Get list of all links whose state is ACTIVE.
396 * @return List of active links. Empty list if no port was found.
397 */
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800398 @Override
Pankaj Berdeff421802013-01-29 20:28:52 -0800399 public List<Link> getActiveLinks() {
yoshi0fee3de2013-11-23 09:13:37 -0800400 Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
yoshi2fd4c7e2013-11-22 15:47:55 -0800401
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800402 List<Link> links = new ArrayList<Link>();
403
Naoki Shiota826e84a2013-06-18 13:13:25 -0700404 for (ISwitchObject srcSw : switches) {
405 for(IPortObject srcPort : srcSw.getPorts()) {
406 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
407 ISwitchObject dstSw = dstPort.getSwitch();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800408
Naoki Shiota826e84a2013-06-18 13:13:25 -0700409 if(dstSw != null && dstSw.getState().equals("ACTIVE")) {
410 links.add(new Link(HexString.toLong(srcSw.getDPID()),
411 srcPort.getNumber(),
412 HexString.toLong(dstSw.getDPID()),
413 dstPort.getNumber()));
414 }
415 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800416 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800417 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800418
Pankaj Berde5024ec12013-01-31 17:07:29 -0800419 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800420 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800421
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700422 @Override
423 public LinkInfo getLinkInfo(Link link) {
424 // TODO implement this
425 return null;
426 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800427
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700428 /**
429 * Finalize the object.
430 */
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800431 @Override
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700432 public void finalize() {
433 close();
434 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800435
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700436 /**
437 * Close LinkStorage.
438 */
439 @Override
440 public void close() {
441 // TODO Auto-generated method stub
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800442// graph.shutdown();
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700443 }
444
Naoki Shiota987a5722013-10-23 11:59:36 -0700445 /**
446 * Update a record of link with meta-information in the LinkStorage.
447 * @param link Record of a link to update.
448 * @param linkinfo Meta-information of a link to be updated.
449 */
450 private boolean setLinkInfoImpl(Link link, LinkInfo linkinfo) {
451 // TODO implement this
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800452
Naoki Shiota987a5722013-10-23 11:59:36 -0700453 return false;
454 }
455
456 private boolean addLinkImpl(Link lt) {
457 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800458
Naoki Shiota987a5722013-10-23 11:59:36 -0700459 IPortObject vportSrc = null, vportDst = null;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800460
Naoki Shiota987a5722013-10-23 11:59:36 -0700461 // get source port vertex
462 String dpid = HexString.toHexString(lt.getSrc());
463 short port = lt.getSrcPort();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800464 log.debug("addLinkImpl Src dpid : {} port : {}", dpid, port);
yoshi0fee3de2013-11-23 09:13:37 -0800465 vportSrc = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800466
Naoki Shiota987a5722013-10-23 11:59:36 -0700467 // get dest port vertex
468 dpid = HexString.toHexString(lt.getDst());
469 port = lt.getDstPort();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800470 log.debug("addLinkImpl Dst dpid : {} port : {}", dpid, port);
yoshi0fee3de2013-11-23 09:13:37 -0800471 vportDst = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800472
473 log.debug("addLinkImpl vportSrc : {} vportDst : {}", vportSrc, vportDst);
474
Naoki Shiota987a5722013-10-23 11:59:36 -0700475 if (vportSrc != null && vportDst != null) {
476 IPortObject portExist = null;
477 // check if the link exists
478 for (IPortObject V : vportSrc.getLinkedPorts()) {
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800479 log.debug("vportSrc.getLinkedPorts() :{}", V);
Naoki Shiota987a5722013-10-23 11:59:36 -0700480 if (V.equals(vportDst)) {
481 portExist = V;
482 break;
483 }
484 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800485
Naoki Shiota987a5722013-10-23 11:59:36 -0700486 if (portExist == null) {
487 vportSrc.setLinkPort(vportDst);
488 success = true;
489 } else {
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800490 log.debug("LinkStorageImpl:addLinkImpl failed link exists {} {} src {} dst {}",
yoshi0fee3de2013-11-23 09:13:37 -0800491 new Object[]{dbop, lt, vportSrc, vportDst});
Naoki Shiota987a5722013-10-23 11:59:36 -0700492 }
493 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800494
Naoki Shiota987a5722013-10-23 11:59:36 -0700495 return success;
496 }
497
498 private boolean deleteLinkImpl(Link lt) {
499 boolean success = false;
500 IPortObject vportSrc = null, vportDst = null;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800501
Naoki Shiota987a5722013-10-23 11:59:36 -0700502 // get source port vertex
503 String dpid = HexString.toHexString(lt.getSrc());
504 short port = lt.getSrcPort();
yoshi0fee3de2013-11-23 09:13:37 -0800505 vportSrc = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800506
Naoki Shiota987a5722013-10-23 11:59:36 -0700507 // get dst port vertex
508 dpid = HexString.toHexString(lt.getDst());
509 port = lt.getDstPort();
yoshi0fee3de2013-11-23 09:13:37 -0800510 vportDst = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800511
Naoki Shiota987a5722013-10-23 11:59:36 -0700512 // FIXME: This needs to remove all edges
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800513 if (vportSrc != null && vportDst != null) {
514 vportSrc.removeLink(vportDst);
515 log.debug("deleteLinkImpl(): deleted edge {} src {} dst {}", new Object[]{
516 lt, vportSrc, vportDst});
517 success = true;
518 }
519
520 return success;
Naoki Shiota987a5722013-10-23 11:59:36 -0700521 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800522}