Bug 1680 - default table-miss-entry feature should be pulled out into separate module
[openflowplugin.git] / applications / table-miss-enforcer / src / main / java / org / opendaylight / openflowplugin / applications / tableMissEnforcer / LLDPPacketPuntEnforcer.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, 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.openflowplugin.applications.tableMissEnforcer;
10
11 import java.math.BigInteger;
12 import java.util.ArrayList;
13 import java.util.List;
14 import java.util.Set;
15 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
16 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
17 import org.opendaylight.openflowplugin.api.OFConstants;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
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.service.rev130819.AddFlowInputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
42 import org.opendaylight.yangtools.yang.binding.DataObject;
43 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
44
45 /**
46  * Created by Martin Bobak mbobak@cisco.com on 8/27/14.
47  */
48 public class LLDPPacketPuntEnforcer implements DataChangeListener {
49
50     private static final short TABLE_ID = (short) 0;
51     private static final String LLDP_PUNT_WHOLE_PACKET_FLOW = "LLDP_PUNT_WHOLE_PACKET_FLOW";
52     private static final String DEFAULT_FLOW_ID = "42";
53     private final SalFlowService flowService;
54
55     public LLDPPacketPuntEnforcer(SalFlowService flowService) {
56         this.flowService = flowService;
57     }
58
59     @Override
60     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
61         final Set<InstanceIdentifier<?>> changedDataKeys = change.getCreatedData().keySet();
62
63         if (changedDataKeys != null) {
64             for (InstanceIdentifier<?> key : changedDataKeys) {
65                 AddFlowInputBuilder addFlowInput = new AddFlowInputBuilder(createFlow());
66                 addFlowInput.setNode(new NodeRef(key.firstIdentifierOf(Node.class)));
67                 this.flowService.addFlow(addFlowInput.build());
68             }
69         }
70     }
71
72
73     protected Flow createFlow() {
74         FlowBuilder flowBuilder = new FlowBuilder();
75         flowBuilder.setMatch(new MatchBuilder().build());
76         flowBuilder.setInstructions(createSendToControllerInstructions().build());
77         flowBuilder.setPriority(0);
78
79         FlowKey key = new FlowKey(new FlowId(DEFAULT_FLOW_ID));
80         flowBuilder.setBarrier(Boolean.FALSE);
81         // flow.setBufferId(new Long(12));
82         BigInteger value = new BigInteger("10", 10);
83         // BigInteger outputPort = new BigInteger("65535", 10);
84         flowBuilder.setCookie(new FlowCookie(value));
85         flowBuilder.setCookieMask(new FlowCookie(value));
86         flowBuilder.setHardTimeout(0);
87         flowBuilder.setIdleTimeout(0);
88         flowBuilder.setInstallHw(false);
89         flowBuilder.setStrict(false);
90         flowBuilder.setContainerName(null);
91         flowBuilder.setFlags(new FlowModFlags(false, false, false, false, true));
92         flowBuilder.setId(new FlowId("12"));
93         flowBuilder.setTableId(TABLE_ID);
94         flowBuilder.setKey(key);
95         flowBuilder.setFlowName(LLDP_PUNT_WHOLE_PACKET_FLOW);
96
97         return flowBuilder.build();
98     }
99
100     private static InstructionsBuilder createSendToControllerInstructions() {
101         List<Action> actionList = new ArrayList<Action>();
102         ActionBuilder ab = new ActionBuilder();
103
104         OutputActionBuilder output = new OutputActionBuilder();
105         output.setMaxLength(OFConstants.OFPCML_NO_BUFFER);
106         Uri value = new Uri(OutputPortValues.CONTROLLER.toString());
107         output.setOutputNodeConnector(value);
108         ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
109         ab.setOrder(0);
110         ab.setKey(new ActionKey(0));
111         actionList.add(ab.build());
112         // Create an Apply Action
113         ApplyActionsBuilder aab = new ApplyActionsBuilder();
114         aab.setAction(actionList);
115
116         // Wrap our Apply Action in an Instruction
117         InstructionBuilder ib = new InstructionBuilder();
118         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
119         ib.setOrder(0);
120         ib.setKey(new InstructionKey(0));
121
122         // Put our Instruction in a list of Instructions
123         InstructionsBuilder isb = new InstructionsBuilder();
124         List<Instruction> instructions = new ArrayList<Instruction>();
125         instructions.add(ib.build());
126         isb.setInstruction(instructions);
127         return isb;
128     }
129
130 }