2 * Copyright 2015 Open Networking Laboratory
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.onosproject.provider.of.group.impl;
18 import com.google.common.collect.Lists;
20 import org.onlab.packet.Ip4Address;
21 import org.onlab.packet.MacAddress;
22 import org.onlab.packet.MplsLabel;
23 import org.onlab.packet.VlanId;
24 import org.onosproject.core.DefaultGroupId;
25 import org.onosproject.core.GroupId;
26 import org.onosproject.net.Lambda;
27 import org.onosproject.net.PortNumber;
28 import org.onosproject.net.flow.DefaultTrafficTreatment;
29 import org.onosproject.net.flow.TrafficTreatment;
30 import org.onosproject.net.flow.instructions.Instructions;
31 import org.onosproject.net.group.DefaultGroupBucket;
32 import org.onosproject.net.group.GroupBucket;
33 import org.onosproject.net.group.GroupBuckets;
34 import org.projectfloodlight.openflow.protocol.OFBucket;
35 import org.projectfloodlight.openflow.protocol.OFGroupType;
36 import org.projectfloodlight.openflow.protocol.action.OFAction;
37 import org.projectfloodlight.openflow.protocol.action.OFActionCircuit;
38 import org.projectfloodlight.openflow.protocol.action.OFActionCopyTtlIn;
39 import org.projectfloodlight.openflow.protocol.action.OFActionCopyTtlOut;
40 import org.projectfloodlight.openflow.protocol.action.OFActionDecMplsTtl;
41 import org.projectfloodlight.openflow.protocol.action.OFActionDecNwTtl;
42 import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
43 import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
44 import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
45 import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls;
46 import org.projectfloodlight.openflow.protocol.action.OFActionPushMpls;
47 import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
48 import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
49 import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
50 import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
51 import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
52 import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
53 import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
54 import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
55 import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic;
56 import org.projectfloodlight.openflow.types.IPv4Address;
57 import org.projectfloodlight.openflow.types.OFVlanVidMatch;
58 import org.projectfloodlight.openflow.types.U32;
59 import org.projectfloodlight.openflow.types.U8;
60 import org.projectfloodlight.openflow.types.VlanPcp;
61 import org.slf4j.Logger;
63 import java.util.List;
65 import static org.slf4j.LoggerFactory.getLogger;
68 * Builder for GroupBucketEntry.
70 public class GroupBucketEntryBuilder {
72 private List<OFBucket> ofBuckets;
73 private OFGroupType type;
75 private final Logger log = getLogger(getClass());
80 * @param ofBuckets list of OFBucket
81 * @param type Group type
83 public GroupBucketEntryBuilder(List<OFBucket> ofBuckets, OFGroupType type) {
84 this.ofBuckets = ofBuckets;
89 * Builds a GroupBuckets.
91 * @return GroupBuckets object, a list of GroupBuckets
93 public GroupBuckets build() {
94 List<GroupBucket> bucketList = Lists.newArrayList();
96 for (OFBucket bucket: ofBuckets) {
97 TrafficTreatment treatment = buildTreatment(bucket.getActions());
98 // TODO: Use GroupBucketEntry
99 GroupBucket groupBucket = null;
103 DefaultGroupBucket.createIndirectGroupBucket(treatment);
107 DefaultGroupBucket.createSelectGroupBucket(treatment);
111 PortNumber.portNumber(bucket.getWatchPort().getPortNumber());
113 new DefaultGroupId(bucket.getWatchGroup().getGroupNumber());
115 DefaultGroupBucket.createFailoverGroupBucket(treatment,
119 log.error("Unsupported Group type : {}", type);
121 if (groupBucket != null) {
122 bucketList.add(groupBucket);
125 return new GroupBuckets(bucketList);
129 private TrafficTreatment buildTreatment(List<OFAction> actions) {
130 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
131 // If this is a drop rule
132 if (actions.size() == 0) {
134 return builder.build();
136 for (OFAction act : actions) {
137 switch (act.getType()) {
139 OFActionOutput out = (OFActionOutput) act;
141 PortNumber.portNumber(out.getPort().getPortNumber()));
144 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
145 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
148 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
149 builder.setVlanPcp(pcp.getVlanPcp().getValue());
158 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
160 MacAddress.valueOf(dldst.getDlAddr().getLong()));
163 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
165 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
169 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
170 IPv4Address di = nwdst.getNwAddr();
171 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
174 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
175 IPv4Address si = nwsrc.getNwAddr();
176 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
179 OFActionExperimenter exp = (OFActionExperimenter) act;
180 if (exp.getExperimenter() == 0x80005A06 ||
181 exp.getExperimenter() == 0x748771) {
182 OFActionCircuit ct = (OFActionCircuit) exp;
183 short lambda = ((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber();
184 builder.add(Instructions.modL0Lambda(Lambda.indexedLambda(lambda)));
186 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
190 OFActionSetField setField = (OFActionSetField) act;
191 handleSetField(builder, setField.getField());
194 OFActionPopMpls popMpls = (OFActionPopMpls) act;
195 builder.popMpls((short) popMpls.getEthertype().getValue());
198 OFActionPushMpls pushMpls = (OFActionPushMpls) act;
202 OFActionCopyTtlIn copyTtlIn = (OFActionCopyTtlIn) act;
206 OFActionCopyTtlOut copyTtlOut = (OFActionCopyTtlOut) act;
207 builder.copyTtlOut();
210 OFActionDecMplsTtl decMplsTtl = (OFActionDecMplsTtl) act;
211 builder.decMplsTtl();
214 OFActionDecNwTtl decNwTtl = (OFActionDecNwTtl) act;
218 OFActionGroup grp = (OFActionGroup) act;
219 builder.group(new DefaultGroupId(grp.getGroup().getGroupNumber()));
235 log.warn("Action type {} not yet implemented.", act.getType());
239 return builder.build();
242 private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) {
243 switch (oxm.getMatchField().id) {
245 @SuppressWarnings("unchecked")
246 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
247 builder.setVlanPcp(vlanpcp.getValue().getValue());
250 @SuppressWarnings("unchecked")
251 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
252 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
255 @SuppressWarnings("unchecked")
256 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
257 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
258 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
261 @SuppressWarnings("unchecked")
262 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
263 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
264 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
267 @SuppressWarnings("unchecked")
268 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
269 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
272 @SuppressWarnings("unchecked")
273 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
274 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
277 @SuppressWarnings("unchecked")
278 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
279 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
282 @SuppressWarnings("unchecked")
283 OFOxm<U8> mplsBos = (OFOxm<U8>) oxm;
284 builder.setMplsBos(mplsBos.getValue() == U8.ZERO ? false : true);
291 case BSN_EGR_PORT_GROUP_ID:
292 case BSN_GLOBAL_VRF_ALLOWED:
293 case BSN_IN_PORTS_128:
294 case BSN_L3_DST_CLASS_ID:
295 case BSN_L3_INTERFACE_CLASS_ID:
296 case BSN_L3_SRC_CLASS_ID:
307 case BSN_VLAN_XLATE_PORT_GROUP_ID:
328 case OCH_SIGID_BASIC:
330 case OCH_SIGTYPE_BASIC:
339 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);