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