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.ArpProvider;
18 import org.opendaylight.netvirt.openstack.netvirt.api.Constants;
19 import org.opendaylight.netvirt.openstack.netvirt.api.Status;
20 import org.opendaylight.netvirt.openstack.netvirt.api.StatusCode;
21 import org.opendaylight.netvirt.openstack.netvirt.providers.ConfigInterface;
22 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
23 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.Service;
24 import org.opendaylight.netvirt.utils.mdsal.openflow.ActionUtils;
25 import org.opendaylight.netvirt.utils.mdsal.openflow.FlowUtils;
26 import org.opendaylight.netvirt.utils.mdsal.openflow.MatchUtils;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
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.instruction.ApplyActionsCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.ServiceReference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
45 public class ArpResponderService extends AbstractServiceInstance implements ArpProvider, ConfigInterface {
46 private static final Logger LOG = LoggerFactory.getLogger(ArpResponderService.class);
48 public ArpResponderService() {
49 super(Service.ARP_RESPONDER);
52 public ArpResponderService(Service service) {
57 public Status programStaticArpEntry(Long dpid, String segmentationId, String macAddressStr,
58 InetAddress ipAddress, Action action) {
59 if (ipAddress instanceof Inet6Address) {
60 // WORKAROUND: For now ipv6 is not supported
61 // TODO: implement ipv6 case
62 LOG.debug("ipv6 address case is not implemented yet. dpid {} segmentationId {} macAddressStr, "
63 + "ipAddress {} action {}",
64 dpid, segmentationId, macAddressStr, ipAddress, action);
65 return new Status(StatusCode.NOTIMPLEMENTED);
68 NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dpid);
69 FlowBuilder flowBuilder = new FlowBuilder();
70 String flowName = "ArpResponder_" + segmentationId + "_" + ipAddress.getHostAddress();
71 FlowUtils.initFlowBuilder(flowBuilder, flowName, getTable()).setPriority(1024);
73 MatchBuilder matchBuilder = new MatchBuilder();
74 MatchUtils.createEtherTypeMatch(matchBuilder, new EtherType(Constants.ARP_ETHERTYPE));
75 MatchUtils.createArpDstIpv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress()));
77 if (segmentationId != null) {
78 final Long inPort = MatchUtils.parseExplicitOFPort(segmentationId);
80 MatchUtils.createInPortMatch(matchBuilder, dpid, inPort);
82 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
86 flowBuilder.setMatch(matchBuilder.build());
88 if (action.equals(Action.ADD)) {
89 // Instructions List Stores Individual Instructions
90 InstructionsBuilder isb = new InstructionsBuilder();
91 List<Instruction> instructions = new ArrayList<>();
92 InstructionBuilder ib = new InstructionBuilder();
93 ApplyActionsBuilder aab = new ApplyActionsBuilder();
94 ActionBuilder ab = new ActionBuilder();
95 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList = new ArrayList<>();
97 // Move Eth Src to Eth Dst
98 ab.setAction(ActionUtils.nxMoveEthSrcToEthDstAction());
100 ab.setKey(new ActionKey(0));
101 actionList.add(ab.build());
104 MacAddress macAddress = new MacAddress(macAddressStr);
105 ab.setAction(ActionUtils.setDlSrcAction(macAddress));
107 ab.setKey(new ActionKey(1));
108 actionList.add(ab.build());
111 ab.setAction(ActionUtils.nxLoadArpOpAction(BigInteger.valueOf(0x02L)));
113 ab.setKey(new ActionKey(2));
114 actionList.add(ab.build());
116 // Move ARP SHA to ARP THA
117 ab.setAction(ActionUtils.nxMoveArpShaToArpThaAction());
119 ab.setKey(new ActionKey(3));
120 actionList.add(ab.build());
122 // Move ARP SPA to ARP TPA
123 ab.setAction(ActionUtils.nxMoveArpSpaToArpTpaAction());
125 ab.setKey(new ActionKey(4));
126 actionList.add(ab.build());
128 // Load Mac to ARP SHA
129 ab.setAction(ActionUtils.nxLoadArpShaAction(macAddress));
131 ab.setKey(new ActionKey(5));
132 actionList.add(ab.build());
134 // Load IP to ARP SPA
135 ab.setAction(ActionUtils.nxLoadArpSpaAction(ipAddress.getHostAddress()));
137 ab.setKey(new ActionKey(6));
138 actionList.add(ab.build());
141 ab.setAction(ActionUtils.outputAction(FlowUtils.getSpecialNodeConnectorId(dpid, "INPORT")));
143 ab.setKey(new ActionKey(7));
144 actionList.add(ab.build());
146 // Create Apply Actions Instruction
147 aab.setAction(actionList);
148 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
150 ib.setKey(new InstructionKey(0));
151 instructions.add(ib.build());
153 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
154 writeFlow(flowBuilder, nodeBuilder);
156 removeFlow(flowBuilder, nodeBuilder);
159 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
160 return new Status(StatusCode.SUCCESS);
164 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
165 super.setDependencies(bundleContext.getServiceReference(ArpProvider.class.getName()), this);
169 public void setDependencies(Object impl) {}