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.netvirt.openstack.netvirt.providers.openflow13.services;
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;
39 public class L3ForwardingService extends AbstractServiceInstance implements L3ForwardingProvider, ConfigInterface {
40 private static final Logger LOG = LoggerFactory.getLogger(L3ForwardingService.class);
42 public L3ForwardingService() {
43 super(Service.L3_FORWARDING);
46 public L3ForwardingService(Service service) {
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);
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);
66 MatchBuilder matchBuilder = new MatchBuilder();
67 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
68 MatchUtils.createDstL3IPv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress()));
70 flowBuilder.setMatch(matchBuilder.build());
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();
78 // Set Dest Mac address
79 InstructionUtils.createDlDstInstructions(ib, new MacAddress(macAddress));
81 ib.setKey(new InstructionKey(0));
82 instructions.add(ib.build());
85 ib = getMutablePipelineInstructionBuilder();
87 ib.setKey(new InstructionKey(1));
88 instructions.add(ib.build());
90 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
92 writeFlow(flowBuilder, nodeBuilder);
94 removeFlow(flowBuilder, nodeBuilder);
97 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
98 return new Status(StatusCode.SUCCESS);
102 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
103 super.setDependencies(bundleContext.getServiceReference(L3ForwardingProvider.class.getName()), this);
107 public void setDependencies(Object impl) {