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