blob: add20bebb4539ea6f8b1f33aabe7e00261eb2105 [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
Ray Milkey84d5a292016-02-22 14:04:01 -080020import org.onlab.packet.EthType;
sangho5afd02a2015-02-03 20:07:35 -080021import org.onlab.packet.Ip4Address;
22import org.onlab.packet.MacAddress;
Michele Santuari4b6019e2014-12-19 11:31:45 +010023import org.onlab.packet.MplsLabel;
sangho5afd02a2015-02-03 20:07:35 -080024import org.onlab.packet.VlanId;
25import org.onosproject.core.DefaultGroupId;
26import org.onosproject.core.GroupId;
Hyunsun Moona834c182015-12-16 03:01:56 -080027import org.onosproject.net.DeviceId;
sangho5afd02a2015-02-03 20:07:35 -080028import org.onosproject.net.PortNumber;
Hyunsun Moona834c182015-12-16 03:01:56 -080029import org.onosproject.net.driver.DefaultDriverData;
30import org.onosproject.net.driver.DefaultDriverHandler;
31import org.onosproject.net.driver.Driver;
32import org.onosproject.net.driver.DriverHandler;
33import org.onosproject.net.driver.DriverService;
sangho5afd02a2015-02-03 20:07:35 -080034import org.onosproject.net.flow.DefaultTrafficTreatment;
35import org.onosproject.net.flow.TrafficTreatment;
36import org.onosproject.net.group.DefaultGroupBucket;
37import org.onosproject.net.group.GroupBucket;
38import org.onosproject.net.group.GroupBuckets;
Hyunsun Moona834c182015-12-16 03:01:56 -080039import org.onosproject.openflow.controller.Dpid;
40import org.onosproject.openflow.controller.ExtensionTreatmentInterpreter;
sangho5afd02a2015-02-03 20:07:35 -080041import org.projectfloodlight.openflow.protocol.OFBucket;
42import org.projectfloodlight.openflow.protocol.OFGroupType;
43import org.projectfloodlight.openflow.protocol.action.OFAction;
sangho5afd02a2015-02-03 20:07:35 -080044import org.projectfloodlight.openflow.protocol.action.OFActionCopyTtlIn;
45import org.projectfloodlight.openflow.protocol.action.OFActionCopyTtlOut;
46import org.projectfloodlight.openflow.protocol.action.OFActionDecMplsTtl;
47import org.projectfloodlight.openflow.protocol.action.OFActionDecNwTtl;
48import org.projectfloodlight.openflow.protocol.action.OFActionExperimenter;
Saurav Das88329902015-05-27 23:22:32 -070049import org.projectfloodlight.openflow.protocol.action.OFActionGroup;
sangho5afd02a2015-02-03 20:07:35 -080050import org.projectfloodlight.openflow.protocol.action.OFActionOutput;
51import org.projectfloodlight.openflow.protocol.action.OFActionPopMpls;
52import org.projectfloodlight.openflow.protocol.action.OFActionPushMpls;
53import org.projectfloodlight.openflow.protocol.action.OFActionSetDlDst;
54import org.projectfloodlight.openflow.protocol.action.OFActionSetDlSrc;
55import org.projectfloodlight.openflow.protocol.action.OFActionSetField;
56import org.projectfloodlight.openflow.protocol.action.OFActionSetNwDst;
57import org.projectfloodlight.openflow.protocol.action.OFActionSetNwSrc;
58import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanPcp;
59import org.projectfloodlight.openflow.protocol.action.OFActionSetVlanVid;
60import org.projectfloodlight.openflow.protocol.oxm.OFOxm;
sangho5afd02a2015-02-03 20:07:35 -080061import org.projectfloodlight.openflow.types.IPv4Address;
62import org.projectfloodlight.openflow.types.OFVlanVidMatch;
63import org.projectfloodlight.openflow.types.U32;
Hyunsun Moona834c182015-12-16 03:01:56 -080064import org.projectfloodlight.openflow.types.U64;
Saurav Das73a7dd42015-08-19 22:20:31 -070065import org.projectfloodlight.openflow.types.U8;
sangho5afd02a2015-02-03 20:07:35 -080066import org.projectfloodlight.openflow.types.VlanPcp;
67import org.slf4j.Logger;
68
69import java.util.List;
70
71import static org.slf4j.LoggerFactory.getLogger;
72
73/*
74 * Builder for GroupBucketEntry.
75 */
76public class GroupBucketEntryBuilder {
77
Hyunsun Moona834c182015-12-16 03:01:56 -080078 private Dpid dpid;
sangho5afd02a2015-02-03 20:07:35 -080079 private List<OFBucket> ofBuckets;
80 private OFGroupType type;
Hyunsun Moona834c182015-12-16 03:01:56 -080081 private DriverService driverService;
sangho5afd02a2015-02-03 20:07:35 -080082
83 private final Logger log = getLogger(getClass());
84
Hyunsun Moona834c182015-12-16 03:01:56 -080085
sangho5afd02a2015-02-03 20:07:35 -080086 /**
87 * Creates a builder.
88 *
Hyunsun Moona834c182015-12-16 03:01:56 -080089 * @param dpid dpid
sangho5afd02a2015-02-03 20:07:35 -080090 * @param ofBuckets list of OFBucket
91 * @param type Group type
Hyunsun Moona834c182015-12-16 03:01:56 -080092 * @param driverService driver service
sangho5afd02a2015-02-03 20:07:35 -080093 */
Hyunsun Moona834c182015-12-16 03:01:56 -080094 public GroupBucketEntryBuilder(Dpid dpid, List<OFBucket> ofBuckets, OFGroupType type,
95 DriverService driverService) {
96 this.dpid = dpid;
sangho5afd02a2015-02-03 20:07:35 -080097 this.ofBuckets = ofBuckets;
98 this.type = type;
Hyunsun Moona834c182015-12-16 03:01:56 -080099 this.driverService = driverService;
sangho5afd02a2015-02-03 20:07:35 -0800100 }
101
102 /**
103 * Builds a GroupBuckets.
104 *
Jonathan Hart8ef6d3b2015-03-08 21:21:27 -0700105 * @return GroupBuckets object, a list of GroupBuckets
sangho5afd02a2015-02-03 20:07:35 -0800106 */
107 public GroupBuckets build() {
108 List<GroupBucket> bucketList = Lists.newArrayList();
109
110 for (OFBucket bucket: ofBuckets) {
111 TrafficTreatment treatment = buildTreatment(bucket.getActions());
112 // TODO: Use GroupBucketEntry
113 GroupBucket groupBucket = null;
114 switch (type) {
115 case INDIRECT:
116 groupBucket =
117 DefaultGroupBucket.createIndirectGroupBucket(treatment);
118 break;
119 case SELECT:
120 groupBucket =
121 DefaultGroupBucket.createSelectGroupBucket(treatment);
122 break;
123 case FF:
124 PortNumber port =
125 PortNumber.portNumber(bucket.getWatchPort().getPortNumber());
126 GroupId groupId =
127 new DefaultGroupId(bucket.getWatchGroup().getGroupNumber());
128 groupBucket =
129 DefaultGroupBucket.createFailoverGroupBucket(treatment,
130 port, groupId);
131 break;
Charles Chanc42e84e2015-10-20 16:24:19 -0700132 case ALL:
133 groupBucket =
134 DefaultGroupBucket.createAllGroupBucket(treatment);
135 break;
sangho5afd02a2015-02-03 20:07:35 -0800136 default:
137 log.error("Unsupported Group type : {}", type);
138 }
139 if (groupBucket != null) {
140 bucketList.add(groupBucket);
141 }
142 }
143 return new GroupBuckets(bucketList);
144 }
145
146
147 private TrafficTreatment buildTreatment(List<OFAction> actions) {
148 TrafficTreatment.Builder builder = DefaultTrafficTreatment.builder();
149 // If this is a drop rule
150 if (actions.size() == 0) {
151 builder.drop();
152 return builder.build();
153 }
154 for (OFAction act : actions) {
155 switch (act.getType()) {
156 case OUTPUT:
157 OFActionOutput out = (OFActionOutput) act;
158 builder.setOutput(
159 PortNumber.portNumber(out.getPort().getPortNumber()));
160 break;
161 case SET_VLAN_VID:
162 OFActionSetVlanVid vlan = (OFActionSetVlanVid) act;
163 builder.setVlanId(VlanId.vlanId(vlan.getVlanVid().getVlan()));
164 break;
165 case SET_VLAN_PCP:
166 OFActionSetVlanPcp pcp = (OFActionSetVlanPcp) act;
167 builder.setVlanPcp(pcp.getVlanPcp().getValue());
168 break;
Jonathan Harte106e4b2015-03-09 16:44:56 -0700169 case POP_VLAN:
170 builder.popVlan();
171 break;
172 case PUSH_VLAN:
173 builder.pushVlan();
174 break;
sangho5afd02a2015-02-03 20:07:35 -0800175 case SET_DL_DST:
176 OFActionSetDlDst dldst = (OFActionSetDlDst) act;
177 builder.setEthDst(
178 MacAddress.valueOf(dldst.getDlAddr().getLong()));
179 break;
180 case SET_DL_SRC:
181 OFActionSetDlSrc dlsrc = (OFActionSetDlSrc) act;
182 builder.setEthSrc(
183 MacAddress.valueOf(dlsrc.getDlAddr().getLong()));
184
185 break;
186 case SET_NW_DST:
187 OFActionSetNwDst nwdst = (OFActionSetNwDst) act;
188 IPv4Address di = nwdst.getNwAddr();
189 builder.setIpDst(Ip4Address.valueOf(di.getInt()));
190 break;
191 case SET_NW_SRC:
192 OFActionSetNwSrc nwsrc = (OFActionSetNwSrc) act;
193 IPv4Address si = nwsrc.getNwAddr();
194 builder.setIpSrc(Ip4Address.valueOf(si.getInt()));
195 break;
196 case EXPERIMENTER:
197 OFActionExperimenter exp = (OFActionExperimenter) act;
Sho SHIMIZUcc137a92016-03-11 15:10:54 -0800198 log.warn("Unsupported OFActionExperimenter {}", exp.getExperimenter());
sangho5afd02a2015-02-03 20:07:35 -0800199 break;
200 case SET_FIELD:
201 OFActionSetField setField = (OFActionSetField) act;
Hyunsun Moona834c182015-12-16 03:01:56 -0800202 handleSetField(builder, setField);
sangho5afd02a2015-02-03 20:07:35 -0800203 break;
204 case POP_MPLS:
205 OFActionPopMpls popMpls = (OFActionPopMpls) act;
Ray Milkey84d5a292016-02-22 14:04:01 -0800206 builder.popMpls(new EthType(popMpls.getEthertype().getValue()));
sangho5afd02a2015-02-03 20:07:35 -0800207 break;
208 case PUSH_MPLS:
209 OFActionPushMpls pushMpls = (OFActionPushMpls) act;
210 builder.pushMpls();
211 break;
212 case COPY_TTL_IN:
213 OFActionCopyTtlIn copyTtlIn = (OFActionCopyTtlIn) act;
214 builder.copyTtlIn();
215 break;
216 case COPY_TTL_OUT:
217 OFActionCopyTtlOut copyTtlOut = (OFActionCopyTtlOut) act;
218 builder.copyTtlOut();
219 break;
220 case DEC_MPLS_TTL:
221 OFActionDecMplsTtl decMplsTtl = (OFActionDecMplsTtl) act;
222 builder.decMplsTtl();
223 break;
224 case DEC_NW_TTL:
225 OFActionDecNwTtl decNwTtl = (OFActionDecNwTtl) act;
226 builder.decNwTtl();
227 break;
Saurav Das88329902015-05-27 23:22:32 -0700228 case GROUP:
229 OFActionGroup grp = (OFActionGroup) act;
230 builder.group(new DefaultGroupId(grp.getGroup().getGroupNumber()));
231 break;
sangho5afd02a2015-02-03 20:07:35 -0800232 case SET_TP_DST:
233 case SET_TP_SRC:
234 case POP_PBB:
sangho5afd02a2015-02-03 20:07:35 -0800235 case PUSH_PBB:
sangho5afd02a2015-02-03 20:07:35 -0800236 case SET_MPLS_LABEL:
237 case SET_MPLS_TC:
238 case SET_MPLS_TTL:
239 case SET_NW_ECN:
240 case SET_NW_TOS:
241 case SET_NW_TTL:
242 case SET_QUEUE:
243 case STRIP_VLAN:
244 case ENQUEUE:
sangho5afd02a2015-02-03 20:07:35 -0800245 default:
246 log.warn("Action type {} not yet implemented.", act.getType());
247 }
248 }
249
250 return builder.build();
251 }
252
Hyunsun Moona834c182015-12-16 03:01:56 -0800253 private void handleSetField(TrafficTreatment.Builder builder, OFActionSetField action) {
254 OFOxm<?> oxm = action.getField();
sangho5afd02a2015-02-03 20:07:35 -0800255 switch (oxm.getMatchField().id) {
256 case VLAN_PCP:
257 @SuppressWarnings("unchecked")
258 OFOxm<VlanPcp> vlanpcp = (OFOxm<VlanPcp>) oxm;
259 builder.setVlanPcp(vlanpcp.getValue().getValue());
260 break;
261 case VLAN_VID:
262 @SuppressWarnings("unchecked")
263 OFOxm<OFVlanVidMatch> vlanvid = (OFOxm<OFVlanVidMatch>) oxm;
264 builder.setVlanId(VlanId.vlanId(vlanvid.getValue().getVlan()));
265 break;
266 case ETH_DST:
267 @SuppressWarnings("unchecked")
268 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethdst =
269 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
270 builder.setEthDst(MacAddress.valueOf(ethdst.getValue().getLong()));
271 break;
272 case ETH_SRC:
273 @SuppressWarnings("unchecked")
274 OFOxm<org.projectfloodlight.openflow.types.MacAddress> ethsrc =
275 (OFOxm<org.projectfloodlight.openflow.types.MacAddress>) oxm;
276 builder.setEthSrc(MacAddress.valueOf(ethsrc.getValue().getLong()));
277 break;
278 case IPV4_DST:
279 @SuppressWarnings("unchecked")
280 OFOxm<IPv4Address> ip4dst = (OFOxm<IPv4Address>) oxm;
281 builder.setIpDst(Ip4Address.valueOf(ip4dst.getValue().getInt()));
282 break;
283 case IPV4_SRC:
284 @SuppressWarnings("unchecked")
285 OFOxm<IPv4Address> ip4src = (OFOxm<IPv4Address>) oxm;
286 builder.setIpSrc(Ip4Address.valueOf(ip4src.getValue().getInt()));
287 break;
288 case MPLS_LABEL:
289 @SuppressWarnings("unchecked")
290 OFOxm<U32> labelId = (OFOxm<U32>) oxm;
Michele Santuari4b6019e2014-12-19 11:31:45 +0100291 builder.setMpls(MplsLabel.mplsLabel((int) labelId.getValue().getValue()));
sangho5afd02a2015-02-03 20:07:35 -0800292 break;
Saurav Das73a7dd42015-08-19 22:20:31 -0700293 case MPLS_BOS:
294 @SuppressWarnings("unchecked")
295 OFOxm<U8> mplsBos = (OFOxm<U8>) oxm;
296 builder.setMplsBos(mplsBos.getValue() == U8.ZERO ? false : true);
297 break;
Hyunsun Moona834c182015-12-16 03:01:56 -0800298 case TUNNEL_ID:
299 @SuppressWarnings("unchecked")
300 OFOxm<U64> tunnelId = (OFOxm<U64>) oxm;
301 builder.setTunnelId(tunnelId.getValue().getValue());
302 break;
303 case TUNNEL_IPV4_DST:
304 DriverHandler driver = getDriver(dpid);
305 ExtensionTreatmentInterpreter interpreter = driver.behaviour(ExtensionTreatmentInterpreter.class);
306 if (interpreter != null) {
307 builder.extension(interpreter.mapAction(action), DeviceId.deviceId(Dpid.uri(dpid)));
308 }
309 break;
sangho5afd02a2015-02-03 20:07:35 -0800310 case ARP_OP:
311 case ARP_SHA:
312 case ARP_SPA:
313 case ARP_THA:
314 case ARP_TPA:
315 case BSN_EGR_PORT_GROUP_ID:
316 case BSN_GLOBAL_VRF_ALLOWED:
317 case BSN_IN_PORTS_128:
318 case BSN_L3_DST_CLASS_ID:
319 case BSN_L3_INTERFACE_CLASS_ID:
320 case BSN_L3_SRC_CLASS_ID:
321 case BSN_LAG_ID:
322 case BSN_TCP_FLAGS:
323 case BSN_UDF0:
324 case BSN_UDF1:
325 case BSN_UDF2:
326 case BSN_UDF3:
327 case BSN_UDF4:
328 case BSN_UDF5:
329 case BSN_UDF6:
330 case BSN_UDF7:
331 case BSN_VLAN_XLATE_PORT_GROUP_ID:
332 case BSN_VRF:
333 case ETH_TYPE:
334 case ICMPV4_CODE:
335 case ICMPV4_TYPE:
336 case ICMPV6_CODE:
337 case ICMPV6_TYPE:
338 case IN_PHY_PORT:
339 case IN_PORT:
340 case IPV6_DST:
341 case IPV6_FLABEL:
342 case IPV6_ND_SLL:
343 case IPV6_ND_TARGET:
344 case IPV6_ND_TLL:
345 case IPV6_SRC:
346 case IP_DSCP:
347 case IP_ECN:
348 case IP_PROTO:
349 case METADATA:
350 case MPLS_TC:
351 case OCH_SIGID:
352 case OCH_SIGID_BASIC:
353 case OCH_SIGTYPE:
354 case OCH_SIGTYPE_BASIC:
355 case SCTP_DST:
356 case SCTP_SRC:
357 case TCP_DST:
358 case TCP_SRC:
sangho5afd02a2015-02-03 20:07:35 -0800359 case UDP_DST:
360 case UDP_SRC:
361 default:
362 log.warn("Set field type {} not yet implemented.", oxm.getMatchField().id);
363 break;
364 }
365 }
Hyunsun Moona834c182015-12-16 03:01:56 -0800366
367 private DriverHandler getDriver(Dpid dpid) {
368 DeviceId deviceId = DeviceId.deviceId(Dpid.uri(dpid));
369 Driver driver = driverService.getDriver(deviceId);
370 DriverHandler handler = new DefaultDriverHandler(new DefaultDriverData(driver, deviceId));
371 return handler;
372 }
sangho5afd02a2015-02-03 20:07:35 -0800373}