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.ovsdb.openstack.netvirt.api.Action;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.L3ForwardingProvider;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.OutboundNatProvider;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
22 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
23 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
24 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
25 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.OF13Provider;
26 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
27 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
28 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Prefix;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
40 import com.google.common.collect.Lists;
41 import org.osgi.framework.BundleContext;
42 import org.osgi.framework.ServiceReference;
44 public class OutboundNatService extends AbstractServiceInstance implements OutboundNatProvider, ConfigInterface {
45 public OutboundNatService() {
46 super(Service.OUTBOUND_NAT);
49 public OutboundNatService(Service service) {
54 public Status programIpRewriteRule(Long dpid, String segmentationId, InetAddress matchAddress,
55 InetAddress rewriteAddress, Action action) {
56 String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
58 MatchBuilder matchBuilder = new MatchBuilder();
59 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
61 // Instructions List Stores Individual Instructions
62 InstructionsBuilder isb = new InstructionsBuilder();
63 List<Instruction> instructions = Lists.newArrayList();
64 InstructionBuilder ib = new InstructionBuilder();
66 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
67 MatchUtils.createDstL3IPv4Match(matchBuilder,
68 MatchUtils.iPv4PrefixFromIPv4Address(matchAddress.getHostAddress()));
70 // Set Dest IP address
71 InstructionUtils.createNwDstInstructions(ib,
72 MatchUtils.iPv4PrefixFromIPv4Address(rewriteAddress.getHostAddress()));
74 ib.setKey(new InstructionKey(0));
75 instructions.add(ib.build());
78 ib = getMutablePipelineInstructionBuilder();
80 ib.setKey(new InstructionKey(1));
81 instructions.add(ib.build());
83 FlowBuilder flowBuilder = new FlowBuilder();
84 flowBuilder.setMatch(matchBuilder.build());
85 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
87 String flowId = "OutboundNAT_" + segmentationId + "_" + 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);
98 if (action.equals(Action.ADD)) {
99 writeFlow(flowBuilder, nodeBuilder);
101 removeFlow(flowBuilder, nodeBuilder);
104 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
105 return new Status(StatusCode.SUCCESS);
109 public Status programIpRewriteExclusion(Long dpid, String segmentationId, String excludedCidr,
111 String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
113 MatchBuilder matchBuilder = new MatchBuilder();
114 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
116 // Instructions List Stores Individual Instructions
117 InstructionsBuilder isb = new InstructionsBuilder();
118 List<Instruction> instructions = Lists.newArrayList();
119 InstructionBuilder ib;
121 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
122 MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
125 ib = getMutablePipelineInstructionBuilder();
127 ib.setKey(new InstructionKey(0));
128 instructions.add(ib.build());
130 FlowBuilder flowBuilder = new FlowBuilder();
131 flowBuilder.setMatch(matchBuilder.build());
132 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
134 String flowId = "OutboundNATExclusion_" + segmentationId + "_" + excludedCidr;
135 flowBuilder.setId(new FlowId(flowId));
136 FlowKey key = new FlowKey(new FlowId(flowId));
137 flowBuilder.setBarrier(true);
138 flowBuilder.setTableId(this.getTable());
139 flowBuilder.setKey(key);
140 flowBuilder.setPriority(1024);
141 flowBuilder.setFlowName(flowId);
142 flowBuilder.setHardTimeout(0);
143 flowBuilder.setIdleTimeout(0);
145 if (action.equals(Action.ADD)) {
146 writeFlow(flowBuilder, nodeBuilder);
148 removeFlow(flowBuilder, nodeBuilder);
151 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
152 return new Status(StatusCode.SUCCESS);
156 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
157 super.setOrchestrator(bundleContext.getServiceReference(OutboundNatProvider.class.getName()), this);
161 public void setDependencies(Object impl) {