-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
+/**
+ * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v1.0 which accompanies this distribution,
import java.math.BigInteger;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
-import java.util.Set;
import java.util.concurrent.Callable;
+import javax.annotation.Nonnull;
import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.openflowplugin.api.OFConstants;
import org.opendaylight.openflowplugin.common.wait.SimpleTaskRetryLooper;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
-import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
-/**
- * Created by Martin Bobak mbobak@cisco.com on 8/27/14.
- */
-public class LLDPPacketPuntEnforcer implements DataChangeListener {
+public class LLDPPacketPuntEnforcer implements AutoCloseable, DataTreeChangeListener<FlowCapableNode> {
private static final long STARTUP_LOOP_TICK = 500L;
private static final int STARTUP_LOOP_MAX_RETRIES = 8;
private static final short TABLE_ID = (short) 0;
private static final String DEFAULT_FLOW_ID = "42";
private final SalFlowService flowService;
private final DataBroker dataBroker;
- private ListenerRegistration<DataChangeListener> dataChangeListenerRegistration;
+ private ListenerRegistration<DataTreeChangeListener> listenerRegistration;
public LLDPPacketPuntEnforcer(SalFlowService flowService, DataBroker dataBroker) {
this.flowService = flowService;
public void start() {
final InstanceIdentifier<FlowCapableNode> path = InstanceIdentifier.create(Nodes.class).child(Node.class).
augmentation(FlowCapableNode.class);
+ final DataTreeIdentifier<FlowCapableNode> identifier = new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, path);
SimpleTaskRetryLooper looper = new SimpleTaskRetryLooper(STARTUP_LOOP_TICK, STARTUP_LOOP_MAX_RETRIES);
try {
- dataChangeListenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<DataChangeListener>>() {
+ listenerRegistration = looper.loopUntilNoException(new Callable<ListenerRegistration<DataTreeChangeListener>>() {
@Override
- public ListenerRegistration<DataChangeListener> call() throws Exception {
- return dataBroker.registerDataChangeListener(
- LogicalDatastoreType.OPERATIONAL,
- path, LLDPPacketPuntEnforcer.this, AsyncDataBroker.DataChangeScope.BASE);
+ public ListenerRegistration<DataTreeChangeListener> call() throws Exception {
+ return dataBroker.registerDataTreeChangeListener(identifier, LLDPPacketPuntEnforcer.this);
}
});
} catch (Exception e) {
}
}
+ @Override
public void close() {
- if(dataChangeListenerRegistration != null) {
- dataChangeListenerRegistration.close();
+ if(listenerRegistration != null) {
+ listenerRegistration.close();
}
}
@Override
- public void onDataChanged(AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> change) {
- final Set<InstanceIdentifier<?>> changedDataKeys = change.getCreatedData().keySet();
-
- if (changedDataKeys != null) {
- for (InstanceIdentifier<?> key : changedDataKeys) {
+ public void onDataTreeChanged(@Nonnull final Collection<DataTreeModification<FlowCapableNode>> modifications) {
+ for (DataTreeModification modification : modifications) {
+ if (modification.getRootNode().getModificationType() == ModificationType.WRITE) {
AddFlowInputBuilder addFlowInput = new AddFlowInputBuilder(createFlow());
- addFlowInput.setNode(new NodeRef(key.firstIdentifierOf(Node.class)));
+ addFlowInput.setNode(new NodeRef(modification.getRootPath().getRootIdentifier().firstIdentifierOf(Node.class)));
this.flowService.addFlow(addFlowInput.build());
}
}
}
-
- protected Flow createFlow() {
+ static Flow createFlow() {
FlowBuilder flowBuilder = new FlowBuilder();
flowBuilder.setMatch(new MatchBuilder().build());
flowBuilder.setInstructions(createSendToControllerInstructions().build());
flowBuilder.setBarrier(Boolean.FALSE);
flowBuilder.setBufferId(OFConstants.OFP_NO_BUFFER);
BigInteger value = BigInteger.valueOf(10L);
- // BigInteger outputPort = BigInteger.valueOf(65535L);
flowBuilder.setCookie(new FlowCookie(value));
flowBuilder.setCookieMask(new FlowCookie(value));
flowBuilder.setHardTimeout(0);
}
private static InstructionsBuilder createSendToControllerInstructions() {
- List<Action> actionList = new ArrayList<Action>();
+ List<Action> actionList = new ArrayList<>();
ActionBuilder ab = new ActionBuilder();
OutputActionBuilder output = new OutputActionBuilder();
// Put our Instruction in a list of Instructions
InstructionsBuilder isb = new InstructionsBuilder();
- List<Instruction> instructions = new ArrayList<Instruction>();
+ List<Instruction> instructions = new ArrayList<>();
instructions.add(ib.build());
isb.setInstruction(instructions);
return isb;
-/*
+/**
* Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
*
* This program and the accompanying materials are made available under the
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
+
+import java.util.Collections;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
+import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
+import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
/**
- * Created by Martin Bobak mbobak@cisco.com on 8/29/14.
+ * Test for {@link LLDPPacketPuntEnforcer}.
*/
+@RunWith(MockitoJUnitRunner.class)
public class LLDPDataChangeListenerTest {
-
+ private LLDPPacketPuntEnforcer lldpPacketPuntEnforcer;
+ private final static InstanceIdentifier<Node> nodeIID = InstanceIdentifier.create(Nodes.class)
+ .child(Node.class, new NodeKey(new NodeId("testnode:1")));
@Mock
- private static SalFlowService flowService;
+ private SalFlowService flowService;
+ @Mock
+ private DataTreeModification<FlowCapableNode> dataTreeModification;
+ @Captor
+ private ArgumentCaptor<AddFlowInput> addFlowInputCaptor;
+
+ @Before
+ public void setUp() {
+ lldpPacketPuntEnforcer = new LLDPPacketPuntEnforcer(flowService, Mockito.mock(DataBroker.class));
+ final DataTreeIdentifier<FlowCapableNode> identifier = new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, nodeIID);
+ Mockito.when(dataTreeModification.getRootPath()).thenReturn(identifier);
+ Mockito.when(dataTreeModification.getRootNode()).thenReturn(Mockito.mock(DataObjectModification.class));
+ Mockito.when(dataTreeModification.getRootNode().getModificationType()).thenReturn(ModificationType.WRITE);
+ }
- /**
- * Test method for {@link org.opendaylight.openflowplugin.applications.tableMissEnforcer.LLDPPacketPuntEnforcer#createFlow()}
- * which ensures that LLDPDataChangeListener creates proper flow for
- */
@Test
- public void testCreateFlow() {
- LLDPPacketPuntEnforcer lldpDataChangeListener = new LLDPPacketPuntEnforcer(flowService, null);
- evaluateFlow(lldpDataChangeListener.createFlow());
+ public void testOnDataTreeChanged() {
+ lldpPacketPuntEnforcer.onDataTreeChanged(Collections.singleton(dataTreeModification));
+ Mockito.verify(flowService).addFlow(addFlowInputCaptor.capture());
+ AddFlowInput captured = addFlowInputCaptor.getValue();
+ Assert.assertEquals(nodeIID, captured.getNode().getValue());
}
- private static void evaluateFlow(final Flow flow) {
+ @Test
+ public void testCreateFlow() {
+ final Flow flow = lldpPacketPuntEnforcer.createFlow();
evaluateInstructions(flow.getInstructions());
}
+ @After
+ public void tearDown() {
+ lldpPacketPuntEnforcer.close();
+ }
+
private static void evaluateInstructions(final Instructions instructions) {
assertNotNull(instructions.getInstruction());
assertEquals(1, instructions.getInstruction().size());