b14399af57b33463c8690f982cdce8fff05ec659
[ovsdb.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / ArpResponderService.java
1 /*
2  * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
10
11 import java.math.BigInteger;
12 import java.net.InetAddress;
13 import java.util.List;
14
15 import org.opendaylight.ovsdb.openstack.netvirt.api.Action;
16 import org.opendaylight.ovsdb.openstack.netvirt.api.ArpProvider;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.Constants;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.Status;
19 import org.opendaylight.ovsdb.openstack.netvirt.api.StatusCode;
20 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
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.yang.types.rev100924.MacAddress;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
40 //import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
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 import org.osgi.framework.BundleContext;
46 import org.osgi.framework.ServiceReference;
47
48 public class ArpResponderService extends AbstractServiceInstance implements ArpProvider, ConfigInterface {
49     public ArpResponderService() {
50         super(Service.ARP_RESPONDER);
51     }
52
53     public ArpResponderService(Service service) {
54         super(service);
55     }
56
57     @Override
58     public Status programStaticArpEntry(Long dpid, String segmentationId, String macAddressStr,
59                                         InetAddress ipAddress, Action action) {
60
61         String nodeName = Constants.OPENFLOW_NODE_PREFIX + dpid;
62         MacAddress macAddress = new MacAddress(macAddressStr);
63
64         MatchBuilder matchBuilder = new MatchBuilder();
65         NodeBuilder nodeBuilder = OF13Provider.createNodeBuilder(nodeName);
66
67         // Instructions List Stores Individual Instructions
68         InstructionsBuilder isb = new InstructionsBuilder();
69         List<Instruction> instructions = Lists.newArrayList();
70         InstructionBuilder ib = new InstructionBuilder();
71         ApplyActionsBuilder aab = new ApplyActionsBuilder();
72         ActionBuilder ab = new ActionBuilder();
73         List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList = Lists.newArrayList();
74
75         if (segmentationId != null) {
76             final Long inPort = MatchUtils.parseExplicitOFPort(segmentationId);
77             if (inPort != null) {
78                 MatchUtils.createInPortMatch(matchBuilder, dpid, inPort);
79             } else {
80                 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
81             }
82         }
83
84         MatchUtils.createEtherTypeMatch(matchBuilder, new EtherType(Constants.ARP_ETHERTYPE));
85         MatchUtils.createArpDstIpv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress()));
86
87         // Move Eth Src to Eth Dst
88         ab.setAction(ActionUtils.nxMoveEthSrcToEthDstAction());
89         ab.setOrder(0);
90         ab.setKey(new ActionKey(0));
91         actionList.add(ab.build());
92
93         // Set Eth Src
94         ab.setAction(ActionUtils.setDlSrcAction(new MacAddress(macAddress)));
95         ab.setOrder(1);
96         ab.setKey(new ActionKey(1));
97         actionList.add(ab.build());
98
99         // Set ARP OP
100         ab.setAction(ActionUtils.nxLoadArpOpAction(BigInteger.valueOf(0x02L)));
101         ab.setOrder(2);
102         ab.setKey(new ActionKey(2));
103         actionList.add(ab.build());
104
105         // Move ARP SHA to ARP THA
106         ab.setAction(ActionUtils.nxMoveArpShaToArpThaAction());
107         ab.setOrder(3);
108         ab.setKey(new ActionKey(3));
109         actionList.add(ab.build());
110
111         // Move ARP SPA to ARP TPA
112         ab.setAction(ActionUtils.nxMoveArpSpaToArpTpaAction());
113         ab.setOrder(4);
114         ab.setKey(new ActionKey(4));
115         actionList.add(ab.build());
116
117         // Load Mac to ARP SHA
118         ab.setAction(ActionUtils.nxLoadArpShaAction(macAddress));
119         ab.setOrder(5);
120         ab.setKey(new ActionKey(5));
121         actionList.add(ab.build());
122
123         // Load IP to ARP SPA
124         ab.setAction(ActionUtils.nxLoadArpSpaAction(ipAddress.getHostAddress()));
125         ab.setOrder(6);
126         ab.setKey(new ActionKey(6));
127         actionList.add(ab.build());
128
129         // Output of InPort
130         ab.setAction(ActionUtils.outputAction(new NodeConnectorId(nodeName + ":INPORT")));
131         ab.setOrder(7);
132         ab.setKey(new ActionKey(7));
133         actionList.add(ab.build());
134
135         // Create Apply Actions Instruction
136         aab.setAction(actionList);
137         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
138         ib.setOrder(0);
139         ib.setKey(new InstructionKey(0));
140         instructions.add(ib.build());
141
142         FlowBuilder flowBuilder = new FlowBuilder();
143         flowBuilder.setMatch(matchBuilder.build());
144         flowBuilder.setInstructions(isb.setInstruction(instructions).build());
145
146         String flowId = "ArpResponder_" + segmentationId + "_" + ipAddress.getHostAddress();
147         flowBuilder.setId(new FlowId(flowId));
148         FlowKey key = new FlowKey(new FlowId(flowId));
149         flowBuilder.setBarrier(true);
150         flowBuilder.setTableId(this.getTable());
151         flowBuilder.setKey(key);
152         flowBuilder.setPriority(1024);
153         flowBuilder.setFlowName(flowId);
154         flowBuilder.setHardTimeout(0);
155         flowBuilder.setIdleTimeout(0);
156
157         if (action.equals(Action.ADD)) {
158             writeFlow(flowBuilder, nodeBuilder);
159         } else {
160             removeFlow(flowBuilder, nodeBuilder);
161         }
162
163         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
164         return new Status(StatusCode.SUCCESS);
165     }
166
167     @Override
168     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
169         super.setDependencies(bundleContext.getServiceReference(ArpProvider.class.getName()), this);
170     }
171
172     @Override
173     public void setDependencies(Object impl) {}
174 }