Merge "Unit test for ovsdb.southbound.ovsdb.transact"
[ovsdb.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / OutboundNatService.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.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 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 public class OutboundNatService extends AbstractServiceInstance implements OutboundNatProvider, ConfigInterface {
53     private static final Logger LOG = LoggerFactory.getLogger(OutboundNatService.class);
54
55     public OutboundNatService() {
56         super(Service.OUTBOUND_NAT);
57     }
58
59     public OutboundNatService(Service service) {
60         super(service);
61     }
62
63     @Override
64     public Status programIpRewriteRule(Long dpidLong,
65                                        String matchSegmentationId,
66                                        String matchDestMacAddress,
67                                        InetAddress matchSrcAddress,
68                                        String rewriteSrcMacAddress,
69                                        String rewriteDestMacAddress,
70                                        InetAddress rewriteSrcAddress,
71                                        Long OutPort,
72                                        Action action) {
73         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpidLong;
74
75         MatchBuilder matchBuilder = new MatchBuilder();
76         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
77
78         MatchUtils.createDmacIpSaMatch(matchBuilder,
79                 matchDestMacAddress,
80                 MatchUtils.iPv4PrefixFromIPv4Address(matchSrcAddress.getHostAddress()),
81                 matchSegmentationId);
82
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();
88
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 =
92                 Lists.newArrayList();
93
94         // Set source Mac address
95         ab.setAction(ActionUtils.setDlSrcAction(new MacAddress(rewriteSrcMacAddress)));
96         ab.setOrder(0);
97         ab.setKey(new ActionKey(0));
98         actionList.add(ab.build());
99
100         // DecTTL
101         ab.setAction(ActionUtils.decNwTtlAction());
102         ab.setOrder(1);
103         ab.setKey(new ActionKey(1));
104         actionList.add(ab.build());
105
106         // Set Destination Mac address
107         ab.setAction(ActionUtils.setDlDstAction(new MacAddress(rewriteDestMacAddress)));
108         ab.setOrder(2);
109         ab.setKey(new ActionKey(2));
110         actionList.add(ab.build());
111
112         // Set source Ip address
113         Ipv4Builder ipb = new Ipv4Builder().setIpv4Address(
114                 MatchUtils.iPv4PrefixFromIPv4Address(rewriteSrcAddress.getHostAddress()));
115         ab.setAction(ActionUtils.setNwSrcAction(ipb.build()));
116         ab.setOrder(3);
117         ab.setKey(new ActionKey(3));
118         actionList.add(ab.build());
119
120         // Create Apply Actions Instruction
121         aab.setAction(actionList);
122         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
123         ib.setOrder(0);
124         ib.setKey(new InstructionKey(0));
125         instructions_tmp.add(ib.build());
126
127         // Set the Output Port/Iface
128         ib = new InstructionBuilder();
129         InstructionUtils.addOutputPortInstructions(ib, dpidLong, OutPort, instructions_tmp);
130         ib.setOrder(0);
131         ib.setKey(new InstructionKey(0));
132         instructions.add(ib.build());
133
134         FlowBuilder flowBuilder = new FlowBuilder();
135         flowBuilder.setMatch(matchBuilder.build());
136         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
137
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);
148
149         if (action.equals(Action.ADD)) {
150             writeFlow(flowBuilder, nodeBuilder);
151         } else {
152             removeFlow(flowBuilder, nodeBuilder);
153         }
154
155         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
156         return new Status(StatusCode.SUCCESS);
157     }
158
159     @Override
160     public Status programIpRewriteExclusion(Long dpid, String segmentationId, String excludedCidr,
161                                             Action action) {
162         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
163
164         MatchBuilder matchBuilder = new MatchBuilder();
165         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
166
167         // Instructions List Stores Individual Instructions
168         InstructionsBuilder isb = new InstructionsBuilder();
169         List<Instruction> instructions = Lists.newArrayList();
170         InstructionBuilder ib;
171
172         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
173         String ipAddress = excludedCidr.substring(0, excludedCidr.indexOf("/"));
174         InetAddress inetAddress;
175         try {
176             inetAddress = InetAddress.getByName(ipAddress);
177         } catch (UnknownHostException e) {
178             return new Status(StatusCode.BADREQUEST);
179         }
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 {}",
184                       excludedCidr);
185             return new Status(StatusCode.NOTIMPLEMENTED);
186         }
187         MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
188
189         // Goto Next Table
190         ib = getMutablePipelineInstructionBuilder();
191         ib.setOrder(0);
192         ib.setKey(new InstructionKey(0));
193         instructions.add(ib.build());
194
195         FlowBuilder flowBuilder = new FlowBuilder();
196         flowBuilder.setMatch(matchBuilder.build());
197         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
198
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);
209
210         if (action.equals(Action.ADD)) {
211             writeFlow(flowBuilder, nodeBuilder);
212         } else {
213             removeFlow(flowBuilder, nodeBuilder);
214         }
215
216         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
217         return new Status(StatusCode.SUCCESS);
218     }
219
220     @Override
221     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
222         super.setDependencies(bundleContext.getServiceReference(OutboundNatProvider.class.getName()), this);
223     }
224
225     @Override
226     public void setDependencies(Object impl) {
227
228     }
229 }