2 * Copyright (C) 2014 Red Hat, Inc.
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
8 * Authors : Madhu Venugopal, Dave Tucker
11 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
13 import java.math.BigInteger;
14 import java.net.InetAddress;
15 import java.util.List;
17 import org.opendaylight.controller.sal.core.Node;
18 import org.opendaylight.controller.sal.utils.Status;
19 import org.opendaylight.controller.sal.utils.StatusCode;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.OutboundNatProvider;
23 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
24 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.OF13Provider;
25 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
26 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
27 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
39 import com.google.common.collect.Lists;
41 public class OutboundNatService extends AbstractServiceInstance implements OutboundNatProvider {
42 public OutboundNatService() {
43 super(Service.OUTBOUND_NAT);
46 public OutboundNatService(Service service) {
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;
55 MatchBuilder matchBuilder = new MatchBuilder();
56 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
58 // Instructions List Stores Individual Instructions
59 InstructionsBuilder isb = new InstructionsBuilder();
60 List<Instruction> instructions = Lists.newArrayList();
61 InstructionBuilder ib = new InstructionBuilder();
63 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
64 MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(matchAddress.getHostAddress()));
66 // Set Dest IP address
67 InstructionUtils.createNwDstInstructions(ib, new Ipv4Prefix(rewriteAddress.getHostAddress()));
69 ib.setKey(new InstructionKey(0));
70 instructions.add(ib.build());
73 ib = getMutablePipelineInstructionBuilder();
75 ib.setKey(new InstructionKey(1));
76 instructions.add(ib.build());
78 FlowBuilder flowBuilder = new FlowBuilder();
79 flowBuilder.setMatch(matchBuilder.build());
80 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
82 String flowId = "OutboundNAT_" + rewriteAddress.getHostAddress();
83 flowBuilder.setId(new FlowId(flowId));
84 FlowKey key = new FlowKey(new FlowId(flowId));
85 flowBuilder.setBarrier(true);
86 flowBuilder.setTableId(this.getTable());
87 flowBuilder.setKey(key);
88 flowBuilder.setPriority(1024);
89 flowBuilder.setFlowName(flowId);
90 flowBuilder.setHardTimeout(0);
91 flowBuilder.setIdleTimeout(0);
92 writeFlow(flowBuilder, nodeBuilder);
94 if (action.equals(Action.ADD)) {
95 writeFlow(flowBuilder, nodeBuilder);
97 removeFlow(flowBuilder, nodeBuilder);
100 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
101 return new Status(StatusCode.SUCCESS);
105 public Status programIpRewriteExclusion(Node node, Long dpid, String segmentationId, String excludedCidr,
107 String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
109 MatchBuilder matchBuilder = new MatchBuilder();
110 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
112 // Instructions List Stores Individual Instructions
113 InstructionsBuilder isb = new InstructionsBuilder();
114 List<Instruction> instructions = Lists.newArrayList();
115 InstructionBuilder ib;
117 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
118 MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
121 ib = getMutablePipelineInstructionBuilder();
123 ib.setKey(new InstructionKey(0));
124 instructions.add(ib.build());
126 FlowBuilder flowBuilder = new FlowBuilder();
127 flowBuilder.setMatch(matchBuilder.build());
128 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
130 String flowId = "OutboundNATExclusion_" + 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 writeFlow(flowBuilder, nodeBuilder);
142 if (action.equals(Action.ADD)) {
143 writeFlow(flowBuilder, nodeBuilder);
145 removeFlow(flowBuilder, nodeBuilder);
148 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
149 return new Status(StatusCode.SUCCESS);