blob: d3a2378245c2854212bc9973ec96c53008a0b200 [file] [log] [blame]
sangho5afd02a2015-02-03 20:07:35 -08001/*
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 */
16package org.onosproject.provider.of.group.impl;
17
18import com.google.common.collect.Lists;
Michele Santuari4b6019e2014-12-19 11:31:45 +010019
sangho5afd02a2015-02-03 20:07:35 -080020import org.onlab.packet.Ip4Address;
21import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010022import org.onlab.packet.MplsLabel;
sangho5afd02a2015-02-03 20:07:35 -080023import org.onlab.packet.VlanId;
24import org.onosproject.core.DefaultGroupId;
25import org.onosproject.core.GroupId;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -070026import org.onosproject.net.Lambda;
sangho5afd02a2015-02-03 20:07:35 -080027import org.onosproject.net.PortNumber;
28import org.onosproject.net.flow.DefaultTrafficTreatment;
29import org.onosproject.net.flow.TrafficTreatment;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -070030import org.onosproject.net.flow.instructions.Instructions;
sangho5afd02a2015-02-03 20:07:35 -080031import org.onosproject.net.group.DefaultGroupBucket;
32import org.onosproject.net.group.GroupBucket;
33import org.onosproject.net.group.GroupBuckets;
34import org.projectfloodlight.openflow.protocol.OFBucket;
35import org.projectfloodlight.openflow.protocol.OFGroupType;
36import org.projectfloodlight.openflow.protocol.action.OFAction;
37import org.projectfloodlight.openflow.protocol.action.OFActionCircuit;
38import org.projectfloodlight.openflow.protocol.action.OFActionCopyTtlIn;
39import org.projectfloodlight.openflow.protocol.action.OFActionCopyTtlOut;
40import org.projectfloodlight.openflow.protocol.action.OFActionDecMplsTtl;
41import org.projectfloodlight.openflow.protocol.action.OFActionDecNwTtl;
42import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
Saurav Das88329902015-05-27 23:22:32 -070043import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
sangho5afd02a2015-02-03 20:07:35 -080044import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
45import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls;
46import org.projectfloodlight.openflow.protocol.action.OFActionPushMpls;
47import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
48import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
49import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
50import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
51import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
52import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
53import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
54import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
55import org.projectfloodlight.openflow.protocol.oxm.OFOxmOchSigidBasic;
56import org.projectfloodlight.openflow.types.IPv4Address;
57import org.projectfloodlight.openflow.types.OFVlanVidMatch;
58import org.projectfloodlight.openflow.types.U32;
Saurav Das73a7dd42015-08-19 22:20:31 -070059import org.projectfloodlight.openflow.types.U8;
sangho5afd02a2015-02-03 20:07:35 -080060import org.projectfloodlight.openflow.types.VlanPcp;
61import org.slf4j.Logger;
62
63import java.util.List;
64
65import static org.slf4j.LoggerFactory.getLogger;
66
67/*
68 * Builder for GroupBucketEntry.
69 */
70public class GroupBucketEntryBuilder {
71
72 private List<OFBucket> ofBuckets;
73 private OFGroupType type;
74
75 private final Logger log = getLogger(getClass());
76
77 /**
78 * Creates a builder.
79 *
80 * @param ofBuckets list of OFBucket
81 * @param type Group type
82 */
83 public GroupBucketEntryBuilder(List<OFBucket> ofBuckets, OFGroupType type) {
84 this.ofBuckets = ofBuckets;
85 this.type = type;
86 }
87
88 /**
89 * Builds a GroupBuckets.
90 *
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -070091 * @return GroupBuckets object, a list of GroupBuckets
sangho5afd02a2015-02-03 20:07:35 -080092 */
93 public GroupBuckets build() {
94 List<GroupBucket> bucketList = Lists.newArrayList();
95
96 for (OFBucket bucket: ofBuckets) {
97 TrafficTreatment treatment = buildTreatment(bucket.getActions());
98 // TODO: Use GroupBucketEntry
99 GroupBucket groupBucket = null;
100 switch (type) {
101 case INDIRECT:
102 groupBucket =
103 DefaultGroupBucket.createIndirectGroupBucket(treatment);
104 break;
105 case SELECT:
106 groupBucket =
107 DefaultGroupBucket.createSelectGroupBucket(treatment);
108 break;
109 case FF:
110 PortNumber port =
111 PortNumber.portNumber(bucket.getWatchPort().getPortNumber());
112 GroupId groupId =
113 new DefaultGroupId(bucket.getWatchGroup().getGroupNumber());
114 groupBucket =
115 DefaultGroupBucket.createFailoverGroupBucket(treatment,
116 port, groupId);
117 break;
Charles Chanc42e84e2015-10-20 16:24:19 -0700118 case ALL:
119 groupBucket =
120 DefaultGroupBucket.createAllGroupBucket(treatment);
121 break;
sangho5afd02a2015-02-03 20:07:35 -0800122 default:
123 log.error("Unsupported Group type : {}", type);
124 }
125 if (groupBucket != null) {
126 bucketList.add(groupBucket);
127 }
128 }
129 return new GroupBuckets(bucketList);
130 }
131
132
133 private TrafficTreatment buildTreatment(List<OFAction> actions) {
134 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
135 // If this is a drop rule
136 if (actions.size() == 0) {
137 builder.drop();
138 return builder.build();
139 }
140 for (OFAction act : actions) {
141 switch (act.getType()) {
142 case OUTPUT:
143 OFActionOutput out = (OFActionOutput) act;
144 builder.setOutput(
145 PortNumber.portNumber(out.getPort().getPortNumber()));
146 break;
147 case SET_VLAN_VID:
148 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
149 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
150 break;
151 case SET_VLAN_PCP:
152 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
153 builder.setVlanPcp(pcp.getVlanPcp().getValue());
154 break;
Jonathan Harte106e4b2015-03-09 16:44:56 -0700155 case POP_VLAN:
156 builder.popVlan();
157 break;
158 case PUSH_VLAN:
159 builder.pushVlan();
160 break;
sangho5afd02a2015-02-03 20:07:35 -0800161 case SET_DL_DST:
162 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
163 builder.setEthDst(
164 MacAddress.valueOf(dldst.getDlAddr().getLong()));
165 break;
166 case SET_DL_SRC:
167 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
168 builder.setEthSrc(
169 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
170
171 break;
172 case SET_NW_DST:
173 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
174 IPv4Address di = nwdst.getNwAddr();
175 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
176 break;
177 case SET_NW_SRC:
178 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
179 IPv4Address si = nwsrc.getNwAddr();
180 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
181 break;
182 case EXPERIMENTER:
183 OFActionExperimenter exp = (OFActionExperimenter) act;
184 if (exp.getExperimenter() == 0x80005A06 ||
185 exp.getExperimenter() == 0x748771) {
186 OFActionCircuit ct = (OFActionCircuit) exp;
Sho SHIMIZU9553bb82015-06-30 16:02:45 -0700187 short lambda = ((OFOxmOchSigidBasic) ct.getField()).getValue().getChannelNumber();
188 builder.add(Instructions.modL0Lambda(Lambda.indexedLambda(lambda)));
sangho5afd02a2015-02-03 20:07:35 -0800189 } else {
190 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
191 }
192 break;
193 case SET_FIELD:
194 OFActionSetField setField = (OFActionSetField) act;
195 handleSetField(builder, setField.getField());
196 break;
197 case POP_MPLS:
198 OFActionPopMpls popMpls = (OFActionPopMpls) act;
199 builder.popMpls((short) popMpls.getEthertype().getValue());
200 break;
201 case PUSH_MPLS:
202 OFActionPushMpls pushMpls = (OFActionPushMpls) act;
203 builder.pushMpls();
204 break;
205 case COPY_TTL_IN:
206 OFActionCopyTtlIn copyTtlIn = (OFActionCopyTtlIn) act;
207 builder.copyTtlIn();
208 break;
209 case COPY_TTL_OUT:
210 OFActionCopyTtlOut copyTtlOut = (OFActionCopyTtlOut) act;
211 builder.copyTtlOut();
212 break;
213 case DEC_MPLS_TTL:
214 OFActionDecMplsTtl decMplsTtl = (OFActionDecMplsTtl) act;
215 builder.decMplsTtl();
216 break;
217 case DEC_NW_TTL:
218 OFActionDecNwTtl decNwTtl = (OFActionDecNwTtl) act;
219 builder.decNwTtl();
220 break;
Saurav Das88329902015-05-27 23:22:32 -0700221 case GROUP:
222 OFActionGroup grp = (OFActionGroup) act;
223 builder.group(new DefaultGroupId(grp.getGroup().getGroupNumber()));
224 break;
sangho5afd02a2015-02-03 20:07:35 -0800225 case SET_TP_DST:
226 case SET_TP_SRC:
227 case POP_PBB:
sangho5afd02a2015-02-03 20:07:35 -0800228 case PUSH_PBB:
sangho5afd02a2015-02-03 20:07:35 -0800229 case SET_MPLS_LABEL:
230 case SET_MPLS_TC:
231 case SET_MPLS_TTL:
232 case SET_NW_ECN:
233 case SET_NW_TOS:
234 case SET_NW_TTL:
235 case SET_QUEUE:
236 case STRIP_VLAN:
237 case ENQUEUE:
sangho5afd02a2015-02-03 20:07:35 -0800238 default:
239 log.warn("Action type {} not yet implemented.", act.getType());
240 }
241 }
242
243 return builder.build();
244 }
245
246 private void handleSetField(TrafficTreatment.Builder builder, OFOxm<?> oxm) {
247 switch (oxm.getMatchField().id) {
248 case VLAN_PCP:
249 @SuppressWarnings("unchecked")
250 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
251 builder.setVlanPcp(vlanpcp.getValue().getValue());
252 break;
253 case VLAN_VID:
254 @SuppressWarnings("unchecked")
255 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
256 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
257 break;
258 case ETH_DST:
259 @SuppressWarnings("unchecked")
260 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
261 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
262 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
263 break;
264 case ETH_SRC:
265 @SuppressWarnings("unchecked")
266 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
267 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
268 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
269 break;
270 case IPV4_DST:
271 @SuppressWarnings("unchecked")
272 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
273 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
274 break;
275 case IPV4_SRC:
276 @SuppressWarnings("unchecked")
277 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
278 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
279 break;
280 case MPLS_LABEL:
281 @SuppressWarnings("unchecked")
282 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100283 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho5afd02a2015-02-03 20:07:35 -0800284 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700285 case MPLS_BOS:
286 @SuppressWarnings("unchecked")
287 OFOxm<U8> mplsBos = (OFOxm<U8>) oxm;
288 builder.setMplsBos(mplsBos.getValue() == U8.ZERO ? false : true);
289 break;
sangho5afd02a2015-02-03 20:07:35 -0800290 case ARP_OP:
291 case ARP_SHA:
292 case ARP_SPA:
293 case ARP_THA:
294 case ARP_TPA:
295 case BSN_EGR_PORT_GROUP_ID:
296 case BSN_GLOBAL_VRF_ALLOWED:
297 case BSN_IN_PORTS_128:
298 case BSN_L3_DST_CLASS_ID:
299 case BSN_L3_INTERFACE_CLASS_ID:
300 case BSN_L3_SRC_CLASS_ID:
301 case BSN_LAG_ID:
302 case BSN_TCP_FLAGS:
303 case BSN_UDF0:
304 case BSN_UDF1:
305 case BSN_UDF2:
306 case BSN_UDF3:
307 case BSN_UDF4:
308 case BSN_UDF5:
309 case BSN_UDF6:
310 case BSN_UDF7:
311 case BSN_VLAN_XLATE_PORT_GROUP_ID:
312 case BSN_VRF:
313 case ETH_TYPE:
314 case ICMPV4_CODE:
315 case ICMPV4_TYPE:
316 case ICMPV6_CODE:
317 case ICMPV6_TYPE:
318 case IN_PHY_PORT:
319 case IN_PORT:
320 case IPV6_DST:
321 case IPV6_FLABEL:
322 case IPV6_ND_SLL:
323 case IPV6_ND_TARGET:
324 case IPV6_ND_TLL:
325 case IPV6_SRC:
326 case IP_DSCP:
327 case IP_ECN:
328 case IP_PROTO:
329 case METADATA:
330 case MPLS_TC:
331 case OCH_SIGID:
332 case OCH_SIGID_BASIC:
333 case OCH_SIGTYPE:
334 case OCH_SIGTYPE_BASIC:
335 case SCTP_DST:
336 case SCTP_SRC:
337 case TCP_DST:
338 case TCP_SRC:
339 case TUNNEL_ID:
340 case UDP_DST:
341 case UDP_SRC:
342 default:
343 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
344 break;
345 }
346 }
347}