Remove plugin dependencies
[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.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;
44
45 import com.google.common.collect.Lists;
46
47 public class ArpResponderService extends AbstractServiceInstance implements ArpProvider {
48     public ArpResponderService() {
49         super(Service.ARP_RESPONDER);
50     }
51
52     public ArpResponderService(Service service) {
53         super(service);
54     }
55
56     @Override
57     public Status programStaticArpEntry(Node node, Long dpid, String segmentationId, String macAddressStr,
58                                         InetAddress ipAddress, Action action) {
59
60         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
61         MacAddress macAddress = new MacAddress(macAddressStr);
62
63         MatchBuilder matchBuilder = new MatchBuilder();
64         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
65
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();
73
74         MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
75         MatchUtils.createEtherTypeMatch(matchBuilder, new EtherType(Constants.ARP_ETHERTYPE));
76         MatchUtils.createArpDstIpv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress()));
77
78         // Move Eth Src to Eth Dst
79         ab.setAction(ActionUtils.nxMoveEthSrcToEthDstAction());
80         ab.setOrder(0);
81         ab.setKey(new ActionKey(0));
82         actionList.add(ab.build());
83
84         // Set Eth Src
85         ab.setAction(ActionUtils.setDlSrcAction(new MacAddress(macAddress)));
86         ab.setOrder(1);
87         ab.setKey(new ActionKey(1));
88         actionList.add(ab.build());
89
90         // Set ARP OP
91         ab.setAction(ActionUtils.nxLoadArpOpAction(BigInteger.valueOf(0x02L)));
92         ab.setOrder(2);
93         ab.setKey(new ActionKey(2));
94         actionList.add(ab.build());
95
96         // Move ARP SHA to ARP THA
97         ab.setAction(ActionUtils.nxMoveArpShaToArpThaAction());
98         ab.setOrder(3);
99         ab.setKey(new ActionKey(3));
100         actionList.add(ab.build());
101
102         // Move ARP SPA to ARP TPA
103         ab.setAction(ActionUtils.nxMoveArpSpaToArpTpaAction());
104         ab.setOrder(4);
105         ab.setKey(new ActionKey(4));
106         actionList.add(ab.build());
107
108         // Load Mac to ARP SHA
109         ab.setAction(ActionUtils.nxLoadArpShaAction(macAddress));
110         ab.setOrder(5);
111         ab.setKey(new ActionKey(5));
112         actionList.add(ab.build());
113
114         // Load IP to ARP SPA
115         ab.setAction(ActionUtils.nxLoadArpSpaAction(ipAddress.getHostAddress()));
116         ab.setOrder(6);
117         ab.setKey(new ActionKey(6));
118         actionList.add(ab.build());
119
120         // Output of InPort
121         ab.setAction(ActionUtils.outputAction(new NodeConnectorId(nodeName + ":INPORT")));
122         ab.setOrder(7);
123         ab.setKey(new ActionKey(7));
124         actionList.add(ab.build());
125
126         // Create Apply Actions Instruction
127         aab.setAction(actionList);
128         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
129         ib.setOrder(0);
130         ib.setKey(new InstructionKey(0));
131         instructions.add(ib.build());
132
133         FlowBuilder flowBuilder = new FlowBuilder();
134         flowBuilder.setMatch(matchBuilder.build());
135         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
136
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);
147
148         if (action.equals(Action.ADD)) {
149             writeFlow(flowBuilder, nodeBuilder);
150         } else {
151             removeFlow(flowBuilder, nodeBuilder);
152         }
153
154         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
155         return new Status(StatusCode.SUCCESS);
156     }
157 }