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
8 package org.opendaylight.openflowplugin.applications.tablemissenforcer;
10 import static java.util.Objects.requireNonNull;
12 import java.util.Collection;
13 import org.opendaylight.infrautils.utils.concurrent.LoggingFutures;
14 import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
15 import org.opendaylight.mdsal.binding.api.DataBroker;
16 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
17 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
18 import org.opendaylight.mdsal.binding.api.DataTreeModification;
19 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
20 import org.opendaylight.openflowplugin.api.OFConstants;
21 import org.opendaylight.openflowplugin.applications.deviceownershipservice.DeviceOwnershipService;
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.ActionBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInputBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowModFlags;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.OutputPortValues;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
45 import org.opendaylight.yangtools.concepts.ListenerRegistration;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
48 import org.opendaylight.yangtools.yang.common.Uint16;
49 import org.opendaylight.yangtools.yang.common.Uint64;
50 import org.opendaylight.yangtools.yang.common.Uint8;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 public class LLDPPacketPuntEnforcer implements AutoCloseable, ClusteredDataTreeChangeListener<FlowCapableNode> {
55 private static final Logger LOG = LoggerFactory.getLogger(LLDPPacketPuntEnforcer.class);
56 private static final long STARTUP_LOOP_TICK = 500L;
57 private static final int STARTUP_LOOP_MAX_RETRIES = 8;
58 private static final Uint8 TABLE_ID = Uint8.ZERO;
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 final DeviceOwnershipService deviceOwnershipService;
64 private ListenerRegistration<?> listenerRegistration;
66 public LLDPPacketPuntEnforcer(final SalFlowService flowService, final DataBroker dataBroker,
67 final DeviceOwnershipService deviceOwnershipService) {
68 this.flowService = flowService;
69 this.dataBroker = dataBroker;
70 this.deviceOwnershipService = requireNonNull(deviceOwnershipService, "DeviceOwnershipService can not be null");
73 @SuppressWarnings("IllegalCatch")
75 final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class)
76 .augmentation(FlowCapableNode.class);
77 final DataTreeIdentifier<FlowCapableNode> identifier = DataTreeIdentifier.create(
78 LogicalDatastoreType.OPERATIONAL, path);
79 SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
81 listenerRegistration = looper.loopUntilNoException(() ->
82 dataBroker.registerDataTreeChangeListener(identifier, LLDPPacketPuntEnforcer.this));
83 } catch (Exception e) {
84 throw new IllegalStateException("registerDataTreeChangeListener failed", e);
90 if (listenerRegistration != null) {
91 listenerRegistration.close();
96 public void onDataTreeChanged(final Collection<DataTreeModification<FlowCapableNode>> modifications) {
97 for (DataTreeModification<FlowCapableNode> modification : modifications) {
98 if (modification.getRootNode().getModificationType() == ModificationType.WRITE) {
99 String nodeId = modification.getRootPath().getRootIdentifier()
100 .firstKeyOf(Node.class).getId().getValue();
101 if (deviceOwnershipService.isEntityOwned(nodeId)) {
102 AddFlowInputBuilder addFlowInput = new AddFlowInputBuilder(createFlow());
103 addFlowInput.setNode(new NodeRef(modification.getRootPath()
104 .getRootIdentifier().firstIdentifierOf(Node.class)));
105 LoggingFutures.addErrorLogging(this.flowService.addFlow(addFlowInput.build()), LOG, "addFlow");
107 LOG.debug("Node {} is not owned by this controller, so skip adding LLDP table miss flow", nodeId);
113 static Flow createFlow() {
114 return new FlowBuilder()
115 .setMatch(new MatchBuilder().build())
116 .setInstructions(createSendToControllerInstructions().build())
117 .setPriority(Uint16.ZERO)
118 .setBarrier(Boolean.FALSE)
119 .setBufferId(OFConstants.OFP_NO_BUFFER)
120 .setCookie(new FlowCookie(Uint64.TEN))
121 .setCookieMask(new FlowCookie(Uint64.TEN))
122 .setHardTimeout(Uint16.ZERO)
123 .setIdleTimeout(Uint16.ZERO)
126 .setContainerName(null)
127 .setFlags(new FlowModFlags(false, false, false, false, true))
128 .setId(new FlowId("12"))
129 .setTableId(TABLE_ID)
130 .withKey(new FlowKey(new FlowId(DEFAULT_FLOW_ID)))
131 .setFlowName(LLDP_PUNT_WHOLE_PACKET_FLOW)
135 private static InstructionsBuilder createSendToControllerInstructions() {
136 return new InstructionsBuilder()
137 .setInstruction(BindingMap.of(new InstructionBuilder()
138 // Wrap our Apply Action in an Instruction
139 .setInstruction(new ApplyActionsCaseBuilder()
140 .setApplyActions(new ApplyActionsBuilder()
141 // Create an Apply Action
142 .setAction(BindingMap.of(new ActionBuilder()
143 .setAction(new OutputActionCaseBuilder()
144 .setOutputAction(new OutputActionBuilder()
145 .setMaxLength(OFConstants.OFPCML_NO_BUFFER)
146 .setOutputNodeConnector(new Uri(OutputPortValues.CONTROLLER.toString()))