Merge "Only use single Apply Actions instruction"
[ovsdb.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / ArpResponderService.java
1 /*
2  * Copyright (C) 2014 Red Hat, Inc.
3  *
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
7  *
8  * Authors : Madhu Venugopal, Dave Tucker
9  */
10 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
11
12 import java.math.BigInteger;
13 import java.net.InetAddress;
14 import java.util.List;
15
16 import org.opendaylight.controller.sal.core.Node;
17 import org.opendaylight.controller.sal.utils.Status;
18 import org.opendaylight.controller.sal.utils.StatusCode;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
20 import org.opendaylight.ovsdb.openstack.netvirt.api.ArpProvider;
21 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
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.NodeBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
43
44 import com.google.common.collect.Lists;
45
46 public class ArpResponderService extends AbstractServiceInstance implements ArpProvider {
47     public ArpResponderService() {
48         super(Service.ARP_RESPONDER);
49     }
50
51     public ArpResponderService(Service service) {
52         super(service);
53     }
54
55     @Override
56     public Status programStaticArpEntry(Node node, Long dpid, String segmentationId, String macAddressStr,
57                                         InetAddress ipAddress, Action action) {
58
59         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
60         MacAddress macAddress = new MacAddress(macAddressStr);
61
62         MatchBuilder matchBuilder = new MatchBuilder();
63         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
64
65         // Instructions List Stores Individual Instructions
66         InstructionsBuilder isb = new InstructionsBuilder();
67         List<Instruction> instructions = Lists.newArrayList();
68         InstructionBuilder ib = new InstructionBuilder();
69         ApplyActionsBuilder aab = new ApplyActionsBuilder();
70         ActionBuilder ab = new ActionBuilder();
71         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList = Lists.newArrayList();
72
73         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
74         MatchUtils.createEtherTypeMatch(matchBuilder, new EtherType(Constants.ARP_ETHERTYPE));
75
76         // Move Eth Src to Eth Dst
77         ab.setAction(ActionUtils.nxMoveEthSrcToEthDstAction());
78         ab.setOrder(0);
79         ab.setKey(new ActionKey(0));
80         actionList.add(ab.build());
81
82         // Set Eth Src
83         ab.setAction(ActionUtils.setDlSrcAction(new MacAddress(macAddress)));
84         ab.setOrder(1);
85         ab.setKey(new ActionKey(1));
86         actionList.add(ab.build());
87
88         // Set ARP OP
89         ab.setAction(ActionUtils.nxLoadArpOpAction(BigInteger.valueOf(0x02L)));
90         ab.setOrder(2);
91         ab.setKey(new ActionKey(2));
92         actionList.add(ab.build());
93
94         // Move ARP SHA to ARP THA
95         ab.setAction(ActionUtils.nxMoveArpShaToArpThaAction());
96         ab.setOrder(3);
97         ab.setKey(new ActionKey(3));
98         actionList.add(ab.build());
99
100         // Move ARP SPA to ARP TPA
101         ab.setAction(ActionUtils.nxMoveArpSpaToArpTpaAction());
102         ab.setOrder(4);
103         ab.setKey(new ActionKey(4));
104         actionList.add(ab.build());
105
106         // Load Mac to ARP SHA
107         ab.setAction(ActionUtils.nxLoadArpShaAction(macAddress));
108         ab.setOrder(5);
109         ab.setKey(new ActionKey(5));
110         actionList.add(ab.build());
111
112         // Load IP to ARP SPA
113         ab.setAction(ActionUtils.nxLoadArpSpaAction(ipAddress.getHostAddress()));
114         ab.setOrder(6);
115         ab.setKey(new ActionKey(6));
116         actionList.add(ab.build());
117
118         // Output of InPort
119         ab.setAction(ActionUtils.outputAction(new NodeConnectorId(nodeName + ":INPORT")));
120         ab.setOrder(7);
121         ab.setKey(new ActionKey(7));
122         actionList.add(ab.build());
123
124         // Create Apply Actions Instruction
125         aab.setAction(actionList);
126         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
127         ib.setOrder(0);
128         ib.setKey(new InstructionKey(0));
129         instructions.add(ib.build());
130
131         FlowBuilder flowBuilder = new FlowBuilder();
132         flowBuilder.setMatch(matchBuilder.build());
133         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
134
135         String flowId = "ArpResponder_" + ipAddress.getHostAddress();
136         flowBuilder.setId(new FlowId(flowId));
137         FlowKey key = new FlowKey(new FlowId(flowId));
138         flowBuilder.setBarrier(true);
139         flowBuilder.setTableId(this.getTable());
140         flowBuilder.setKey(key);
141         flowBuilder.setPriority(1024);
142         flowBuilder.setFlowName(flowId);
143         flowBuilder.setHardTimeout(0);
144         flowBuilder.setIdleTimeout(0);
145         writeFlow(flowBuilder, nodeBuilder);
146
147         if (action.equals(Action.ADD)) {
148             writeFlow(flowBuilder, nodeBuilder);
149         } else {
150             removeFlow(flowBuilder, nodeBuilder);
151         }
152
153         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
154         return new Status(StatusCode.SUCCESS);
155     }
156 }