83de1d1d7d7e0076716de2aa25d7c695beb43dce
[netvirt.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.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;
44
45 import com.google.common.collect.Lists;
46 import org.osgi.framework.BundleContext;
47 import org.osgi.framework.ServiceReference;
48
49 public class ArpResponderService extends AbstractServiceInstance implements ArpProvider, ConfigInterface {
50     public ArpResponderService() {
51         super(Service.ARP_RESPONDER);
52     }
53
54     public ArpResponderService(Service service) {
55         super(service);
56     }
57
58     @Override
59     public Status programStaticArpEntry(Long dpid, String segmentationId, String macAddressStr,
60                                         InetAddress ipAddress, Action action) {
61
62         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
63         MacAddress macAddress = new MacAddress(macAddressStr);
64
65         MatchBuilder matchBuilder = new MatchBuilder();
66         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
67
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();
75
76         if (segmentationId != null) {
77             final Long inPort = MatchUtils.parseExplicitOFPort(segmentationId);
78             if (inPort != null) {
79                 MatchUtils.createInPortMatch(matchBuilder, dpid, inPort);
80             } else {
81                 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
82             }
83         }
84
85         MatchUtils.createEtherTypeMatch(matchBuilder, new EtherType(Constants.ARP_ETHERTYPE));
86         MatchUtils.createArpDstIpv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress()));
87
88         // Move Eth Src to Eth Dst
89         ab.setAction(ActionUtils.nxMoveEthSrcToEthDstAction());
90         ab.setOrder(0);
91         ab.setKey(new ActionKey(0));
92         actionList.add(ab.build());
93
94         // Set Eth Src
95         ab.setAction(ActionUtils.setDlSrcAction(new MacAddress(macAddress)));
96         ab.setOrder(1);
97         ab.setKey(new ActionKey(1));
98         actionList.add(ab.build());
99
100         // Set ARP OP
101         ab.setAction(ActionUtils.nxLoadArpOpAction(BigInteger.valueOf(0x02L)));
102         ab.setOrder(2);
103         ab.setKey(new ActionKey(2));
104         actionList.add(ab.build());
105
106         // Move ARP SHA to ARP THA
107         ab.setAction(ActionUtils.nxMoveArpShaToArpThaAction());
108         ab.setOrder(3);
109         ab.setKey(new ActionKey(3));
110         actionList.add(ab.build());
111
112         // Move ARP SPA to ARP TPA
113         ab.setAction(ActionUtils.nxMoveArpSpaToArpTpaAction());
114         ab.setOrder(4);
115         ab.setKey(new ActionKey(4));
116         actionList.add(ab.build());
117
118         // Load Mac to ARP SHA
119         ab.setAction(ActionUtils.nxLoadArpShaAction(macAddress));
120         ab.setOrder(5);
121         ab.setKey(new ActionKey(5));
122         actionList.add(ab.build());
123
124         // Load IP to ARP SPA
125         ab.setAction(ActionUtils.nxLoadArpSpaAction(ipAddress.getHostAddress()));
126         ab.setOrder(6);
127         ab.setKey(new ActionKey(6));
128         actionList.add(ab.build());
129
130         // Output of InPort
131         ab.setAction(ActionUtils.outputAction(new NodeConnectorId(nodeName + ":INPORT")));
132         ab.setOrder(7);
133         ab.setKey(new ActionKey(7));
134         actionList.add(ab.build());
135
136         // Create Apply Actions Instruction
137         aab.setAction(actionList);
138         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
139         ib.setOrder(0);
140         ib.setKey(new InstructionKey(0));
141         instructions.add(ib.build());
142
143         FlowBuilder flowBuilder = new FlowBuilder();
144         flowBuilder.setMatch(matchBuilder.build());
145         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
146
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);
157
158         if (action.equals(Action.ADD)) {
159             writeFlow(flowBuilder, nodeBuilder);
160         } else {
161             removeFlow(flowBuilder, nodeBuilder);
162         }
163
164         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
165         return new Status(StatusCode.SUCCESS);
166     }
167
168     @Override
169     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
170         super.setDependencies(bundleContext.getServiceReference(ArpProvider.class.getName()), this);
171     }
172
173     @Override
174     public void setDependencies(Object impl) {}
175 }