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.InetAddress;
13 import java.util.List;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.ArpProvider;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
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.OF13Provider;
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.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.action.types.rev131112.action.list.ActionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
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.inventory.rev130819.tables.table.FlowKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
40 //import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
44 import com.google.common.collect.Lists;
45 import org.osgi.framework.BundleContext;
46 import org.osgi.framework.ServiceReference;
48 public class ArpResponderService extends AbstractServiceInstance implements ArpProvider, ConfigInterface {
49 public ArpResponderService() {
50 super(Service.ARP_RESPONDER);
53 public ArpResponderService(Service service) {
58 public Status programStaticArpEntry(Long dpid, String segmentationId, String macAddressStr,
59 InetAddress ipAddress, Action action) {
61 String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
62 MacAddress macAddress = new MacAddress(macAddressStr);
64 MatchBuilder matchBuilder = new MatchBuilder();
65 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
67 // Instructions List Stores Individual Instructions
68 InstructionsBuilder isb = new InstructionsBuilder();
69 List<Instruction> instructions = Lists.newArrayList();
70 InstructionBuilder ib = new InstructionBuilder();
71 ApplyActionsBuilder aab = new ApplyActionsBuilder();
72 ActionBuilder ab = new ActionBuilder();
73 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList = Lists.newArrayList();
75 if (segmentationId != null) {
76 final Long inPort = MatchUtils.parseExplicitOFPort(segmentationId);
78 MatchUtils.createInPortMatch(matchBuilder, dpid, inPort);
80 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
84 MatchUtils.createEtherTypeMatch(matchBuilder, new EtherType(Constants.ARP_ETHERTYPE));
85 MatchUtils.createArpDstIpv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress()));
87 // Move Eth Src to Eth Dst
88 ab.setAction(ActionUtils.nxMoveEthSrcToEthDstAction());
90 ab.setKey(new ActionKey(0));
91 actionList.add(ab.build());
94 ab.setAction(ActionUtils.setDlSrcAction(new MacAddress(macAddress)));
96 ab.setKey(new ActionKey(1));
97 actionList.add(ab.build());
100 ab.setAction(ActionUtils.nxLoadArpOpAction(BigInteger.valueOf(0x02L)));
102 ab.setKey(new ActionKey(2));
103 actionList.add(ab.build());
105 // Move ARP SHA to ARP THA
106 ab.setAction(ActionUtils.nxMoveArpShaToArpThaAction());
108 ab.setKey(new ActionKey(3));
109 actionList.add(ab.build());
111 // Move ARP SPA to ARP TPA
112 ab.setAction(ActionUtils.nxMoveArpSpaToArpTpaAction());
114 ab.setKey(new ActionKey(4));
115 actionList.add(ab.build());
117 // Load Mac to ARP SHA
118 ab.setAction(ActionUtils.nxLoadArpShaAction(macAddress));
120 ab.setKey(new ActionKey(5));
121 actionList.add(ab.build());
123 // Load IP to ARP SPA
124 ab.setAction(ActionUtils.nxLoadArpSpaAction(ipAddress.getHostAddress()));
126 ab.setKey(new ActionKey(6));
127 actionList.add(ab.build());
130 ab.setAction(ActionUtils.outputAction(new NodeConnectorId(nodeName + ":INPORT")));
132 ab.setKey(new ActionKey(7));
133 actionList.add(ab.build());
135 // Create Apply Actions Instruction
136 aab.setAction(actionList);
137 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
139 ib.setKey(new InstructionKey(0));
140 instructions.add(ib.build());
142 FlowBuilder flowBuilder = new FlowBuilder();
143 flowBuilder.setMatch(matchBuilder.build());
144 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
146 String flowId = "ArpResponder_" + segmentationId + "_" + ipAddress.getHostAddress();
147 flowBuilder.setId(new FlowId(flowId));
148 FlowKey key = new FlowKey(new FlowId(flowId));
149 flowBuilder.setBarrier(true);
150 flowBuilder.setTableId(this.getTable());
151 flowBuilder.setKey(key);
152 flowBuilder.setPriority(1024);
153 flowBuilder.setFlowName(flowId);
154 flowBuilder.setHardTimeout(0);
155 flowBuilder.setIdleTimeout(0);
157 if (action.equals(Action.ADD)) {
158 writeFlow(flowBuilder, nodeBuilder);
160 removeFlow(flowBuilder, nodeBuilder);
163 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
164 return new Status(StatusCode.SUCCESS);
168 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
169 super.setDependencies(bundleContext.getServiceReference(ArpProvider.class.getName()), this);
173 public void setDependencies(Object impl) {}