Bump upstreams for Silicon
[neutron.git] / neutron-logger / src / main / java / org / opendaylight / neutron / logger / NeutronLogger.java
index b9d15db7d1613808abb5bcb11db355c8dcb4aa89..a854a47dff125009193a1a45f4776641d273c1b6 100644 (file)
@@ -5,19 +5,22 @@
  * 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.neutron.logger;
 
-import com.google.common.base.Preconditions;
+import static java.util.Objects.requireNonNull;
 
 import java.util.Collection;
-import javax.annotation.Nonnull;
-import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
-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.DataTreeIdentifier;
-import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import javax.annotation.PostConstruct;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.mdsal.binding.api.ClusteredDataTreeChangeListener;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.DataObjectModification;
+import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
+import org.opendaylight.mdsal.binding.api.DataTreeModification;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
 import org.opendaylight.yangtools.yang.binding.DataObject;
@@ -25,29 +28,30 @@ import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public class NeutronLogger implements AutoCloseable {
+@Singleton
+public final class NeutronLogger {
     private static final Logger LOG = LoggerFactory.getLogger(NeutronLogger.class);
 
-    private DataBroker db;
+    private final DataBroker db;
     private ClusteredDataTreeChangeListener<Neutron> configurationDataTreeChangeListener;
     private ListenerRegistration<? extends ClusteredDataTreeChangeListener<Neutron>> configurationRegisteredListener;
     private ClusteredDataTreeChangeListener<Neutron> operationalDataTreeChangeListener;
     private ListenerRegistration<? extends ClusteredDataTreeChangeListener<Neutron>> operationalRegisteredListener;
 
-    public NeutronLogger(@Nonnull DataBroker db) {
+    @Inject
+    public NeutronLogger(@NonNull DataBroker db) {
         LOG.info("Creating NeutronLogger {}", db);
-        this.db = Preconditions.checkNotNull(db, "null db");
+        this.db = requireNonNull(db, "null db");
     }
 
-    private <T extends DataObject>
-        void formatModification(@Nonnull final StringBuilder messageBuilder,
-                                @Nonnull final DataObjectModification<T> objectModification) {
+    private <T extends DataObject> void formatModification(@NonNull final StringBuilder messageBuilder,
+            @NonNull final DataObjectModification<T> objectModification) {
         final String typeName = objectModification.getDataType().getSimpleName();
 
         switch (objectModification.getModificationType()) {
             case SUBTREE_MODIFIED:
                 for (final DataObjectModification<? extends DataObject> child :
-                             objectModification.getModifiedChildren()) {
+                        objectModification.getModifiedChildren()) {
                     formatModification(messageBuilder, child);
                 }
                 break;
@@ -59,7 +63,9 @@ public class NeutronLogger implements AutoCloseable {
                 break;
             case DELETE:
                 messageBuilder.append("\n");
-                messageBuilder.append("DELETE: ").append(typeName);
+                messageBuilder.append("DELETE: type: ").append(typeName).append("\n");
+                final T dataBefore = objectModification.getDataBefore();
+                messageBuilder.append(dataBefore.toString());
                 break;
             default:
                 LOG.warn("unknown modification type: {}", typeName);
@@ -67,9 +73,8 @@ public class NeutronLogger implements AutoCloseable {
         }
     }
 
-    private <T extends DataObject>
-        void formatChanges(@Nonnull final StringBuilder messageBuilder,
-                           @Nonnull final Collection<DataTreeModification<T>> changes) {
+    private <T extends DataObject> void formatChanges(@NonNull final StringBuilder messageBuilder,
+            @NonNull final Collection<DataTreeModification<T>> changes) {
         for (DataTreeModification<T> modification : changes) {
             final DataTreeIdentifier<T> identifier = modification.getRootPath();
             final LogicalDatastoreType datastoreType = identifier.getDatastoreType();
@@ -84,49 +89,33 @@ public class NeutronLogger implements AutoCloseable {
         }
     }
 
-    private void logChanges(String prefix, @Nonnull Collection<DataTreeModification<Neutron>> changes) {
-        final StringBuilder messageBuilder = new StringBuilder();
-        messageBuilder.append(prefix);
-        formatChanges(messageBuilder, changes);
-        LOG.info(messageBuilder.toString());
-    }
-
-    private void configurationDataTreeChanged(@Nonnull Collection<DataTreeModification<Neutron>> changes) {
-        logChanges("Configuration DataTreeChanged ", changes);
-    }
-
-    private void operationalDataTreeChanged(@Nonnull Collection<DataTreeModification<Neutron>> changes) {
-        logChanges("Operational DataTreeChanged ", changes);
+    private void logChanges(String prefix, @NonNull Collection<DataTreeModification<Neutron>> changes) {
+        if (LOG.isInfoEnabled()) {
+            final StringBuilder messageBuilder = new StringBuilder(prefix);
+            formatChanges(messageBuilder, changes);
+            LOG.info("Changes: {}", messageBuilder.toString());
+        }
     }
 
+    @PostConstruct
     public void init() {
         LOG.info("Register listener for Neutron model data changes");
-        InstanceIdentifier<Neutron> instanceId = Preconditions.checkNotNull(InstanceIdentifier.create(Neutron.class));
+        InstanceIdentifier<Neutron> instanceId = InstanceIdentifier.create(Neutron.class);
 
-        DataTreeIdentifier<Neutron> configurationDataTreeId =
-            new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceId);
-        configurationDataTreeChangeListener = new ClusteredDataTreeChangeListener<Neutron>() {
-            @Override
-            public void onDataTreeChanged(Collection<DataTreeModification<Neutron>> changes) {
-                configurationDataTreeChanged(changes);
-            }
-        };
+        DataTreeIdentifier<Neutron> configurationDataTreeId = DataTreeIdentifier.create(
+                LogicalDatastoreType.CONFIGURATION, instanceId);
+        configurationDataTreeChangeListener = changes -> logChanges("Configuration DataTreeChanged ", changes);
         configurationRegisteredListener = db.registerDataTreeChangeListener(configurationDataTreeId,
-                                                                            configurationDataTreeChangeListener);
+                configurationDataTreeChangeListener);
 
-        DataTreeIdentifier<Neutron> operationalDataTreeId =
-            new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, instanceId);
-        operationalDataTreeChangeListener = new ClusteredDataTreeChangeListener<Neutron>() {
-            @Override
-            public void onDataTreeChanged(Collection<DataTreeModification<Neutron>> changes) {
-                operationalDataTreeChanged(changes);
-            }
-        };
+        DataTreeIdentifier<Neutron> operationalDataTreeId = DataTreeIdentifier.create(
+                LogicalDatastoreType.OPERATIONAL, instanceId);
+        operationalDataTreeChangeListener = changes -> logChanges("Operational DataTreeChanged ", changes);
         operationalRegisteredListener = db.registerDataTreeChangeListener(operationalDataTreeId,
-                                                                          operationalDataTreeChangeListener);
+                operationalDataTreeChangeListener);
     }
 
-    @Override
+    @PreDestroy
     public void close() throws Exception {
         configurationRegisteredListener.close();
         configurationRegisteredListener = null;