2e70b8c26442aeb81cc68db642830f60613f700c
[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.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.openflow13.AbstractServiceInstance;
23 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.OF13Provider;
24 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
25 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
26 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
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
40 public class OutboundNatService extends AbstractServiceInstance implements OutboundNatProvider {
41     public OutboundNatService() {
42         super(Service.OUTBOUND_NAT);
43     }
44
45     public OutboundNatService(Service service) {
46         super(service);
47     }
48
49     @Override
50     public Status programIpRewriteRule(Long dpid, String segmentationId, InetAddress matchAddress,
51                                        InetAddress rewriteAddress, Action action) {
52         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
53
54         MatchBuilder matchBuilder = new MatchBuilder();
55         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
56
57         // Instructions List Stores Individual Instructions
58         InstructionsBuilder isb = new InstructionsBuilder();
59         List<Instruction> instructions = Lists.newArrayList();
60         InstructionBuilder ib = new InstructionBuilder();
61
62         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
63         MatchUtils.createDstL3IPv4Match(matchBuilder,
64                                         MatchUtils.iPv4PrefixFromIPv4Address(matchAddress.getHostAddress()));
65
66         // Set Dest IP address
67         InstructionUtils.createNwDstInstructions(ib,
68                                                  MatchUtils.iPv4PrefixFromIPv4Address(rewriteAddress.getHostAddress()));
69         ib.setOrder(0);
70         ib.setKey(new InstructionKey(0));
71         instructions.add(ib.build());
72
73         // Goto Next Table
74         ib = getMutablePipelineInstructionBuilder();
75         ib.setOrder(1);
76         ib.setKey(new InstructionKey(1));
77         instructions.add(ib.build());
78
79         FlowBuilder flowBuilder = new FlowBuilder();
80         flowBuilder.setMatch(matchBuilder.build());
81         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
82
83         String flowId = "OutboundNAT_" + segmentationId + "_" + rewriteAddress.getHostAddress();
84         flowBuilder.setId(new FlowId(flowId));
85         FlowKey key = new FlowKey(new FlowId(flowId));
86         flowBuilder.setBarrier(true);
87         flowBuilder.setTableId(this.getTable());
88         flowBuilder.setKey(key);
89         flowBuilder.setPriority(1024);
90         flowBuilder.setFlowName(flowId);
91         flowBuilder.setHardTimeout(0);
92         flowBuilder.setIdleTimeout(0);
93
94         if (action.equals(Action.ADD)) {
95             writeFlow(flowBuilder, nodeBuilder);
96         } else {
97             removeFlow(flowBuilder, nodeBuilder);
98         }
99
100         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
101         return new Status(StatusCode.SUCCESS);
102     }
103
104     @Override
105     public Status programIpRewriteExclusion(Long dpid, String segmentationId, String excludedCidr,
106                                             Action action) {
107         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
108
109         MatchBuilder matchBuilder = new MatchBuilder();
110         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
111
112         // Instructions List Stores Individual Instructions
113         InstructionsBuilder isb = new InstructionsBuilder();
114         List<Instruction> instructions = Lists.newArrayList();
115         InstructionBuilder ib;
116
117         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
118         MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
119
120         // Goto Next Table
121         ib = getMutablePipelineInstructionBuilder();
122         ib.setOrder(0);
123         ib.setKey(new InstructionKey(0));
124         instructions.add(ib.build());
125
126         FlowBuilder flowBuilder = new FlowBuilder();
127         flowBuilder.setMatch(matchBuilder.build());
128         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
129
130         String flowId = "OutboundNATExclusion_" + segmentationId + "_" + excludedCidr;
131         flowBuilder.setId(new FlowId(flowId));
132         FlowKey key = new FlowKey(new FlowId(flowId));
133         flowBuilder.setBarrier(true);
134         flowBuilder.setTableId(this.getTable());
135         flowBuilder.setKey(key);
136         flowBuilder.setPriority(1024);
137         flowBuilder.setFlowName(flowId);
138         flowBuilder.setHardTimeout(0);
139         flowBuilder.setIdleTimeout(0);
140
141         if (action.equals(Action.ADD)) {
142             writeFlow(flowBuilder, nodeBuilder);
143         } else {
144             removeFlow(flowBuilder, nodeBuilder);
145         }
146
147         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
148         return new Status(StatusCode.SUCCESS);
149     }
150 }