Bug 4611: null pointer exception while processing interface event
[ovsdb.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / 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.ovsdb.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.List;
15
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.L3ForwardingProvider;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
21 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
22 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
23 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.OF13Provider;
24 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
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.yang.types.rev100924.MacAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
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.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 import com.google.common.collect.Lists;
41 import org.osgi.framework.BundleContext;
42 import org.osgi.framework.ServiceReference;
43
44 public class L3ForwardingService extends AbstractServiceInstance implements L3ForwardingProvider, ConfigInterface {
45     private static final Logger LOG = LoggerFactory.getLogger(L3ForwardingService.class);
46
47     public L3ForwardingService() {
48         super(Service.L3_FORWARDING);
49     }
50
51     public L3ForwardingService(Service service) {
52         super(service);
53     }
54
55     @Override
56     public Status programForwardingTableEntry(Long dpid, String segmentationId, InetAddress ipAddress,
57                                               String macAddress, Action action) {
58         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
59
60         MatchBuilder matchBuilder = new MatchBuilder();
61         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
62
63         // Instructions List Stores Individual Instructions
64         InstructionsBuilder isb = new InstructionsBuilder();
65         List<Instruction> instructions = Lists.newArrayList();
66         InstructionBuilder ib = new InstructionBuilder();
67
68         if (ipAddress instanceof Inet6Address) {
69             // WORKAROUND: For now ipv6 is not supported
70             // TODO: implement ipv6 case
71             LOG.debug("ipv6 address is not implemented yet. dpid {} segmentationId {} ipAddress {} macAddress {} Action {}",
72                       dpid, segmentationId, ipAddress, macAddress, action);
73             return new Status(StatusCode.NOTIMPLEMENTED);
74         }
75
76         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
77         MatchUtils.createDstL3IPv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress()));
78
79         // Set Dest Mac address
80         InstructionUtils.createDlDstInstructions(ib, new MacAddress(macAddress));
81         ib.setOrder(0);
82         ib.setKey(new InstructionKey(0));
83         instructions.add(ib.build());
84
85         // Goto Next Table
86         ib = getMutablePipelineInstructionBuilder();
87         ib.setOrder(1);
88         ib.setKey(new InstructionKey(1));
89         instructions.add(ib.build());
90
91         FlowBuilder flowBuilder = new FlowBuilder();
92         flowBuilder.setMatch(matchBuilder.build());
93         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
94
95         String flowId = "L3Forwarding_" + segmentationId + "_" + ipAddress.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);
105
106         if (action.equals(Action.ADD)) {
107             writeFlow(flowBuilder, nodeBuilder);
108         } else {
109             removeFlow(flowBuilder, nodeBuilder);
110         }
111
112         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
113         return new Status(StatusCode.SUCCESS);
114     }
115
116     @Override
117     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
118         super.setDependencies(bundleContext.getServiceReference(L3ForwardingProvider.class.getName()), this);
119     }
120
121     @Override
122     public void setDependencies(Object impl) {
123
124     }
125 }