blob: 82e1b08a4c50e7f0e11fb8784d7b9d467d0203f6 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
alshabibeec3a062014-09-17 18:01:26 -070019package org.onlab.onos.provider.of.flow.impl;
20
21import static org.slf4j.LoggerFactory.getLogger;
22
alshabibeec3a062014-09-17 18:01:26 -070023import org.onlab.onos.net.flow.FlowRule;
24import org.onlab.onos.net.flow.TrafficSelector;
alshabibeec3a062014-09-17 18:01:26 -070025import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion;
26import org.onlab.onos.net.flow.criteria.Criteria.EthTypeCriterion;
27import org.onlab.onos.net.flow.criteria.Criteria.IPCriterion;
28import org.onlab.onos.net.flow.criteria.Criteria.IPProtocolCriterion;
Marc De Leenheer49087752014-10-23 13:54:09 -070029import org.onlab.onos.net.flow.criteria.Criteria.LambdaCriterion;
alshabibeec3a062014-09-17 18:01:26 -070030import org.onlab.onos.net.flow.criteria.Criteria.PortCriterion;
Jonathan Hart34bc6142014-10-17 11:00:43 -070031import org.onlab.onos.net.flow.criteria.Criteria.TcpPortCriterion;
alshabibeec3a062014-09-17 18:01:26 -070032import org.onlab.onos.net.flow.criteria.Criteria.VlanIdCriterion;
33import org.onlab.onos.net.flow.criteria.Criteria.VlanPcpCriterion;
34import org.onlab.onos.net.flow.criteria.Criterion;
alshabibeec3a062014-09-17 18:01:26 -070035import org.projectfloodlight.openflow.protocol.OFFactory;
alshabib193525b2014-10-08 18:58:03 -070036import org.projectfloodlight.openflow.protocol.OFFlowAdd;
37import org.projectfloodlight.openflow.protocol.OFFlowDelete;
alshabibeec3a062014-09-17 18:01:26 -070038import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabibeec3a062014-09-17 18:01:26 -070039import org.projectfloodlight.openflow.protocol.match.Match;
40import org.projectfloodlight.openflow.protocol.match.MatchField;
Marc De Leenheer49087752014-10-23 13:54:09 -070041import org.projectfloodlight.openflow.types.CircuitSignalID;
alshabibeec3a062014-09-17 18:01:26 -070042import org.projectfloodlight.openflow.types.EthType;
43import org.projectfloodlight.openflow.types.IPv4Address;
44import org.projectfloodlight.openflow.types.IpProtocol;
45import org.projectfloodlight.openflow.types.MacAddress;
46import org.projectfloodlight.openflow.types.Masked;
alshabibeec3a062014-09-17 18:01:26 -070047import org.projectfloodlight.openflow.types.OFPort;
48import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Jonathan Hart34bc6142014-10-17 11:00:43 -070049import org.projectfloodlight.openflow.types.TransportPort;
alshabibeec3a062014-09-17 18:01:26 -070050import org.projectfloodlight.openflow.types.VlanPcp;
51import org.projectfloodlight.openflow.types.VlanVid;
52import org.slf4j.Logger;
53
Jonathan Hart86e59352014-10-22 10:42:16 -070054/**
55 * Builder for OpenFlow flow mods based on FlowRules.
56 */
57public abstract class FlowModBuilder {
alshabibeec3a062014-09-17 18:01:26 -070058
59 private final Logger log = getLogger(getClass());
60
61 private final OFFactory factory;
Jonathan Hart86e59352014-10-22 10:42:16 -070062 private final FlowRule flowRule;
alshabibeec3a062014-09-17 18:01:26 -070063 private final TrafficSelector selector;
64
Jonathan Hart86e59352014-10-22 10:42:16 -070065 /**
66 * Creates a new flow mod builder.
67 *
68 * @param flowRule the flow rule to transform into a flow mod
69 * @param factory the OpenFlow factory to use to build the flow mod
70 * @return the new flow mod builder
71 */
72 public static FlowModBuilder builder(FlowRule flowRule, OFFactory factory) {
73 switch (factory.getVersion()) {
74 case OF_10:
75 return new FlowModBuilderVer10(flowRule, factory);
76 case OF_13:
77 return new FlowModBuilderVer13(flowRule, factory);
78 default:
79 throw new UnsupportedOperationException(
80 "No flow mod builder for protocol version " + factory.getVersion());
81 }
82 }
alshabibeec3a062014-09-17 18:01:26 -070083
Jonathan Hart86e59352014-10-22 10:42:16 -070084 /**
85 * Constructs a flow mod builder.
86 *
87 * @param flowRule the flow rule to transform into a flow mod
88 * @param factory the OpenFlow factory to use to build the flow mod
89 */
90 protected FlowModBuilder(FlowRule flowRule, OFFactory factory) {
alshabibeec3a062014-09-17 18:01:26 -070091 this.factory = factory;
Jonathan Hart86e59352014-10-22 10:42:16 -070092 this.flowRule = flowRule;
alshabibeec3a062014-09-17 18:01:26 -070093 this.selector = flowRule.selector();
alshabibeec3a062014-09-17 18:01:26 -070094 }
95
Jonathan Hart86e59352014-10-22 10:42:16 -070096 /**
97 * Builds an ADD flow mod.
98 *
99 * @return the flow mod
100 */
101 public abstract OFFlowAdd buildFlowAdd();
alshabibeec3a062014-09-17 18:01:26 -0700102
Jonathan Hart86e59352014-10-22 10:42:16 -0700103 /**
104 * Builds a MODIFY flow mod.
105 *
106 * @return the flow mod
107 */
108 public abstract OFFlowMod buildFlowMod();
alshabibeec3a062014-09-17 18:01:26 -0700109
Jonathan Hart86e59352014-10-22 10:42:16 -0700110 /**
111 * Builds a DELETE flow mod.
112 *
113 * @return the flow mod
114 */
115 public abstract OFFlowDelete buildFlowDel();
alshabibeec3a062014-09-17 18:01:26 -0700116
Jonathan Hart86e59352014-10-22 10:42:16 -0700117 /**
118 * Builds the match for the flow mod.
119 *
120 * @return the match
121 */
122 protected Match buildMatch() {
alshabibeec3a062014-09-17 18:01:26 -0700123 Match.Builder mBuilder = factory.buildMatch();
124 EthCriterion eth;
125 IPCriterion ip;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700126 TcpPortCriterion tp;
alshabibeec3a062014-09-17 18:01:26 -0700127 for (Criterion c : selector.criteria()) {
128 switch (c.type()) {
129 case IN_PORT:
130 PortCriterion inport = (PortCriterion) c;
131 mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong()));
132 break;
133 case ETH_SRC:
134 eth = (EthCriterion) c;
135 mBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(eth.mac().toLong()));
136 break;
137 case ETH_DST:
138 eth = (EthCriterion) c;
139 mBuilder.setExact(MatchField.ETH_DST, MacAddress.of(eth.mac().toLong()));
140 break;
141 case ETH_TYPE:
142 EthTypeCriterion ethType = (EthTypeCriterion) c;
143 mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType()));
144 break;
145 case IPV4_DST:
146 ip = (IPCriterion) c;
147 if (ip.ip().isMasked()) {
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700148 Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()),
149 IPv4Address.of(ip.ip().netmask().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700150 mBuilder.setMasked(MatchField.IPV4_DST, maskedIp);
151 } else {
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700152 mBuilder.setExact(MatchField.IPV4_DST, IPv4Address.of(ip.ip().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700153 }
154 break;
155 case IPV4_SRC:
156 ip = (IPCriterion) c;
157 if (ip.ip().isMasked()) {
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700158 Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()),
159 IPv4Address.of(ip.ip().netmask().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700160 mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp);
161 } else {
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700162 mBuilder.setExact(MatchField.IPV4_SRC, IPv4Address.of(ip.ip().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700163 }
164 break;
165 case IP_PROTO:
166 IPProtocolCriterion p = (IPProtocolCriterion) c;
167 mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol()));
168 break;
169 case VLAN_PCP:
170 VlanPcpCriterion vpcp = (VlanPcpCriterion) c;
171 mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority()));
172 break;
173 case VLAN_VID:
174 VlanIdCriterion vid = (VlanIdCriterion) c;
175 mBuilder.setExact(MatchField.VLAN_VID,
176 OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort())));
177 break;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700178 case TCP_DST:
179 tp = (TcpPortCriterion) c;
180 mBuilder.setExact(MatchField.TCP_DST, TransportPort.of(tp.tcpPort()));
181 break;
182 case TCP_SRC:
183 tp = (TcpPortCriterion) c;
184 mBuilder.setExact(MatchField.TCP_SRC, TransportPort.of(tp.tcpPort()));
185 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700186 case OCH_SIGID:
187 LambdaCriterion lc = (LambdaCriterion) c;
188 mBuilder.setExact(MatchField.OCH_SIGID,
189 new CircuitSignalID((byte) 1, (byte) 2, lc.lambda(), (short) 1));
190 break;
alshabibeec3a062014-09-17 18:01:26 -0700191 case ARP_OP:
192 case ARP_SHA:
193 case ARP_SPA:
194 case ARP_THA:
195 case ARP_TPA:
196 case ICMPV4_CODE:
197 case ICMPV4_TYPE:
198 case ICMPV6_CODE:
199 case ICMPV6_TYPE:
200 case IN_PHY_PORT:
201 case IPV6_DST:
202 case IPV6_EXTHDR:
203 case IPV6_FLABEL:
204 case IPV6_ND_SLL:
205 case IPV6_ND_TARGET:
206 case IPV6_ND_TLL:
207 case IPV6_SRC:
208 case IP_DSCP:
209 case IP_ECN:
210 case METADATA:
211 case MPLS_BOS:
212 case MPLS_LABEL:
213 case MPLS_TC:
214 case PBB_ISID:
215 case SCTP_DST:
216 case SCTP_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700217 case TUNNEL_ID:
218 case UDP_DST:
219 case UDP_SRC:
220 default:
221 log.warn("Match type {} not yet implemented.", c.type());
222 }
223 }
224 return mBuilder.build();
225 }
226
Jonathan Hart86e59352014-10-22 10:42:16 -0700227 /**
228 * Returns the flow rule for this builder.
229 *
230 * @return the flow rule
231 */
232 protected FlowRule flowRule() {
233 return flowRule;
234 }
alshabib219ebaa2014-09-22 15:41:24 -0700235
Jonathan Hart86e59352014-10-22 10:42:16 -0700236 /**
237 * Returns the factory used for building OpenFlow constructs.
238 *
239 * @return the factory
240 */
241 protected OFFactory factory() {
242 return factory;
243 }
alshabib219ebaa2014-09-22 15:41:24 -0700244
alshabibeec3a062014-09-17 18:01:26 -0700245}