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