Fix license header violations in net-virt-providers
[netvirt.git] / openstack / net-virt-providers / src / main / java / org / opendaylight / ovsdb / openstack / netvirt / providers / openflow13 / services / ClassifierService.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.util.List;
13
14 import org.opendaylight.ovsdb.openstack.netvirt.api.ClassifierProvider;
15 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
16 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
17 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
18 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
19 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
20 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
42
43 import com.google.common.collect.Lists;
44 import org.osgi.framework.BundleContext;
45 import org.osgi.framework.ServiceReference;
46
47 public class ClassifierService extends AbstractServiceInstance implements ClassifierProvider, ConfigInterface {
48     public final static long REG_VALUE_FROM_LOCAL = 0x1L;
49     public final static long REG_VALUE_FROM_REMOTE = 0x2L;
50     public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg0.class;
51
52     public ClassifierService() {
53         super(Service.CLASSIFIER);
54     }
55
56     public ClassifierService(Service service) {
57         super(service);
58     }
59
60     /*
61      * (Table:Classifier) Egress VM Traffic Towards TEP
62      * Match: Destination Ethernet Addr and OpenFlow InPort
63      * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
64      * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
65      * actions=set_field:5->tun_id,goto_table=<next-table>"
66      */
67
68     @Override
69     public void programLocalInPort(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
70         String nodeName = OPENFLOW + dpidLong;
71
72         MatchBuilder matchBuilder = new MatchBuilder();
73         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
74         FlowBuilder flowBuilder = new FlowBuilder();
75
76         // Create the OF Match using MatchBuilder
77         flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
78         // TODO Broken In_Port Match
79         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
80
81         String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
82         // Add Flow Attributes
83         flowBuilder.setId(new FlowId(flowId));
84         FlowKey key = new FlowKey(new FlowId(flowId));
85         flowBuilder.setStrict(true);
86         flowBuilder.setBarrier(false);
87         flowBuilder.setTableId(getTable());
88         flowBuilder.setKey(key);
89         flowBuilder.setFlowName(flowId);
90         flowBuilder.setHardTimeout(0);
91         flowBuilder.setIdleTimeout(0);
92
93         if (write) {
94             // Instantiate the Builders for the OF Actions and Instructions
95             InstructionBuilder ib = new InstructionBuilder();
96             InstructionsBuilder isb = new InstructionsBuilder();
97
98             // Instructions List Stores Individual Instructions
99             List<Instruction> instructions = Lists.newArrayList();
100
101             // TODO Broken SetTunID
102             InstructionUtils.createSetTunnelIdInstructions(ib, new BigInteger(segmentationId));
103             ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
104             List<Action> actionList = aac.getApplyActions().getAction();
105
106             ActionBuilder ab = new ActionBuilder();
107             ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
108                     BigInteger.valueOf(REG_VALUE_FROM_LOCAL)));
109             ab.setOrder(1);
110             ab.setKey(new ActionKey(1));
111             actionList.add(ab.build());
112
113             ib.setOrder(0);
114             ib.setKey(new InstructionKey(0));
115             instructions.add(ib.build());
116
117             // Next service GOTO Instructions Need to be appended to the List
118             ib = this.getMutablePipelineInstructionBuilder();
119             ib.setOrder(1);
120             ib.setKey(new InstructionKey(1));
121             instructions.add(ib.build());
122
123             // Add InstructionBuilder to the Instruction(s)Builder List
124             isb.setInstruction(instructions);
125
126             // Add InstructionsBuilder to FlowBuilder
127             flowBuilder.setInstructions(isb.build());
128
129             writeFlow(flowBuilder, nodeBuilder);
130         } else {
131             removeFlow(flowBuilder, nodeBuilder);
132         }
133     }
134
135     /*
136      * (Table:Classifier) Egress VM Traffic Towards TEP
137      * Match: Source Ethernet Addr and OpenFlow InPort
138      * Instruction: Set VLANID and GOTO Table Egress (n)
139      * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
140      * actions=push_vlan, set_field:5->vlan_id,goto_table=<Next-Table>"
141      * table=0,in_port=1,vlan_tci=0x0000/0x1fff,dl_src=fa:16:3e:70:2f:c2 actions=push_vlan:0x8100,set_field:6097->vlan_vid,goto_table:20
142      */
143
144     @Override
145     public void programLocalInPortSetVlan(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
146
147         String nodeName = OPENFLOW + dpidLong;
148
149         MatchBuilder matchBuilder = new MatchBuilder();
150         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
151         FlowBuilder flowBuilder = new FlowBuilder();
152
153         // Create the OF Match using MatchBuilder
154         flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
155         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
156         /* openflowplugin requires a vlan match to add a vlan */
157         flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(0), false).build());
158
159         String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
160         // Add Flow Attributes
161         flowBuilder.setId(new FlowId(flowId));
162         FlowKey key = new FlowKey(new FlowId(flowId));
163         flowBuilder.setStrict(true);
164         flowBuilder.setBarrier(false);
165         flowBuilder.setTableId(getTable());
166         flowBuilder.setKey(key);
167         flowBuilder.setFlowName(flowId);
168         flowBuilder.setHardTimeout(0);
169         flowBuilder.setIdleTimeout(0);
170
171         if (write) {
172             // Instantiate the Builders for the OF Actions and Instructions
173             InstructionBuilder ib = new InstructionBuilder();
174             InstructionsBuilder isb = new InstructionsBuilder();
175
176             // Instructions List Stores Individual Instructions
177             List<Instruction> instructions = Lists.newArrayList();
178
179             // Set VLAN ID Instruction
180             InstructionUtils.createSetVlanInstructions(ib, new VlanId(Integer.valueOf(segmentationId)));
181             ib.setOrder(0);
182             ib.setKey(new InstructionKey(0));
183             instructions.add(ib.build());
184
185             // Next service GOTO Instructions Need to be appended to the List
186             ib = this.getMutablePipelineInstructionBuilder();
187             ib.setOrder(1);
188             ib.setKey(new InstructionKey(1));
189             instructions.add(ib.build());
190
191             // Add InstructionBuilder to the Instruction(s)Builder List
192             isb.setInstruction(instructions);
193
194             // Add InstructionsBuilder to FlowBuilder
195             flowBuilder.setInstructions(isb.build());
196
197             writeFlow(flowBuilder, nodeBuilder);
198         } else {
199             removeFlow(flowBuilder, nodeBuilder);
200         }
201     }
202
203     /*
204      * (Table:Classifier) Drop frames source from a VM that do not
205      * match the associated MAC address of the local VM.
206      * Match: Low priority anything not matching the VM SMAC
207      * Instruction: Drop
208      * table=0,priority=16384,in_port=1 actions=drop"
209      */
210
211     @Override
212     public void programDropSrcIface(Long dpidLong, Long inPort, boolean write) {
213
214         String nodeName = OPENFLOW + dpidLong;
215
216         MatchBuilder matchBuilder = new MatchBuilder();
217         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
218         FlowBuilder flowBuilder = new FlowBuilder();
219
220         // Create the OF Match using MatchBuilder
221         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
222
223         if (write) {
224             // Instantiate the Builders for the OF Actions and Instructions
225             InstructionBuilder ib = new InstructionBuilder();
226             InstructionsBuilder isb = new InstructionsBuilder();
227
228             // Instructions List Stores Individual Instructions
229             List<Instruction> instructions = Lists.newArrayList();
230
231             // Call the InstructionBuilder Methods Containing Actions
232             InstructionUtils.createDropInstructions(ib);
233             ib.setOrder(0);
234             ib.setKey(new InstructionKey(0));
235             instructions.add(ib.build());
236
237             // Add InstructionBuilder to the Instruction(s)Builder List
238             isb.setInstruction(instructions);
239
240             // Add InstructionsBuilder to FlowBuilder
241             flowBuilder.setInstructions(isb.build());
242         }
243
244         String flowId = "DropFilter_"+inPort;
245         // Add Flow Attributes
246         flowBuilder.setId(new FlowId(flowId));
247         FlowKey key = new FlowKey(new FlowId(flowId));
248         flowBuilder.setStrict(true);
249         flowBuilder.setBarrier(false);
250         flowBuilder.setTableId(getTable());
251         flowBuilder.setKey(key);
252         flowBuilder.setFlowName(flowId);
253         flowBuilder.setPriority(8192);
254         flowBuilder.setHardTimeout(0);
255         flowBuilder.setIdleTimeout(0);
256         if (write) {
257             writeFlow(flowBuilder, nodeBuilder);
258         } else {
259             removeFlow(flowBuilder, nodeBuilder);
260         }
261     }
262     /*
263      * (Table:0) Ingress Tunnel Traffic
264      * Match: OpenFlow InPort and Tunnel ID
265      * Action: GOTO Local Table (10)
266      * table=0,tun_id=0x5,in_port=10, actions=goto_table:2
267      */
268
269     @Override
270     public void programTunnelIn(Long dpidLong, String segmentationId,
271             Long ofPort, boolean write) {
272
273         String nodeName = OPENFLOW + dpidLong;
274
275         BigInteger tunnelId = new BigInteger(segmentationId);
276         MatchBuilder matchBuilder = new MatchBuilder();
277         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
278         FlowBuilder flowBuilder = new FlowBuilder();
279
280         // Create Match(es) and Set them in the FlowBuilder Object
281         flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, tunnelId).build());
282         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ofPort).build());
283
284         if (write) {
285             // Create the OF Actions and Instructions
286             InstructionBuilder ib = new InstructionBuilder();
287             InstructionsBuilder isb = new InstructionsBuilder();
288
289             // Instructions List Stores Individual Instructions
290             List<Instruction> instructions = Lists.newArrayList();
291
292             List<Action> actionList = Lists.newArrayList();
293             ActionBuilder ab = new ActionBuilder();
294             ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
295                     BigInteger.valueOf(REG_VALUE_FROM_REMOTE)));
296             ab.setOrder(0);
297             ab.setKey(new ActionKey(0));
298             actionList.add(ab.build());
299
300             ApplyActionsBuilder aab = new ApplyActionsBuilder();
301             aab.setAction(actionList);
302             ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
303
304             // Call the InstructionBuilder Methods Containing Actions
305             ib.setOrder(0);
306             ib.setKey(new InstructionKey(0));
307             instructions.add(ib.build());
308
309             // Append the default pipeline after the first classification
310             ib = this.getMutablePipelineInstructionBuilder();
311             ib.setOrder(1);
312             ib.setKey(new InstructionKey(1));
313             instructions.add(ib.build());
314
315             // Add InstructionBuilder to the Instruction(s)Builder List
316             isb.setInstruction(instructions);
317
318             // Add InstructionsBuilder to FlowBuilder
319             flowBuilder.setInstructions(isb.build());
320         }
321
322         String flowId = "TunnelIn_"+segmentationId+"_"+ofPort;
323         // Add Flow Attributes
324         flowBuilder.setId(new FlowId(flowId));
325         FlowKey key = new FlowKey(new FlowId(flowId));
326         flowBuilder.setStrict(true);
327         flowBuilder.setBarrier(false);
328         flowBuilder.setTableId(getTable());
329         flowBuilder.setKey(key);
330         flowBuilder.setFlowName(flowId);
331         flowBuilder.setHardTimeout(0);
332         flowBuilder.setIdleTimeout(0);
333
334         if (write) {
335             writeFlow(flowBuilder, nodeBuilder);
336         } else {
337             removeFlow(flowBuilder, nodeBuilder);
338         }
339     }
340
341     /*
342      * (Table:0) Ingress VLAN Traffic
343      * Match: OpenFlow InPort and vlan ID
344      * Action: GOTO Local Table (20)
345      * table=0,vlan_id=0x5,in_port=10, actions=goto_table:2
346      * table=0,in_port=2,dl_vlan=2001 actions=goto_table:20
347      */
348
349     @Override
350     public void programVlanIn(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
351
352         String nodeName = OPENFLOW + dpidLong;
353
354         MatchBuilder matchBuilder = new MatchBuilder();
355         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
356         FlowBuilder flowBuilder = new FlowBuilder();
357
358         // Create Match(es) and Set them in the FlowBuilder Object
359         flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
360         flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ethPort).build());
361
362         if (write) {
363             // Create the OF Actions and Instructions
364             InstructionsBuilder isb = new InstructionsBuilder();
365
366             // Instructions List Stores Individual Instructions
367             List<Instruction> instructions = Lists.newArrayList();
368
369             // Append the default pipeline after the first classification
370             InstructionBuilder ib = this.getMutablePipelineInstructionBuilder();
371             ib.setOrder(0);
372             ib.setKey(new InstructionKey(0));
373             instructions.add(ib.build());
374
375             // Add InstructionBuilder to the Instruction(s)Builder List
376             isb.setInstruction(instructions);
377
378             // Add InstructionsBuilder to FlowBuilder
379             flowBuilder.setInstructions(isb.build());
380         }
381
382         String flowId = "VlanIn_"+segmentationId+"_"+ethPort;
383         // Add Flow Attributes
384         flowBuilder.setId(new FlowId(flowId));
385         FlowKey key = new FlowKey(new FlowId(flowId));
386         flowBuilder.setStrict(true);
387         flowBuilder.setBarrier(false);
388         flowBuilder.setTableId(getTable());
389         flowBuilder.setKey(key);
390         flowBuilder.setFlowName(flowId);
391         flowBuilder.setHardTimeout(0);
392         flowBuilder.setIdleTimeout(0);
393         if (write) {
394             writeFlow(flowBuilder, nodeBuilder);
395         } else {
396             removeFlow(flowBuilder, nodeBuilder);
397         }
398     }
399
400     /*
401      * Create an LLDP Flow Rule to encapsulate into
402      * a packet_in that is sent to the controller
403      * for topology handling.
404      * Match: Ethertype 0x88CCL
405      * Action: Punt to Controller in a Packet_In msg
406      */
407
408     @Override
409     public void programLLDPPuntRule(Long dpidLong) {
410
411         String nodeName = OPENFLOW + dpidLong;
412         EtherType etherType = new EtherType(0x88CCL);
413
414         MatchBuilder matchBuilder = new MatchBuilder();
415         NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
416         FlowBuilder flowBuilder = new FlowBuilder();
417
418         // Create Match(es) and Set them in the FlowBuilder Object
419         flowBuilder.setMatch(MatchUtils.createEtherTypeMatch(matchBuilder, etherType).build());
420
421         // Create the OF Actions and Instructions
422         InstructionBuilder ib = new InstructionBuilder();
423         InstructionsBuilder isb = new InstructionsBuilder();
424
425         // Instructions List Stores Individual Instructions
426         List<Instruction> instructions = Lists.newArrayList();
427
428         // Call the InstructionBuilder Methods Containing Actions
429         InstructionUtils.createSendToControllerInstructions(nodeName, ib);
430         ib.setOrder(0);
431         ib.setKey(new InstructionKey(0));
432         instructions.add(ib.build());
433
434         // Add InstructionBuilder to the Instruction(s)Builder List
435         isb.setInstruction(instructions);
436
437         // Add InstructionsBuilder to FlowBuilder
438         flowBuilder.setInstructions(isb.build());
439
440         String flowId = "LLDP";
441         flowBuilder.setId(new FlowId(flowId));
442         FlowKey key = new FlowKey(new FlowId(flowId));
443         flowBuilder.setBarrier(true);
444         flowBuilder.setTableId(getTable());
445         flowBuilder.setKey(key);
446         flowBuilder.setFlowName(flowId);
447         flowBuilder.setHardTimeout(0);
448         flowBuilder.setIdleTimeout(0);
449         writeFlow(flowBuilder, nodeBuilder);
450     }
451
452     @Override
453     public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
454         super.setDependencies(bundleContext.getServiceReference(ClassifierProvider.class.getName()), this);
455     }
456
457     @Override
458     public void setDependencies(Object impl) {}
459 }