BUG-4117: add support for meter and group old Notif 43/31543/4
authorMartin Bobak <mbobak@cisco.com>
Thu, 20 Aug 2015 21:45:07 +0000 (23:45 +0200)
committermichal rehak <mirehak@cisco.com>
Wed, 20 Jan 2016 12:36:17 +0000 (12:36 +0000)
 - keep backward old notification compatibility for FlowCabaleNode items

Note: Original commit https://git.opendaylight.org/gerrit/#/c/25965/
has to be broken to a chain of smaller commits

Change-Id: Ib0011bde41264b4d3db413e4783cf2a0d76946fc
Signed-off-by: Martin Bobak <mbobak@cisco.com>
Signed-off-by: Vaclav Demcak <vdemcak@cisco.com>
(cherry picked from commit 31f456d72a3b208d4a6c61f11b798ca93a44ddc4)

applications/old-notification-supplier/src/main/java/org/opendaylight/openflowplugin/applications/old/notification/supplier/OldNotifProviderImpl.java
applications/old-notification-supplier/src/main/java/org/opendaylight/openflowplugin/applications/old/notification/supplier/impl/item/GroupNotificationSupplierImpl.java [new file with mode: 0644]
applications/old-notification-supplier/src/main/java/org/opendaylight/openflowplugin/applications/old/notification/supplier/impl/item/MeterNotificationSupplierImpl.java [new file with mode: 0644]
applications/old-notification-supplier/src/test/java/org/opendaylight/openflowplugin/applications/old/notification/supplier/impl/item/GroupNotificationSupplierImplTest.java [new file with mode: 0644]
applications/old-notification-supplier/src/test/java/org/opendaylight/openflowplugin/applications/old/notification/supplier/impl/item/MeterNotificationSupplierImplTest.java [new file with mode: 0644]

index 566abde56c485a9eab5f64eb116162ce3008e615..00da49a8d9e0d7c965f3d9f983393ba50a3a03da 100644 (file)
@@ -18,17 +18,27 @@ import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
 import org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.NodeConnectorNotificationSupplierImpl;
 import org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.NodeNotificationSupplierImpl;
 import org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.item.FlowNotificationSupplierImpl;
+import org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.item.GroupNotificationSupplierImpl;
+import org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.item.MeterNotificationSupplierImpl;
 import org.opendaylight.openflowplugin.applications.old.notification.supplier.tools.OldNotifProviderConfig;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
 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.FlowAdded;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupAdded;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRemoved;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorUpdated;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRemoved;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterAdded;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterUpdated;
 
 /**
  * Provider Implementation
@@ -44,6 +54,8 @@ public class OldNotifProviderImpl implements OldNotifProvider {
     private OldNotifSupplierForItemRoot<FlowCapableNode, NodeUpdated, NodeRemoved> nodeSupp;
     private OldNotifSupplierForItemRoot<FlowCapableNodeConnector, NodeConnectorUpdated, NodeConnectorRemoved> connectorSupp;
     private OldNotifSupplierForItem<Flow, FlowAdded, FlowUpdated, FlowRemoved> flowSupp;
+    private OldNotifSupplierForItem<Meter, MeterAdded, MeterUpdated, MeterRemoved> meterSupp;
+    private OldNotifSupplierForItem<Group, GroupAdded, GroupUpdated, GroupRemoved> groupSupp;
 
     /**
      * Provider constructor set all needed final parameters
@@ -64,8 +76,10 @@ public class OldNotifProviderImpl implements OldNotifProvider {
         nodeSupp = new NodeNotificationSupplierImpl(nps, db);
         connectorSupp = new NodeConnectorNotificationSupplierImpl(nps, db);
         flowSupp = config.isFlowSupport() ? new FlowNotificationSupplierImpl(nps, db) : null;
+        meterSupp = config.isMeterSupport() ? new MeterNotificationSupplierImpl(nps, db) : null;
+        groupSupp = config.isGroupSupport() ? new GroupNotificationSupplierImpl(nps, db) : null;
 
-        supplierList = new ArrayList<>(Arrays.asList(nodeSupp, connectorSupp, flowSupp));
+        supplierList = new ArrayList<>(Arrays.asList(nodeSupp, connectorSupp, flowSupp, meterSupp, groupSupp));
     }
 
     @Override
diff --git a/applications/old-notification-supplier/src/main/java/org/opendaylight/openflowplugin/applications/old/notification/supplier/impl/item/GroupNotificationSupplierImpl.java b/applications/old-notification-supplier/src/main/java/org/opendaylight/openflowplugin/applications/old/notification/supplier/impl/item/GroupNotificationSupplierImpl.java
new file mode 100644 (file)
index 0000000..55f8bb5
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2015 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,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.item;
+
+import com.google.common.base.Preconditions;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupAdded;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupAddedBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupRemovedBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupUpdatedBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupRef;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Implementation define a contract between {@link Group} data object
+ * and {@link GroupAdded}, {@link GroupUpdated} and {@link GroupRemoved} notifications.
+ */
+public class GroupNotificationSupplierImpl extends
+        AbstractNotifSupplierForItem<Group, GroupAdded, GroupUpdated, GroupRemoved> {
+
+    private static final InstanceIdentifier<Group> wildCardedInstanceIdent = getNodeWildII().augmentation(FlowCapableNode.class).child(Group.class);
+
+    /**
+     * Constructor register supplier as DataChangeLister and create wildCarded InstanceIdentifier.
+     *
+     * @param notifProviderService - {@link NotificationProviderService}
+     * @param db - {@link DataBroker}
+     */
+    public GroupNotificationSupplierImpl(final NotificationProviderService notifProviderService, final DataBroker db) {
+        super(notifProviderService, db, Group.class);
+    }
+
+    @Override
+    public InstanceIdentifier<Group> getWildCardPath() {
+        return wildCardedInstanceIdent;
+    }
+
+    @Override
+    public GroupAdded createNotification(final Group o, final InstanceIdentifier<Group> path) {
+        Preconditions.checkArgument(o != null);
+        Preconditions.checkArgument(path != null);
+        final GroupAddedBuilder builder = new GroupAddedBuilder(o);
+        builder.setGroupRef(new GroupRef(path));
+        builder.setNode(createNodeRef(path));
+        return builder.build();
+    }
+
+    @Override
+    public GroupUpdated updateNotification(final Group o, final InstanceIdentifier<Group> path) {
+        Preconditions.checkArgument(o != null);
+        Preconditions.checkArgument(path != null);
+        final GroupUpdatedBuilder builder = new GroupUpdatedBuilder(o);
+        builder.setGroupRef(new GroupRef(path));
+        builder.setNode(createNodeRef(path));
+        return builder.build();
+    }
+
+    @Override
+    public GroupRemoved deleteNotification(final InstanceIdentifier<Group> path) {
+        Preconditions.checkArgument(path != null);
+        final GroupRemovedBuilder builder = new GroupRemovedBuilder();
+        builder.setGroupId(path.firstKeyOf(Group.class, GroupKey.class).getGroupId());
+        builder.setGroupRef(new GroupRef(path));
+        builder.setNode(createNodeRef(path));
+        return builder.build();
+    }
+}
+
diff --git a/applications/old-notification-supplier/src/main/java/org/opendaylight/openflowplugin/applications/old/notification/supplier/impl/item/MeterNotificationSupplierImpl.java b/applications/old-notification-supplier/src/main/java/org/opendaylight/openflowplugin/applications/old/notification/supplier/impl/item/MeterNotificationSupplierImpl.java
new file mode 100644 (file)
index 0000000..1836083
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * Copyright (c) 2015 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,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.item;
+
+import com.google.common.base.Preconditions;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterAdded;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterAddedBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterRemovedBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterUpdatedBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterRef;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ * Implementation define a contract between {@link Meter} data object
+ * and {@link MeterAdded}, {@link MeterUpdated} and {@link MeterRemoved} notifications.
+ */
+public class MeterNotificationSupplierImpl extends
+        AbstractNotifSupplierForItem<Meter, MeterAdded, MeterUpdated, MeterRemoved> {
+
+    private static final InstanceIdentifier<Meter> wildCardedInstanceIdent = getNodeWildII().augmentation(FlowCapableNode.class).child(Meter.class);
+
+    /**
+     * Constructor register supplier as DataChangeLister and create wildCarded InstanceIdentifier.
+     *
+     * @param notifProviderService - {@link NotificationProviderService}
+     * @param db - {@link DataBroker}
+     */
+    public MeterNotificationSupplierImpl(final NotificationProviderService notifProviderService, final DataBroker db) {
+        super(notifProviderService, db, Meter.class);
+    }
+
+    @Override
+    public InstanceIdentifier<Meter> getWildCardPath() {
+        return wildCardedInstanceIdent;
+    }
+
+    @Override
+    public MeterAdded createNotification(final Meter o, final InstanceIdentifier<Meter> path) {
+        Preconditions.checkArgument(o != null);
+        Preconditions.checkArgument(path != null);
+        final MeterAddedBuilder builder = new MeterAddedBuilder(o);
+        builder.setMeterRef(new MeterRef(path));
+        builder.setNode(createNodeRef(path));
+        return builder.build();
+    }
+
+    @Override
+    public MeterUpdated updateNotification(final Meter o, final InstanceIdentifier<Meter> path) {
+        Preconditions.checkArgument(o != null);
+        Preconditions.checkArgument(path != null);
+        final MeterUpdatedBuilder builder = new MeterUpdatedBuilder(o);
+        builder.setMeterRef(new MeterRef(path));
+        builder.setNode(createNodeRef(path));
+        return builder.build();
+    }
+
+    @Override
+    public MeterRemoved deleteNotification(final InstanceIdentifier<Meter> path) {
+        Preconditions.checkArgument(path != null);
+        final MeterRemovedBuilder builder = new MeterRemovedBuilder();
+        builder.setMeterId(path.firstKeyOf(Meter.class, MeterKey.class).getMeterId());
+        builder.setMeterRef(new MeterRef(path));
+        builder.setNode(createNodeRef(path));
+        return builder.build();
+    }
+}
+
diff --git a/applications/old-notification-supplier/src/test/java/org/opendaylight/openflowplugin/applications/old/notification/supplier/impl/item/GroupNotificationSupplierImplTest.java b/applications/old-notification-supplier/src/test/java/org/opendaylight/openflowplugin/applications/old/notification/supplier/impl/item/GroupNotificationSupplierImplTest.java
new file mode 100644 (file)
index 0000000..404651d
--- /dev/null
@@ -0,0 +1,164 @@
+/*
+ * Copyright (c) 2015 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,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.item;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Matchers;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
+import org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.helper.TestChangeEventBuildHelper;
+import org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.helper.TestSupplierVerifyHelper;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupAdded;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupId;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.Group;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.groups.GroupKey;
+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.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ *
+ */
+public class GroupNotificationSupplierImplTest {
+
+    private static final String FLOW_NODE_ID = "test-111";
+    private static final Long GROUP_ID = 111L;
+    private GroupNotificationSupplierImpl notifSupplierImpl;
+    private NotificationProviderService notifProviderService;
+    private DataBroker dataBroker;
+
+    @Before
+    public void initalization() {
+        notifProviderService = mock(NotificationProviderService.class);
+        dataBroker = mock(DataBroker.class);
+        notifSupplierImpl = new GroupNotificationSupplierImpl(notifProviderService, dataBroker);
+        TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNullChangeEvent() {
+        notifSupplierImpl.onDataChanged(null);
+    }
+
+    @Test
+    public void testNullableChangeEvent() {
+        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
+    }
+
+    @Test
+    public void testEmptyChangeEvent() {
+        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
+    }
+
+    @Test
+    public void testCreate() {
+        final GroupAdded notification = notifSupplierImpl.createNotification(createTestGroup(), createTestGroupPath());
+        assertNotNull(notification);
+        assertEquals(GROUP_ID, notification.getGroupId().getValue());
+        assertEquals(GROUP_ID, notification.getGroupRef().getValue().firstKeyOf(Group.class, GroupKey.class).getGroupId().getValue());
+        assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+    }
+
+    @Test
+    public void testCreateChangeEvent() {
+        final Map<InstanceIdentifier<?>, DataObject> createdData = new HashMap<>();
+        createdData.put(createTestGroupPath(), createTestGroup());
+        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
+        verify(notifProviderService, times(1)).publish(Matchers.any(GroupAdded.class));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateFromNullNodeConnector() {
+        notifSupplierImpl.createNotification(null, createTestGroupPath());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateFromNullPath() {
+        notifSupplierImpl.createNotification(createTestGroup(), null);
+    }
+
+    @Test
+    public void testUpdate() {
+        final GroupUpdated notification = notifSupplierImpl.updateNotification(createTestGroup(), createTestGroupPath());
+        assertNotNull(notification);
+        assertEquals(GROUP_ID, notification.getGroupId().getValue());
+        assertEquals(GROUP_ID, notification.getGroupRef().getValue().firstKeyOf(Group.class, GroupKey.class).getGroupId().getValue());
+        assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+    }
+
+    @Test
+    public void testUpdateChangeEvent() {
+        final Map<InstanceIdentifier<?>, DataObject> createdData = new HashMap<>();
+        createdData.put(createTestGroupPath(), createTestGroup());
+        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
+        verify(notifProviderService, times(1)).publish(Matchers.any(GroupUpdated.class));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateFromNullNodeConnector() {
+        notifSupplierImpl.createNotification(null, createTestGroupPath());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateFromNullPath() {
+        notifSupplierImpl.createNotification(createTestGroup(), null);
+    }
+
+    @Test
+    public void testDelete() {
+        final GroupRemoved notification = notifSupplierImpl.deleteNotification(createTestGroupPath());
+        assertNotNull(notification);
+        assertEquals(GROUP_ID, notification.getGroupId().getValue());
+        assertEquals(GROUP_ID, notification.getGroupRef().getValue().firstKeyOf(Group.class, GroupKey.class).getGroupId().getValue());
+        assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+    }
+
+    @Test
+    public void testDeleteChangeEvent() {
+        final Set<InstanceIdentifier<?>> removeData = new HashSet<>();
+        removeData.add(createTestGroupPath());
+        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(null, null, removeData));
+        verify(notifProviderService, times(1)).publish(Matchers.any(GroupRemoved.class));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testDeleteFromNullPath() {
+        notifSupplierImpl.deleteNotification(null);
+    }
+
+    private static InstanceIdentifier<Group> createTestGroupPath() {
+        return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
+                .augmentation(FlowCapableNode.class).child(Group.class, new GroupKey(new GroupId(GROUP_ID)));
+    }
+
+    private static Group createTestGroup() {
+        final GroupBuilder builder = new GroupBuilder();
+        builder.setGroupId(new GroupId(GROUP_ID));
+        return builder.build();
+    }
+}
+
diff --git a/applications/old-notification-supplier/src/test/java/org/opendaylight/openflowplugin/applications/old/notification/supplier/impl/item/MeterNotificationSupplierImplTest.java b/applications/old-notification-supplier/src/test/java/org/opendaylight/openflowplugin/applications/old/notification/supplier/impl/item/MeterNotificationSupplierImplTest.java
new file mode 100644 (file)
index 0000000..2a936f8
--- /dev/null
@@ -0,0 +1,165 @@
+/*
+ * Copyright (c) 2015 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,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.item;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Matchers;
+import org.opendaylight.controller.md.sal.binding.api.DataBroker;
+import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
+import org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.helper.TestChangeEventBuildHelper;
+import org.opendaylight.openflowplugin.applications.old.notification.supplier.impl.helper.TestSupplierVerifyHelper;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.Meter;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.meters.MeterKey;
+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.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterAdded;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterRemoved;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.service.rev130918.MeterUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.meter.types.rev130918.MeterId;
+import org.opendaylight.yangtools.yang.binding.DataObject;
+import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
+
+/**
+ *
+ */
+public class MeterNotificationSupplierImplTest {
+
+    private static final String FLOW_NODE_ID = "test-111";
+    private static final Long METER_ID = 111L;
+    private MeterNotificationSupplierImpl notifSupplierImpl;
+    private NotificationProviderService notifProviderService;
+    private DataBroker dataBroker;
+
+    @Before
+    public void initalization() {
+        notifProviderService = mock(NotificationProviderService.class);
+        dataBroker = mock(DataBroker.class);
+        notifSupplierImpl = new MeterNotificationSupplierImpl(notifProviderService, dataBroker);
+        TestSupplierVerifyHelper.verifyDataChangeRegistration(dataBroker);
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testNullChangeEvent() {
+        notifSupplierImpl.onDataChanged(null);
+    }
+
+    @Test
+    public void testNullableChangeEvent() {
+        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
+    }
+
+    @Test
+    public void testEmptyChangeEvent() {
+        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createEmptyTestDataEvent());
+    }
+
+    @Test
+    public void testCreate() {
+        final MeterAdded notification = notifSupplierImpl.createNotification(createTestMeter(), createTestMeterPath());
+        assertNotNull(notification);
+        assertEquals(METER_ID, notification.getMeterId().getValue());
+        assertEquals(METER_ID, notification.getMeterRef().getValue().firstKeyOf(Meter.class, MeterKey.class).getMeterId().getValue());
+        assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+    }
+
+    @Test
+    public void testCreateChangeEvent() {
+        final Map<InstanceIdentifier<?>, DataObject> createdData = new HashMap<>();
+        createdData.put(createTestMeterPath(), createTestMeter());
+        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
+        verify(notifProviderService, times(1)).publish(Matchers.any(MeterAdded.class));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateFromNullNodeConnector() {
+        notifSupplierImpl.createNotification(null, createTestMeterPath());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testCreateFromNullPath() {
+        notifSupplierImpl.createNotification(createTestMeter(), null);
+    }
+
+    @Test
+    public void testUpdate() {
+        final MeterUpdated notification = notifSupplierImpl
+                .updateNotification(createTestMeter(), createTestMeterPath());
+        assertNotNull(notification);
+        assertEquals(METER_ID, notification.getMeterId().getValue());
+        assertEquals(METER_ID, notification.getMeterRef().getValue().firstKeyOf(Meter.class, MeterKey.class).getMeterId().getValue());
+        assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+    }
+
+    @Test
+    public void testUdateChangeEvent() {
+        final Map<InstanceIdentifier<?>, DataObject> createdData = new HashMap<>();
+        createdData.put(createTestMeterPath(), createTestMeter());
+        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(createdData, null, null));
+        verify(notifProviderService, times(1)).publish(Matchers.any(MeterUpdated.class));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateFromNullNodeConnector() {
+        notifSupplierImpl.createNotification(null, createTestMeterPath());
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testUpdateFromNullPath() {
+        notifSupplierImpl.createNotification(createTestMeter(), null);
+    }
+
+    @Test
+    public void testDelete() {
+        final MeterRemoved notification = notifSupplierImpl.deleteNotification(createTestMeterPath());
+        assertNotNull(notification);
+        assertEquals(METER_ID, notification.getMeterId().getValue());
+        assertEquals(METER_ID, notification.getMeterRef().getValue().firstKeyOf(Meter.class, MeterKey.class).getMeterId().getValue());
+        assertEquals(FLOW_NODE_ID, notification.getNode().getValue().firstKeyOf(Node.class, NodeKey.class).getId().getValue());
+    }
+
+    @Test
+    public void testDeleteChangeEvent() {
+        final Set<InstanceIdentifier<?>> removeData = new HashSet<>();
+        removeData.add(createTestMeterPath());
+        notifSupplierImpl.onDataChanged(TestChangeEventBuildHelper.createTestDataEvent(null, null, removeData));
+        verify(notifProviderService, times(1)).publish(Matchers.any(MeterRemoved.class));
+    }
+
+    @Test(expected = IllegalArgumentException.class)
+    public void testDeleteFromNullPath() {
+        notifSupplierImpl.deleteNotification(null);
+    }
+
+    private static InstanceIdentifier<Meter> createTestMeterPath() {
+        return InstanceIdentifier.create(Nodes.class).child(Node.class, new NodeKey(new NodeId(FLOW_NODE_ID)))
+                .augmentation(FlowCapableNode.class).child(Meter.class, new MeterKey(new MeterId(METER_ID)));
+    }
+
+    private static Meter createTestMeter() {
+        final MeterBuilder builder = new MeterBuilder();
+        builder.setMeterId(new MeterId(METER_ID));
+        return builder.build();
+    }
+}
+