e0da167af03f9e04852ccdd967c67f5a0faf7a00
[netvirt.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / netvirt / 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.netvirt.openstack.netvirt.providers.openflow13.services;
10
11 import java.math.BigInteger;
12 import java.net.Inet6Address;
13 import java.net.InetAddress;
14 import java.util.ArrayList;
15 import java.util.List;
16 import org.opendaylight.netvirt.openstack.netvirt.api.Action;
17 import org.opendaylight.netvirt.openstack.netvirt.api.ArpProvider;
18 import org.opendaylight.netvirt.openstack.netvirt.api.Constants;
19 import org.opendaylight.netvirt.openstack.netvirt.api.Status;
20 import org.opendaylight.netvirt.openstack.netvirt.api.StatusCode;
21 import org.opendaylight.netvirt.openstack.netvirt.providers.ConfigInterface;
22 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
23 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.Service;
24 import org.opendaylight.netvirt.utils.mdsal.openflow.ActionUtils;
25 import org.opendaylight.netvirt.utils.mdsal.openflow.FlowUtils;
26 import org.opendaylight.netvirt.utils.mdsal.openflow.MatchUtils;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.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.tables.table.FlowBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
40 import org.osgi.framework.BundleContext;
41 import org.osgi.framework.ServiceReference;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class ArpResponderService extends AbstractServiceInstance implements ArpProvider, ConfigInterface {
46     private static final Logger LOG = LoggerFactory.getLogger(ArpResponderService.class);
47
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(Long dpid, String segmentationId, String macAddressStr,
58                                         InetAddress ipAddress, Action action) {
59         if (ipAddress instanceof Inet6Address) {
60             // WORKAROUND: For now ipv6 is not supported
61             // TODO: implement ipv6 case
62             LOG.debug("ipv6 address case is not implemented yet. dpid {} segmentationId {} macAddressStr, "
63                     + "ipAddress {} action {}",
64                     dpid, segmentationId, macAddressStr, ipAddress, action);
65             return new Status(StatusCode.NOTIMPLEMENTED);
66         }
67
68         NodeBuilder nodeBuilder = FlowUtils.createNodeBuilder(dpid);
69         FlowBuilder flowBuilder = new FlowBuilder();
70         String flowName = "ArpResponder_" + segmentationId + "_" + ipAddress.getHostAddress();
71         FlowUtils.initFlowBuilder(flowBuilder, flowName, getTable()).setPriority(1024);
72
73         MatchBuilder matchBuilder = new MatchBuilder();
74         MatchUtils.createEtherTypeMatch(matchBuilder, new EtherType(Constants.ARP_ETHERTYPE));
75         MatchUtils.createArpDstIpv4Match(matchBuilder, MatchUtils.iPv4PrefixFromIPv4Address(ipAddress.getHostAddress()));
76
77         if (segmentationId != null) {
78             final Long inPort = MatchUtils.parseExplicitOFPort(segmentationId);
79             if (inPort != null) {
80                 MatchUtils.createInPortMatch(matchBuilder, dpid, inPort);
81             } else {
82                 MatchUtils.createTunnelIDMatch(matchBuilder, new BigInteger(segmentationId));
83             }
84         }
85
86         flowBuilder.setMatch(matchBuilder.build());
87
88         if (action.equals(Action.ADD)) {
89             // Instructions List Stores Individual Instructions
90             InstructionsBuilder isb = new InstructionsBuilder();
91             List<Instruction> instructions = new ArrayList<>();
92             InstructionBuilder ib = new InstructionBuilder();
93             ApplyActionsBuilder aab = new ApplyActionsBuilder();
94             ActionBuilder ab = new ActionBuilder();
95             List<org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action> actionList = new ArrayList<>();
96
97             // Move Eth Src to Eth Dst
98             ab.setAction(ActionUtils.nxMoveEthSrcToEthDstAction());
99             ab.setOrder(0);
100             ab.setKey(new ActionKey(0));
101             actionList.add(ab.build());
102
103             // Set Eth Src
104             MacAddress macAddress = new MacAddress(macAddressStr);
105             ab.setAction(ActionUtils.setDlSrcAction(macAddress));
106             ab.setOrder(1);
107             ab.setKey(new ActionKey(1));
108             actionList.add(ab.build());
109
110             // Set ARP OP
111             ab.setAction(ActionUtils.nxLoadArpOpAction(BigInteger.valueOf(0x02L)));
112             ab.setOrder(2);
113             ab.setKey(new ActionKey(2));
114             actionList.add(ab.build());
115
116             // Move ARP SHA to ARP THA
117             ab.setAction(ActionUtils.nxMoveArpShaToArpThaAction());
118             ab.setOrder(3);
119             ab.setKey(new ActionKey(3));
120             actionList.add(ab.build());
121
122             // Move ARP SPA to ARP TPA
123             ab.setAction(ActionUtils.nxMoveArpSpaToArpTpaAction());
124             ab.setOrder(4);
125             ab.setKey(new ActionKey(4));
126             actionList.add(ab.build());
127
128             // Load Mac to ARP SHA
129             ab.setAction(ActionUtils.nxLoadArpShaAction(macAddress));
130             ab.setOrder(5);
131             ab.setKey(new ActionKey(5));
132             actionList.add(ab.build());
133
134             // Load IP to ARP SPA
135             ab.setAction(ActionUtils.nxLoadArpSpaAction(ipAddress.getHostAddress()));
136             ab.setOrder(6);
137             ab.setKey(new ActionKey(6));
138             actionList.add(ab.build());
139
140             // Output of InPort
141             ab.setAction(ActionUtils.outputAction(FlowUtils.getSpecialNodeConnectorId(dpid, "INPORT")));
142             ab.setOrder(7);
143             ab.setKey(new ActionKey(7));
144             actionList.add(ab.build());
145
146             // Create Apply Actions Instruction
147             aab.setAction(actionList);
148             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
149             ib.setOrder(0);
150             ib.setKey(new InstructionKey(0));
151             instructions.add(ib.build());
152
153             flowBuilder.setInstructions(isb.setInstruction(instructions).build());
154             writeFlow(flowBuilder, nodeBuilder);
155         } else {
156             removeFlow(flowBuilder, nodeBuilder);
157         }
158
159         // ToDo: WriteFlow/RemoveFlow should return something we can use to check success
160         return new Status(StatusCode.SUCCESS);
161     }
162
163     @Override
164     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
165         super.setDependencies(bundleContext.getServiceReference(ArpProvider.class.getName()), this);
166     }
167
168     @Override
169     public void setDependencies(Object impl) {}
170 }