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