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 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;
35 import com.google.common.collect.Lists;
37 import java.math.BigInteger;
38 import java.net.InetAddress;
39 import java.util.List;
41 public class OutboundNatService extends AbstractServiceInstance implements OutboundNatProvider {
42 public OutboundNatService() {
43 super(Service.OUTBOUND_NAT);
46 public OutboundNatService(Service service) {
51 public boolean isBridgeInPipeline (String nodeId) {
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;
60 MatchBuilder matchBuilder = new MatchBuilder();
61 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
63 // Instructions List Stores Individual Instructions
64 InstructionsBuilder isb = new InstructionsBuilder();
65 List<Instruction> instructions = Lists.newArrayList();
66 InstructionBuilder ib = new InstructionBuilder();
68 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
69 MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(matchAddress.getHostAddress()));
71 // Set Dest IP address
72 InstructionUtils.createNwDstInstructions(ib, new Ipv4Prefix(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_" + 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);
99 if (action.equals(Action.ADD)) {
100 writeFlow(flowBuilder, nodeBuilder);
102 removeFlow(flowBuilder, nodeBuilder);
105 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
106 return new Status(StatusCode.SUCCESS);
110 public Status programIpRewriteExclusion(Node node, Long dpid, String segmentationId, String excludedCidr,
112 String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
114 MatchBuilder matchBuilder = new MatchBuilder();
115 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
117 // Instructions List Stores Individual Instructions
118 InstructionsBuilder isb = new InstructionsBuilder();
119 List<Instruction> instructions = Lists.newArrayList();
120 InstructionBuilder ib;
122 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
123 MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
126 ib = getMutablePipelineInstructionBuilder();
128 ib.setKey(new InstructionKey(0));
129 instructions.add(ib.build());
131 FlowBuilder flowBuilder = new FlowBuilder();
132 flowBuilder.setMatch(matchBuilder.build());
133 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
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);
147 if (action.equals(Action.ADD)) {
148 writeFlow(flowBuilder, nodeBuilder);
150 removeFlow(flowBuilder, nodeBuilder);
153 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
154 return new Status(StatusCode.SUCCESS);