Bug 4045 - ipv6 work around
[ovsdb.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / InboundNatService.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
10
11 import java.math.BigInteger;
12 import java.net.InetAddress;
13 import java.util.List;
14
15 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.InboundNatProvider;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
20 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
21 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
22 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.OF13Provider;
23 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
24 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
25 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
26 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
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.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
43 import org.osgi.framework.BundleContext;
44 import org.osgi.framework.ServiceReference;
45
46 public class InboundNatService extends AbstractServiceInstance implements ConfigInterface, InboundNatProvider {
47     public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg3.class;
48
49     public InboundNatService() {
50         super(Service.INBOUND_NAT);
51     }
52
53     public InboundNatService(Service service) {
54         super(service);
55     }
56
57     @Override
58     public Status programIpRewriteRule(Long dpid, Long inPort, String destSegId, InetAddress matchAddress,
59                                        InetAddress rewriteAddress, Action action) {
60         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
61
62         MatchBuilder matchBuilder = new MatchBuilder();
63         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
64
65         // Instructions List Stores Individual Instructions
66         InstructionsBuilder isb = new InstructionsBuilder();
67         List<Instruction> instructions = Lists.newArrayList();
68         InstructionBuilder ib = new InstructionBuilder();
69
70         MatchUtils.createInPortMatch(matchBuilder, dpid, inPort);
71         MatchUtils.createDstL3IPv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(matchAddress.getHostAddress()));
72
73         // Set register to indicate that rewrite took place
74         ActionBuilder actionBuilder = new ActionBuilder();
75         actionBuilder.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
76                 new BigInteger(destSegId)));
77
78         // Set Dest IP address and set REG_FIELD
79         InstructionUtils.createNwDstInstructions(ib,
80                 MatchUtils.iPv4PrefixFromIPv4Address(rewriteAddress.getHostAddress()), actionBuilder);
81         ib.setOrder(0);
82         ib.setKey(new InstructionKey(0));
83         instructions.add(ib.build());
84
85         // Goto Next Table
86         ib = getMutablePipelineInstructionBuilder();
87         ib.setOrder(1);
88         ib.setKey(new InstructionKey(1));
89         instructions.add(ib.build());
90
91         FlowBuilder flowBuilder = new FlowBuilder();
92         flowBuilder.setMatch(matchBuilder.build());
93         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
94
95         String flowId = "InboundNAT_" + inPort + "_" + destSegId + "_" + matchAddress.getHostAddress();
96         flowBuilder.setId(new FlowId(flowId));
97         FlowKey key = new FlowKey(new FlowId(flowId));
98         flowBuilder.setBarrier(true);
99         flowBuilder.setTableId(this.getTable());
100         flowBuilder.setKey(key);
101         flowBuilder.setPriority(1024);
102         flowBuilder.setFlowName(flowId);
103         flowBuilder.setHardTimeout(0);
104         flowBuilder.setIdleTimeout(0);
105
106         if (action.equals(Action.ADD)) {
107             writeFlow(flowBuilder, nodeBuilder);
108         } else {
109             removeFlow(flowBuilder, nodeBuilder);
110         }
111
112         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
113         return new Status(StatusCode.SUCCESS);
114     }
115
116     @Override
117     public Status programIpRewriteExclusion(Long dpid, String segmentationId, String excludedCidr,
118                                             Action action) {
119         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
120
121         MatchBuilder matchBuilder = new MatchBuilder();
122         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
123
124         // Instructions List Stores Individual Instructions
125         InstructionsBuilder isb = new InstructionsBuilder();
126         List<Instruction> instructions = Lists.newArrayList();
127         InstructionBuilder ib;
128
129         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
130         MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
131
132         // Goto Next Table
133         ib = getMutablePipelineInstructionBuilder();
134         ib.setOrder(0);
135         ib.setKey(new InstructionKey(0));
136         instructions.add(ib.build());
137
138         FlowBuilder flowBuilder = new FlowBuilder();
139         flowBuilder.setMatch(matchBuilder.build());
140         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
141
142         String flowId = "InboundNATExclusion_" + segmentationId + "_" + excludedCidr;
143         flowBuilder.setId(new FlowId(flowId));
144         FlowKey key = new FlowKey(new FlowId(flowId));
145         flowBuilder.setBarrier(true);
146         flowBuilder.setTableId(this.getTable());
147         flowBuilder.setKey(key);
148         flowBuilder.setPriority(1024);
149         flowBuilder.setFlowName(flowId);
150         flowBuilder.setHardTimeout(0);
151         flowBuilder.setIdleTimeout(0);
152
153         if (action.equals(Action.ADD)) {
154             writeFlow(flowBuilder, nodeBuilder);
155         } else {
156             removeFlow(flowBuilder, nodeBuilder);
157         }
158
159         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
160         return new Status(StatusCode.SUCCESS);
161     }
162
163     @Override
164     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
165         super.setDependencies(bundleContext.getServiceReference(InboundNatProvider.class.getName()), this);
166     }
167
168     @Override
169     public void setDependencies(Object impl) {}
170 }