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.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.inet.types.rev100924.Ipv4Prefix;
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;
47 public class ArpResponderService extends AbstractServiceInstance implements ArpProvider {
48 public ArpResponderService() {
49 super(Service.ARP_RESPONDER);
52 public ArpResponderService(Service service) {
57 public Status programStaticArpEntry(Node node, Long dpid, String segmentationId, String macAddressStr,
58 InetAddress ipAddress, Action action) {
60 String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
61 MacAddress macAddress = new MacAddress(macAddressStr);
63 MatchBuilder matchBuilder = new MatchBuilder();
64 NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
66 // Instructions List Stores Individual Instructions
67 InstructionsBuilder isb = new InstructionsBuilder();
68 List<Instruction> instructions = Lists.newArrayList();
69 InstructionBuilder ib = new InstructionBuilder();
70 ApplyActionsBuilder aab = new ApplyActionsBuilder();
71 ActionBuilder ab = new ActionBuilder();
72 List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList = Lists.newArrayList();
74 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
75 MatchUtils.createEtherTypeMatch(matchBuilder, new EtherType(Constants.ARP_ETHERTYPE));
76 MatchUtils.createArpDstIpv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress()));
78 // Move Eth Src to Eth Dst
79 ab.setAction(ActionUtils.nxMoveEthSrcToEthDstAction());
81 ab.setKey(new ActionKey(0));
82 actionList.add(ab.build());
85 ab.setAction(ActionUtils.setDlSrcAction(new MacAddress(macAddress)));
87 ab.setKey(new ActionKey(1));
88 actionList.add(ab.build());
91 ab.setAction(ActionUtils.nxLoadArpOpAction(BigInteger.valueOf(0x02L)));
93 ab.setKey(new ActionKey(2));
94 actionList.add(ab.build());
96 // Move ARP SHA to ARP THA
97 ab.setAction(ActionUtils.nxMoveArpShaToArpThaAction());
99 ab.setKey(new ActionKey(3));
100 actionList.add(ab.build());
102 // Move ARP SPA to ARP TPA
103 ab.setAction(ActionUtils.nxMoveArpSpaToArpTpaAction());
105 ab.setKey(new ActionKey(4));
106 actionList.add(ab.build());
108 // Load Mac to ARP SHA
109 ab.setAction(ActionUtils.nxLoadArpShaAction(macAddress));
111 ab.setKey(new ActionKey(5));
112 actionList.add(ab.build());
114 // Load IP to ARP SPA
115 ab.setAction(ActionUtils.nxLoadArpSpaAction(ipAddress.getHostAddress()));
117 ab.setKey(new ActionKey(6));
118 actionList.add(ab.build());
121 ab.setAction(ActionUtils.outputAction(new NodeConnectorId(nodeName + ":INPORT")));
123 ab.setKey(new ActionKey(7));
124 actionList.add(ab.build());
126 // Create Apply Actions Instruction
127 aab.setAction(actionList);
128 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
130 ib.setKey(new InstructionKey(0));
131 instructions.add(ib.build());
133 FlowBuilder flowBuilder = new FlowBuilder();
134 flowBuilder.setMatch(matchBuilder.build());
135 flowBuilder.setInstructions(isb.setInstruction(instructions).build());
137 String flowId = "ArpResponder_" + ipAddress.getHostAddress();
138 flowBuilder.setId(new FlowId(flowId));
139 FlowKey key = new FlowKey(new FlowId(flowId));
140 flowBuilder.setBarrier(true);
141 flowBuilder.setTableId(this.getTable());
142 flowBuilder.setKey(key);
143 flowBuilder.setPriority(1024);
144 flowBuilder.setFlowName(flowId);
145 flowBuilder.setHardTimeout(0);
146 flowBuilder.setIdleTimeout(0);
148 if (action.equals(Action.ADD)) {
149 writeFlow(flowBuilder, nodeBuilder);
151 removeFlow(flowBuilder, nodeBuilder);
154 // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
155 return new Status(StatusCode.SUCCESS);