Bug-2094 : L3 North-South does not work -- fix outbound rewrite
[ovsdb.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / OutboundNatService.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
3  *
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
7  *
8  * Authors : Madhu Venugopal, Dave Tucker
9  */
10
11 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
12
13 import java.math.BigInteger;
14 import java.net.InetAddress;
15 import java.util.List;
16
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;
45
46 import com.google.common.collect.Lists;
47 import org.osgi.framework.BundleContext;
48 import org.osgi.framework.ServiceReference;
49
50 public class OutboundNatService extends AbstractServiceInstance implements OutboundNatProvider, ConfigInterface {
51     public OutboundNatService() {
52         super(Service.OUTBOUND_NAT);
53     }
54
55     public OutboundNatService(Service service) {
56         super(service);
57     }
58
59     @Override
60     public Status programIpRewriteRule(Long dpidLong,
61                                        String matchSegmentationId,
62                                        String matchDestMacAddress,
63                                        InetAddress matchSrcAddress,
64                                        String rewriteSrcMacAddress,
65                                        String rewriteDestMacAddress,
66                                        InetAddress rewriteSrcAddress,
67                                        Long OutPort,
68                                        Action action) {
69         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpidLong;
70
71         MatchBuilder matchBuilder = new MatchBuilder();
72         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
73
74         MatchUtils.createDmacIpSaMatch(matchBuilder,
75                 matchDestMacAddress,
76                 MatchUtils.iPv4PrefixFromIPv4Address(matchSrcAddress.getHostAddress()),
77                 matchSegmentationId);
78
79         // Instructions List Stores Individual Instructions
80         InstructionsBuilder isb = new InstructionsBuilder();
81         List<Instruction> instructions = Lists.newArrayList();
82         List<Instruction> instructions_tmp = Lists.newArrayList();
83         InstructionBuilder ib = new InstructionBuilder();
84
85         ApplyActionsBuilder aab = new ApplyActionsBuilder();
86         ActionBuilder ab = new ActionBuilder();
87         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList =
88                 Lists.newArrayList();
89
90         // Set source Mac address
91         ab.setAction(ActionUtils.setDlSrcAction(new MacAddress(rewriteSrcMacAddress)));
92         ab.setOrder(0);
93         ab.setKey(new ActionKey(0));
94         actionList.add(ab.build());
95
96         // DecTTL
97         ab.setAction(ActionUtils.decNwTtlAction());
98         ab.setOrder(1);
99         ab.setKey(new ActionKey(1));
100         actionList.add(ab.build());
101
102         // Set Destination Mac address
103         ab.setAction(ActionUtils.setDlDstAction(new MacAddress(rewriteDestMacAddress)));
104         ab.setOrder(2);
105         ab.setKey(new ActionKey(2));
106         actionList.add(ab.build());
107
108         // Set source Ip address
109         Ipv4Builder ipb = new Ipv4Builder().setIpv4Address(
110                 MatchUtils.iPv4PrefixFromIPv4Address(rewriteSrcAddress.getHostAddress()));
111         ab.setAction(ActionUtils.setNwSrcAction(ipb.build()));
112         ab.setOrder(3);
113         ab.setKey(new ActionKey(3));
114         actionList.add(ab.build());
115
116         // Create Apply Actions Instruction
117         aab.setAction(actionList);
118         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
119         ib.setOrder(0);
120         ib.setKey(new InstructionKey(0));
121         instructions_tmp.add(ib.build());
122
123         // Set the Output Port/Iface
124         ib = new InstructionBuilder();
125         InstructionUtils.addOutputPortInstructions(ib, dpidLong, OutPort, instructions_tmp);
126         ib.setOrder(0);
127         ib.setKey(new InstructionKey(0));
128         instructions.add(ib.build());
129
130         FlowBuilder flowBuilder = new FlowBuilder();
131         flowBuilder.setMatch(matchBuilder.build());
132         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
133
134         String flowId = "OutboundNAT_" + matchSegmentationId + "_" + matchSrcAddress.getHostAddress();
135         flowBuilder.setId(new FlowId(flowId));
136         FlowKey key = new FlowKey(new FlowId(flowId));
137         flowBuilder.setBarrier(true);
138         flowBuilder.setTableId(this.getTable());
139         flowBuilder.setKey(key);
140         flowBuilder.setPriority(512);
141         flowBuilder.setFlowName(flowId);
142         flowBuilder.setHardTimeout(0);
143         flowBuilder.setIdleTimeout(0);
144
145         if (action.equals(Action.ADD)) {
146             writeFlow(flowBuilder, nodeBuilder);
147         } else {
148             removeFlow(flowBuilder, nodeBuilder);
149         }
150
151         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
152         return new Status(StatusCode.SUCCESS);
153     }
154
155     @Override
156     public Status programIpRewriteExclusion(Long dpid, String segmentationId, String excludedCidr,
157                                             Action action) {
158         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
159
160         MatchBuilder matchBuilder = new MatchBuilder();
161         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
162
163         // Instructions List Stores Individual Instructions
164         InstructionsBuilder isb = new InstructionsBuilder();
165         List<Instruction> instructions = Lists.newArrayList();
166         InstructionBuilder ib;
167
168         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
169         MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
170
171         // Goto Next Table
172         ib = getMutablePipelineInstructionBuilder();
173         ib.setOrder(0);
174         ib.setKey(new InstructionKey(0));
175         instructions.add(ib.build());
176
177         FlowBuilder flowBuilder = new FlowBuilder();
178         flowBuilder.setMatch(matchBuilder.build());
179         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
180
181         String flowId = "OutboundNATExclusion_" + segmentationId + "_" + excludedCidr;
182         flowBuilder.setId(new FlowId(flowId));
183         FlowKey key = new FlowKey(new FlowId(flowId));
184         flowBuilder.setBarrier(true);
185         flowBuilder.setTableId(this.getTable());
186         flowBuilder.setKey(key);
187         flowBuilder.setPriority(1024);
188         flowBuilder.setFlowName(flowId);
189         flowBuilder.setHardTimeout(0);
190         flowBuilder.setIdleTimeout(0);
191
192         if (action.equals(Action.ADD)) {
193             writeFlow(flowBuilder, nodeBuilder);
194         } else {
195             removeFlow(flowBuilder, nodeBuilder);
196         }
197
198         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
199         return new Status(StatusCode.SUCCESS);
200     }
201
202     @Override
203     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
204         super.setDependencies(bundleContext.getServiceReference(OutboundNatProvider.class.getName()), this);
205     }
206
207     @Override
208     public void setDependencies(Object impl) {
209
210     }
211 }