blob: 0a0c0c7d196808a25b6e47d50519255a367ee2c8 [file] [log] [blame]
Thomas Vachuska781d18b2014-10-27 10:31:25 -07001/*
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07002 * Copyright 2014 Open Networking Laboratory
Thomas Vachuska781d18b2014-10-27 10:31:25 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * 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
Thomas Vachuska781d18b2014-10-27 10:31:25 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * 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.
Thomas Vachuska781d18b2014-10-27 10:31:25 -070015 */
alshabibeec3a062014-09-17 18:01:26 -070016package org.onlab.onos.provider.of.flow.impl;
17
18import static org.slf4j.LoggerFactory.getLogger;
19
alshabibeec3a062014-09-17 18:01:26 -070020import org.onlab.onos.net.flow.FlowRule;
21import org.onlab.onos.net.flow.TrafficSelector;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -080022import org.onlab.onos.net.flow.criteria.Criteria;
alshabibeec3a062014-09-17 18:01:26 -070023import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion;
24import org.onlab.onos.net.flow.criteria.Criteria.EthTypeCriterion;
25import org.onlab.onos.net.flow.criteria.Criteria.IPCriterion;
26import org.onlab.onos.net.flow.criteria.Criteria.IPProtocolCriterion;
Marc De Leenheer49087752014-10-23 13:54:09 -070027import org.onlab.onos.net.flow.criteria.Criteria.LambdaCriterion;
alshabibeec3a062014-09-17 18:01:26 -070028import org.onlab.onos.net.flow.criteria.Criteria.PortCriterion;
Jonathan Hart34bc6142014-10-17 11:00:43 -070029import org.onlab.onos.net.flow.criteria.Criteria.TcpPortCriterion;
alshabibeec3a062014-09-17 18:01:26 -070030import org.onlab.onos.net.flow.criteria.Criteria.VlanIdCriterion;
31import org.onlab.onos.net.flow.criteria.Criteria.VlanPcpCriterion;
32import org.onlab.onos.net.flow.criteria.Criterion;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -070033import org.onlab.packet.IpAddress;
34import org.onlab.packet.IpPrefix;
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;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -080050import org.projectfloodlight.openflow.types.U8;
alshabibeec3a062014-09-17 18:01:26 -070051import org.projectfloodlight.openflow.types.VlanPcp;
52import org.projectfloodlight.openflow.types.VlanVid;
53import org.slf4j.Logger;
54
Jonathan Hart86e59352014-10-22 10:42:16 -070055/**
56 * Builder for OpenFlow flow mods based on FlowRules.
57 */
58public abstract class FlowModBuilder {
alshabibeec3a062014-09-17 18:01:26 -070059
60 private final Logger log = getLogger(getClass());
61
62 private final OFFactory factory;
Jonathan Hart86e59352014-10-22 10:42:16 -070063 private final FlowRule flowRule;
alshabibeec3a062014-09-17 18:01:26 -070064 private final TrafficSelector selector;
65
Jonathan Hart86e59352014-10-22 10:42:16 -070066 /**
67 * Creates a new flow mod builder.
68 *
69 * @param flowRule the flow rule to transform into a flow mod
70 * @param factory the OpenFlow factory to use to build the flow mod
71 * @return the new flow mod builder
72 */
73 public static FlowModBuilder builder(FlowRule flowRule, OFFactory factory) {
74 switch (factory.getVersion()) {
75 case OF_10:
76 return new FlowModBuilderVer10(flowRule, factory);
77 case OF_13:
78 return new FlowModBuilderVer13(flowRule, factory);
79 default:
80 throw new UnsupportedOperationException(
81 "No flow mod builder for protocol version " + factory.getVersion());
82 }
83 }
alshabibeec3a062014-09-17 18:01:26 -070084
Jonathan Hart86e59352014-10-22 10:42:16 -070085 /**
86 * Constructs a flow mod builder.
87 *
88 * @param flowRule the flow rule to transform into a flow mod
89 * @param factory the OpenFlow factory to use to build the flow mod
90 */
91 protected FlowModBuilder(FlowRule flowRule, OFFactory factory) {
alshabibeec3a062014-09-17 18:01:26 -070092 this.factory = factory;
Jonathan Hart86e59352014-10-22 10:42:16 -070093 this.flowRule = flowRule;
alshabibeec3a062014-09-17 18:01:26 -070094 this.selector = flowRule.selector();
alshabibeec3a062014-09-17 18:01:26 -070095 }
96
Jonathan Hart86e59352014-10-22 10:42:16 -070097 /**
98 * Builds an ADD flow mod.
99 *
100 * @return the flow mod
101 */
102 public abstract OFFlowAdd buildFlowAdd();
alshabibeec3a062014-09-17 18:01:26 -0700103
Jonathan Hart86e59352014-10-22 10:42:16 -0700104 /**
105 * Builds a MODIFY flow mod.
106 *
107 * @return the flow mod
108 */
109 public abstract OFFlowMod buildFlowMod();
alshabibeec3a062014-09-17 18:01:26 -0700110
Jonathan Hart86e59352014-10-22 10:42:16 -0700111 /**
112 * Builds a DELETE flow mod.
113 *
114 * @return the flow mod
115 */
116 public abstract OFFlowDelete buildFlowDel();
alshabibeec3a062014-09-17 18:01:26 -0700117
Jonathan Hart86e59352014-10-22 10:42:16 -0700118 /**
119 * Builds the match for the flow mod.
120 *
121 * @return the match
122 */
123 protected Match buildMatch() {
alshabibeec3a062014-09-17 18:01:26 -0700124 Match.Builder mBuilder = factory.buildMatch();
125 EthCriterion eth;
126 IPCriterion ip;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700127 TcpPortCriterion tp;
alshabibeec3a062014-09-17 18:01:26 -0700128 for (Criterion c : selector.criteria()) {
129 switch (c.type()) {
130 case IN_PORT:
131 PortCriterion inport = (PortCriterion) c;
132 mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong()));
133 break;
134 case ETH_SRC:
135 eth = (EthCriterion) c;
136 mBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(eth.mac().toLong()));
137 break;
138 case ETH_DST:
139 eth = (EthCriterion) c;
140 mBuilder.setExact(MatchField.ETH_DST, MacAddress.of(eth.mac().toLong()));
141 break;
142 case ETH_TYPE:
143 EthTypeCriterion ethType = (EthTypeCriterion) c;
144 mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType()));
145 break;
146 case IPV4_DST:
147 ip = (IPCriterion) c;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700148 if (ip.ip().prefixLength() != IpPrefix.MAX_INET_MASK_LENGTH) {
149 IpAddress maskAddr =
150 IpAddress.makeMaskPrefix(ip.ip().prefixLength());
151 Masked<IPv4Address> maskedIp =
152 Masked.of(IPv4Address.of(ip.ip().address().toInt()),
153 IPv4Address.of(maskAddr.toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700154 mBuilder.setMasked(MatchField.IPV4_DST, maskedIp);
155 } else {
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700156 mBuilder.setExact(MatchField.IPV4_DST,
157 IPv4Address.of(ip.ip().address().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700158 }
159 break;
160 case IPV4_SRC:
161 ip = (IPCriterion) c;
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700162 if (ip.ip().prefixLength() != IpPrefix.MAX_INET_MASK_LENGTH) {
163 IpAddress maskAddr =
164 IpAddress.makeMaskPrefix(ip.ip().prefixLength());
165 Masked<IPv4Address> maskedIp =
166 Masked.of(IPv4Address.of(ip.ip().address().toInt()),
167 IPv4Address.of(maskAddr.toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700168 mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp);
169 } else {
Pavlin Radoslavov855ea2d2014-10-30 15:32:39 -0700170 mBuilder.setExact(MatchField.IPV4_SRC,
171 IPv4Address.of(ip.ip().address().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700172 }
173 break;
174 case IP_PROTO:
175 IPProtocolCriterion p = (IPProtocolCriterion) c;
176 mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol()));
177 break;
178 case VLAN_PCP:
179 VlanPcpCriterion vpcp = (VlanPcpCriterion) c;
180 mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority()));
181 break;
182 case VLAN_VID:
183 VlanIdCriterion vid = (VlanIdCriterion) c;
184 mBuilder.setExact(MatchField.VLAN_VID,
185 OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort())));
186 break;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700187 case TCP_DST:
188 tp = (TcpPortCriterion) c;
189 mBuilder.setExact(MatchField.TCP_DST, TransportPort.of(tp.tcpPort()));
190 break;
191 case TCP_SRC:
192 tp = (TcpPortCriterion) c;
193 mBuilder.setExact(MatchField.TCP_SRC, TransportPort.of(tp.tcpPort()));
194 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700195 case OCH_SIGID:
196 LambdaCriterion lc = (LambdaCriterion) c;
197 mBuilder.setExact(MatchField.OCH_SIGID,
198 new CircuitSignalID((byte) 1, (byte) 2, lc.lambda(), (short) 1));
199 break;
Praseed Balakrishnan2dd5abd2014-11-03 14:56:28 -0800200 case OCH_SIGTYPE:
201 Criteria.OpticalSignalTypeCriterion sc =
202 (Criteria.OpticalSignalTypeCriterion) c;
203 mBuilder.setExact(MatchField.OCH_SIGTYPE,
204 U8.of(sc.signalType()));
205 break;
alshabibeec3a062014-09-17 18:01:26 -0700206 case ARP_OP:
207 case ARP_SHA:
208 case ARP_SPA:
209 case ARP_THA:
210 case ARP_TPA:
211 case ICMPV4_CODE:
212 case ICMPV4_TYPE:
213 case ICMPV6_CODE:
214 case ICMPV6_TYPE:
215 case IN_PHY_PORT:
216 case IPV6_DST:
217 case IPV6_EXTHDR:
218 case IPV6_FLABEL:
219 case IPV6_ND_SLL:
220 case IPV6_ND_TARGET:
221 case IPV6_ND_TLL:
222 case IPV6_SRC:
223 case IP_DSCP:
224 case IP_ECN:
225 case METADATA:
226 case MPLS_BOS:
227 case MPLS_LABEL:
228 case MPLS_TC:
229 case PBB_ISID:
230 case SCTP_DST:
231 case SCTP_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700232 case TUNNEL_ID:
233 case UDP_DST:
234 case UDP_SRC:
235 default:
236 log.warn("Match type {} not yet implemented.", c.type());
237 }
238 }
239 return mBuilder.build();
240 }
241
Jonathan Hart86e59352014-10-22 10:42:16 -0700242 /**
243 * Returns the flow rule for this builder.
244 *
245 * @return the flow rule
246 */
247 protected FlowRule flowRule() {
248 return flowRule;
249 }
alshabib219ebaa2014-09-22 15:41:24 -0700250
Jonathan Hart86e59352014-10-22 10:42:16 -0700251 /**
252 * Returns the factory used for building OpenFlow constructs.
253 *
254 * @return the factory
255 */
256 protected OFFactory factory() {
257 return factory;
258 }
alshabib219ebaa2014-09-22 15:41:24 -0700259
alshabibeec3a062014-09-17 18:01:26 -0700260}