Merge "Unit test for ovsdb.southbound.ovsdb.transact"
[netvirt.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.net.Inet6Address;
14 import java.net.UnknownHostException;
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.InboundNatProvider;
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.opendaylight.action.types.rev131112.action.list.ActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
40
41 import com.google.common.collect.Lists;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
45 import org.osgi.framework.BundleContext;
46 import org.osgi.framework.ServiceReference;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public class InboundNatService extends AbstractServiceInstance implements ConfigInterface, InboundNatProvider {
51     private static final Logger LOG = LoggerFactory.getLogger(InboundNatService.class);
52     public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg3.class;
53
54     public InboundNatService() {
55         super(Service.INBOUND_NAT);
56     }
57
58     public InboundNatService(Service service) {
59         super(service);
60     }
61
62     @Override
63     public Status programIpRewriteRule(Long dpid, Long inPort, String destSegId, InetAddress matchAddress,
64                                        InetAddress rewriteAddress, Action action) {
65         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
66
67         MatchBuilder matchBuilder = new MatchBuilder();
68         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
69
70         // Instructions List Stores Individual Instructions
71         InstructionsBuilder isb = new InstructionsBuilder();
72         List<Instruction> instructions = Lists.newArrayList();
73         InstructionBuilder ib = new InstructionBuilder();
74
75         MatchUtils.createInPortMatch(matchBuilder, dpid, inPort);
76         MatchUtils.createDstL3IPv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(matchAddress.getHostAddress()));
77
78         // Set register to indicate that rewrite took place
79         ActionBuilder actionBuilder = new ActionBuilder();
80         actionBuilder.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
81                 new BigInteger(destSegId)));
82
83         // Set Dest IP address and set REG_FIELD
84         InstructionUtils.createNwDstInstructions(ib,
85                 MatchUtils.iPv4PrefixFromIPv4Address(rewriteAddress.getHostAddress()), actionBuilder);
86         ib.setOrder(0);
87         ib.setKey(new InstructionKey(0));
88         instructions.add(ib.build());
89
90         // Goto Next Table
91         ib = getMutablePipelineInstructionBuilder();
92         ib.setOrder(1);
93         ib.setKey(new InstructionKey(1));
94         instructions.add(ib.build());
95
96         FlowBuilder flowBuilder = new FlowBuilder();
97         flowBuilder.setMatch(matchBuilder.build());
98         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
99
100         String flowId = "InboundNAT_" + inPort + "_" + destSegId + "_" + matchAddress.getHostAddress();
101         flowBuilder.setId(new FlowId(flowId));
102         FlowKey key = new FlowKey(new FlowId(flowId));
103         flowBuilder.setBarrier(true);
104         flowBuilder.setTableId(this.getTable());
105         flowBuilder.setKey(key);
106         flowBuilder.setPriority(1024);
107         flowBuilder.setFlowName(flowId);
108         flowBuilder.setHardTimeout(0);
109         flowBuilder.setIdleTimeout(0);
110
111         if (action.equals(Action.ADD)) {
112             writeFlow(flowBuilder, nodeBuilder);
113         } else {
114             removeFlow(flowBuilder, nodeBuilder);
115         }
116
117         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
118         return new Status(StatusCode.SUCCESS);
119     }
120
121     @Override
122     public Status programIpRewriteExclusion(Long dpid, String segmentationId, String excludedCidr,
123                                             Action action) {
124         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
125
126         MatchBuilder matchBuilder = new MatchBuilder();
127         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
128
129         // Instructions List Stores Individual Instructions
130         InstructionsBuilder isb = new InstructionsBuilder();
131         List<Instruction> instructions = Lists.newArrayList();
132         InstructionBuilder ib;
133
134         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
135         String ipAddress = excludedCidr.substring(0, excludedCidr.indexOf("/"));
136         InetAddress inetAddress;
137         try {
138             inetAddress = InetAddress.getByName(ipAddress);
139         } catch (UnknownHostException e) {
140             return new Status(StatusCode.BADREQUEST);
141         }
142         if (inetAddress instanceof Inet6Address) {
143             // WORKAROUND: For now ipv6 is not supported
144             // TODO: implement ipv6 cidr case
145             LOG.debug("ipv6 cidr is not implemented yet. cidr {}",
146                       excludedCidr);
147             return new Status(StatusCode.NOTIMPLEMENTED);
148         }
149         MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
150
151         // Goto Next Table
152         ib = getMutablePipelineInstructionBuilder();
153         ib.setOrder(0);
154         ib.setKey(new InstructionKey(0));
155         instructions.add(ib.build());
156
157         FlowBuilder flowBuilder = new FlowBuilder();
158         flowBuilder.setMatch(matchBuilder.build());
159         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
160
161         String flowId = "InboundNATExclusion_" + segmentationId + "_" + excludedCidr;
162         flowBuilder.setId(new FlowId(flowId));
163         FlowKey key = new FlowKey(new FlowId(flowId));
164         flowBuilder.setBarrier(true);
165         flowBuilder.setTableId(this.getTable());
166         flowBuilder.setKey(key);
167         flowBuilder.setPriority(1024);
168         flowBuilder.setFlowName(flowId);
169         flowBuilder.setHardTimeout(0);
170         flowBuilder.setIdleTimeout(0);
171
172         if (action.equals(Action.ADD)) {
173             writeFlow(flowBuilder, nodeBuilder);
174         } else {
175             removeFlow(flowBuilder, nodeBuilder);
176         }
177
178         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
179         return new Status(StatusCode.SUCCESS);
180     }
181
182     @Override
183     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
184         super.setDependencies(bundleContext.getServiceReference(InboundNatProvider.class.getName()), this);
185     }
186
187     @Override
188     public void setDependencies(Object impl) {}
189 }