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