sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.provider.of.group.impl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 19 | |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 20 | import org.onlab.packet.Ip4Address; |
| 21 | import org.onlab.packet.MacAddress; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 22 | import org.onlab.packet.MplsLabel; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 23 | import org.onlab.packet.VlanId; |
| 24 | import org.onosproject.core.DefaultGroupId; |
| 25 | import org.onosproject.core.GroupId; |
| 26 | import org.onosproject.net.PortNumber; |
| 27 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 28 | import org.onosproject.net.flow.TrafficTreatment; |
| 29 | import org.onosproject.net.group.DefaultGroupBucket; |
| 30 | import org.onosproject.net.group.GroupBucket; |
| 31 | import org.onosproject.net.group.GroupBuckets; |
| 32 | import org.projectfloodlight.openflow.protocol.OFBucket; |
| 33 | import org.projectfloodlight.openflow.protocol.OFGroupType; |
| 34 | import org.projectfloodlight.openflow.protocol.action.OFAction; |
| 35 | import org.projectfloodlight.openflow.protocol.action.OFActionCircuit; |
| 36 | import org.projectfloodlight.openflow.protocol.action.OFActionCopyTtlIn; |
| 37 | import org.projectfloodlight.openflow.protocol.action.OFActionCopyTtlOut; |
| 38 | import org.projectfloodlight.openflow.protocol.action.OFActionDecMplsTtl; |
| 39 | import org.projectfloodlight.openflow.protocol.action.OFActionDecNwTtl; |
| 40 | import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter; |
Saurav Das | 8832990 | 2015-05-27 23:22:32 -0700 | [diff] [blame] | 41 | import org.projectfloodlight.openflow.protocol.action.OFActionGroup; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 42 | import org.projectfloodlight.openflow.protocol.action.OFActionOutput; |
| 43 | import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls; |
| 44 | import org.projectfloodlight.openflow.protocol.action.OFActionPushMpls; |
| 45 | import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst; |
| 46 | import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc; |
| 47 | import org.projectfloodlight.openflow.protocol.action.OFActionSetField; |
| 48 | import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst; |
| 49 | import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc; |
| 50 | import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp; |
| 51 | import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid; |
| 52 | import org.projectfloodlight.openflow.protocol.oxm.OFOxm; |
| 53 | import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic; |
| 54 | import org.projectfloodlight.openflow.types.IPv4Address; |
| 55 | import org.projectfloodlight.openflow.types.OFVlanVidMatch; |
| 56 | import org.projectfloodlight.openflow.types.U32; |
| 57 | import org.projectfloodlight.openflow.types.VlanPcp; |
| 58 | import org.slf4j.Logger; |
| 59 | |
| 60 | import java.util.List; |
| 61 | |
| 62 | import static org.slf4j.LoggerFactory.getLogger; |
| 63 | |
| 64 | /* |
| 65 | * Builder for GroupBucketEntry. |
| 66 | */ |
| 67 | public class GroupBucketEntryBuilder { |
| 68 | |
| 69 | private List<OFBucket> ofBuckets; |
| 70 | private OFGroupType type; |
| 71 | |
| 72 | private final Logger log = getLogger(getClass()); |
| 73 | |
| 74 | /** |
| 75 | * Creates a builder. |
| 76 | * |
| 77 | * @param ofBuckets list of OFBucket |
| 78 | * @param type Group type |
| 79 | */ |
| 80 | public GroupBucketEntryBuilder(List<OFBucket> ofBuckets, OFGroupType type) { |
| 81 | this.ofBuckets = ofBuckets; |
| 82 | this.type = type; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Builds a GroupBuckets. |
| 87 | * |
Jonathan Hart | 8ef6d3b | 2015-03-08 21:21:27 -0700 | [diff] [blame] | 88 | * @return GroupBuckets object, a list of GroupBuckets |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 89 | */ |
| 90 | public GroupBuckets build() { |
| 91 | List<GroupBucket> bucketList = Lists.newArrayList(); |
| 92 | |
| 93 | for (OFBucket bucket: ofBuckets) { |
| 94 | TrafficTreatment treatment = buildTreatment(bucket.getActions()); |
| 95 | // TODO: Use GroupBucketEntry |
| 96 | GroupBucket groupBucket = null; |
| 97 | switch (type) { |
| 98 | case INDIRECT: |
| 99 | groupBucket = |
| 100 | DefaultGroupBucket.createIndirectGroupBucket(treatment); |
| 101 | break; |
| 102 | case SELECT: |
| 103 | groupBucket = |
| 104 | DefaultGroupBucket.createSelectGroupBucket(treatment); |
| 105 | break; |
| 106 | case FF: |
| 107 | PortNumber port = |
| 108 | PortNumber.portNumber(bucket.getWatchPort().getPortNumber()); |
| 109 | GroupId groupId = |
| 110 | new DefaultGroupId(bucket.getWatchGroup().getGroupNumber()); |
| 111 | groupBucket = |
| 112 | DefaultGroupBucket.createFailoverGroupBucket(treatment, |
| 113 | port, groupId); |
| 114 | break; |
| 115 | default: |
| 116 | log.error("Unsupported Group type : {}", type); |
| 117 | } |
| 118 | if (groupBucket != null) { |
| 119 | bucketList.add(groupBucket); |
| 120 | } |
| 121 | } |
| 122 | return new GroupBuckets(bucketList); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | private TrafficTreatment buildTreatment(List<OFAction> actions) { |
| 127 | TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder(); |
| 128 | // If this is a drop rule |
| 129 | if (actions.size() == 0) { |
| 130 | builder.drop(); |
| 131 | return builder.build(); |
| 132 | } |
| 133 | for (OFAction act : actions) { |
| 134 | switch (act.getType()) { |
| 135 | case OUTPUT: |
| 136 | OFActionOutput out = (OFActionOutput) act; |
| 137 | builder.setOutput( |
| 138 | PortNumber.portNumber(out.getPort().getPortNumber())); |
| 139 | break; |
| 140 | case SET_VLAN_VID: |
| 141 | OFActionSetVlanVid vlan = (OFActionSetVlanVid) act; |
| 142 | builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan())); |
| 143 | break; |
| 144 | case SET_VLAN_PCP: |
| 145 | OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act; |
| 146 | builder.setVlanPcp(pcp.getVlanPcp().getValue()); |
| 147 | break; |
Jonathan Hart | e106e4b | 2015-03-09 16:44:56 -0700 | [diff] [blame] | 148 | case POP_VLAN: |
| 149 | builder.popVlan(); |
| 150 | break; |
| 151 | case PUSH_VLAN: |
| 152 | builder.pushVlan(); |
| 153 | break; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 154 | case SET_DL_DST: |
| 155 | OFActionSetDlDst dldst = (OFActionSetDlDst) act; |
| 156 | builder.setEthDst( |
| 157 | MacAddress.valueOf(dldst.getDlAddr().getLong())); |
| 158 | break; |
| 159 | case SET_DL_SRC: |
| 160 | OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act; |
| 161 | builder.setEthSrc( |
| 162 | MacAddress.valueOf(dlsrc.getDlAddr().getLong())); |
| 163 | |
| 164 | break; |
| 165 | case SET_NW_DST: |
| 166 | OFActionSetNwDst nwdst = (OFActionSetNwDst) act; |
| 167 | IPv4Address di = nwdst.getNwAddr(); |
| 168 | builder.setIpDst(Ip4Address.valueOf(di.getInt())); |
| 169 | break; |
| 170 | case SET_NW_SRC: |
| 171 | OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act; |
| 172 | IPv4Address si = nwsrc.getNwAddr(); |
| 173 | builder.setIpSrc(Ip4Address.valueOf(si.getInt())); |
| 174 | break; |
| 175 | case EXPERIMENTER: |
| 176 | OFActionExperimenter exp = (OFActionExperimenter) act; |
| 177 | if (exp.getExperimenter() == 0x80005A06 || |
| 178 | exp.getExperimenter() == 0x748771) { |
| 179 | OFActionCircuit ct = (OFActionCircuit) exp; |
| 180 | builder.setLambda(((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber()); |
| 181 | } else { |
| 182 | log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter()); |
| 183 | } |
| 184 | break; |
| 185 | case SET_FIELD: |
| 186 | OFActionSetField setField = (OFActionSetField) act; |
| 187 | handleSetField(builder, setField.getField()); |
| 188 | break; |
| 189 | case POP_MPLS: |
| 190 | OFActionPopMpls popMpls = (OFActionPopMpls) act; |
| 191 | builder.popMpls((short) popMpls.getEthertype().getValue()); |
| 192 | break; |
| 193 | case PUSH_MPLS: |
| 194 | OFActionPushMpls pushMpls = (OFActionPushMpls) act; |
| 195 | builder.pushMpls(); |
| 196 | break; |
| 197 | case COPY_TTL_IN: |
| 198 | OFActionCopyTtlIn copyTtlIn = (OFActionCopyTtlIn) act; |
| 199 | builder.copyTtlIn(); |
| 200 | break; |
| 201 | case COPY_TTL_OUT: |
| 202 | OFActionCopyTtlOut copyTtlOut = (OFActionCopyTtlOut) act; |
| 203 | builder.copyTtlOut(); |
| 204 | break; |
| 205 | case DEC_MPLS_TTL: |
| 206 | OFActionDecMplsTtl decMplsTtl = (OFActionDecMplsTtl) act; |
| 207 | builder.decMplsTtl(); |
| 208 | break; |
| 209 | case DEC_NW_TTL: |
| 210 | OFActionDecNwTtl decNwTtl = (OFActionDecNwTtl) act; |
| 211 | builder.decNwTtl(); |
| 212 | break; |
Saurav Das | 8832990 | 2015-05-27 23:22:32 -0700 | [diff] [blame] | 213 | case GROUP: |
| 214 | OFActionGroup grp = (OFActionGroup) act; |
| 215 | builder.group(new DefaultGroupId(grp.getGroup().getGroupNumber())); |
| 216 | break; |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 217 | case SET_TP_DST: |
| 218 | case SET_TP_SRC: |
| 219 | case POP_PBB: |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 220 | case PUSH_PBB: |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 221 | case SET_MPLS_LABEL: |
| 222 | case SET_MPLS_TC: |
| 223 | case SET_MPLS_TTL: |
| 224 | case SET_NW_ECN: |
| 225 | case SET_NW_TOS: |
| 226 | case SET_NW_TTL: |
| 227 | case SET_QUEUE: |
| 228 | case STRIP_VLAN: |
| 229 | case ENQUEUE: |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 230 | default: |
| 231 | log.warn("Action type {} not yet implemented.", act.getType()); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | return builder.build(); |
| 236 | } |
| 237 | |
| 238 | private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) { |
| 239 | switch (oxm.getMatchField().id) { |
| 240 | case VLAN_PCP: |
| 241 | @SuppressWarnings("unchecked") |
| 242 | OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm; |
| 243 | builder.setVlanPcp(vlanpcp.getValue().getValue()); |
| 244 | break; |
| 245 | case VLAN_VID: |
| 246 | @SuppressWarnings("unchecked") |
| 247 | OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm; |
| 248 | builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan())); |
| 249 | break; |
| 250 | case ETH_DST: |
| 251 | @SuppressWarnings("unchecked") |
| 252 | OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst = |
| 253 | (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm; |
| 254 | builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong())); |
| 255 | break; |
| 256 | case ETH_SRC: |
| 257 | @SuppressWarnings("unchecked") |
| 258 | OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc = |
| 259 | (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm; |
| 260 | builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong())); |
| 261 | break; |
| 262 | case IPV4_DST: |
| 263 | @SuppressWarnings("unchecked") |
| 264 | OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm; |
| 265 | builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt())); |
| 266 | break; |
| 267 | case IPV4_SRC: |
| 268 | @SuppressWarnings("unchecked") |
| 269 | OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm; |
| 270 | builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt())); |
| 271 | break; |
| 272 | case MPLS_LABEL: |
| 273 | @SuppressWarnings("unchecked") |
| 274 | OFOxm<U32> labelId = (OFOxm<U32>) oxm; |
Michele Santuari | 4b6019e | 2014-12-19 11:31:45 +0100 | [diff] [blame] | 275 | builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue())); |
sangho | 5afd02a | 2015-02-03 20:07:35 -0800 | [diff] [blame] | 276 | break; |
| 277 | case ARP_OP: |
| 278 | case ARP_SHA: |
| 279 | case ARP_SPA: |
| 280 | case ARP_THA: |
| 281 | case ARP_TPA: |
| 282 | case BSN_EGR_PORT_GROUP_ID: |
| 283 | case BSN_GLOBAL_VRF_ALLOWED: |
| 284 | case BSN_IN_PORTS_128: |
| 285 | case BSN_L3_DST_CLASS_ID: |
| 286 | case BSN_L3_INTERFACE_CLASS_ID: |
| 287 | case BSN_L3_SRC_CLASS_ID: |
| 288 | case BSN_LAG_ID: |
| 289 | case BSN_TCP_FLAGS: |
| 290 | case BSN_UDF0: |
| 291 | case BSN_UDF1: |
| 292 | case BSN_UDF2: |
| 293 | case BSN_UDF3: |
| 294 | case BSN_UDF4: |
| 295 | case BSN_UDF5: |
| 296 | case BSN_UDF6: |
| 297 | case BSN_UDF7: |
| 298 | case BSN_VLAN_XLATE_PORT_GROUP_ID: |
| 299 | case BSN_VRF: |
| 300 | case ETH_TYPE: |
| 301 | case ICMPV4_CODE: |
| 302 | case ICMPV4_TYPE: |
| 303 | case ICMPV6_CODE: |
| 304 | case ICMPV6_TYPE: |
| 305 | case IN_PHY_PORT: |
| 306 | case IN_PORT: |
| 307 | case IPV6_DST: |
| 308 | case IPV6_FLABEL: |
| 309 | case IPV6_ND_SLL: |
| 310 | case IPV6_ND_TARGET: |
| 311 | case IPV6_ND_TLL: |
| 312 | case IPV6_SRC: |
| 313 | case IP_DSCP: |
| 314 | case IP_ECN: |
| 315 | case IP_PROTO: |
| 316 | case METADATA: |
| 317 | case MPLS_TC: |
| 318 | case OCH_SIGID: |
| 319 | case OCH_SIGID_BASIC: |
| 320 | case OCH_SIGTYPE: |
| 321 | case OCH_SIGTYPE_BASIC: |
| 322 | case SCTP_DST: |
| 323 | case SCTP_SRC: |
| 324 | case TCP_DST: |
| 325 | case TCP_SRC: |
| 326 | case TUNNEL_ID: |
| 327 | case UDP_DST: |
| 328 | case UDP_SRC: |
| 329 | default: |
| 330 | log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id); |
| 331 | break; |
| 332 | } |
| 333 | } |
| 334 | } |