6e35c1569077c5a9a3a156507d0152f08e9156fb
[ovsdb.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / OutboundNatService.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
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  * Authors : Madhu Venugopal, Dave Tucker
9  */
10
11 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
12
13 import java.math.BigInteger;
14 import java.net.InetAddress;
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.L3ForwardingProvider;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.OutboundNatProvider;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
23 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
24 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
25 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.OF13Provider;
26 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
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.flow.inventory.rev130819.FlowId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
39
40 import com.google.common.collect.Lists;
41 import org.osgi.framework.BundleContext;
42 import org.osgi.framework.ServiceReference;
43
44 public class OutboundNatService extends AbstractServiceInstance implements OutboundNatProvider, ConfigInterface {
45     public OutboundNatService() {
46         super(Service.OUTBOUND_NAT);
47     }
48
49     public OutboundNatService(Service service) {
50         super(service);
51     }
52
53     @Override
54     public Status programIpRewriteRule(Long dpid, String segmentationId, InetAddress matchAddress,
55                                        InetAddress rewriteAddress, Action action) {
56         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
57
58         MatchBuilder matchBuilder = new MatchBuilder();
59         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
60
61         // Instructions List Stores Individual Instructions
62         InstructionsBuilder isb = new InstructionsBuilder();
63         List<Instruction> instructions = Lists.newArrayList();
64         InstructionBuilder ib = new InstructionBuilder();
65
66         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
67         MatchUtils.createDstL3IPv4Match(matchBuilder,
68                                         MatchUtils.iPv4PrefixFromIPv4Address(matchAddress.getHostAddress()));
69
70         // Set Dest IP address
71         InstructionUtils.createNwDstInstructions(ib,
72                                                  MatchUtils.iPv4PrefixFromIPv4Address(rewriteAddress.getHostAddress()));
73         ib.setOrder(0);
74         ib.setKey(new InstructionKey(0));
75         instructions.add(ib.build());
76
77         // Goto Next Table
78         ib = getMutablePipelineInstructionBuilder();
79         ib.setOrder(1);
80         ib.setKey(new InstructionKey(1));
81         instructions.add(ib.build());
82
83         FlowBuilder flowBuilder = new FlowBuilder();
84         flowBuilder.setMatch(matchBuilder.build());
85         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
86
87         String flowId = "OutboundNAT_" + segmentationId + "_" + rewriteAddress.getHostAddress();
88         flowBuilder.setId(new FlowId(flowId));
89         FlowKey key = new FlowKey(new FlowId(flowId));
90         flowBuilder.setBarrier(true);
91         flowBuilder.setTableId(this.getTable());
92         flowBuilder.setKey(key);
93         flowBuilder.setPriority(1024);
94         flowBuilder.setFlowName(flowId);
95         flowBuilder.setHardTimeout(0);
96         flowBuilder.setIdleTimeout(0);
97
98         if (action.equals(Action.ADD)) {
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 nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
112
113         MatchBuilder matchBuilder = new MatchBuilder();
114         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
115
116         // Instructions List Stores Individual Instructions
117         InstructionsBuilder isb = new InstructionsBuilder();
118         List<Instruction> instructions = Lists.newArrayList();
119         InstructionBuilder ib;
120
121         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
122         MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
123
124         // Goto Next Table
125         ib = getMutablePipelineInstructionBuilder();
126         ib.setOrder(0);
127         ib.setKey(new InstructionKey(0));
128         instructions.add(ib.build());
129
130         FlowBuilder flowBuilder = new FlowBuilder();
131         flowBuilder.setMatch(matchBuilder.build());
132         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
133
134         String flowId = "OutboundNATExclusion_" + segmentationId + "_" + excludedCidr;
135         flowBuilder.setId(new FlowId(flowId));
136         FlowKey key = new FlowKey(new FlowId(flowId));
137         flowBuilder.setBarrier(true);
138         flowBuilder.setTableId(this.getTable());
139         flowBuilder.setKey(key);
140         flowBuilder.setPriority(1024);
141         flowBuilder.setFlowName(flowId);
142         flowBuilder.setHardTimeout(0);
143         flowBuilder.setIdleTimeout(0);
144
145         if (action.equals(Action.ADD)) {
146             writeFlow(flowBuilder, nodeBuilder);
147         } else {
148             removeFlow(flowBuilder, nodeBuilder);
149         }
150
151         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
152         return new Status(StatusCode.SUCCESS);
153     }
154
155     @Override
156     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
157         super.setOrchestrator(bundleContext.getServiceReference(OutboundNatProvider.class.getName()), this);
158     }
159
160     @Override
161     public void setDependencies(Object impl) {
162
163     }
164 }