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.ArpProvider;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
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.Service;
24 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
25 import org.opendaylight.ovsdb.utils.mdsal.openflow.FlowUtils;
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.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.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 import com.google.common.collect.Lists;
44 import org.osgi.framework.BundleContext;
45 import org.osgi.framework.ServiceReference;
47 public class ArpResponderService extends AbstractServiceInstance implements ArpProvider, ConfigInterface {
48 private static final Logger LOG = LoggerFactory.getLogger(ArpResponderService.class);
50 public ArpResponderService() {
51 super(Service.ARP_RESPONDER);
54 public ArpResponderService(Service service) {
59 public Status programStaticArpEntry(Long dpid, String segmentationId, String macAddressStr,
60 InetAddress ipAddress, Action action) {
61 NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dpid);
62 FlowBuilder flowBuilder = new FlowBuilder();
63 String flowName = "ArpResponder_" + segmentationId + "_" + ipAddress.getHostAddress();
64 FlowUtils.initFlowBuilder(flowBuilder, flowName, getTable()).setPriority(1024);
66 MatchBuilder matchBuilder = new MatchBuilder();
67 MatchUtils.createEtherTypeMatch(matchBuilder, new EtherType(Constants.ARP_ETHERTYPE));
68 MatchUtils.createArpDstIpv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress()));
70 if (segmentationId != null) {
71 final Long inPort = MatchUtils.parseExplicitOFPort(segmentationId);
73 MatchUtils.createInPortMatch(matchBuilder, dpid, inPort);
75 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
79 if (ipAddress instanceof Inet6Address) {
80 // WORKAROUND: For now ipv6 is not supported
81 // TODO: implement ipv6 case
82 LOG.debug("ipv6 address case is not implemented yet. dpid {} segmentationId {} macAddressStr, "
83 + "ipAddress {} action {}",
84 dpid, segmentationId, macAddressStr, ipAddress, action);
85 return new Status(StatusCode.NOTIMPLEMENTED);
88 flowBuilder.setMatch(matchBuilder.build());
90 if (action.equals(Action.ADD)) {
91 // Instructions List Stores Individual Instructions
92 InstructionsBuilder isb = new InstructionsBuilder();
93 List<Instruction> instructions = Lists.newArrayList();
94 InstructionBuilder ib = new InstructionBuilder();
95 ApplyActionsBuilder aab = new ApplyActionsBuilder();
96 ActionBuilder ab = new ActionBuilder();
97 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList = Lists.newArrayList();
99 // Move Eth Src to Eth Dst
100 ab.setAction(ActionUtils.nxMoveEthSrcToEthDstAction());
102 ab.setKey(new ActionKey(0));
103 actionList.add(ab.build());
106 MacAddress macAddress = new MacAddress(macAddressStr);
107 ab.setAction(ActionUtils.setDlSrcAction(macAddress));
109 ab.setKey(new ActionKey(1));
110 actionList.add(ab.build());
113 ab.setAction(ActionUtils.nxLoadArpOpAction(BigInteger.valueOf(0x02L)));
115 ab.setKey(new ActionKey(2));
116 actionList.add(ab.build());
118 // Move ARP SHA to ARP THA
119 ab.setAction(ActionUtils.nxMoveArpShaToArpThaAction());
121 ab.setKey(new ActionKey(3));
122 actionList.add(ab.build());
124 // Move ARP SPA to ARP TPA
125 ab.setAction(ActionUtils.nxMoveArpSpaToArpTpaAction());
127 ab.setKey(new ActionKey(4));
128 actionList.add(ab.build());
130 // Load Mac to ARP SHA
131 ab.setAction(ActionUtils.nxLoadArpShaAction(macAddress));
133 ab.setKey(new ActionKey(5));
134 actionList.add(ab.build());
136 // Load IP to ARP SPA
137 ab.setAction(ActionUtils.nxLoadArpSpaAction(ipAddress.getHostAddress()));
139 ab.setKey(new ActionKey(6));
140 actionList.add(ab.build());
143 ab.setAction(ActionUtils.outputAction(FlowUtils.getSpecialNodeConnectorId(dpid, "INPORT")));
145 ab.setKey(new ActionKey(7));
146 actionList.add(ab.build());
148 // Create Apply Actions Instruction
149 aab.setAction(actionList);
150 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
152 ib.setKey(new InstructionKey(0));
153 instructions.add(ib.build());
155 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
156 writeFlow(flowBuilder, nodeBuilder);
158 removeFlow(flowBuilder, nodeBuilder);
161 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
162 return new Status(StatusCode.SUCCESS);
166 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
167 super.setDependencies(bundleContext.getServiceReference(ArpProvider.class.getName()), this);
171 public void setDependencies(Object impl) {}