2 * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
11 import java.math.BigInteger;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.util.List;
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;
37 import com.google.common.collect.Lists;
38 import org.osgi.framework.BundleContext;
39 import org.osgi.framework.ServiceReference;
41 public class L3ForwardingService extends AbstractServiceInstance implements L3ForwardingProvider, ConfigInterface {
42 private static final Logger LOG = LoggerFactory.getLogger(L3ForwardingService.class);
44 public L3ForwardingService() {
45 super(Service.L3_FORWARDING);
48 public L3ForwardingService(Service service) {
53 public Status programForwardingTableEntry(Long dpid, String segmentationId, InetAddress ipAddress,
54 String macAddress, Action action) {
55 NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dpid);
56 FlowBuilder flowBuilder = new FlowBuilder();
57 String flowName = "L3Forwarding_" + segmentationId + "_" + ipAddress.getHostAddress();
58 FlowUtils.initFlowBuilder(flowBuilder, flowName, getTable()).setPriority(1024);
60 MatchBuilder matchBuilder = new MatchBuilder();
61 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
62 MatchUtils.createDstL3IPv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress()));
64 if (ipAddress instanceof Inet6Address) {
65 // WORKAROUND: For now ipv6 is not supported
66 // TODO: implement ipv6 case
67 LOG.debug("ipv6 address is not implemented yet. dpid {} segmentationId {} ipAddress {} macAddress {} Action {}",
68 dpid, segmentationId, ipAddress, macAddress, action);
69 return new Status(StatusCode.NOTIMPLEMENTED);
71 flowBuilder.setMatch(matchBuilder.build());
73 if (action.equals(Action.ADD)) {
74 // Instructions List Stores Individual Instructions
75 InstructionsBuilder isb = new InstructionsBuilder();
76 List<Instruction> instructions = Lists.newArrayList();
77 InstructionBuilder ib = new InstructionBuilder();
79 // Set Dest Mac address
80 InstructionUtils.createDlDstInstructions(ib, new MacAddress(macAddress));
82 ib.setKey(new InstructionKey(0));
83 instructions.add(ib.build());
86 ib = getMutablePipelineInstructionBuilder();
88 ib.setKey(new InstructionKey(1));
89 instructions.add(ib.build());
91 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
93 writeFlow(flowBuilder, nodeBuilder);
95 removeFlow(flowBuilder, nodeBuilder);
98 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
99 return new Status(StatusCode.SUCCESS);
103 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
104 super.setDependencies(bundleContext.getServiceReference(L3ForwardingProvider.class.getName()), this);
108 public void setDependencies(Object impl) {