From b3f699ff3a7684d444bd1a76a940187cfa982bf1 Mon Sep 17 00:00:00 2001 From: Isaku Yamahata Date: Sat, 27 Feb 2016 01:28:04 -0800 Subject: [PATCH] Logger on neutron model This patch implements logger on neutron model change for debug. Change-Id: Ifd079de8cded725d79ec2fe638415ab7be640318 Signed-off-by: Isaku Yamahata --- features/production/pom.xml | 5 + .../production/src/main/features/features.xml | 6 + features/test/pom.xml | 2 +- features/test/src/main/features/features.xml | 4 +- integration/test/pom.xml | 10 +- .../test/src/main/features/features.xml | 4 +- karaf/pom.xml | 2 +- neutron-logger/pom.xml | 101 +++++++++++++ .../neutron/logger/NeutronLogger.java | 136 ++++++++++++++++++ .../opendaylight/blueprint/neutron-logger.xml | 17 +++ pom.xml | 1 + 11 files changed, 277 insertions(+), 11 deletions(-) create mode 100644 neutron-logger/pom.xml create mode 100644 neutron-logger/src/main/java/org/opendaylight/neutron/logger/NeutronLogger.java create mode 100644 neutron-logger/src/main/resources/org/opendaylight/blueprint/neutron-logger.xml diff --git a/features/production/pom.xml b/features/production/pom.xml index fa07c2d9e..7f616f54d 100644 --- a/features/production/pom.xml +++ b/features/production/pom.xml @@ -127,6 +127,11 @@ transcriber ${project.version} + + ${project.groupId} + neutron-logger + ${project.version} + scm:git:ssh://git.opendaylight.org:29418/neutron.git diff --git a/features/production/src/main/features/features.xml b/features/production/src/main/features/features.xml index 102b5b7be..a4e9a7753 100644 --- a/features/production/src/main/features/features.xml +++ b/features/production/src/main/features/features.xml @@ -43,4 +43,10 @@ mvn:org.opendaylight.neutron/model/{{VERSION}} mvn:org.osgi/org.osgi.core/{{VERSION}} + + odl-mdsal-broker + mvn:org.opendaylight.neutron/model/{{VERSION}} + mvn:org.opendaylight.neutron/neutron-logger/{{VERSION}} + mvn:org.osgi/org.osgi.core/{{VERSION}} + diff --git a/features/test/pom.xml b/features/test/pom.xml index 32f036a2b..a1814a13f 100644 --- a/features/test/pom.xml +++ b/features/test/pom.xml @@ -22,7 +22,7 @@ ${project.groupId} - dummyprovider + neutron-logger ${project.version} diff --git a/features/test/src/main/features/features.xml b/features/test/src/main/features/features.xml index 83ca4d720..e1b2a44c6 100644 --- a/features/test/src/main/features/features.xml +++ b/features/test/src/main/features/features.xml @@ -4,8 +4,8 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.2.0 http://karaf.apache.org/xmlns/features/v1.2.0"> mvn:org.opendaylight.neutron/features-neutron/{{VERSION}}/xml/features - + odl-neutron-service - mvn:org.opendaylight.neutron/dummyprovider/{{VERSION}} + mvn:org.opendaylight.neutron/neutron-logger/{{VERSION}} diff --git a/integration/test/pom.xml b/integration/test/pom.xml index fc67fafc9..b835d2a31 100644 --- a/integration/test/pom.xml +++ b/integration/test/pom.xml @@ -27,11 +27,6 @@ features xml - - ${project.groupId} - dummyprovider - ${project.version} - org.opendaylight.neutron neutron-karaf @@ -71,6 +66,11 @@ + + ${project.groupId} + neutron-logger + ${project.version} + ${project.groupId} neutron-spi diff --git a/integration/test/src/main/features/features.xml b/integration/test/src/main/features/features.xml index 83ca4d720..e1b2a44c6 100644 --- a/integration/test/src/main/features/features.xml +++ b/integration/test/src/main/features/features.xml @@ -4,8 +4,8 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://karaf.apache.org/xmlns/features/v1.2.0 http://karaf.apache.org/xmlns/features/v1.2.0"> mvn:org.opendaylight.neutron/features-neutron/{{VERSION}}/xml/features - + odl-neutron-service - mvn:org.opendaylight.neutron/dummyprovider/{{VERSION}} + mvn:org.opendaylight.neutron/neutron-logger/{{VERSION}} diff --git a/karaf/pom.xml b/karaf/pom.xml index 16dbc77bf..952cc639d 100644 --- a/karaf/pom.xml +++ b/karaf/pom.xml @@ -21,7 +21,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html INTERNAL 3.1.1 - odl-neutron-dummyprovider-test + odl-neutron-neutron-logger-test ${project.parent.basedir}/src/main/resources diff --git a/neutron-logger/pom.xml b/neutron-logger/pom.xml new file mode 100644 index 000000000..33645c30a --- /dev/null +++ b/neutron-logger/pom.xml @@ -0,0 +1,101 @@ + + + 4.0.0 + + org.opendaylight.controller + config-parent + 0.5.0-SNAPSHOT + + + + org.opendaylight.neutron + neutron-logger + 0.7.0-SNAPSHOT + bundle + + ${project.parent.basedir}/src/main/resources + + + + + org.opendaylight.mdsal.model + mdsal-model-artifacts + 0.9.0-SNAPSHOT + pom + import + + + + + + org.opendaylight.neutron + neutron-spi + ${project.version} + + + org.opendaylight.neutron + model + ${project.version} + + + org.osgi + org.osgi.core + + + org.slf4j + slf4j-api + + + org.opendaylight.controller + config-api + 0.5.0-SNAPSHOT + + + + + + org.apache.felix + maven-bundle-plugin + true + + + * + + ${project.basedir}/src/main/resources/META-INF + + + + + + scm:git:ssh://git.opendaylight.org:29418/neutron.git + scm:git:ssh://git.opendaylight.org:29418/neutron.git + HEAD + https://wiki.opendaylight.org/view/NeutronNorthBound:Main + + + + + opendaylight-release + ${nexusproxy}/repositories/${nexus.repository.release}/ + + + + opendaylight-snapshot + ${nexusproxy}/repositories/${nexus.repository.snapshot}/ + + + + opendaylight-site + ${nexus.site.url}/${project.artifactId}/ + + + + + ${odl.site.url}/${project.groupId}/${stream}/${project.artifactId}/ + diff --git a/neutron-logger/src/main/java/org/opendaylight/neutron/logger/NeutronLogger.java b/neutron-logger/src/main/java/org/opendaylight/neutron/logger/NeutronLogger.java new file mode 100644 index 000000000..95de39f4e --- /dev/null +++ b/neutron-logger/src/main/java/org/opendaylight/neutron/logger/NeutronLogger.java @@ -0,0 +1,136 @@ +/* + * 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 com.google.common.base.Preconditions; + +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 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; + +public class NeutronLogger implements AutoCloseable { + private static final Logger LOG = LoggerFactory.getLogger(NeutronLogger.class); + + private DataBroker db; + private ClusteredDataTreeChangeListener configurationDataTreeChangeListener; + private ListenerRegistration configurationRegisteredListener; + private ClusteredDataTreeChangeListener operationalDataTreeChangeListener; + private ListenerRegistration operationalRegisteredListener; + + public NeutronLogger(@Nonnull DataBroker db) { + LOG.info("Creating NeutronLogger {}", db); + this.db = Preconditions.checkNotNull(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: ").append(typeName); + 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) { + final StringBuilder messageBuilder = new StringBuilder(); + messageBuilder.append(prefix); + formatChanges(messageBuilder, changes); + LOG.info(messageBuilder.toString()); + } + + private void configurationDataTreeChanged(@Nonnull Collection> changes) { + logChanges("Configuration DataTreeChanged ", changes); + } + + private void operationalDataTreeChanged(@Nonnull Collection> changes) { + logChanges("Operational DataTreeChanged ", changes); + } + + public void init() { + LOG.info("Register listener for Neutron model data changes"); + InstanceIdentifier instanceId = Preconditions.checkNotNull(InstanceIdentifier.create(Neutron.class)); + + DataTreeIdentifier configurationDataTreeId = + new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceId); + configurationDataTreeChangeListener = new ClusteredDataTreeChangeListener() { + @Override + public void onDataTreeChanged(Collection> changes) { + configurationDataTreeChanged(changes); + } + }; + configurationRegisteredListener = db.registerDataTreeChangeListener(configurationDataTreeId, + configurationDataTreeChangeListener); + + DataTreeIdentifier operationalDataTreeId = + new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, instanceId); + operationalDataTreeChangeListener = new ClusteredDataTreeChangeListener() { + @Override + public void onDataTreeChanged(Collection> changes) { + operationalDataTreeChanged(changes); + } + }; + operationalRegisteredListener = db.registerDataTreeChangeListener(operationalDataTreeId, + operationalDataTreeChangeListener); + } + + @Override + public void close() throws Exception { + configurationRegisteredListener.close(); + configurationRegisteredListener = null; + operationalRegisteredListener.close(); + operationalRegisteredListener = null; + } +} diff --git a/neutron-logger/src/main/resources/org/opendaylight/blueprint/neutron-logger.xml b/neutron-logger/src/main/resources/org/opendaylight/blueprint/neutron-logger.xml new file mode 100644 index 000000000..a2dc1bad4 --- /dev/null +++ b/neutron-logger/src/main/resources/org/opendaylight/blueprint/neutron-logger.xml @@ -0,0 +1,17 @@ + + + + + + + + + + diff --git a/pom.xml b/pom.xml index b44e593c7..843ef9a5c 100644 --- a/pom.xml +++ b/pom.xml @@ -94,6 +94,7 @@ northbound-api transcriber dummyprovider + neutron-logger features/production features/test karaf -- 2.36.6