2 * Copyright (C) 2014 Red Hat, Inc.
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
8 * Authors : Madhu Venugopal, Dave Tucker
10 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
12 import java.math.BigInteger;
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.OF13Provider;
24 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
25 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
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.FlowId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
41 //import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
45 import com.google.common.collect.Lists;
46 import org.osgi.framework.BundleContext;
47 import org.osgi.framework.ServiceReference;
49 public class ArpResponderService extends AbstractServiceInstance implements ArpProvider, ConfigInterface {
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) {
62 String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
63 MacAddress macAddress = new MacAddress(macAddressStr);
65 MatchBuilder matchBuilder = new MatchBuilder();
66 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
68 // Instructions List Stores Individual Instructions
69 InstructionsBuilder isb = new InstructionsBuilder();
70 List<Instruction> instructions = Lists.newArrayList();
71 InstructionBuilder ib = new InstructionBuilder();
72 ApplyActionsBuilder aab = new ApplyActionsBuilder();
73 ActionBuilder ab = new ActionBuilder();
74 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList = Lists.newArrayList();
76 if (segmentationId != null) {
77 final Long inPort = MatchUtils.parseExplicitOFPort(segmentationId);
79 MatchUtils.createInPortMatch(matchBuilder, dpid, inPort);
81 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
85 MatchUtils.createEtherTypeMatch(matchBuilder, new EtherType(Constants.ARP_ETHERTYPE));
86 MatchUtils.createArpDstIpv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress()));
88 // Move Eth Src to Eth Dst
89 ab.setAction(ActionUtils.nxMoveEthSrcToEthDstAction());
91 ab.setKey(new ActionKey(0));
92 actionList.add(ab.build());
95 ab.setAction(ActionUtils.setDlSrcAction(new MacAddress(macAddress)));
97 ab.setKey(new ActionKey(1));
98 actionList.add(ab.build());
101 ab.setAction(ActionUtils.nxLoadArpOpAction(BigInteger.valueOf(0x02L)));
103 ab.setKey(new ActionKey(2));
104 actionList.add(ab.build());
106 // Move ARP SHA to ARP THA
107 ab.setAction(ActionUtils.nxMoveArpShaToArpThaAction());
109 ab.setKey(new ActionKey(3));
110 actionList.add(ab.build());
112 // Move ARP SPA to ARP TPA
113 ab.setAction(ActionUtils.nxMoveArpSpaToArpTpaAction());
115 ab.setKey(new ActionKey(4));
116 actionList.add(ab.build());
118 // Load Mac to ARP SHA
119 ab.setAction(ActionUtils.nxLoadArpShaAction(macAddress));
121 ab.setKey(new ActionKey(5));
122 actionList.add(ab.build());
124 // Load IP to ARP SPA
125 ab.setAction(ActionUtils.nxLoadArpSpaAction(ipAddress.getHostAddress()));
127 ab.setKey(new ActionKey(6));
128 actionList.add(ab.build());
131 ab.setAction(ActionUtils.outputAction(new NodeConnectorId(nodeName + ":INPORT")));
133 ab.setKey(new ActionKey(7));
134 actionList.add(ab.build());
136 // Create Apply Actions Instruction
137 aab.setAction(actionList);
138 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
140 ib.setKey(new InstructionKey(0));
141 instructions.add(ib.build());
143 FlowBuilder flowBuilder = new FlowBuilder();
144 flowBuilder.setMatch(matchBuilder.build());
145 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
147 String flowId = "ArpResponder_" + segmentationId + "_" + ipAddress.getHostAddress();
148 flowBuilder.setId(new FlowId(flowId));
149 FlowKey key = new FlowKey(new FlowId(flowId));
150 flowBuilder.setBarrier(true);
151 flowBuilder.setTableId(this.getTable());
152 flowBuilder.setKey(key);
153 flowBuilder.setPriority(1024);
154 flowBuilder.setFlowName(flowId);
155 flowBuilder.setHardTimeout(0);
156 flowBuilder.setIdleTimeout(0);
158 if (action.equals(Action.ADD)) {
159 writeFlow(flowBuilder, nodeBuilder);
161 removeFlow(flowBuilder, nodeBuilder);
164 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
165 return new Status(StatusCode.SUCCESS);
169 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
170 super.setDependencies(bundleContext.getServiceReference(ArpProvider.class.getName()), this);
174 public void setDependencies(Object impl) {}