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