2 * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
11 import java.math.BigInteger;
12 import java.net.InetAddress;
13 import java.util.List;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.InboundNatProvider;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
20 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
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.ActionUtils;
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.action.types.rev131112.action.list.ActionBuilder;
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;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
43 import org.osgi.framework.BundleContext;
44 import org.osgi.framework.ServiceReference;
46 public class InboundNatService extends AbstractServiceInstance implements ConfigInterface, InboundNatProvider {
47 public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg3.class;
49 public InboundNatService() {
50 super(Service.INBOUND_NAT);
53 public InboundNatService(Service service) {
58 public Status programIpRewriteRule(Long dpid, Long inPort, String destSegId, InetAddress matchAddress,
59 InetAddress rewriteAddress, Action action) {
60 String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
62 MatchBuilder matchBuilder = new MatchBuilder();
63 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
65 // Instructions List Stores Individual Instructions
66 InstructionsBuilder isb = new InstructionsBuilder();
67 List<Instruction> instructions = Lists.newArrayList();
68 InstructionBuilder ib = new InstructionBuilder();
70 MatchUtils.createInPortMatch(matchBuilder, dpid, inPort);
71 MatchUtils.createDstL3IPv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(matchAddress.getHostAddress()));
73 // Set register to indicate that rewrite took place
74 ActionBuilder actionBuilder = new ActionBuilder();
75 actionBuilder.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
76 new BigInteger(destSegId)));
78 // Set Dest IP address and set REG_FIELD
79 InstructionUtils.createNwDstInstructions(ib,
80 MatchUtils.iPv4PrefixFromIPv4Address(rewriteAddress.getHostAddress()), actionBuilder);
82 ib.setKey(new InstructionKey(0));
83 instructions.add(ib.build());
86 ib = getMutablePipelineInstructionBuilder();
88 ib.setKey(new InstructionKey(1));
89 instructions.add(ib.build());
91 FlowBuilder flowBuilder = new FlowBuilder();
92 flowBuilder.setMatch(matchBuilder.build());
93 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
95 String flowId = "InboundNAT_" + inPort + "_" + destSegId + "_" + matchAddress.getHostAddress();
96 flowBuilder.setId(new FlowId(flowId));
97 FlowKey key = new FlowKey(new FlowId(flowId));
98 flowBuilder.setBarrier(true);
99 flowBuilder.setTableId(this.getTable());
100 flowBuilder.setKey(key);
101 flowBuilder.setPriority(1024);
102 flowBuilder.setFlowName(flowId);
103 flowBuilder.setHardTimeout(0);
104 flowBuilder.setIdleTimeout(0);
106 if (action.equals(Action.ADD)) {
107 writeFlow(flowBuilder, nodeBuilder);
109 removeFlow(flowBuilder, nodeBuilder);
112 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
113 return new Status(StatusCode.SUCCESS);
117 public Status programIpRewriteExclusion(Long dpid, String segmentationId, String excludedCidr,
119 String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
121 MatchBuilder matchBuilder = new MatchBuilder();
122 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
124 // Instructions List Stores Individual Instructions
125 InstructionsBuilder isb = new InstructionsBuilder();
126 List<Instruction> instructions = Lists.newArrayList();
127 InstructionBuilder ib;
129 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
130 MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
133 ib = getMutablePipelineInstructionBuilder();
135 ib.setKey(new InstructionKey(0));
136 instructions.add(ib.build());
138 FlowBuilder flowBuilder = new FlowBuilder();
139 flowBuilder.setMatch(matchBuilder.build());
140 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
142 String flowId = "InboundNATExclusion_" + segmentationId + "_" + excludedCidr;
143 flowBuilder.setId(new FlowId(flowId));
144 FlowKey key = new FlowKey(new FlowId(flowId));
145 flowBuilder.setBarrier(true);
146 flowBuilder.setTableId(this.getTable());
147 flowBuilder.setKey(key);
148 flowBuilder.setPriority(1024);
149 flowBuilder.setFlowName(flowId);
150 flowBuilder.setHardTimeout(0);
151 flowBuilder.setIdleTimeout(0);
153 if (action.equals(Action.ADD)) {
154 writeFlow(flowBuilder, nodeBuilder);
156 removeFlow(flowBuilder, nodeBuilder);
159 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
160 return new Status(StatusCode.SUCCESS);
164 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
165 super.setDependencies(bundleContext.getServiceReference(InboundNatProvider.class.getName()), this);
169 public void setDependencies(Object impl) {}