Update .gitreview for new repo
[netvirt.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / netvirt / 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.netvirt.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.netvirt.openstack.netvirt.api.OutboundNatProvider;
18 import org.opendaylight.netvirt.openstack.netvirt.api.StatusCode;
19 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
20 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.Service;
21 import org.opendaylight.netvirt.openstack.netvirt.api.Action;
22 import org.opendaylight.netvirt.openstack.netvirt.api.Status;
23 import org.opendaylight.netvirt.openstack.netvirt.providers.ConfigInterface;
24 import org.opendaylight.netvirt.utils.mdsal.openflow.ActionUtils;
25 import org.opendaylight.netvirt.utils.mdsal.openflow.FlowUtils;
26 import org.opendaylight.netvirt.utils.mdsal.openflow.InstructionUtils;
27 import org.opendaylight.netvirt.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.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.address.address.Ipv4Builder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
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.instruction.ApplyActionsCaseBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
42
43 import com.google.common.collect.Lists;
44 import org.osgi.framework.BundleContext;
45 import org.osgi.framework.ServiceReference;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 public class OutboundNatService extends AbstractServiceInstance implements OutboundNatProvider, ConfigInterface {
50     private static final Logger LOG = LoggerFactory.getLogger(OutboundNatService.class);
51
52     public OutboundNatService() {
53         super(Service.OUTBOUND_NAT);
54     }
55
56     public OutboundNatService(Service service) {
57         super(service);
58     }
59
60     @Override
61     public Status programIpRewriteRule(Long dpidLong,
62                                        String matchSegmentationId,
63                                        String matchDestMacAddress,
64                                        InetAddress matchSrcAddress,
65                                        String rewriteSrcMacAddress,
66                                        String rewriteDestMacAddress,
67                                        InetAddress rewriteSrcAddress,
68                                        Long OutPort,
69                                        Action action) {
70         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dpidLong);
71         FlowBuilder flowBuilder = new FlowBuilder();
72         String flowName = "OutboundNAT_" + matchSegmentationId + "_" + matchSrcAddress.getHostAddress();
73         FlowUtils.initFlowBuilder(flowBuilder, flowName, getTable()).setPriority(512);
74
75         MatchBuilder matchBuilder = new MatchBuilder();
76         MatchUtils.createDmacIpSaMatch(matchBuilder,
77                 matchDestMacAddress,
78                 MatchUtils.iPv4PrefixFromIPv4Address(matchSrcAddress.getHostAddress()),
79                 matchSegmentationId);
80         flowBuilder.setMatch(matchBuilder.build());
81
82         if (action.equals(Action.ADD)) {
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.setInstructions(isb.setInstruction(instructions).build());
135
136             writeFlow(flowBuilder, nodeBuilder);
137         } else {
138             removeFlow(flowBuilder, nodeBuilder);
139         }
140
141         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
142         return new Status(StatusCode.SUCCESS);
143     }
144
145     @Override
146     public Status programIpRewriteExclusion(Long dpid, String segmentationId, String excludedCidr,
147                                             Action action) {
148         String ipAddress = excludedCidr.substring(0, excludedCidr.indexOf("/"));
149         InetAddress inetAddress;
150         try {
151             inetAddress = InetAddress.getByName(ipAddress);
152         } catch (UnknownHostException e) {
153             return new Status(StatusCode.BADREQUEST);
154         }
155         if (inetAddress instanceof Inet6Address) {
156             // WORKAROUND: For now ipv6 is not supported
157             // TODO: implement ipv6 cidr case
158             LOG.debug("ipv6 cidr is not implemented yet. cidr {}",
159                       excludedCidr);
160             return new Status(StatusCode.NOTIMPLEMENTED);
161         }
162
163         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dpid);
164         FlowBuilder flowBuilder = new FlowBuilder();
165         String flowName = "OutboundNATExclusion_" + segmentationId + "_" + excludedCidr;
166         FlowUtils.initFlowBuilder(flowBuilder, flowName, getTable()).setPriority(1024);
167
168         MatchBuilder matchBuilder = new MatchBuilder();
169         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
170
171         MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
172         flowBuilder.setMatch(matchBuilder.build());
173
174         if (action.equals(Action.ADD)) {
175             // Instructions List Stores Individual Instructions
176             InstructionsBuilder isb = new InstructionsBuilder();
177             List<Instruction> instructions = Lists.newArrayList();
178             InstructionBuilder ib;
179
180             // Goto Next Table
181             ib = getMutablePipelineInstructionBuilder();
182             ib.setOrder(0);
183             ib.setKey(new InstructionKey(0));
184             instructions.add(ib.build());
185
186             flowBuilder.setInstructions(isb.setInstruction(instructions).build());
187
188             writeFlow(flowBuilder, nodeBuilder);
189         } else {
190             removeFlow(flowBuilder, nodeBuilder);
191         }
192
193         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
194         return new Status(StatusCode.SUCCESS);
195     }
196
197     @Override
198     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
199         super.setDependencies(bundleContext.getServiceReference(OutboundNatProvider.class.getName()), this);
200     }
201
202     @Override
203     public void setDependencies(Object impl) {
204
205     }
206 }