Merge "Making changes to LBaaSHandler and Activator to add logic for LB Neutron calls."
[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 org.opendaylight.controller.sal.core.Node;
14 import org.opendaylight.controller.sal.utils.Status;
15 import org.opendaylight.controller.sal.utils.StatusCode;
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.OutboundNatProvider;
19 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
20 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.OF13Provider;
21 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
22 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
23 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
34
35 import com.google.common.collect.Lists;
36
37 import java.math.BigInteger;
38 import java.net.InetAddress;
39 import java.util.List;
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 boolean isBridgeInPipeline (String nodeId) {
52         return true;
53     }
54
55     @Override
56     public Status programIpRewriteRule(Node node, Long dpid, String segmentationId, InetAddress matchAddress,
57                                        InetAddress rewriteAddress, Action action) {
58         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
59
60         MatchBuilder matchBuilder = new MatchBuilder();
61         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
62
63         // Instructions List Stores Individual Instructions
64         InstructionsBuilder isb = new InstructionsBuilder();
65         List<Instruction> instructions = Lists.newArrayList();
66         InstructionBuilder ib = new InstructionBuilder();
67
68         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
69         MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(matchAddress.getHostAddress()));
70
71         // Set Dest IP address
72         InstructionUtils.createNwDstInstructions(ib, new Ipv4Prefix(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_" + 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         writeFlow(flowBuilder, nodeBuilder);
98
99         if (action.equals(Action.ADD)) {
100             writeFlow(flowBuilder, nodeBuilder);
101         } else {
102             removeFlow(flowBuilder, nodeBuilder);
103         }
104
105         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
106         return new Status(StatusCode.SUCCESS);
107     }
108
109     @Override
110     public Status programIpRewriteExclusion(Node node, Long dpid, String segmentationId, String excludedCidr,
111                                             Action action) {
112         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
113
114         MatchBuilder matchBuilder = new MatchBuilder();
115         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
116
117         // Instructions List Stores Individual Instructions
118         InstructionsBuilder isb = new InstructionsBuilder();
119         List<Instruction> instructions = Lists.newArrayList();
120         InstructionBuilder ib;
121
122         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
123         MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
124
125         // Goto Next Table
126         ib = getMutablePipelineInstructionBuilder();
127         ib.setOrder(0);
128         ib.setKey(new InstructionKey(0));
129         instructions.add(ib.build());
130
131         FlowBuilder flowBuilder = new FlowBuilder();
132         flowBuilder.setMatch(matchBuilder.build());
133         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
134
135         String flowId = "OutboundNATExclusion_" + excludedCidr;
136         flowBuilder.setId(new FlowId(flowId));
137         FlowKey key = new FlowKey(new FlowId(flowId));
138         flowBuilder.setBarrier(true);
139         flowBuilder.setTableId(this.getTable());
140         flowBuilder.setKey(key);
141         flowBuilder.setPriority(1024);
142         flowBuilder.setFlowName(flowId);
143         flowBuilder.setHardTimeout(0);
144         flowBuilder.setIdleTimeout(0);
145         writeFlow(flowBuilder, nodeBuilder);
146
147         if (action.equals(Action.ADD)) {
148             writeFlow(flowBuilder, nodeBuilder);
149         } else {
150             removeFlow(flowBuilder, nodeBuilder);
151         }
152
153         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
154         return new Status(StatusCode.SUCCESS);
155     }
156 }