Fix issues related to checkstyle enforcement
[openflowplugin.git] / applications / table-miss-enforcer / src / main / java / org / opendaylight / openflowplugin / applications / tablemissenforcer / LLDPPacketPuntEnforcer.java
1 /*
2  * Copyright (c) 2015, 2017 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.Collection;
14 import java.util.List;
15 import java.util.concurrent.Callable;
16 import javax.annotation.Nonnull;
17 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
18 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
19 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
20 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.openflowplugin.api.OFConstants;
25 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
52 import org.opendaylight.yangtools.concepts.ListenerRegistration;
53 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
54
55 public class LLDPPacketPuntEnforcer implements AutoCloseable, ClusteredDataTreeChangeListener<FlowCapableNode> {
56     private static final long STARTUP_LOOP_TICK = 500L;
57     private static final int STARTUP_LOOP_MAX_RETRIES = 8;
58     private static final short TABLE_ID = (short) 0;
59     private static final String LLDP_PUNT_WHOLE_PACKET_FLOW = "LLDP_PUNT_WHOLE_PACKET_FLOW";
60     private static final String DEFAULT_FLOW_ID = "42";
61     private final SalFlowService flowService;
62     private final DataBroker dataBroker;
63     private ListenerRegistration<DataTreeChangeListener> listenerRegistration;
64
65     public LLDPPacketPuntEnforcer(SalFlowService flowService, DataBroker dataBroker) {
66         this.flowService = flowService;
67         this.dataBroker = dataBroker;
68     }
69
70     @SuppressWarnings("IllegalCatch")
71     public void start() {
72         final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
73                 .augmentation(FlowCapableNode.class);
74         final DataTreeIdentifier<FlowCapableNode> identifier = new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
75                                                                                       path);
76         SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
77         try {
78             listenerRegistration = looper
79                     .loopUntilNoException(new Callable<ListenerRegistration<DataTreeChangeListener>>() {
80                         @Override
81                         public ListenerRegistration<DataTreeChangeListener> call() throws Exception {
82                             return dataBroker.registerDataTreeChangeListener(identifier, LLDPPacketPuntEnforcer.this);
83                         }
84                     });
85         } catch (Exception e) {
86             throw new IllegalStateException("registerDataTreeChangeListener failed", e);
87         }
88     }
89
90     @Override
91     public void close() {
92         if (listenerRegistration != null) {
93             listenerRegistration.close();
94         }
95     }
96
97     @Override
98     public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<FlowCapableNode>> modifications) {
99         for (DataTreeModification modification : modifications) {
100             if (modification.getRootNode().getModificationType() == ModificationType.WRITE) {
101                 AddFlowInputBuilder addFlowInput = new AddFlowInputBuilder(createFlow());
102                 addFlowInput.setNode(
103                         new NodeRef(modification.getRootPath().getRootIdentifier().firstIdentifierOf(Node.class)));
104                 this.flowService.addFlow(addFlowInput.build());
105             }
106         }
107     }
108
109     static 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         flowBuilder.setCookie(new FlowCookie(value));
120         flowBuilder.setCookieMask(new FlowCookie(value));
121         flowBuilder.setHardTimeout(0);
122         flowBuilder.setIdleTimeout(0);
123         flowBuilder.setInstallHw(false);
124         flowBuilder.setStrict(false);
125         flowBuilder.setContainerName(null);
126         flowBuilder.setFlags(new FlowModFlags(false, false, false, false, true));
127         flowBuilder.setId(new FlowId("12"));
128         flowBuilder.setTableId(TABLE_ID);
129         flowBuilder.setKey(key);
130         flowBuilder.setFlowName(LLDP_PUNT_WHOLE_PACKET_FLOW);
131
132         return flowBuilder.build();
133     }
134
135     private static InstructionsBuilder createSendToControllerInstructions() {
136         final List<Action> actionList = new ArrayList<>();
137         ActionBuilder ab = new ActionBuilder();
138
139         OutputActionBuilder output = new OutputActionBuilder();
140         output.setMaxLength(OFConstants.OFPCML_NO_BUFFER);
141         Uri value = new Uri(OutputPortValues.CONTROLLER.toString());
142         output.setOutputNodeConnector(value);
143         ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
144         ab.setOrder(0);
145         ab.setKey(new ActionKey(0));
146         actionList.add(ab.build());
147         // Create an Apply Action
148         ApplyActionsBuilder aab = new ApplyActionsBuilder();
149         aab.setAction(actionList);
150
151         // Wrap our Apply Action in an Instruction
152         InstructionBuilder ib = new InstructionBuilder();
153         ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
154         ib.setOrder(0);
155         ib.setKey(new InstructionKey(0));
156
157         // Put our Instruction in a list of Instructions
158         InstructionsBuilder isb = new InstructionsBuilder();
159         List<Instruction> instructions = new ArrayList<>();
160         instructions.add(ib.build());
161         isb.setInstruction(instructions);
162         return isb;
163     }
164
165 }