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