blob: 85c900b89ad28e873084607a886159455eaaff85 [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;
alshabibeec3a062014-09-17 18:01:26 -070022import org.onlab.onos.net.flow.criteria.Criteria.EthCriterion;
23import org.onlab.onos.net.flow.criteria.Criteria.EthTypeCriterion;
24import org.onlab.onos.net.flow.criteria.Criteria.IPCriterion;
25import org.onlab.onos.net.flow.criteria.Criteria.IPProtocolCriterion;
Marc De Leenheer49087752014-10-23 13:54:09 -070026import org.onlab.onos.net.flow.criteria.Criteria.LambdaCriterion;
alshabibeec3a062014-09-17 18:01:26 -070027import org.onlab.onos.net.flow.criteria.Criteria.PortCriterion;
Jonathan Hart34bc6142014-10-17 11:00:43 -070028import org.onlab.onos.net.flow.criteria.Criteria.TcpPortCriterion;
alshabibeec3a062014-09-17 18:01:26 -070029import org.onlab.onos.net.flow.criteria.Criteria.VlanIdCriterion;
30import org.onlab.onos.net.flow.criteria.Criteria.VlanPcpCriterion;
31import org.onlab.onos.net.flow.criteria.Criterion;
alshabibeec3a062014-09-17 18:01:26 -070032import org.projectfloodlight.openflow.protocol.OFFactory;
alshabib193525b2014-10-08 18:58:03 -070033import org.projectfloodlight.openflow.protocol.OFFlowAdd;
34import org.projectfloodlight.openflow.protocol.OFFlowDelete;
alshabibeec3a062014-09-17 18:01:26 -070035import org.projectfloodlight.openflow.protocol.OFFlowMod;
alshabibeec3a062014-09-17 18:01:26 -070036import org.projectfloodlight.openflow.protocol.match.Match;
37import org.projectfloodlight.openflow.protocol.match.MatchField;
Marc De Leenheer49087752014-10-23 13:54:09 -070038import org.projectfloodlight.openflow.types.CircuitSignalID;
alshabibeec3a062014-09-17 18:01:26 -070039import org.projectfloodlight.openflow.types.EthType;
40import org.projectfloodlight.openflow.types.IPv4Address;
41import org.projectfloodlight.openflow.types.IpProtocol;
42import org.projectfloodlight.openflow.types.MacAddress;
43import org.projectfloodlight.openflow.types.Masked;
alshabibeec3a062014-09-17 18:01:26 -070044import org.projectfloodlight.openflow.types.OFPort;
45import org.projectfloodlight.openflow.types.OFVlanVidMatch;
Jonathan Hart34bc6142014-10-17 11:00:43 -070046import org.projectfloodlight.openflow.types.TransportPort;
alshabibeec3a062014-09-17 18:01:26 -070047import org.projectfloodlight.openflow.types.VlanPcp;
48import org.projectfloodlight.openflow.types.VlanVid;
49import org.slf4j.Logger;
50
Jonathan Hart86e59352014-10-22 10:42:16 -070051/**
52 * Builder for OpenFlow flow mods based on FlowRules.
53 */
54public abstract class FlowModBuilder {
alshabibeec3a062014-09-17 18:01:26 -070055
56 private final Logger log = getLogger(getClass());
57
58 private final OFFactory factory;
Jonathan Hart86e59352014-10-22 10:42:16 -070059 private final FlowRule flowRule;
alshabibeec3a062014-09-17 18:01:26 -070060 private final TrafficSelector selector;
61
Jonathan Hart86e59352014-10-22 10:42:16 -070062 /**
63 * Creates a new flow mod builder.
64 *
65 * @param flowRule the flow rule to transform into a flow mod
66 * @param factory the OpenFlow factory to use to build the flow mod
67 * @return the new flow mod builder
68 */
69 public static FlowModBuilder builder(FlowRule flowRule, OFFactory factory) {
70 switch (factory.getVersion()) {
71 case OF_10:
72 return new FlowModBuilderVer10(flowRule, factory);
73 case OF_13:
74 return new FlowModBuilderVer13(flowRule, factory);
75 default:
76 throw new UnsupportedOperationException(
77 "No flow mod builder for protocol version " + factory.getVersion());
78 }
79 }
alshabibeec3a062014-09-17 18:01:26 -070080
Jonathan Hart86e59352014-10-22 10:42:16 -070081 /**
82 * Constructs a flow mod builder.
83 *
84 * @param flowRule the flow rule to transform into a flow mod
85 * @param factory the OpenFlow factory to use to build the flow mod
86 */
87 protected FlowModBuilder(FlowRule flowRule, OFFactory factory) {
alshabibeec3a062014-09-17 18:01:26 -070088 this.factory = factory;
Jonathan Hart86e59352014-10-22 10:42:16 -070089 this.flowRule = flowRule;
alshabibeec3a062014-09-17 18:01:26 -070090 this.selector = flowRule.selector();
alshabibeec3a062014-09-17 18:01:26 -070091 }
92
Jonathan Hart86e59352014-10-22 10:42:16 -070093 /**
94 * Builds an ADD flow mod.
95 *
96 * @return the flow mod
97 */
98 public abstract OFFlowAdd buildFlowAdd();
alshabibeec3a062014-09-17 18:01:26 -070099
Jonathan Hart86e59352014-10-22 10:42:16 -0700100 /**
101 * Builds a MODIFY flow mod.
102 *
103 * @return the flow mod
104 */
105 public abstract OFFlowMod buildFlowMod();
alshabibeec3a062014-09-17 18:01:26 -0700106
Jonathan Hart86e59352014-10-22 10:42:16 -0700107 /**
108 * Builds a DELETE flow mod.
109 *
110 * @return the flow mod
111 */
112 public abstract OFFlowDelete buildFlowDel();
alshabibeec3a062014-09-17 18:01:26 -0700113
Jonathan Hart86e59352014-10-22 10:42:16 -0700114 /**
115 * Builds the match for the flow mod.
116 *
117 * @return the match
118 */
119 protected Match buildMatch() {
alshabibeec3a062014-09-17 18:01:26 -0700120 Match.Builder mBuilder = factory.buildMatch();
121 EthCriterion eth;
122 IPCriterion ip;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700123 TcpPortCriterion tp;
alshabibeec3a062014-09-17 18:01:26 -0700124 for (Criterion c : selector.criteria()) {
125 switch (c.type()) {
126 case IN_PORT:
127 PortCriterion inport = (PortCriterion) c;
128 mBuilder.setExact(MatchField.IN_PORT, OFPort.of((int) inport.port().toLong()));
129 break;
130 case ETH_SRC:
131 eth = (EthCriterion) c;
132 mBuilder.setExact(MatchField.ETH_SRC, MacAddress.of(eth.mac().toLong()));
133 break;
134 case ETH_DST:
135 eth = (EthCriterion) c;
136 mBuilder.setExact(MatchField.ETH_DST, MacAddress.of(eth.mac().toLong()));
137 break;
138 case ETH_TYPE:
139 EthTypeCriterion ethType = (EthTypeCriterion) c;
140 mBuilder.setExact(MatchField.ETH_TYPE, EthType.of(ethType.ethType()));
141 break;
142 case IPV4_DST:
143 ip = (IPCriterion) c;
144 if (ip.ip().isMasked()) {
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700145 Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()),
146 IPv4Address.of(ip.ip().netmask().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700147 mBuilder.setMasked(MatchField.IPV4_DST, maskedIp);
148 } else {
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700149 mBuilder.setExact(MatchField.IPV4_DST, IPv4Address.of(ip.ip().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700150 }
151 break;
152 case IPV4_SRC:
153 ip = (IPCriterion) c;
154 if (ip.ip().isMasked()) {
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700155 Masked<IPv4Address> maskedIp = Masked.of(IPv4Address.of(ip.ip().toInt()),
156 IPv4Address.of(ip.ip().netmask().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700157 mBuilder.setMasked(MatchField.IPV4_SRC, maskedIp);
158 } else {
Jonathan Hartbcae7bd2014-10-16 10:24:41 -0700159 mBuilder.setExact(MatchField.IPV4_SRC, IPv4Address.of(ip.ip().toInt()));
alshabibeec3a062014-09-17 18:01:26 -0700160 }
161 break;
162 case IP_PROTO:
163 IPProtocolCriterion p = (IPProtocolCriterion) c;
164 mBuilder.setExact(MatchField.IP_PROTO, IpProtocol.of(p.protocol()));
165 break;
166 case VLAN_PCP:
167 VlanPcpCriterion vpcp = (VlanPcpCriterion) c;
168 mBuilder.setExact(MatchField.VLAN_PCP, VlanPcp.of(vpcp.priority()));
169 break;
170 case VLAN_VID:
171 VlanIdCriterion vid = (VlanIdCriterion) c;
172 mBuilder.setExact(MatchField.VLAN_VID,
173 OFVlanVidMatch.ofVlanVid(VlanVid.ofVlan(vid.vlanId().toShort())));
174 break;
Jonathan Hart34bc6142014-10-17 11:00:43 -0700175 case TCP_DST:
176 tp = (TcpPortCriterion) c;
177 mBuilder.setExact(MatchField.TCP_DST, TransportPort.of(tp.tcpPort()));
178 break;
179 case TCP_SRC:
180 tp = (TcpPortCriterion) c;
181 mBuilder.setExact(MatchField.TCP_SRC, TransportPort.of(tp.tcpPort()));
182 break;
Marc De Leenheer49087752014-10-23 13:54:09 -0700183 case OCH_SIGID:
184 LambdaCriterion lc = (LambdaCriterion) c;
185 mBuilder.setExact(MatchField.OCH_SIGID,
186 new CircuitSignalID((byte) 1, (byte) 2, lc.lambda(), (short) 1));
187 break;
alshabibeec3a062014-09-17 18:01:26 -0700188 case ARP_OP:
189 case ARP_SHA:
190 case ARP_SPA:
191 case ARP_THA:
192 case ARP_TPA:
193 case ICMPV4_CODE:
194 case ICMPV4_TYPE:
195 case ICMPV6_CODE:
196 case ICMPV6_TYPE:
197 case IN_PHY_PORT:
198 case IPV6_DST:
199 case IPV6_EXTHDR:
200 case IPV6_FLABEL:
201 case IPV6_ND_SLL:
202 case IPV6_ND_TARGET:
203 case IPV6_ND_TLL:
204 case IPV6_SRC:
205 case IP_DSCP:
206 case IP_ECN:
207 case METADATA:
208 case MPLS_BOS:
209 case MPLS_LABEL:
210 case MPLS_TC:
211 case PBB_ISID:
212 case SCTP_DST:
213 case SCTP_SRC:
alshabibeec3a062014-09-17 18:01:26 -0700214 case TUNNEL_ID:
215 case UDP_DST:
216 case UDP_SRC:
217 default:
218 log.warn("Match type {} not yet implemented.", c.type());
219 }
220 }
221 return mBuilder.build();
222 }
223
Jonathan Hart86e59352014-10-22 10:42:16 -0700224 /**
225 * Returns the flow rule for this builder.
226 *
227 * @return the flow rule
228 */
229 protected FlowRule flowRule() {
230 return flowRule;
231 }
alshabib219ebaa2014-09-22 15:41:24 -0700232
Jonathan Hart86e59352014-10-22 10:42:16 -0700233 /**
234 * Returns the factory used for building OpenFlow constructs.
235 *
236 * @return the factory
237 */
238 protected OFFactory factory() {
239 return factory;
240 }
alshabib219ebaa2014-09-22 15:41:24 -0700241
alshabibeec3a062014-09-17 18:01:26 -0700242}