All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.opendaylight.neutron.logger.NeutronLogger Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2016 Intel Corporation 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.neutron.logger;

import static java.util.Objects.requireNonNull;

import java.util.Collection;
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;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Singleton
public final class NeutronLogger {
    private static final Logger LOG = LoggerFactory.getLogger(NeutronLogger.class);

    private final DataBroker db;
    private ClusteredDataTreeChangeListener configurationDataTreeChangeListener;
    private ListenerRegistration> configurationRegisteredListener;
    private ClusteredDataTreeChangeListener operationalDataTreeChangeListener;
    private ListenerRegistration> operationalRegisteredListener;

    @Inject
    public NeutronLogger(@NonNull DataBroker db) {
        LOG.info("Creating NeutronLogger {}", db);
        this.db = requireNonNull(db, "null db");
    }

    private  void formatModification(@NonNull final StringBuilder messageBuilder,
            @NonNull final DataObjectModification objectModification) {
        final String typeName = objectModification.getDataType().getSimpleName();

        switch (objectModification.getModificationType()) {
            case SUBTREE_MODIFIED:
                for (final DataObjectModification child :
                        objectModification.getModifiedChildren()) {
                    formatModification(messageBuilder, child);
                }
                break;
            case WRITE:
                messageBuilder.append("\n");
                messageBuilder.append("WRITE: type: ").append(typeName).append("\n");
                final T dataAfter = objectModification.getDataAfter();
                messageBuilder.append(dataAfter.toString());
                break;
            case DELETE:
                messageBuilder.append("\n");
                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);
                break;
        }
    }

    private  void formatChanges(@NonNull final StringBuilder messageBuilder,
            @NonNull final Collection> changes) {
        for (DataTreeModification modification : changes) {
            final DataTreeIdentifier identifier = modification.getRootPath();
            final LogicalDatastoreType datastoreType = identifier.getDatastoreType();
            if (datastoreType == LogicalDatastoreType.OPERATIONAL) {
                messageBuilder.append("OPERATIONAL: ");
            } else {
                messageBuilder.append("CONFIGURATION: ");
            }

            final DataObjectModification objectModification = modification.getRootNode();
            formatModification(messageBuilder, objectModification);
        }
    }

    private void logChanges(String prefix, @NonNull Collection> 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 instanceId = InstanceIdentifier.create(Neutron.class);

        DataTreeIdentifier configurationDataTreeId = DataTreeIdentifier.create(
                LogicalDatastoreType.CONFIGURATION, instanceId);
        configurationDataTreeChangeListener = changes -> logChanges("Configuration DataTreeChanged ", changes);
        configurationRegisteredListener = db.registerDataTreeChangeListener(configurationDataTreeId,
                configurationDataTreeChangeListener);

        DataTreeIdentifier operationalDataTreeId = DataTreeIdentifier.create(
                LogicalDatastoreType.OPERATIONAL, instanceId);
        operationalDataTreeChangeListener = changes -> logChanges("Operational DataTreeChanged ", changes);
        operationalRegisteredListener = db.registerDataTreeChangeListener(operationalDataTreeId,
                operationalDataTreeChangeListener);
    }

    @PreDestroy
    public void close() throws Exception {
        configurationRegisteredListener.close();
        configurationRegisteredListener = null;
        operationalRegisteredListener.close();
        operationalRegisteredListener = null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy