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 / L3ForwardingService.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.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.netvirt.openstack.netvirt.api.Action;
17 import org.opendaylight.netvirt.openstack.netvirt.api.L3ForwardingProvider;
18 import org.opendaylight.netvirt.openstack.netvirt.api.Status;
19 import org.opendaylight.netvirt.openstack.netvirt.api.StatusCode;
20 import org.opendaylight.netvirt.openstack.netvirt.providers.ConfigInterface;
21 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
22 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.Service;
23 import org.opendaylight.netvirt.utils.mdsal.openflow.FlowUtils;
24 import org.opendaylight.netvirt.utils.mdsal.openflow.InstructionUtils;
25 import org.opendaylight.netvirt.utils.mdsal.openflow.MatchUtils;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
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;
34 import org.osgi.framework.BundleContext;
35 import org.osgi.framework.ServiceReference;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class L3ForwardingService extends AbstractServiceInstance implements L3ForwardingProvider, ConfigInterface {
40     private static final Logger LOG = LoggerFactory.getLogger(L3ForwardingService.class);
41
42     public L3ForwardingService() {
43         super(Service.L3_FORWARDING);
44     }
45
46     public L3ForwardingService(Service service) {
47         super(service);
48     }
49
50     @Override
51     public Status programForwardingTableEntry(Long dpid, String segmentationId, InetAddress ipAddress,
52                                               String macAddress, Action action) {
53         if (ipAddress instanceof Inet6Address) {
54             // WORKAROUND: For now ipv6 is not supported
55             // TODO: implement ipv6 case
56             LOG.debug("ipv6 address is not implemented yet. dpid {} segmentationId {} ipAddress {} macAddress {} Action {}",
57                       dpid, segmentationId, ipAddress, macAddress, action);
58             return new Status(StatusCode.NOTIMPLEMENTED);
59         }
60
61         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dpid);
62         FlowBuilder flowBuilder = new FlowBuilder();
63         String flowName = "L3Forwarding_" + segmentationId + "_" + ipAddress.getHostAddress();
64         FlowUtils.initFlowBuilder(flowBuilder, flowName, getTable()).setPriority(1024);
65
66         MatchBuilder matchBuilder = new MatchBuilder();
67         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
68         MatchUtils.createDstL3IPv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress()));
69
70         flowBuilder.setMatch(matchBuilder.build());
71
72         if (action.equals(Action.ADD)) {
73             // Instructions List Stores Individual Instructions
74             InstructionsBuilder isb = new InstructionsBuilder();
75             List<Instruction> instructions = new ArrayList<>();
76             InstructionBuilder ib = new InstructionBuilder();
77
78             // Set Dest Mac address
79             InstructionUtils.createDlDstInstructions(ib, new MacAddress(macAddress));
80             ib.setOrder(0);
81             ib.setKey(new InstructionKey(0));
82             instructions.add(ib.build());
83
84             // Goto Next Table
85             ib = getMutablePipelineInstructionBuilder();
86             ib.setOrder(1);
87             ib.setKey(new InstructionKey(1));
88             instructions.add(ib.build());
89
90             flowBuilder.setInstructions(isb.setInstruction(instructions).build());
91
92             writeFlow(flowBuilder, nodeBuilder);
93         } else {
94             removeFlow(flowBuilder, nodeBuilder);
95         }
96
97         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
98         return new Status(StatusCode.SUCCESS);
99     }
100
101     @Override
102     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
103         super.setDependencies(bundleContext.getServiceReference(L3ForwardingProvider.class.getName()), this);
104     }
105
106     @Override
107     public void setDependencies(Object impl) {
108
109     }
110 }