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