f1969badec528208d7145bd437109efdfe8d63d4
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / lldp / 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.openflow.md.lldp;
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.openflow.md.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.yangtools.yang.binding.DataObject;
42 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
43
44 /**
45  * Created by Martin Bobak mbobak@cisco.com on 8/27/14.
46  */
47 public class LLDPPAcketPuntEnforcer implements DataChangeListener {
48
49     private static final short TABLE_ID = (short) 0;
50     private static final String LLDP_PUNT_WHOLE_PACKET_FLOW = "LLDP_PUNT_WHOLE_PACKET_FLOW";
51     private static final String DEFAULT_FLOW_ID = "42";
52     private final SalFlowService flowService;
53
54     public LLDPPAcketPuntEnforcer(SalFlowService flowService) {
55         this.flowService = flowService;
56     }
57
58     @Override
59     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
60         final Set<InstanceIdentifier<?>> changedDataKeys = change.getCreatedData().keySet();
61
62         for (InstanceIdentifier<?> key : changedDataKeys) {
63             AddFlowInputBuilder addFlowInput = new AddFlowInputBuilder(createFlow());
64             addFlowInput.setNode(new NodeRef(key));
65             this.flowService.addFlow(addFlowInput.build());
66         }
67     }
68
69
70     protected Flow createFlow() {
71         FlowBuilder flowBuilder = new FlowBuilder();
72         flowBuilder.setMatch(new MatchBuilder().build());
73         flowBuilder.setInstructions(createSendToControllerInstructions().build());
74         flowBuilder.setPriority(0);
75
76         FlowKey key = new FlowKey(new FlowId(DEFAULT_FLOW_ID));
77         flowBuilder.setBarrier(Boolean.FALSE);
78         // flow.setBufferId(new Long(12));
79         BigInteger value = new BigInteger("10", 10);
80         // BigInteger outputPort = new BigInteger("65535", 10);
81         flowBuilder.setCookie(new FlowCookie(value));
82         flowBuilder.setCookieMask(new FlowCookie(value));
83         flowBuilder.setHardTimeout(0);
84         flowBuilder.setIdleTimeout(0);
85         flowBuilder.setInstallHw(false);
86         flowBuilder.setStrict(false);
87         flowBuilder.setContainerName(null);
88         flowBuilder.setFlags(new FlowModFlags(false, false, false, false, true));
89         flowBuilder.setId(new FlowId("12"));
90         flowBuilder.setTableId(TABLE_ID);
91         flowBuilder.setKey(key);
92         flowBuilder.setFlowName(LLDP_PUNT_WHOLE_PACKET_FLOW);
93
94         return flowBuilder.build();
95     }
96
97     private static InstructionsBuilder createSendToControllerInstructions() {
98         List<Action> actionList = new ArrayList<Action>();
99         ActionBuilder ab = new ActionBuilder();
100
101         OutputActionBuilder output = new OutputActionBuilder();
102         output.setMaxLength(OFConstants.OFPCML_NO_BUFFER);
103         Uri value = new Uri(OutputPortValues.CONTROLLER.toString());
104         output.setOutputNodeConnector(value);
105         ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
106         ab.setOrder(0);
107         ab.setKey(new ActionKey(0));
108         actionList.add(ab.build());
109         // Create an Apply Action
110         ApplyActionsBuilder aab = new ApplyActionsBuilder();
111         aab.setAction(actionList);
112
113         // Wrap our Apply Action in an Instruction
114         InstructionBuilder ib = new InstructionBuilder();
115         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
116         ib.setOrder(0);
117         ib.setKey(new InstructionKey(0));
118
119         // Put our Instruction in a list of Instructions
120         InstructionsBuilder isb = new InstructionsBuilder();
121         List<Instruction> instructions = new ArrayList<Instruction>();
122         instructions.add(ib.build());
123         isb.setInstruction(instructions);
124         return isb;
125     }
126
127 }