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