Upgrade ietf-{inet,yang}-types to 2013-07-15
[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 java.util.concurrent.Callable;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
18 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
19 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.openflowplugin.api.OFConstants;
22 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
49 import org.opendaylight.yangtools.concepts.ListenerRegistration;
50 import org.opendaylight.yangtools.yang.binding.DataObject;
51 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
52
53 /**
54  * Created by Martin Bobak mbobak@cisco.com on 8/27/14.
55  */
56 public class LLDPPacketPuntEnforcer implements DataChangeListener {
57     private static final long STARTUP_LOOP_TICK = 500L;
58     private static final int STARTUP_LOOP_MAX_RETRIES = 8;
59     private static final short TABLE_ID = (short) 0;
60     private static final String LLDP_PUNT_WHOLE_PACKET_FLOW = "LLDP_PUNT_WHOLE_PACKET_FLOW";
61     private static final String DEFAULT_FLOW_ID = "42";
62     private final SalFlowService flowService;
63     private final DataBroker dataBroker;
64     private ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
65
66     public LLDPPacketPuntEnforcer(SalFlowService flowService, DataBroker dataBroker) {
67         this.flowService = flowService;
68         this.dataBroker = dataBroker;
69     }
70
71     public void start() {
72         final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class).
73                 augmentation(FlowCapableNode.class);
74         SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
75         try {
76             dataChangeListenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<DataChangeListener>>() {
77                 @Override
78                 public ListenerRegistration<DataChangeListener> call() throws Exception {
79                     return dataBroker.registerDataChangeListener(
80                             LogicalDatastoreType.OPERATIONAL,
81                             path, LLDPPacketPuntEnforcer.this, AsyncDataBroker.DataChangeScope.BASE);
82                 }
83             });
84         } catch (Exception e) {
85             throw new IllegalStateException("registerDataChangeListener failed", e);
86         }
87     }
88
89     public void close() {
90         if(dataChangeListenerRegistration != null) {
91             dataChangeListenerRegistration.close();
92         }
93     }
94
95     @Override
96     public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
97         final Set<InstanceIdentifier<?>> changedDataKeys = change.getCreatedData().keySet();
98
99         if (changedDataKeys != null) {
100             for (InstanceIdentifier<?> key : changedDataKeys) {
101                 AddFlowInputBuilder addFlowInput = new AddFlowInputBuilder(createFlow());
102                 addFlowInput.setNode(new NodeRef(key.firstIdentifierOf(Node.class)));
103                 this.flowService.addFlow(addFlowInput.build());
104             }
105         }
106     }
107
108
109     protected Flow createFlow() {
110         FlowBuilder flowBuilder = new FlowBuilder();
111         flowBuilder.setMatch(new MatchBuilder().build());
112         flowBuilder.setInstructions(createSendToControllerInstructions().build());
113         flowBuilder.setPriority(0);
114
115         FlowKey key = new FlowKey(new FlowId(DEFAULT_FLOW_ID));
116         flowBuilder.setBarrier(Boolean.FALSE);
117         flowBuilder.setBufferId(OFConstants.OFP_NO_BUFFER);
118         BigInteger value = BigInteger.valueOf(10L);
119         // BigInteger outputPort = BigInteger.valueOf(65535L);
120         flowBuilder.setCookie(new FlowCookie(value));
121         flowBuilder.setCookieMask(new FlowCookie(value));
122         flowBuilder.setHardTimeout(0);
123         flowBuilder.setIdleTimeout(0);
124         flowBuilder.setInstallHw(false);
125         flowBuilder.setStrict(false);
126         flowBuilder.setContainerName(null);
127         flowBuilder.setFlags(new FlowModFlags(false, false, false, false, true));
128         flowBuilder.setId(new FlowId("12"));
129         flowBuilder.setTableId(TABLE_ID);
130         flowBuilder.setKey(key);
131         flowBuilder.setFlowName(LLDP_PUNT_WHOLE_PACKET_FLOW);
132
133         return flowBuilder.build();
134     }
135
136     private static InstructionsBuilder createSendToControllerInstructions() {
137         List<Action> actionList = new ArrayList<Action>();
138         ActionBuilder ab = new ActionBuilder();
139
140         OutputActionBuilder output = new OutputActionBuilder();
141         output.setMaxLength(OFConstants.OFPCML_NO_BUFFER);
142         Uri value = new Uri(OutputPortValues.CONTROLLER.toString());
143         output.setOutputNodeConnector(value);
144         ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
145         ab.setOrder(0);
146         ab.setKey(new ActionKey(0));
147         actionList.add(ab.build());
148         // Create an Apply Action
149         ApplyActionsBuilder aab = new ApplyActionsBuilder();
150         aab.setAction(actionList);
151
152         // Wrap our Apply Action in an Instruction
153         InstructionBuilder ib = new InstructionBuilder();
154         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
155         ib.setOrder(0);
156         ib.setKey(new InstructionKey(0));
157
158         // Put our Instruction in a list of Instructions
159         InstructionsBuilder isb = new InstructionsBuilder();
160         List<Instruction> instructions = new ArrayList<Instruction>();
161         instructions.add(ib.build());
162         isb.setInstruction(instructions);
163         return isb;
164     }
165
166 }