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