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