blob: 1f7815d4d7f02a5e1dd3ea9aad8e58149bcc1ccd [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) {
Yoshi Muroi5804ce92014-02-08 03:58:04 -080036 this.dbop = GraphDBManager.getDBOperation();
yoshid38cd312013-12-02 19:54:44 -080037
Naoki Shiotab2d17e82013-10-18 18:08:16 -070038 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080039
Naoki Shiota987a5722013-10-23 11:59:36 -070040 // Method designing policy:
41 // op.commit() and op.rollback() MUST called in public (first-class) methods.
42 // A first-class method MUST NOT call other first-class method.
43 // Routine process should be implemented in private method.
44 // A private method MUST NOT call commit or rollback.
yoshi2fd4c7e2013-11-22 15:47:55 -080045
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -080046
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080047 /**
Naoki Shiota987a5722013-10-23 11:59:36 -070048 * Update a record in the LinkStorage in a way provided by dmop.
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080049 * @param link Record of a link to be updated.
Naoki Shiota987a5722013-10-23 11:59:36 -070050 * @param linkinfo Meta-information of a link to be updated.
51 * @param dmop Operation to be done.
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -080052 */
53 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -070054 public boolean update(Link link, LinkInfo linkinfo, DM_OPERATION dmop) {
55 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -080056
Naoki Shiota987a5722013-10-23 11:59:36 -070057 switch (dmop) {
Naoki Shiotab2d17e82013-10-18 18:08:16 -070058 case CREATE:
59 case INSERT:
Naoki Shiota987a5722013-10-23 11:59:36 -070060 if (link != null) {
61 try {
62 if (addLinkImpl(link)) {
yoshi0fee3de2013-11-23 09:13:37 -080063 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -070064 success = true;
65 }
66 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -080067 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -070068 e.printStackTrace();
69 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
70 }
71 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070072 break;
73 case UPDATE:
Naoki Shiota987a5722013-10-23 11:59:36 -070074 if (link != null && linkinfo != null) {
75 try {
76 if (setLinkInfoImpl(link, linkinfo)) {
yoshi0fee3de2013-11-23 09:13:37 -080077 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -070078 success = true;
79 }
80 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -080081 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -070082 e.printStackTrace();
83 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
84 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -070085 }
86 break;
87 case DELETE:
Naoki Shiota987a5722013-10-23 11:59:36 -070088 if (link != null) {
89 try {
90 if (deleteLinkImpl(link)) {
yoshi0fee3de2013-11-23 09:13:37 -080091 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -070092 success = true;
93 log.debug("LinkStorageImpl:update {} link:{} succeeded", dmop, link);
94 } else {
yoshi0fee3de2013-11-23 09:13:37 -080095 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -070096 log.debug("LinkStorageImpl:update {} link:{} failed", dmop, link);
97 }
98 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -080099 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700100 e.printStackTrace();
101 log.error("LinkStorageImpl:update {} link:{} failed", dmop, link);
102 }
103 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700104 break;
105 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800106
Naoki Shiota987a5722013-10-23 11:59:36 -0700107 return success;
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800108 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800109
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700110 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700111 public boolean addLink(Link link) {
112 return addLink(link, null);
113 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800114
pingping-lin00926032013-12-18 12:13:08 +0800115 private void deleteDeviceOnPort(Long dpid, Short number)
116 {
Masayoshi Kobayashi1015e362013-12-17 22:54:49 -0800117 IPortObject srcPortObject = dbop.searchPort(HexString.toHexString(dpid), number);
Yuta HIGUCHId489ced2014-01-08 15:46:28 -0800118 if (srcPortObject == null)
119 return;
120 Iterable<IDeviceObject> devices = srcPortObject.getDevices();
121 if (devices == null)
122 return;
123 if (devices.iterator().hasNext()) {
pingping-lin00926032013-12-18 12:13:08 +0800124 for (IDeviceObject deviceObject: srcPortObject.getDevices()) {
125 srcPortObject.removeDevice(deviceObject);
126 log.debug("delete Device "+ deviceObject.getMACAddress() +
127 " from sw: {} port: {} due to a new link added",
128 dpid, number);
129 }
130 }
131 }
132
Naoki Shiota987a5722013-10-23 11:59:36 -0700133 @Override
134 public boolean addLink(Link link, LinkInfo linfo) {
135 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800136
Naoki Shiota987a5722013-10-23 11:59:36 -0700137 try {
pingping-lin00926032013-12-18 12:13:08 +0800138 //delete the Device attachment points for the related switch and port
139 deleteDeviceOnPort(link.getSrc(),link.getSrcPort());
140 deleteDeviceOnPort(link.getDst(),link.getDstPort());
141
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800142 pm.addlink_start();
Naoki Shiota987a5722013-10-23 11:59:36 -0700143 if (addLinkImpl(link)) {
144 // Set LinkInfo only if linfo is non-null.
145 if (linfo != null && (! setLinkInfoImpl(link, linfo))) {
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700146 log.debug("Adding linkinfo failed: {}", link);
yoshi0fee3de2013-11-23 09:13:37 -0800147 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700148 }
yoshi0fee3de2013-11-23 09:13:37 -0800149 dbop.commit();
Masayoshi Kobayashi536d72e2014-01-06 13:15:52 -0800150 pm.addlink_end();
Naoki Shiota987a5722013-10-23 11:59:36 -0700151 success = true;
152 } else {
Yuta HIGUCHI1df38152014-01-08 13:26:29 -0800153 pm.addlink_end();
Jonathan Hart13ccdca2013-10-30 15:23:28 -0700154 // If we fail here that's because the ports aren't added
155 // before we try to add the link
156 log.debug("Adding link failed: {}", link);
yoshi0fee3de2013-11-23 09:13:37 -0800157 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700158 }
159 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800160 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700161 e.printStackTrace();
162 log.error("LinkStorageImpl:addLink link:{} linfo:{} failed", link, linfo);
163 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800164
Naoki Shiota987a5722013-10-23 11:59:36 -0700165 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700166 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800167
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800168 /**
169 * Update multiple records in the LinkStorage in a way provided by op.
170 * @param links List of records to be updated.
171 * @param op Operation to be done.
172 */
173 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700174 public boolean addLinks(List<Link> links) {
175 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800176
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800177 for (Link lt: links) {
Naoki Shiota987a5722013-10-23 11:59:36 -0700178 if (! addLinkImpl(lt)) {
179 return false;
180 }
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700181 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800182
Naoki Shiota987a5722013-10-23 11:59:36 -0700183 try {
yoshi0fee3de2013-11-23 09:13:37 -0800184 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700185 success = true;
186 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800187 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700188 e.printStackTrace();
189 log.error("LinkStorageImpl:addLinks link:s{} failed", links);
190 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800191
Naoki Shiota987a5722013-10-23 11:59:36 -0700192 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700193 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800194
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700195 /**
Naoki Shiota987a5722013-10-23 11:59:36 -0700196 * Delete a record in the LinkStorage.
197 * @param lt Record to be deleted.
198 */
199 @Override
200 public boolean deleteLink(Link lt) {
201 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800202
Naoki Shiota987a5722013-10-23 11:59:36 -0700203 log.debug("LinkStorageImpl:deleteLink(): {}", lt);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800204
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800205 try {
Naoki Shiota987a5722013-10-23 11:59:36 -0700206 if (deleteLinkImpl(lt)) {
yoshi0fee3de2013-11-23 09:13:37 -0800207 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700208 success = true;
209 log.debug("LinkStorageImpl:deleteLink(): deleted edges {}", lt);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800210 } else {
yoshi0fee3de2013-11-23 09:13:37 -0800211 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700212 log.error("LinkStorageImpl:deleteLink(): failed invalid vertices {}", lt);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800213 }
Naoki Shiota987a5722013-10-23 11:59:36 -0700214 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800215 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700216 log.error("LinkStorageImpl:deleteLink(): failed {} {}",
217 new Object[]{lt, e.toString()});
218 e.printStackTrace();
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800219 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800220
Naoki Shiota987a5722013-10-23 11:59:36 -0700221 return success;
222 }
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800223
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700224 /**
225 * Delete multiple records in LinkStorage.
226 * @param links List of records to be deleted.
227 */
228 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700229 public boolean deleteLinks(List<Link> links) {
230 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800231
Naoki Shiota987a5722013-10-23 11:59:36 -0700232 try {
233 for (Link lt : links) {
234 if (! deleteLinkImpl(lt)) {
yoshi0fee3de2013-11-23 09:13:37 -0800235 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700236 return false;
237 }
238 }
yoshi0fee3de2013-11-23 09:13:37 -0800239 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700240 success = true;
241 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800242 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700243 e.printStackTrace();
244 log.error("LinkStorageImpl:deleteLinks failed invalid vertices {}", links);
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800245 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800246
Naoki Shiota987a5722013-10-23 11:59:36 -0700247 return success;
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700248 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800249
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800250 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700251 * Get list of all links connected to the port specified by given DPID and port number.
252 * @param dpid DPID of desired port.
253 * @param port Port number of desired port.
254 * @return List of links. Empty list if no port was found.
255 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800256 @Override
257 public List<Link> getLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800258 List<Link> links = new ArrayList<Link>();
yoshi0fee3de2013-11-23 09:13:37 -0800259 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800260 if (srcPort == null)
261 return links;
262 ISwitchObject srcSw = srcPort.getSwitch();
263 if (srcSw == null)
264 return links;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800265
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800266 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
267 ISwitchObject dstSw = dstPort.getSwitch();
268 if (dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800269 Link link = new Link(dpid, port,
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800270 HexString.toLong(dstSw.getDPID()),
271 dstPort.getNumber());
272 links.add(link);
273 }
274 }
275 return links;
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800276 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800277
Pavlin Radoslavov43781cd2013-11-04 18:19:10 -0800278 /**
279 * Get list of all reverse links connected to the port specified by given DPID and port number.
280 * @param dpid DPID of desired port.
281 * @param port Port number of desired port.
282 * @return List of reverse links. Empty list if no port was found.
283 */
284 @Override
285 public List<Link> getReverseLinks(Long dpid, short port) {
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800286 List<Link> links = new ArrayList<Link>();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800287
yoshi0fee3de2013-11-23 09:13:37 -0800288 IPortObject srcPort = dbop.searchPort(HexString.toHexString(dpid), port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800289 if (srcPort == null)
290 return links;
291 ISwitchObject srcSw = srcPort.getSwitch();
292 if (srcSw == null)
293 return links;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800294
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800295 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
296 ISwitchObject dstSw = dstPort.getSwitch();
297 if (dstSw != null) {
298 Link link = new Link(HexString.toLong(dstSw.getDPID()),
299 dstPort.getNumber(),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800300 dpid, port);
Pavlin Radoslavovd8226792013-11-04 20:28:07 -0800301 links.add(link);
302 }
303 }
304 return links;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800305 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800306
Naoki Shiota11516712013-06-05 22:36:01 -0700307 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700308 * Delete records of the links connected to the port specified by given DPID and port number.
309 * @param dpid DPID of desired port.
310 * @param port Port number of desired port.
311 */
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800312 @Override
Naoki Shiota987a5722013-10-23 11:59:36 -0700313 public boolean deleteLinksOnPort(Long dpid, short port) {
314 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800315
Naoki Shiota987a5722013-10-23 11:59:36 -0700316 List<Link> linksToDelete = getLinks(dpid, port);
317 try {
318 for(Link l : linksToDelete) {
319 if (! deleteLinkImpl(l)) {
yoshi0fee3de2013-11-23 09:13:37 -0800320 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700321 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
322 return false;
323 }
324 }
yoshi0fee3de2013-11-23 09:13:37 -0800325 dbop.commit();
Naoki Shiota987a5722013-10-23 11:59:36 -0700326 success = true;
327 } catch (Exception e) {
yoshi0fee3de2013-11-23 09:13:37 -0800328 dbop.rollback();
Naoki Shiota987a5722013-10-23 11:59:36 -0700329 e.printStackTrace();
330 log.error("LinkStorageImpl:deleteLinksOnPort dpid:{} port:{} failed", dpid, port);
mininet403d5892013-06-05 03:48:17 -0700331 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800332
Naoki Shiota987a5722013-10-23 11:59:36 -0700333 return success;
Umesh Krishnaswamyf962d642013-01-23 19:04:23 -0800334 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800335
Naoki Shiota11516712013-06-05 22:36:01 -0700336 /**
337 * Get list of all links connected to the switch specified by given DPID.
338 * @param dpid DPID of desired switch.
339 * @return List of links. Empty list if no port was found.
340 */
Pankaj Berdeff421802013-01-29 20:28:52 -0800341 @Override
342 public List<Link> getLinks(String dpid) {
mininet403d5892013-06-05 03:48:17 -0700343 List<Link> links = new ArrayList<Link>();
yoshi0fee3de2013-11-23 09:13:37 -0800344 ISwitchObject srcSw = dbop.searchSwitch(dpid);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800345
Naoki Shiota93ec1712013-06-13 15:49:36 -0700346 if(srcSw != null) {
347 for(IPortObject srcPort : srcSw.getPorts()) {
348 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
349 ISwitchObject dstSw = dstPort.getSwitch();
350 if(dstSw != null) {
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800351 Link link = new Link(HexString.toLong(dpid),
Naoki Shiota93ec1712013-06-13 15:49:36 -0700352 srcPort.getNumber(),
353 HexString.toLong(dstSw.getDPID()),
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800354 dstPort.getNumber());
Naoki Shiota93ec1712013-06-13 15:49:36 -0700355 links.add(link);
356 }
357 }
358 }
359 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800360
mininet403d5892013-06-05 03:48:17 -0700361 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800362 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800363
Naoki Shiota11516712013-06-05 22:36:01 -0700364 /**
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700365 * Get list of all reverse links connected to the switch specified by
366 * given DPID.
367 * @param dpid DPID of desired switch.
368 * @return List of reverse links. Empty list if no port was found.
369 */
370 @Override
371 public List<Link> getReverseLinks(String dpid) {
372 List<Link> links = new ArrayList<Link>();
yoshi2fd4c7e2013-11-22 15:47:55 -0800373
yoshi0fee3de2013-11-23 09:13:37 -0800374 ISwitchObject srcSw = dbop.searchSwitch(dpid);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800375
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700376 if(srcSw != null) {
377 for(IPortObject srcPort : srcSw.getPorts()) {
378 for(IPortObject dstPort : srcPort.getReverseLinkedPorts()) {
379 ISwitchObject dstSw = dstPort.getSwitch();
380 if(dstSw != null) {
381 Link link = new Link(
382 HexString.toLong(dstSw.getDPID()),
383 dstPort.getNumber(),
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800384
Pavlin Radoslavov0aad60c2013-11-05 08:33:57 -0800385 HexString.toLong(dpid),
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700386 srcPort.getNumber());
387 links.add(link);
388 }
389 }
390 }
391 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800392
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700393 return links;
394 }
yoshi2fd4c7e2013-11-22 15:47:55 -0800395
Pavlin Radoslavovc934b4a2013-11-02 14:53:52 -0700396 /**
Naoki Shiota11516712013-06-05 22:36:01 -0700397 * Get list of all links whose state is ACTIVE.
398 * @return List of active links. Empty list if no port was found.
399 */
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800400 @Override
Pankaj Berdeff421802013-01-29 20:28:52 -0800401 public List<Link> getActiveLinks() {
yoshi0fee3de2013-11-23 09:13:37 -0800402 Iterable<ISwitchObject> switches = dbop.getActiveSwitches();
yoshi2fd4c7e2013-11-22 15:47:55 -0800403
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800404 List<Link> links = new ArrayList<Link>();
405
Naoki Shiota826e84a2013-06-18 13:13:25 -0700406 for (ISwitchObject srcSw : switches) {
407 for(IPortObject srcPort : srcSw.getPorts()) {
408 for(IPortObject dstPort : srcPort.getLinkedPorts()) {
409 ISwitchObject dstSw = dstPort.getSwitch();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800410
Naoki Shiota826e84a2013-06-18 13:13:25 -0700411 if(dstSw != null && dstSw.getState().equals("ACTIVE")) {
412 links.add(new Link(HexString.toLong(srcSw.getDPID()),
413 srcPort.getNumber(),
414 HexString.toLong(dstSw.getDPID()),
415 dstPort.getNumber()));
416 }
417 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800418 }
Pankaj Berde5024ec12013-01-31 17:07:29 -0800419 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800420
Pankaj Berde5024ec12013-01-31 17:07:29 -0800421 return links;
Pankaj Berdeff421802013-01-29 20:28:52 -0800422 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800423
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700424 @Override
425 public LinkInfo getLinkInfo(Link link) {
426 // TODO implement this
427 return null;
428 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800429
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700430 /**
431 * Finalize the object.
432 */
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800433 @Override
Yuta HIGUCHI67a7a3e2014-01-03 14:51:34 -0800434 protected void finalize() {
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700435 close();
436 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800437
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700438 /**
439 * Close LinkStorage.
440 */
441 @Override
442 public void close() {
443 // TODO Auto-generated method stub
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800444// graph.shutdown();
Naoki Shiotab2d17e82013-10-18 18:08:16 -0700445 }
446
Naoki Shiota987a5722013-10-23 11:59:36 -0700447 /**
448 * Update a record of link with meta-information in the LinkStorage.
449 * @param link Record of a link to update.
450 * @param linkinfo Meta-information of a link to be updated.
451 */
452 private boolean setLinkInfoImpl(Link link, LinkInfo linkinfo) {
453 // TODO implement this
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800454
Naoki Shiota987a5722013-10-23 11:59:36 -0700455 return false;
456 }
457
458 private boolean addLinkImpl(Link lt) {
459 boolean success = false;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800460
Naoki Shiota987a5722013-10-23 11:59:36 -0700461 IPortObject vportSrc = null, vportDst = null;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800462
Naoki Shiota987a5722013-10-23 11:59:36 -0700463 // get source port vertex
464 String dpid = HexString.toHexString(lt.getSrc());
465 short port = lt.getSrcPort();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800466 log.debug("addLinkImpl Src dpid : {} port : {}", dpid, port);
yoshi0fee3de2013-11-23 09:13:37 -0800467 vportSrc = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800468
Naoki Shiota987a5722013-10-23 11:59:36 -0700469 // get dest port vertex
470 dpid = HexString.toHexString(lt.getDst());
471 port = lt.getDstPort();
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800472 log.debug("addLinkImpl Dst dpid : {} port : {}", dpid, port);
yoshi0fee3de2013-11-23 09:13:37 -0800473 vportDst = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800474
475 log.debug("addLinkImpl vportSrc : {} vportDst : {}", vportSrc, vportDst);
476
Naoki Shiota987a5722013-10-23 11:59:36 -0700477 if (vportSrc != null && vportDst != null) {
478 IPortObject portExist = null;
479 // check if the link exists
480 for (IPortObject V : vportSrc.getLinkedPorts()) {
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800481 log.debug("vportSrc.getLinkedPorts() :{}", V);
Naoki Shiota987a5722013-10-23 11:59:36 -0700482 if (V.equals(vportDst)) {
483 portExist = V;
484 break;
485 }
486 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800487
Naoki Shiota987a5722013-10-23 11:59:36 -0700488 if (portExist == null) {
489 vportSrc.setLinkPort(vportDst);
490 success = true;
491 } else {
Yuta HIGUCHI1df38152014-01-08 13:26:29 -0800492 log.error("LinkStorageImpl:addLinkImpl failed link exists {} {} src {} dst {}",
yoshi0fee3de2013-11-23 09:13:37 -0800493 new Object[]{dbop, lt, vportSrc, vportDst});
Naoki Shiota987a5722013-10-23 11:59:36 -0700494 }
Naoki Shiotaf74d5f32014-01-09 21:29:38 -0800495 } else {
496 log.error("Ports not found : {}", lt);
Naoki Shiota987a5722013-10-23 11:59:36 -0700497 }
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800498
Naoki Shiota987a5722013-10-23 11:59:36 -0700499 return success;
500 }
501
502 private boolean deleteLinkImpl(Link lt) {
503 boolean success = false;
504 IPortObject vportSrc = null, vportDst = null;
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800505
Naoki Shiota987a5722013-10-23 11:59:36 -0700506 // get source port vertex
507 String dpid = HexString.toHexString(lt.getSrc());
508 short port = lt.getSrcPort();
yoshi0fee3de2013-11-23 09:13:37 -0800509 vportSrc = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800510
Naoki Shiota987a5722013-10-23 11:59:36 -0700511 // get dst port vertex
512 dpid = HexString.toHexString(lt.getDst());
513 port = lt.getDstPort();
yoshi0fee3de2013-11-23 09:13:37 -0800514 vportDst = dbop.searchPort(dpid, port);
Yuta HIGUCHIaa5e4a72014-01-06 21:55:25 -0800515
Naoki Shiota987a5722013-10-23 11:59:36 -0700516 // FIXME: This needs to remove all edges
Yuta HIGUCHI5302ddf2014-01-06 12:53:35 -0800517 if (vportSrc != null && vportDst != null) {
518 vportSrc.removeLink(vportDst);
519 log.debug("deleteLinkImpl(): deleted edge {} src {} dst {}", new Object[]{
520 lt, vportSrc, vportDst});
521 success = true;
522 }
523
524 return success;
Naoki Shiota987a5722013-10-23 11:59:36 -0700525 }
Umesh Krishnaswamyb676ca22013-01-11 12:39:25 -0800526}