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