2 * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.applications.tablemissenforcer;
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.Future;
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.DataTreeIdentifier;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.infrautils.utils.concurrent.JdkFutures;
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.AddFlowOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
53 import org.opendaylight.yangtools.concepts.ListenerRegistration;
54 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
55 import org.opendaylight.yangtools.yang.common.RpcResult;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
59 public class LLDPPacketPuntEnforcer implements AutoCloseable, ClusteredDataTreeChangeListener<FlowCapableNode> {
60 private static final Logger LOG = LoggerFactory.getLogger(LLDPPacketPuntEnforcer.class);
61 private static final long STARTUP_LOOP_TICK = 500L;
62 private static final int STARTUP_LOOP_MAX_RETRIES = 8;
63 private static final short TABLE_ID = (short) 0;
64 private static final String LLDP_PUNT_WHOLE_PACKET_FLOW = "LLDP_PUNT_WHOLE_PACKET_FLOW";
65 private static final String DEFAULT_FLOW_ID = "42";
66 private final SalFlowService flowService;
67 private final DataBroker dataBroker;
68 private ListenerRegistration<?> listenerRegistration;
70 public LLDPPacketPuntEnforcer(SalFlowService flowService, DataBroker dataBroker) {
71 this.flowService = flowService;
72 this.dataBroker = dataBroker;
75 @SuppressWarnings("IllegalCatch")
77 final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
78 .augmentation(FlowCapableNode.class);
79 final DataTreeIdentifier<FlowCapableNode> identifier = new DataTreeIdentifier<>(
80 LogicalDatastoreType.OPERATIONAL, path);
81 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
83 listenerRegistration = looper.loopUntilNoException(() ->
84 dataBroker.registerDataTreeChangeListener(identifier, LLDPPacketPuntEnforcer.this));
85 } catch (Exception e) {
86 throw new IllegalStateException("registerDataTreeChangeListener failed", e);
92 if (listenerRegistration != null) {
93 listenerRegistration.close();
98 public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<FlowCapableNode>> modifications) {
99 for (DataTreeModification<FlowCapableNode> 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 final Future<RpcResult<AddFlowOutput>> resultFuture = this.flowService.addFlow(addFlowInput.build());
105 JdkFutures.addErrorLogging(resultFuture, LOG, "addFlow");
110 static Flow createFlow() {
111 FlowBuilder flowBuilder = new FlowBuilder();
112 flowBuilder.setMatch(new MatchBuilder().build());
113 flowBuilder.setInstructions(createSendToControllerInstructions().build());
114 flowBuilder.setPriority(0);
116 FlowKey key = new FlowKey(new FlowId(DEFAULT_FLOW_ID));
117 flowBuilder.setBarrier(Boolean.FALSE);
118 flowBuilder.setBufferId(OFConstants.OFP_NO_BUFFER);
119 BigInteger value = BigInteger.valueOf(10L);
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);
133 return flowBuilder.build();
136 private static InstructionsBuilder createSendToControllerInstructions() {
137 final List<Action> actionList = new ArrayList<>();
138 ActionBuilder ab = new ActionBuilder();
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());
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);
152 // Wrap our Apply Action in an Instruction
153 InstructionBuilder ib = new InstructionBuilder();
154 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
156 ib.setKey(new InstructionKey(0));
158 // Put our Instruction in a list of Instructions
159 InstructionsBuilder isb = new InstructionsBuilder();
160 List<Instruction> instructions = new ArrayList<>();
161 instructions.add(ib.build());
162 isb.setInstruction(instructions);