Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 1 | package net.onrc.onos.ofcontroller.flowmanager; |
| 2 | |
| 3 | import java.io.PrintWriter; |
| 4 | import java.io.StringWriter; |
| 5 | import java.util.ArrayList; |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 6 | import java.util.Comparator; |
| 7 | import java.util.LinkedList; |
| 8 | import java.util.List; |
| 9 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 10 | |
| 11 | import net.floodlightcontroller.util.MACAddress; |
| 12 | |
| 13 | import net.onrc.onos.graph.GraphDBOperation; |
| 14 | |
| 15 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowEntry; |
| 16 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IFlowPath; |
| 17 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.IPortObject; |
| 18 | import net.onrc.onos.ofcontroller.core.INetMapTopologyObjects.ISwitchObject; |
| 19 | import net.onrc.onos.ofcontroller.util.*; |
| 20 | |
| 21 | import org.slf4j.Logger; |
| 22 | import org.slf4j.LoggerFactory; |
| 23 | |
| 24 | /** |
| 25 | * Class for performing Flow-related operations on the Database. |
| 26 | */ |
Pavlin Radoslavov | 6bfaea6 | 2013-12-03 14:55:57 -0800 | [diff] [blame] | 27 | public class FlowDatabaseOperation { |
Yuta HIGUCHI | 6ac8d18 | 2013-10-22 15:24:56 -0700 | [diff] [blame] | 28 | private final static Logger log = LoggerFactory.getLogger(FlowDatabaseOperation.class); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Add a flow. |
| 32 | * |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 33 | * @param dbHandler the Graph Database handler to use. |
| 34 | * @param flowPath the Flow Path to install. |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 35 | * @return true on success, otherwise false. |
| 36 | */ |
Pavlin Radoslavov | 051abb4 | 2013-12-05 17:24:50 -0800 | [diff] [blame] | 37 | static boolean addFlow(GraphDBOperation dbHandler, FlowPath flowPath) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 38 | IFlowPath flowObj = null; |
| 39 | boolean found = false; |
| 40 | try { |
| 41 | if ((flowObj = dbHandler.searchFlowPath(flowPath.flowId())) != null) { |
| 42 | found = true; |
| 43 | } else { |
| 44 | flowObj = dbHandler.newFlowPath(); |
| 45 | } |
| 46 | } catch (Exception e) { |
| 47 | dbHandler.rollback(); |
| 48 | |
| 49 | StringWriter sw = new StringWriter(); |
| 50 | e.printStackTrace(new PrintWriter(sw)); |
| 51 | String stacktrace = sw.toString(); |
| 52 | |
| 53 | log.error(":addFlow FlowId:{} failed: {}", |
| 54 | flowPath.flowId().toString(), |
| 55 | stacktrace); |
| 56 | return false; |
| 57 | } |
| 58 | if (flowObj == null) { |
| 59 | log.error(":addFlow FlowId:{} failed: Flow object not created", |
| 60 | flowPath.flowId().toString()); |
| 61 | dbHandler.rollback(); |
| 62 | return false; |
| 63 | } |
| 64 | |
| 65 | // |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 66 | // Remove the old Flow Entries |
| 67 | // |
| 68 | if (found) { |
| 69 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 70 | LinkedList<IFlowEntry> deleteFlowEntries = |
| 71 | new LinkedList<IFlowEntry>(); |
| 72 | for (IFlowEntry flowEntryObj : flowEntries) |
| 73 | deleteFlowEntries.add(flowEntryObj); |
| 74 | for (IFlowEntry flowEntryObj : deleteFlowEntries) { |
| 75 | flowObj.removeFlowEntry(flowEntryObj); |
| 76 | dbHandler.removeFlowEntry(flowEntryObj); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 81 | // Set the Flow key: |
| 82 | // - flowId |
| 83 | // |
| 84 | flowObj.setFlowId(flowPath.flowId().toString()); |
| 85 | flowObj.setType("flow"); |
| 86 | |
| 87 | // |
| 88 | // Set the Flow attributes: |
| 89 | // - flowPath.installerId() |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 90 | // - flowPath.flowPathType() |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 91 | // - flowPath.flowPathUserState() |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 92 | // - flowPath.flowPathFlags() |
| 93 | // - flowPath.dataPath().srcPort() |
| 94 | // - flowPath.dataPath().dstPort() |
| 95 | // - flowPath.matchSrcMac() |
| 96 | // - flowPath.matchDstMac() |
| 97 | // - flowPath.matchEthernetFrameType() |
| 98 | // - flowPath.matchVlanId() |
| 99 | // - flowPath.matchVlanPriority() |
| 100 | // - flowPath.matchSrcIPv4Net() |
| 101 | // - flowPath.matchDstIPv4Net() |
| 102 | // - flowPath.matchIpProto() |
| 103 | // - flowPath.matchIpToS() |
| 104 | // - flowPath.matchSrcTcpUdpPort() |
| 105 | // - flowPath.matchDstTcpUdpPort() |
| 106 | // - flowPath.flowEntryActions() |
| 107 | // |
| 108 | flowObj.setInstallerId(flowPath.installerId().toString()); |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 109 | flowObj.setFlowPathType(flowPath.flowPathType().toString()); |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 110 | flowObj.setFlowPathUserState(flowPath.flowPathUserState().toString()); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 111 | flowObj.setFlowPathFlags(flowPath.flowPathFlags().flags()); |
| 112 | flowObj.setSrcSwitch(flowPath.dataPath().srcPort().dpid().toString()); |
| 113 | flowObj.setSrcPort(flowPath.dataPath().srcPort().port().value()); |
| 114 | flowObj.setDstSwitch(flowPath.dataPath().dstPort().dpid().toString()); |
| 115 | flowObj.setDstPort(flowPath.dataPath().dstPort().port().value()); |
| 116 | if (flowPath.flowEntryMatch().matchSrcMac()) { |
| 117 | flowObj.setMatchSrcMac(flowPath.flowEntryMatch().srcMac().toString()); |
| 118 | } |
| 119 | if (flowPath.flowEntryMatch().matchDstMac()) { |
| 120 | flowObj.setMatchDstMac(flowPath.flowEntryMatch().dstMac().toString()); |
| 121 | } |
| 122 | if (flowPath.flowEntryMatch().matchEthernetFrameType()) { |
| 123 | flowObj.setMatchEthernetFrameType(flowPath.flowEntryMatch().ethernetFrameType()); |
| 124 | } |
| 125 | if (flowPath.flowEntryMatch().matchVlanId()) { |
| 126 | flowObj.setMatchVlanId(flowPath.flowEntryMatch().vlanId()); |
| 127 | } |
| 128 | if (flowPath.flowEntryMatch().matchVlanPriority()) { |
| 129 | flowObj.setMatchVlanPriority(flowPath.flowEntryMatch().vlanPriority()); |
| 130 | } |
| 131 | if (flowPath.flowEntryMatch().matchSrcIPv4Net()) { |
| 132 | flowObj.setMatchSrcIPv4Net(flowPath.flowEntryMatch().srcIPv4Net().toString()); |
| 133 | } |
| 134 | if (flowPath.flowEntryMatch().matchDstIPv4Net()) { |
| 135 | flowObj.setMatchDstIPv4Net(flowPath.flowEntryMatch().dstIPv4Net().toString()); |
| 136 | } |
| 137 | if (flowPath.flowEntryMatch().matchIpProto()) { |
| 138 | flowObj.setMatchIpProto(flowPath.flowEntryMatch().ipProto()); |
| 139 | } |
| 140 | if (flowPath.flowEntryMatch().matchIpToS()) { |
| 141 | flowObj.setMatchIpToS(flowPath.flowEntryMatch().ipToS()); |
| 142 | } |
| 143 | if (flowPath.flowEntryMatch().matchSrcTcpUdpPort()) { |
| 144 | flowObj.setMatchSrcTcpUdpPort(flowPath.flowEntryMatch().srcTcpUdpPort()); |
| 145 | } |
| 146 | if (flowPath.flowEntryMatch().matchDstTcpUdpPort()) { |
| 147 | flowObj.setMatchDstTcpUdpPort(flowPath.flowEntryMatch().dstTcpUdpPort()); |
| 148 | } |
| 149 | if (! flowPath.flowEntryActions().actions().isEmpty()) { |
| 150 | flowObj.setActions(flowPath.flowEntryActions().toString()); |
| 151 | } |
Pavlin Radoslavov | bcc86ef | 2013-10-26 12:06:25 -0700 | [diff] [blame] | 152 | flowObj.setDataPathSummary(flowPath.dataPath().dataPathSummary()); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 153 | |
| 154 | if (found) |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 155 | flowObj.setFlowPathUserState("FP_USER_MODIFY"); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 156 | else |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 157 | flowObj.setFlowPathUserState("FP_USER_ADD"); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 158 | |
| 159 | // Flow edges: |
| 160 | // HeadFE |
| 161 | |
| 162 | |
| 163 | // |
| 164 | // Flow Entries: |
| 165 | // flowPath.dataPath().flowEntries() |
| 166 | // |
| 167 | for (FlowEntry flowEntry : flowPath.dataPath().flowEntries()) { |
Pavlin Radoslavov | 2fca8d1 | 2013-12-04 09:39:06 -0800 | [diff] [blame] | 168 | if (flowEntry.flowEntryUserState() == FlowEntryUserState.FE_USER_DELETE) |
| 169 | continue; // Skip: all Flow Entries were deleted earlier |
| 170 | |
Pavlin Radoslavov | 67bf762 | 2013-12-04 12:28:23 -0800 | [diff] [blame] | 171 | if (addFlowEntry(dbHandler, flowObj, flowEntry) == null) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 172 | dbHandler.rollback(); |
| 173 | return false; |
| 174 | } |
| 175 | } |
| 176 | dbHandler.commit(); |
| 177 | |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Add a flow entry to the Network MAP. |
| 183 | * |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 184 | * @param dbHandler the Graph Database handler to use. |
| 185 | * @param flowObj the corresponding Flow Path object for the Flow Entry. |
| 186 | * @param flowEntry the Flow Entry to install. |
| 187 | * @return the added Flow Entry object on success, otherwise null. |
| 188 | */ |
Pavlin Radoslavov | 67bf762 | 2013-12-04 12:28:23 -0800 | [diff] [blame] | 189 | static IFlowEntry addFlowEntry(GraphDBOperation dbHandler, |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 190 | IFlowPath flowObj, |
| 191 | FlowEntry flowEntry) { |
| 192 | // Flow edges |
| 193 | // HeadFE (TODO) |
| 194 | |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 195 | IFlowEntry flowEntryObj = null; |
| 196 | boolean found = false; |
| 197 | try { |
| 198 | if ((flowEntryObj = |
| 199 | dbHandler.searchFlowEntry(flowEntry.flowEntryId())) != null) { |
| 200 | found = true; |
| 201 | } else { |
| 202 | flowEntryObj = dbHandler.newFlowEntry(); |
| 203 | } |
| 204 | } catch (Exception e) { |
| 205 | log.error(":addFlow FlowEntryId:{} failed", |
| 206 | flowEntry.flowEntryId().toString()); |
| 207 | return null; |
| 208 | } |
| 209 | if (flowEntryObj == null) { |
| 210 | log.error(":addFlow FlowEntryId:{} failed: FlowEntry object not created", |
| 211 | flowEntry.flowEntryId().toString()); |
| 212 | return null; |
| 213 | } |
| 214 | |
| 215 | // |
| 216 | // Set the Flow Entry key: |
| 217 | // - flowEntry.flowEntryId() |
| 218 | // |
| 219 | flowEntryObj.setFlowEntryId(flowEntry.flowEntryId().toString()); |
| 220 | flowEntryObj.setType("flow_entry"); |
| 221 | |
| 222 | // |
| 223 | // Set the Flow Entry Edges and attributes: |
| 224 | // - Switch edge |
| 225 | // - InPort edge |
| 226 | // - OutPort edge |
| 227 | // |
| 228 | // - flowEntry.dpid() |
| 229 | // - flowEntry.flowEntryUserState() |
| 230 | // - flowEntry.flowEntrySwitchState() |
| 231 | // - flowEntry.flowEntryErrorState() |
| 232 | // - flowEntry.matchInPort() |
| 233 | // - flowEntry.matchSrcMac() |
| 234 | // - flowEntry.matchDstMac() |
| 235 | // - flowEntry.matchEthernetFrameType() |
| 236 | // - flowEntry.matchVlanId() |
| 237 | // - flowEntry.matchVlanPriority() |
| 238 | // - flowEntry.matchSrcIPv4Net() |
| 239 | // - flowEntry.matchDstIPv4Net() |
| 240 | // - flowEntry.matchIpProto() |
| 241 | // - flowEntry.matchIpToS() |
| 242 | // - flowEntry.matchSrcTcpUdpPort() |
| 243 | // - flowEntry.matchDstTcpUdpPort() |
| 244 | // - flowEntry.actionOutputPort() |
| 245 | // - flowEntry.actions() |
| 246 | // |
| 247 | ISwitchObject sw = dbHandler.searchSwitch(flowEntry.dpid().toString()); |
| 248 | flowEntryObj.setSwitchDpid(flowEntry.dpid().toString()); |
| 249 | flowEntryObj.setSwitch(sw); |
| 250 | if (flowEntry.flowEntryMatch().matchInPort()) { |
Jonathan Hart | 5b3ad19 | 2013-12-06 17:34:46 -0800 | [diff] [blame] | 251 | IPortObject inport = |
| 252 | dbHandler.searchPort(flowEntry.dpid().toString(), |
| 253 | flowEntry.flowEntryMatch().inPort().value()); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 254 | flowEntryObj.setMatchInPort(flowEntry.flowEntryMatch().inPort().value()); |
Jonathan Hart | 5b3ad19 | 2013-12-06 17:34:46 -0800 | [diff] [blame] | 255 | flowEntryObj.setInPort(inport); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 256 | } |
| 257 | if (flowEntry.flowEntryMatch().matchSrcMac()) { |
| 258 | flowEntryObj.setMatchSrcMac(flowEntry.flowEntryMatch().srcMac().toString()); |
| 259 | } |
| 260 | if (flowEntry.flowEntryMatch().matchDstMac()) { |
| 261 | flowEntryObj.setMatchDstMac(flowEntry.flowEntryMatch().dstMac().toString()); |
| 262 | } |
| 263 | if (flowEntry.flowEntryMatch().matchEthernetFrameType()) { |
| 264 | flowEntryObj.setMatchEthernetFrameType(flowEntry.flowEntryMatch().ethernetFrameType()); |
| 265 | } |
| 266 | if (flowEntry.flowEntryMatch().matchVlanId()) { |
| 267 | flowEntryObj.setMatchVlanId(flowEntry.flowEntryMatch().vlanId()); |
| 268 | } |
| 269 | if (flowEntry.flowEntryMatch().matchVlanPriority()) { |
| 270 | flowEntryObj.setMatchVlanPriority(flowEntry.flowEntryMatch().vlanPriority()); |
| 271 | } |
| 272 | if (flowEntry.flowEntryMatch().matchSrcIPv4Net()) { |
| 273 | flowEntryObj.setMatchSrcIPv4Net(flowEntry.flowEntryMatch().srcIPv4Net().toString()); |
| 274 | } |
| 275 | if (flowEntry.flowEntryMatch().matchDstIPv4Net()) { |
| 276 | flowEntryObj.setMatchDstIPv4Net(flowEntry.flowEntryMatch().dstIPv4Net().toString()); |
| 277 | } |
| 278 | if (flowEntry.flowEntryMatch().matchIpProto()) { |
| 279 | flowEntryObj.setMatchIpProto(flowEntry.flowEntryMatch().ipProto()); |
| 280 | } |
| 281 | if (flowEntry.flowEntryMatch().matchIpToS()) { |
| 282 | flowEntryObj.setMatchIpToS(flowEntry.flowEntryMatch().ipToS()); |
| 283 | } |
| 284 | if (flowEntry.flowEntryMatch().matchSrcTcpUdpPort()) { |
| 285 | flowEntryObj.setMatchSrcTcpUdpPort(flowEntry.flowEntryMatch().srcTcpUdpPort()); |
| 286 | } |
| 287 | if (flowEntry.flowEntryMatch().matchDstTcpUdpPort()) { |
| 288 | flowEntryObj.setMatchDstTcpUdpPort(flowEntry.flowEntryMatch().dstTcpUdpPort()); |
| 289 | } |
| 290 | |
| 291 | for (FlowEntryAction fa : flowEntry.flowEntryActions().actions()) { |
| 292 | if (fa.actionOutput() != null) { |
Jonathan Hart | 5b3ad19 | 2013-12-06 17:34:46 -0800 | [diff] [blame] | 293 | IPortObject outport = |
| 294 | dbHandler.searchPort(flowEntry.dpid().toString(), |
| 295 | fa.actionOutput().port().value()); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 296 | flowEntryObj.setActionOutputPort(fa.actionOutput().port().value()); |
Jonathan Hart | 5b3ad19 | 2013-12-06 17:34:46 -0800 | [diff] [blame] | 297 | flowEntryObj.setOutPort(outport); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | if (! flowEntry.flowEntryActions().isEmpty()) { |
| 301 | flowEntryObj.setActions(flowEntry.flowEntryActions().toString()); |
| 302 | } |
| 303 | |
| 304 | // TODO: Hacks with hard-coded state names! |
| 305 | if (found) |
| 306 | flowEntryObj.setUserState("FE_USER_MODIFY"); |
| 307 | else |
| 308 | flowEntryObj.setUserState("FE_USER_ADD"); |
Pavlin Radoslavov | ebc8b19 | 2013-10-29 15:35:35 -0700 | [diff] [blame] | 309 | flowEntryObj.setSwitchState(flowEntry.flowEntrySwitchState().toString()); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 310 | // |
| 311 | // TODO: Take care of the FlowEntryErrorState. |
| 312 | // |
| 313 | |
| 314 | // Flow Entries edges: |
| 315 | // Flow |
| 316 | // NextFE (TODO) |
| 317 | if (! found) { |
| 318 | flowObj.addFlowEntry(flowEntryObj); |
| 319 | flowEntryObj.setFlow(flowObj); |
| 320 | } |
| 321 | |
| 322 | return flowEntryObj; |
| 323 | } |
| 324 | |
| 325 | /** |
Pavlin Radoslavov | 7407ab5 | 2013-11-01 22:19:00 -0700 | [diff] [blame] | 326 | * Delete a flow entry from the Network MAP. |
| 327 | * |
| 328 | * @param dbHandler the Graph Database handler to use. |
| 329 | * @param flowObj the corresponding Flow Path object for the Flow Entry. |
| 330 | * @param flowEntry the Flow Entry to delete. |
| 331 | * @return true on success, otherwise false. |
| 332 | */ |
| 333 | static boolean deleteFlowEntry(GraphDBOperation dbHandler, |
| 334 | IFlowPath flowObj, |
| 335 | FlowEntry flowEntry) { |
| 336 | IFlowEntry flowEntryObj = null; |
| 337 | try { |
| 338 | flowEntryObj = dbHandler.searchFlowEntry(flowEntry.flowEntryId()); |
| 339 | } catch (Exception e) { |
| 340 | log.error(":deleteFlowEntry FlowEntryId:{} failed", |
| 341 | flowEntry.flowEntryId().toString()); |
| 342 | return false; |
| 343 | } |
| 344 | // |
| 345 | // TODO: Don't print an error for now, because multiple controller |
| 346 | // instances might be deleting the same flow entry. |
| 347 | // |
| 348 | /* |
| 349 | if (flowEntryObj == null) { |
| 350 | log.error(":deleteFlowEntry FlowEntryId:{} failed: FlowEntry object not found", |
| 351 | flowEntry.flowEntryId().toString()); |
| 352 | return false; |
| 353 | } |
| 354 | */ |
| 355 | if (flowEntryObj == null) |
| 356 | return true; |
| 357 | |
| 358 | flowObj.removeFlowEntry(flowEntryObj); |
| 359 | dbHandler.removeFlowEntry(flowEntryObj); |
| 360 | return true; |
| 361 | } |
| 362 | |
| 363 | /** |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 364 | * Delete all previously added flows. |
| 365 | * |
| 366 | * @param dbHandler the Graph Database handler to use. |
| 367 | * @return true on success, otherwise false. |
| 368 | */ |
| 369 | static boolean deleteAllFlows(GraphDBOperation dbHandler) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 370 | List<FlowId> allFlowIds = new LinkedList<FlowId>(); |
| 371 | |
| 372 | // Get all Flow IDs |
| 373 | Iterable<IFlowPath> allFlowPaths = dbHandler.getAllFlowPaths(); |
| 374 | for (IFlowPath flowPathObj : allFlowPaths) { |
| 375 | if (flowPathObj == null) |
| 376 | continue; |
| 377 | String flowIdStr = flowPathObj.getFlowId(); |
| 378 | if (flowIdStr == null) |
| 379 | continue; |
| 380 | FlowId flowId = new FlowId(flowIdStr); |
| 381 | allFlowIds.add(flowId); |
| 382 | } |
| 383 | |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 384 | // Delete all flows one-by-one |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 385 | for (FlowId flowId : allFlowIds) { |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 386 | deleteFlow(dbHandler, flowId); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | return true; |
| 390 | } |
| 391 | |
| 392 | /** |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 393 | * Delete a previously added flow. |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 394 | * |
| 395 | * @param dbHandler the Graph Database handler to use. |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 396 | * @param flowId the Flow ID of the flow to delete. |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 397 | * @return true on success, otherwise false. |
| 398 | */ |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 399 | static boolean deleteFlow(GraphDBOperation dbHandler, FlowId flowId) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 400 | IFlowPath flowObj = null; |
| 401 | try { |
| 402 | flowObj = dbHandler.searchFlowPath(flowId); |
| 403 | } catch (Exception e) { |
| 404 | // TODO: handle exceptions |
| 405 | dbHandler.rollback(); |
Pavlin Radoslavov | f2a5265 | 2013-11-22 12:35:42 -0800 | [diff] [blame] | 406 | log.error(":deleteFlow FlowId:{} failed", flowId.toString()); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 407 | return false; |
| 408 | } |
| 409 | if (flowObj == null) { |
| 410 | dbHandler.commit(); |
| 411 | return true; // OK: No such flow |
| 412 | } |
| 413 | |
| 414 | // |
| 415 | // Remove all Flow Entries |
| 416 | // |
| 417 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 418 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 419 | flowObj.removeFlowEntry(flowEntryObj); |
| 420 | dbHandler.removeFlowEntry(flowEntryObj); |
| 421 | } |
| 422 | // Remove the Flow itself |
| 423 | dbHandler.removeFlowPath(flowObj); |
| 424 | dbHandler.commit(); |
| 425 | |
| 426 | return true; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Get a previously added flow. |
| 431 | * |
| 432 | * @param dbHandler the Graph Database handler to use. |
| 433 | * @param flowId the Flow ID of the flow to get. |
| 434 | * @return the Flow Path if found, otherwise null. |
| 435 | */ |
| 436 | static FlowPath getFlow(GraphDBOperation dbHandler, FlowId flowId) { |
| 437 | IFlowPath flowObj = null; |
| 438 | try { |
| 439 | flowObj = dbHandler.searchFlowPath(flowId); |
| 440 | } catch (Exception e) { |
| 441 | // TODO: handle exceptions |
| 442 | dbHandler.rollback(); |
| 443 | log.error(":getFlow FlowId:{} failed", flowId.toString()); |
| 444 | return null; |
| 445 | } |
| 446 | if (flowObj == null) { |
| 447 | dbHandler.commit(); |
| 448 | return null; // Flow not found |
| 449 | } |
| 450 | |
| 451 | // |
| 452 | // Extract the Flow state |
| 453 | // |
| 454 | FlowPath flowPath = extractFlowPath(flowObj); |
| 455 | dbHandler.commit(); |
| 456 | |
| 457 | return flowPath; |
| 458 | } |
| 459 | |
| 460 | /** |
| 461 | * Get all installed flows by all installers. |
| 462 | * |
| 463 | * @param dbHandler the Graph Database handler to use. |
| 464 | * @return the Flow Paths if found, otherwise null. |
| 465 | */ |
| 466 | static ArrayList<FlowPath> getAllFlows(GraphDBOperation dbHandler) { |
| 467 | Iterable<IFlowPath> flowPathsObj = null; |
| 468 | ArrayList<FlowPath> flowPaths = new ArrayList<FlowPath>(); |
| 469 | |
| 470 | try { |
| 471 | flowPathsObj = dbHandler.getAllFlowPaths(); |
| 472 | } catch (Exception e) { |
| 473 | // TODO: handle exceptions |
| 474 | dbHandler.rollback(); |
| 475 | log.error(":getAllFlowPaths failed"); |
| 476 | return flowPaths; |
| 477 | } |
| 478 | if ((flowPathsObj == null) || (flowPathsObj.iterator().hasNext() == false)) { |
| 479 | dbHandler.commit(); |
| 480 | return flowPaths; // No Flows found |
| 481 | } |
| 482 | |
| 483 | for (IFlowPath flowObj : flowPathsObj) { |
| 484 | // |
| 485 | // Extract the Flow state |
| 486 | // |
| 487 | FlowPath flowPath = extractFlowPath(flowObj); |
| 488 | if (flowPath != null) |
| 489 | flowPaths.add(flowPath); |
| 490 | } |
| 491 | |
| 492 | dbHandler.commit(); |
| 493 | |
| 494 | return flowPaths; |
| 495 | } |
| 496 | |
| 497 | /** |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 498 | * Extract Flow Path State from a Titan Database Object @ref IFlowPath. |
| 499 | * |
| 500 | * @param flowObj the object to extract the Flow Path State from. |
| 501 | * @return the extracted Flow Path State. |
| 502 | */ |
| 503 | private static FlowPath extractFlowPath(IFlowPath flowObj) { |
| 504 | // |
| 505 | // Extract the Flow state |
| 506 | // |
| 507 | String flowIdStr = flowObj.getFlowId(); |
| 508 | String installerIdStr = flowObj.getInstallerId(); |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 509 | String flowPathType = flowObj.getFlowPathType(); |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 510 | String flowPathUserState = flowObj.getFlowPathUserState(); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 511 | Long flowPathFlags = flowObj.getFlowPathFlags(); |
| 512 | String srcSwitchStr = flowObj.getSrcSwitch(); |
| 513 | Short srcPortShort = flowObj.getSrcPort(); |
| 514 | String dstSwitchStr = flowObj.getDstSwitch(); |
| 515 | Short dstPortShort = flowObj.getDstPort(); |
| 516 | |
| 517 | if ((flowIdStr == null) || |
| 518 | (installerIdStr == null) || |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 519 | (flowPathType == null) || |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 520 | (flowPathUserState == null) || |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 521 | (flowPathFlags == null) || |
| 522 | (srcSwitchStr == null) || |
| 523 | (srcPortShort == null) || |
| 524 | (dstSwitchStr == null) || |
| 525 | (dstPortShort == null)) { |
| 526 | // TODO: A work-around, becauuse of some bogus database objects |
| 527 | return null; |
| 528 | } |
| 529 | |
| 530 | FlowPath flowPath = new FlowPath(); |
| 531 | flowPath.setFlowId(new FlowId(flowIdStr)); |
| 532 | flowPath.setInstallerId(new CallerId(installerIdStr)); |
Pavlin Radoslavov | d28cf7c | 2013-10-26 11:27:43 -0700 | [diff] [blame] | 533 | flowPath.setFlowPathType(FlowPathType.valueOf(flowPathType)); |
Pavlin Radoslavov | 7d4a40e | 2013-10-27 23:39:40 -0700 | [diff] [blame] | 534 | flowPath.setFlowPathUserState(FlowPathUserState.valueOf(flowPathUserState)); |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 535 | flowPath.setFlowPathFlags(new FlowPathFlags(flowPathFlags)); |
| 536 | flowPath.dataPath().srcPort().setDpid(new Dpid(srcSwitchStr)); |
| 537 | flowPath.dataPath().srcPort().setPort(new Port(srcPortShort)); |
| 538 | flowPath.dataPath().dstPort().setDpid(new Dpid(dstSwitchStr)); |
| 539 | flowPath.dataPath().dstPort().setPort(new Port(dstPortShort)); |
| 540 | // |
| 541 | // Extract the match conditions common for all Flow Entries |
| 542 | // |
| 543 | { |
| 544 | FlowEntryMatch match = new FlowEntryMatch(); |
| 545 | String matchSrcMac = flowObj.getMatchSrcMac(); |
| 546 | if (matchSrcMac != null) |
| 547 | match.enableSrcMac(MACAddress.valueOf(matchSrcMac)); |
| 548 | String matchDstMac = flowObj.getMatchDstMac(); |
| 549 | if (matchDstMac != null) |
| 550 | match.enableDstMac(MACAddress.valueOf(matchDstMac)); |
| 551 | Short matchEthernetFrameType = flowObj.getMatchEthernetFrameType(); |
| 552 | if (matchEthernetFrameType != null) |
| 553 | match.enableEthernetFrameType(matchEthernetFrameType); |
| 554 | Short matchVlanId = flowObj.getMatchVlanId(); |
| 555 | if (matchVlanId != null) |
| 556 | match.enableVlanId(matchVlanId); |
| 557 | Byte matchVlanPriority = flowObj.getMatchVlanPriority(); |
| 558 | if (matchVlanPriority != null) |
| 559 | match.enableVlanPriority(matchVlanPriority); |
| 560 | String matchSrcIPv4Net = flowObj.getMatchSrcIPv4Net(); |
| 561 | if (matchSrcIPv4Net != null) |
| 562 | match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net)); |
| 563 | String matchDstIPv4Net = flowObj.getMatchDstIPv4Net(); |
| 564 | if (matchDstIPv4Net != null) |
| 565 | match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net)); |
| 566 | Byte matchIpProto = flowObj.getMatchIpProto(); |
| 567 | if (matchIpProto != null) |
| 568 | match.enableIpProto(matchIpProto); |
| 569 | Byte matchIpToS = flowObj.getMatchIpToS(); |
| 570 | if (matchIpToS != null) |
| 571 | match.enableIpToS(matchIpToS); |
| 572 | Short matchSrcTcpUdpPort = flowObj.getMatchSrcTcpUdpPort(); |
| 573 | if (matchSrcTcpUdpPort != null) |
| 574 | match.enableSrcTcpUdpPort(matchSrcTcpUdpPort); |
| 575 | Short matchDstTcpUdpPort = flowObj.getMatchDstTcpUdpPort(); |
| 576 | if (matchDstTcpUdpPort != null) |
| 577 | match.enableDstTcpUdpPort(matchDstTcpUdpPort); |
| 578 | |
| 579 | flowPath.setFlowEntryMatch(match); |
| 580 | } |
| 581 | // |
| 582 | // Extract the actions for the first Flow Entry |
| 583 | // |
| 584 | { |
| 585 | String actionsStr = flowObj.getActions(); |
| 586 | if (actionsStr != null) { |
| 587 | FlowEntryActions flowEntryActions = new FlowEntryActions(actionsStr); |
| 588 | flowPath.setFlowEntryActions(flowEntryActions); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | // |
| 593 | // Extract all Flow Entries |
| 594 | // |
| 595 | Iterable<IFlowEntry> flowEntries = flowObj.getFlowEntries(); |
| 596 | for (IFlowEntry flowEntryObj : flowEntries) { |
| 597 | FlowEntry flowEntry = extractFlowEntry(flowEntryObj); |
| 598 | if (flowEntry == null) |
| 599 | continue; |
| 600 | flowPath.dataPath().flowEntries().add(flowEntry); |
| 601 | } |
| 602 | |
| 603 | return flowPath; |
| 604 | } |
| 605 | |
| 606 | /** |
| 607 | * Extract Flow Entry State from a Titan Database Object @ref IFlowEntry. |
| 608 | * |
| 609 | * @param flowEntryObj the object to extract the Flow Entry State from. |
| 610 | * @return the extracted Flow Entry State. |
| 611 | */ |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 612 | public static FlowEntry extractFlowEntry(IFlowEntry flowEntryObj) { |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 613 | String flowEntryIdStr = flowEntryObj.getFlowEntryId(); |
| 614 | String switchDpidStr = flowEntryObj.getSwitchDpid(); |
| 615 | String userState = flowEntryObj.getUserState(); |
| 616 | String switchState = flowEntryObj.getSwitchState(); |
| 617 | |
| 618 | if ((flowEntryIdStr == null) || |
| 619 | (switchDpidStr == null) || |
| 620 | (userState == null) || |
| 621 | (switchState == null)) { |
Brian O'Connor | a8e4980 | 2013-10-30 20:49:59 -0700 | [diff] [blame] | 622 | // TODO: A work-around, because of some bogus database objects |
Pavlin Radoslavov | 661c86f | 2013-10-21 12:40:40 -0700 | [diff] [blame] | 623 | return null; |
| 624 | } |
| 625 | |
| 626 | FlowEntry flowEntry = new FlowEntry(); |
| 627 | flowEntry.setFlowEntryId(new FlowEntryId(flowEntryIdStr)); |
| 628 | flowEntry.setDpid(new Dpid(switchDpidStr)); |
| 629 | |
| 630 | // |
| 631 | // Extract the match conditions |
| 632 | // |
| 633 | FlowEntryMatch match = new FlowEntryMatch(); |
| 634 | Short matchInPort = flowEntryObj.getMatchInPort(); |
| 635 | if (matchInPort != null) |
| 636 | match.enableInPort(new Port(matchInPort)); |
| 637 | String matchSrcMac = flowEntryObj.getMatchSrcMac(); |
| 638 | if (matchSrcMac != null) |
| 639 | match.enableSrcMac(MACAddress.valueOf(matchSrcMac)); |
| 640 | String matchDstMac = flowEntryObj.getMatchDstMac(); |
| 641 | if (matchDstMac != null) |
| 642 | match.enableDstMac(MACAddress.valueOf(matchDstMac)); |
| 643 | Short matchEthernetFrameType = flowEntryObj.getMatchEthernetFrameType(); |
| 644 | if (matchEthernetFrameType != null) |
| 645 | match.enableEthernetFrameType(matchEthernetFrameType); |
| 646 | Short matchVlanId = flowEntryObj.getMatchVlanId(); |
| 647 | if (matchVlanId != null) |
| 648 | match.enableVlanId(matchVlanId); |
| 649 | Byte matchVlanPriority = flowEntryObj.getMatchVlanPriority(); |
| 650 | if (matchVlanPriority != null) |
| 651 | match.enableVlanPriority(matchVlanPriority); |
| 652 | String matchSrcIPv4Net = flowEntryObj.getMatchSrcIPv4Net(); |
| 653 | if (matchSrcIPv4Net != null) |
| 654 | match.enableSrcIPv4Net(new IPv4Net(matchSrcIPv4Net)); |
| 655 | String matchDstIPv4Net = flowEntryObj.getMatchDstIPv4Net(); |
| 656 | if (matchDstIPv4Net != null) |
| 657 | match.enableDstIPv4Net(new IPv4Net(matchDstIPv4Net)); |
| 658 | Byte matchIpProto = flowEntryObj.getMatchIpProto(); |
| 659 | if (matchIpProto != null) |
| 660 | match.enableIpProto(matchIpProto); |
| 661 | Byte matchIpToS = flowEntryObj.getMatchIpToS(); |
| 662 | if (matchIpToS != null) |
| 663 | match.enableIpToS(matchIpToS); |
| 664 | Short matchSrcTcpUdpPort = flowEntryObj.getMatchSrcTcpUdpPort(); |
| 665 | if (matchSrcTcpUdpPort != null) |
| 666 | match.enableSrcTcpUdpPort(matchSrcTcpUdpPort); |
| 667 | Short matchDstTcpUdpPort = flowEntryObj.getMatchDstTcpUdpPort(); |
| 668 | if (matchDstTcpUdpPort != null) |
| 669 | match.enableDstTcpUdpPort(matchDstTcpUdpPort); |
| 670 | flowEntry.setFlowEntryMatch(match); |
| 671 | |
| 672 | // |
| 673 | // Extract the actions |
| 674 | // |
| 675 | FlowEntryActions actions = new FlowEntryActions(); |
| 676 | String actionsStr = flowEntryObj.getActions(); |
| 677 | if (actionsStr != null) |
| 678 | actions = new FlowEntryActions(actionsStr); |
| 679 | flowEntry.setFlowEntryActions(actions); |
| 680 | flowEntry.setFlowEntryUserState(FlowEntryUserState.valueOf(userState)); |
| 681 | flowEntry.setFlowEntrySwitchState(FlowEntrySwitchState.valueOf(switchState)); |
| 682 | // |
| 683 | // TODO: Take care of FlowEntryErrorState. |
| 684 | // |
| 685 | return flowEntry; |
| 686 | } |
| 687 | } |