Fix NPE triggered after disabling SG on a port
[netvirt.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / netvirt / openstack / netvirt / providers / openflow13 / services / InboundNatService.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.services;
10
11 import java.math.BigInteger;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.net.UnknownHostException;
15 import java.util.ArrayList;
16 import java.util.List;
17 import org.opendaylight.netvirt.openstack.netvirt.api.Action;
18 import org.opendaylight.netvirt.openstack.netvirt.api.InboundNatProvider;
19 import org.opendaylight.netvirt.openstack.netvirt.api.Status;
20 import org.opendaylight.netvirt.openstack.netvirt.api.StatusCode;
21 import org.opendaylight.netvirt.openstack.netvirt.providers.ConfigInterface;
22 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
23 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.Service;
24 import org.opendaylight.netvirt.utils.mdsal.openflow.ActionUtils;
25 import org.opendaylight.netvirt.utils.mdsal.openflow.FlowUtils;
26 import org.opendaylight.netvirt.utils.mdsal.openflow.InstructionUtils;
27 import org.opendaylight.netvirt.utils.mdsal.openflow.MatchUtils;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
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.types.rev131026.flow.InstructionsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg3;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.ServiceReference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class InboundNatService extends AbstractServiceInstance implements ConfigInterface, InboundNatProvider {
46     private static final Logger LOG = LoggerFactory.getLogger(InboundNatService.class);
47     public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg3.class;
48
49     public InboundNatService() {
50         super(Service.INBOUND_NAT);
51     }
52
53     public InboundNatService(Service service) {
54         super(service);
55     }
56
57     @Override
58     public Status programIpRewriteRule(Long dpid, Long inPort, String destSegId, InetAddress matchAddress,
59                                        InetAddress rewriteAddress, Action action) {
60         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dpid);
61         FlowBuilder flowBuilder = new FlowBuilder();
62         String flowName = "InboundNAT_" + inPort + "_" + destSegId + "_" + matchAddress.getHostAddress();
63         FlowUtils.initFlowBuilder(flowBuilder, flowName, getTable()).setPriority(1024);
64
65         MatchBuilder matchBuilder = new MatchBuilder();
66         MatchUtils.createInPortMatch(matchBuilder, dpid, inPort);
67         MatchUtils.createDstL3IPv4Match(matchBuilder,
68                 MatchUtils.iPv4PrefixFromIPv4Address(matchAddress.getHostAddress()));
69         flowBuilder.setMatch(matchBuilder.build());
70
71         if (action.equals(Action.ADD)) {
72             // Instructions List Stores Individual Instructions
73             InstructionsBuilder isb = new InstructionsBuilder();
74             List<Instruction> instructions = new ArrayList<>();
75             InstructionBuilder ib = new InstructionBuilder();
76
77             // Set register to indicate that rewrite took place
78             ActionBuilder actionBuilder = new ActionBuilder();
79             actionBuilder.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
80                     new BigInteger(destSegId)));
81
82             // Set Dest IP address and set REG_FIELD
83             InstructionUtils.createNwDstInstructions(ib,
84                     MatchUtils.iPv4PrefixFromIPv4Address(rewriteAddress.getHostAddress()), actionBuilder);
85             ib.setOrder(0);
86             ib.setKey(new InstructionKey(0));
87             instructions.add(ib.build());
88
89             // Goto Next Table
90             ib = getMutablePipelineInstructionBuilder();
91             ib.setOrder(1);
92             ib.setKey(new InstructionKey(1));
93             instructions.add(ib.build());
94
95             flowBuilder.setInstructions(isb.setInstruction(instructions).build());
96
97             writeFlow(flowBuilder, nodeBuilder);
98         } else {
99             removeFlow(flowBuilder, nodeBuilder);
100         }
101
102         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
103         return new Status(StatusCode.SUCCESS);
104     }
105
106     @Override
107     public Status programIpRewriteExclusion(Long dpid, String segmentationId, String excludedCidr,
108                                             Action action) {
109         String ipAddress = excludedCidr.substring(0, excludedCidr.indexOf("/"));
110         InetAddress inetAddress;
111         try {
112             inetAddress = InetAddress.getByName(ipAddress);
113         } catch (UnknownHostException e) {
114             return new Status(StatusCode.BADREQUEST);
115         }
116         if (inetAddress instanceof Inet6Address) {
117             // WORKAROUND: For now ipv6 is not supported
118             // TODO: implement ipv6 cidr case
119             LOG.debug("ipv6 cidr is not implemented yet. cidr {}",
120                     excludedCidr);
121             return new Status(StatusCode.NOTIMPLEMENTED);
122         }
123
124         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dpid);
125         FlowBuilder flowBuilder = new FlowBuilder();
126         String flowName = "InboundNATExclusion_" + segmentationId + "_" + excludedCidr;
127         FlowUtils.initFlowBuilder(flowBuilder, flowName, getTable()).setPriority(1024);
128
129         MatchBuilder matchBuilder = new MatchBuilder();
130         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
131
132         MatchUtils.createDstL3IPv4Match(matchBuilder, new Ipv4Prefix(excludedCidr));
133         flowBuilder.setMatch(matchBuilder.build());
134
135         if (action.equals(Action.ADD)) {
136             // Instructions List Stores Individual Instructions
137             InstructionsBuilder isb = new InstructionsBuilder();
138             List<Instruction> instructions = new ArrayList<>();
139             InstructionBuilder ib;
140
141             // Goto Next Table
142             ib = getMutablePipelineInstructionBuilder();
143             ib.setOrder(0);
144             ib.setKey(new InstructionKey(0));
145             instructions.add(ib.build());
146
147             flowBuilder.setInstructions(isb.setInstruction(instructions).build());
148
149             writeFlow(flowBuilder, nodeBuilder);
150         } else {
151             removeFlow(flowBuilder, nodeBuilder);
152         }
153
154         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
155         return new Status(StatusCode.SUCCESS);
156     }
157
158     @Override
159     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
160         super.setDependencies(bundleContext.getServiceReference(InboundNatProvider.class.getName()), this);
161     }
162
163     @Override
164     public void setDependencies(Object impl) {}
165 }