Bug-2094 : L3 North-South does not work -- fix inbound 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.InstructionUtils;
27 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
38
39 import com.google.common.collect.Lists;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.ServiceReference;
42
43 public class OutboundNatService extends AbstractServiceInstance implements OutboundNatProvider, ConfigInterface {
44     public OutboundNatService() {
45         super(Service.OUTBOUND_NAT);
46     }
47
48     public OutboundNatService(Service service) {
49         super(service);
50     }
51
52     @Override
53     public Status programIpRewriteRule(Long dpid, String segmentationId, InetAddress matchAddress,
54                                        InetAddress rewriteAddress, Action action) {
55         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
56
57         MatchBuilder matchBuilder = new MatchBuilder();
58         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
59
60         // Instructions List Stores Individual Instructions
61         InstructionsBuilder isb = new InstructionsBuilder();
62         List<Instruction> instructions = Lists.newArrayList();
63         InstructionBuilder ib = new InstructionBuilder();
64
65         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
66         MatchUtils.createDstL3IPv4Match(matchBuilder,
67                                         MatchUtils.iPv4PrefixFromIPv4Address(matchAddress.getHostAddress()));
68
69         // Set Dest IP address
70         InstructionUtils.createNwDstInstructions(ib,
71                 MatchUtils.iPv4PrefixFromIPv4Address(rewriteAddress.getHostAddress()),
72                 null);
73         ib.setOrder(0);
74         ib.setKey(new InstructionKey(0));
75         instructions.add(ib.build());
76
77         // Goto Next Table
78         ib = getMutablePipelineInstructionBuilder();
79         ib.setOrder(1);
80         ib.setKey(new InstructionKey(1));
81         instructions.add(ib.build());
82
83         FlowBuilder flowBuilder = new FlowBuilder();
84         flowBuilder.setMatch(matchBuilder.build());
85         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
86
87         String flowId = "OutboundNAT_" + segmentationId + "_" + rewriteAddress.getHostAddress();
88         flowBuilder.setId(new FlowId(flowId));
89         FlowKey key = new FlowKey(new FlowId(flowId));
90         flowBuilder.setBarrier(true);
91         flowBuilder.setTableId(this.getTable());
92         flowBuilder.setKey(key);
93         flowBuilder.setPriority(1024);
94         flowBuilder.setFlowName(flowId);
95         flowBuilder.setHardTimeout(0);
96         flowBuilder.setIdleTimeout(0);
97
98         if (action.equals(Action.ADD)) {
99             writeFlow(flowBuilder, nodeBuilder);
100         } else {
101             removeFlow(flowBuilder, nodeBuilder);
102         }
103
104         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
105         return new Status(StatusCode.SUCCESS);
106     }
107
108     @Override
109     public Status programIpRewriteExclusion(Long dpid, String segmentationId, String excludedCidr,
110                                             Action action) {
111         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
112
113         MatchBuilder matchBuilder = new MatchBuilder();
114         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
115
116         // Instructions List Stores Individual Instructions
117         InstructionsBuilder isb = new InstructionsBuilder();
118         List<Instruction> instructions = Lists.newArrayList();
119         InstructionBuilder ib;
120
121         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
122         MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
123
124         // Goto Next Table
125         ib = getMutablePipelineInstructionBuilder();
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 = "OutboundNATExclusion_" + segmentationId + "_" + excludedCidr;
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(1024);
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 void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
157         super.setDependencies(bundleContext.getServiceReference(OutboundNatProvider.class.getName()), this);
158     }
159
160     @Override
161     public void setDependencies(Object impl) {
162
163     }
164 }