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 com.google.common.base.Preconditions;
12 import java.math.BigInteger;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.concurrent.Future;
17 import javax.annotation.Nonnull;
18 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
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.infrautils.utils.concurrent.JdkFutures;
25 import org.opendaylight.openflowplugin.api.OFConstants;
26 import org.opendaylight.openflowplugin.applications.deviceownershipservice.DeviceOwnershipService;
27 import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Uri;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCaseBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.output.action._case.OutputActionBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
55 import org.opendaylight.yangtools.concepts.ListenerRegistration;
56 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
57 import org.opendaylight.yangtools.yang.common.RpcResult;
58 import org.slf4j.Logger;
59 import org.slf4j.LoggerFactory;
61 public class LLDPPacketPuntEnforcer implements AutoCloseable, ClusteredDataTreeChangeListener<FlowCapableNode> {
62 private static final Logger LOG = LoggerFactory.getLogger(LLDPPacketPuntEnforcer.class);
63 private static final long STARTUP_LOOP_TICK = 500L;
64 private static final int STARTUP_LOOP_MAX_RETRIES = 8;
65 private static final short TABLE_ID = (short) 0;
66 private static final String LLDP_PUNT_WHOLE_PACKET_FLOW = "LLDP_PUNT_WHOLE_PACKET_FLOW";
67 private static final String DEFAULT_FLOW_ID = "42";
68 private final SalFlowService flowService;
69 private final DataBroker dataBroker;
70 private final DeviceOwnershipService deviceOwnershipService;
71 private ListenerRegistration<?> listenerRegistration;
73 public LLDPPacketPuntEnforcer(SalFlowService flowService, DataBroker dataBroker,
74 DeviceOwnershipService deviceOwnershipService) {
75 this.flowService = flowService;
76 this.dataBroker = dataBroker;
77 this.deviceOwnershipService = Preconditions.checkNotNull(deviceOwnershipService,
78 "DeviceOwnershipService can not be null");
81 @SuppressWarnings("IllegalCatch")
83 final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
84 .augmentation(FlowCapableNode.class);
85 final DataTreeIdentifier<FlowCapableNode> identifier = new DataTreeIdentifier<>(
86 LogicalDatastoreType.OPERATIONAL, path);
87 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
89 listenerRegistration = looper.loopUntilNoException(() ->
90 dataBroker.registerDataTreeChangeListener(identifier, LLDPPacketPuntEnforcer.this));
91 } catch (Exception e) {
92 throw new IllegalStateException("registerDataTreeChangeListener failed", e);
98 if (listenerRegistration != null) {
99 listenerRegistration.close();
104 public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<FlowCapableNode>> modifications) {
105 for (DataTreeModification<FlowCapableNode> modification : modifications) {
106 if (modification.getRootNode().getModificationType() == ModificationType.WRITE) {
107 String nodeId = modification.getRootPath().getRootIdentifier()
108 .firstKeyOf(Node.class).getId().getValue();
109 if (deviceOwnershipService.isEntityOwned(nodeId)) {
110 AddFlowInputBuilder addFlowInput = new AddFlowInputBuilder(createFlow());
111 addFlowInput.setNode(new NodeRef(modification.getRootPath()
112 .getRootIdentifier().firstIdentifierOf(Node.class)));
113 final Future<RpcResult<AddFlowOutput>> resultFuture = this.flowService
114 .addFlow(addFlowInput.build());
115 JdkFutures.addErrorLogging(resultFuture, LOG, "addFlow");
117 LOG.debug("Node {} is not owned by this controller, so skip adding LLDP table miss flow",
125 static Flow createFlow() {
126 FlowBuilder flowBuilder = new FlowBuilder();
127 flowBuilder.setMatch(new MatchBuilder().build());
128 flowBuilder.setInstructions(createSendToControllerInstructions().build());
129 flowBuilder.setPriority(0);
131 FlowKey key = new FlowKey(new FlowId(DEFAULT_FLOW_ID));
132 flowBuilder.setBarrier(Boolean.FALSE);
133 flowBuilder.setBufferId(OFConstants.OFP_NO_BUFFER);
134 BigInteger value = BigInteger.valueOf(10L);
135 flowBuilder.setCookie(new FlowCookie(value));
136 flowBuilder.setCookieMask(new FlowCookie(value));
137 flowBuilder.setHardTimeout(0);
138 flowBuilder.setIdleTimeout(0);
139 flowBuilder.setInstallHw(false);
140 flowBuilder.setStrict(false);
141 flowBuilder.setContainerName(null);
142 flowBuilder.setFlags(new FlowModFlags(false, false, false, false, true));
143 flowBuilder.setId(new FlowId("12"));
144 flowBuilder.setTableId(TABLE_ID);
145 flowBuilder.withKey(key);
146 flowBuilder.setFlowName(LLDP_PUNT_WHOLE_PACKET_FLOW);
148 return flowBuilder.build();
151 private static InstructionsBuilder createSendToControllerInstructions() {
152 final List<Action> actionList = new ArrayList<>();
153 ActionBuilder ab = new ActionBuilder();
155 OutputActionBuilder output = new OutputActionBuilder();
156 output.setMaxLength(OFConstants.OFPCML_NO_BUFFER);
157 Uri value = new Uri(OutputPortValues.CONTROLLER.toString());
158 output.setOutputNodeConnector(value);
159 ab.setAction(new OutputActionCaseBuilder().setOutputAction(output.build()).build());
161 ab.withKey(new ActionKey(0));
162 actionList.add(ab.build());
163 // Create an Apply Action
164 ApplyActionsBuilder aab = new ApplyActionsBuilder();
165 aab.setAction(actionList);
167 // Wrap our Apply Action in an Instruction
168 InstructionBuilder ib = new InstructionBuilder();
169 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
171 ib.withKey(new InstructionKey(0));
173 // Put our Instruction in a list of Instructions
174 InstructionsBuilder isb = new InstructionsBuilder();
175 List<Instruction> instructions = new ArrayList<>();
176 instructions.add(ib.build());
177 isb.setInstruction(instructions);