Bug 6372 table-miss-enforcer - DTCL instead of DTL 11/43511/2
authorAndrej Leitner <andrej.leitner@pantheon.tech>
Tue, 9 Aug 2016 12:41:12 +0000 (14:41 +0200)
committerAndrej Leitner <andrej.leitner@pantheon.tech>
Tue, 9 Aug 2016 14:56:59 +0000 (16:56 +0200)
 - table-miss-enforcer app converted to use DataTreeChangeListener
   instead of deprecated DataChangeListener
 - updated tests

Change-Id: I74c2c6755c1d6604a25bfee8d58d79787bef1c48
Signed-off-by: Andrej Leitner <andrej.leitner@pantheon.tech>
applications/table-miss-enforcer/src/main/java/org/opendaylight/openflowplugin/applications/tableMissEnforcer/LLDPPacketPuntEnforcer.java
applications/table-miss-enforcer/src/main/resources/org/opendaylight/blueprint/table-miss-enforcer.xml
applications/table-miss-enforcer/src/test/java/org/opendaylight/openflowplugin/applications/tableMissEnforcer/LLDPDataChangeListenerTest.java

index d2e726529928af633712157231047be9df54d9a8..31dacc2e266fdcee5981d6efa3862c4fe0b17a09 100644 (file)
@@ -1,5 +1,5 @@
-/*
- * 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,
@@ -10,13 +10,15 @@ package org.opendaylight.openflowplugin.applications.tableMissEnforcer;
 
 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;
@@ -47,13 +49,9 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef
 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;
@@ -61,7 +59,7 @@ public class LLDPPacketPuntEnforcer implements DataChangeListener {
     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;
@@ -71,14 +69,13 @@ public class LLDPPacketPuntEnforcer implements DataChangeListener {
     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) {
@@ -86,27 +83,25 @@ public class LLDPPacketPuntEnforcer implements DataChangeListener {
         }
     }
 
+    @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());
@@ -116,7 +111,6 @@ public class LLDPPacketPuntEnforcer implements DataChangeListener {
         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);
@@ -134,7 +128,7 @@ public class LLDPPacketPuntEnforcer implements DataChangeListener {
     }
 
     private static InstructionsBuilder createSendToControllerInstructions() {
-        List<Action> actionList = new ArrayList<Action>();
+        List<Action> actionList = new ArrayList<>();
         ActionBuilder ab = new ActionBuilder();
 
         OutputActionBuilder output = new OutputActionBuilder();
@@ -157,7 +151,7 @@ public class LLDPPacketPuntEnforcer implements DataChangeListener {
 
         // 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;
index ff9ff6c2f026577b4f9843f8cc4b88799a354511..f644908dedfcbef673a73f4d06508d00a67d552b 100644 (file)
@@ -1,7 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
            xmlns:odl="http://opendaylight.org/xmlns/blueprint/v1.0.0"
-        odl:use-default-for-reference-types="true">
+           odl:use-default-for-reference-types="true">
 
   <reference id="dataBroker" interface="org.opendaylight.controller.md.sal.binding.api.DataBroker"/>
 
index 94dd5975d6aab86f2f8be4766a95c0e48a42b742..08df369ef45a87282ba19683893a94b2af8f5e0d 100644 (file)
@@ -1,4 +1,4 @@
-/*
+/**
  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -10,38 +10,82 @@ package org.opendaylight.openflowplugin.applications.tableMissEnforcer;
 
 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());