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