2 * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
11 import java.math.BigInteger;
12 import java.net.InetAddress;
13 import java.net.Inet6Address;
14 import java.net.UnknownHostException;
15 import java.util.List;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.OutboundNatProvider;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
22 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
23 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
24 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.OF13Provider;
25 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
26 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
27 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
28 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
46 import com.google.common.collect.Lists;
47 import org.osgi.framework.BundleContext;
48 import org.osgi.framework.ServiceReference;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class OutboundNatService extends AbstractServiceInstance implements OutboundNatProvider, ConfigInterface {
53 private static final Logger LOG = LoggerFactory.getLogger(OutboundNatService.class);
55 public OutboundNatService() {
56 super(Service.OUTBOUND_NAT);
59 public OutboundNatService(Service service) {
64 public Status programIpRewriteRule(Long dpidLong,
65 String matchSegmentationId,
66 String matchDestMacAddress,
67 InetAddress matchSrcAddress,
68 String rewriteSrcMacAddress,
69 String rewriteDestMacAddress,
70 InetAddress rewriteSrcAddress,
73 String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpidLong;
75 MatchBuilder matchBuilder = new MatchBuilder();
76 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
78 MatchUtils.createDmacIpSaMatch(matchBuilder,
80 MatchUtils.iPv4PrefixFromIPv4Address(matchSrcAddress.getHostAddress()),
83 // Instructions List Stores Individual Instructions
84 InstructionsBuilder isb = new InstructionsBuilder();
85 List<Instruction> instructions = Lists.newArrayList();
86 List<Instruction> instructions_tmp = Lists.newArrayList();
87 InstructionBuilder ib = new InstructionBuilder();
89 ApplyActionsBuilder aab = new ApplyActionsBuilder();
90 ActionBuilder ab = new ActionBuilder();
91 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList =
94 // Set source Mac address
95 ab.setAction(ActionUtils.setDlSrcAction(new MacAddress(rewriteSrcMacAddress)));
97 ab.setKey(new ActionKey(0));
98 actionList.add(ab.build());
101 ab.setAction(ActionUtils.decNwTtlAction());
103 ab.setKey(new ActionKey(1));
104 actionList.add(ab.build());
106 // Set Destination Mac address
107 ab.setAction(ActionUtils.setDlDstAction(new MacAddress(rewriteDestMacAddress)));
109 ab.setKey(new ActionKey(2));
110 actionList.add(ab.build());
112 // Set source Ip address
113 Ipv4Builder ipb = new Ipv4Builder().setIpv4Address(
114 MatchUtils.iPv4PrefixFromIPv4Address(rewriteSrcAddress.getHostAddress()));
115 ab.setAction(ActionUtils.setNwSrcAction(ipb.build()));
117 ab.setKey(new ActionKey(3));
118 actionList.add(ab.build());
120 // Create Apply Actions Instruction
121 aab.setAction(actionList);
122 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
124 ib.setKey(new InstructionKey(0));
125 instructions_tmp.add(ib.build());
127 // Set the Output Port/Iface
128 ib = new InstructionBuilder();
129 InstructionUtils.addOutputPortInstructions(ib, dpidLong, OutPort, instructions_tmp);
131 ib.setKey(new InstructionKey(0));
132 instructions.add(ib.build());
134 FlowBuilder flowBuilder = new FlowBuilder();
135 flowBuilder.setMatch(matchBuilder.build());
136 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
138 String flowId = "OutboundNAT_" + matchSegmentationId + "_" + matchSrcAddress.getHostAddress();
139 flowBuilder.setId(new FlowId(flowId));
140 FlowKey key = new FlowKey(new FlowId(flowId));
141 flowBuilder.setBarrier(true);
142 flowBuilder.setTableId(this.getTable());
143 flowBuilder.setKey(key);
144 flowBuilder.setPriority(512);
145 flowBuilder.setFlowName(flowId);
146 flowBuilder.setHardTimeout(0);
147 flowBuilder.setIdleTimeout(0);
149 if (action.equals(Action.ADD)) {
150 writeFlow(flowBuilder, nodeBuilder);
152 removeFlow(flowBuilder, nodeBuilder);
155 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
156 return new Status(StatusCode.SUCCESS);
160 public Status programIpRewriteExclusion(Long dpid, String segmentationId, String excludedCidr,
162 String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
164 MatchBuilder matchBuilder = new MatchBuilder();
165 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
167 // Instructions List Stores Individual Instructions
168 InstructionsBuilder isb = new InstructionsBuilder();
169 List<Instruction> instructions = Lists.newArrayList();
170 InstructionBuilder ib;
172 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
173 String ipAddress = excludedCidr.substring(0, excludedCidr.indexOf("/"));
174 InetAddress inetAddress;
176 inetAddress = InetAddress.getByName(ipAddress);
177 } catch (UnknownHostException e) {
178 return new Status(StatusCode.BADREQUEST);
180 if (inetAddress instanceof Inet6Address) {
181 // WORKAROUND: For now ipv6 is not supported
182 // TODO: implement ipv6 cidr case
183 LOG.debug("ipv6 cidr is not implemented yet. cidr {}",
185 return new Status(StatusCode.NOTIMPLEMENTED);
187 MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
190 ib = getMutablePipelineInstructionBuilder();
192 ib.setKey(new InstructionKey(0));
193 instructions.add(ib.build());
195 FlowBuilder flowBuilder = new FlowBuilder();
196 flowBuilder.setMatch(matchBuilder.build());
197 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
199 String flowId = "OutboundNATExclusion_" + segmentationId + "_" + excludedCidr;
200 flowBuilder.setId(new FlowId(flowId));
201 FlowKey key = new FlowKey(new FlowId(flowId));
202 flowBuilder.setBarrier(true);
203 flowBuilder.setTableId(this.getTable());
204 flowBuilder.setKey(key);
205 flowBuilder.setPriority(1024);
206 flowBuilder.setFlowName(flowId);
207 flowBuilder.setHardTimeout(0);
208 flowBuilder.setIdleTimeout(0);
210 if (action.equals(Action.ADD)) {
211 writeFlow(flowBuilder, nodeBuilder);
213 removeFlow(flowBuilder, nodeBuilder);
216 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
217 return new Status(StatusCode.SUCCESS);
221 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
222 super.setDependencies(bundleContext.getServiceReference(OutboundNatProvider.class.getName()), this);
226 public void setDependencies(Object impl) {