Logger on neutron model 92/40392/6
authorIsaku Yamahata <isaku.yamahata@intel.com>
Sat, 27 Feb 2016 09:28:04 +0000 (01:28 -0800)
committerIsaku Yamahata <isaku.yamahata@intel.com>
Fri, 1 Jul 2016 03:45:14 +0000 (20:45 -0700)
This patch implements logger on neutron model change for debug.

Change-Id: Ifd079de8cded725d79ec2fe638415ab7be640318
Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com>
features/production/pom.xml
features/production/src/main/features/features.xml
features/test/pom.xml
features/test/src/main/features/features.xml
integration/test/pom.xml
integration/test/src/main/features/features.xml
karaf/pom.xml
neutron-logger/pom.xml [new file with mode: 0644]
neutron-logger/src/main/java/org/opendaylight/neutron/logger/NeutronLogger.java [new file with mode: 0644]
neutron-logger/src/main/resources/org/opendaylight/blueprint/neutron-logger.xml [new file with mode: 0644]
pom.xml

index fa07c2d9e5cc27ea920400b79f6f65b65a2f7bf0..7f616f54d48ac98baf89474fc714946acd603857 100644 (file)
       <artifactId>transcriber</artifactId>
       <version>${project.version}</version>
     </dependency>
+    <dependency>
+      <groupId>${project.groupId}</groupId>
+      <artifactId>neutron-logger</artifactId>
+      <version>${project.version}</version>
+    </dependency>
   </dependencies>
   <scm>
     <connection>scm:git:ssh://git.opendaylight.org:29418/neutron.git</connection>
index 102b5b7bec113e17647250d372e93558b5a607b1..a4e9a7753d46f73050b9b24c7733ef1de1a5176e 100644 (file)
     <bundle>mvn:org.opendaylight.neutron/model/{{VERSION}}</bundle>
     <bundle>mvn:org.osgi/org.osgi.core/{{VERSION}}</bundle>
   </feature>
+  <feature name='odl-neutron-logger' version='${project.version}' description="OpenDaylight :: Neutron :: Logger">
+    <feature version='${controller.mdsal.version}'>odl-mdsal-broker</feature>
+    <bundle>mvn:org.opendaylight.neutron/model/{{VERSION}}</bundle>
+    <bundle>mvn:org.opendaylight.neutron/neutron-logger/{{VERSION}}</bundle>
+    <bundle>mvn:org.osgi/org.osgi.core/{{VERSION}}</bundle>
+  </feature>
 </features>
index 32f036a2b2efbabaec0c8e962848b4e8294382e7..a1814a13fc424cc3837a28a8a36860a926ce1b5d 100644 (file)
@@ -22,7 +22,7 @@
     </dependency>
     <dependency>
       <groupId>${project.groupId}</groupId>
-      <artifactId>dummyprovider</artifactId>
+      <artifactId>neutron-logger</artifactId>
       <version>${project.version}</version>
     </dependency>
   </dependencies>
index 83ca4d72009fd65aff414d9f290c3bc97e633589..e1b2a44c6ef241083308dcb7740d7594b17a0b4a 100644 (file)
@@ -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">
    <repository>mvn:org.opendaylight.neutron/features-neutron/{{VERSION}}/xml/features</repository>
-   <feature name='odl-neutron-dummyprovider-test' version='${project.version}' description="OpenDaylight :: Neutron :: Dummy Provider Test">
+   <feature name='odl-neutron-neutron-logger-test' version='${project.version}' description="OpenDaylight :: Neutron :: Neutron Logger Test">
     <feature version='${project.version}'>odl-neutron-service</feature>
-    <bundle>mvn:org.opendaylight.neutron/dummyprovider/{{VERSION}}</bundle>
+    <bundle>mvn:org.opendaylight.neutron/neutron-logger/{{VERSION}}</bundle>
   </feature>
 </features>
index fc67fafc9db891af299c110a8b6b7700c31b79bf..b835d2a31b8f860b69c7e9ab0c2f6cc61f41c2fd 100644 (file)
       <classifier>features</classifier>
       <type>xml</type>
     </dependency>
-    <dependency>
-      <groupId>${project.groupId}</groupId>
-      <artifactId>dummyprovider</artifactId>
-      <version>${project.version}</version>
-    </dependency>
     <dependency>
       <groupId>org.opendaylight.neutron</groupId>
       <artifactId>neutron-karaf</artifactId>
             </goals>
             <configuration>
               <artifactItems>
+                <artifactItem>
+                  <groupId>${project.groupId}</groupId>
+                  <artifactId>neutron-logger</artifactId>
+                  <version>${project.version}</version>
+                </artifactItem>
                 <artifactItem>
                   <groupId>${project.groupId}</groupId>
                   <artifactId>neutron-spi</artifactId>
index 83ca4d72009fd65aff414d9f290c3bc97e633589..e1b2a44c6ef241083308dcb7740d7594b17a0b4a 100644 (file)
@@ -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">
    <repository>mvn:org.opendaylight.neutron/features-neutron/{{VERSION}}/xml/features</repository>
-   <feature name='odl-neutron-dummyprovider-test' version='${project.version}' description="OpenDaylight :: Neutron :: Dummy Provider Test">
+   <feature name='odl-neutron-neutron-logger-test' version='${project.version}' description="OpenDaylight :: Neutron :: Neutron Logger Test">
     <feature version='${project.version}'>odl-neutron-service</feature>
-    <bundle>mvn:org.opendaylight.neutron/dummyprovider/{{VERSION}}</bundle>
+    <bundle>mvn:org.opendaylight.neutron/neutron-logger/{{VERSION}}</bundle>
   </feature>
 </features>
index 16dbc77bf855b2358dc12a1929a40f97e98b7d6c..952cc639d0262f6aed9cfdc973ce85f467749426 100644 (file)
@@ -21,7 +21,7 @@ and is available at http://www.eclipse.org/legal/epl-v10.html INTERNAL
     <maven>3.1.1</maven>
   </prerequisites>
   <properties>
-    <karaf.localFeature>odl-neutron-dummyprovider-test</karaf.localFeature>
+    <karaf.localFeature>odl-neutron-neutron-logger-test</karaf.localFeature>
     <checkstyle.location>${project.parent.basedir}/src/main/resources</checkstyle.location>
   </properties>
   <dependencies>
diff --git a/neutron-logger/pom.xml b/neutron-logger/pom.xml
new file mode 100644 (file)
index 0000000..33645c3
--- /dev/null
@@ -0,0 +1,101 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.opendaylight.controller</groupId>
+    <artifactId>config-parent</artifactId>
+    <version>0.5.0-SNAPSHOT</version>
+    <relativePath/>
+  </parent>
+
+  <groupId>org.opendaylight.neutron</groupId>
+  <artifactId>neutron-logger</artifactId>
+  <version>0.7.0-SNAPSHOT</version>
+  <packaging>bundle</packaging>
+  <properties>
+    <checkstyle.location>${project.parent.basedir}/src/main/resources</checkstyle.location>
+  </properties>
+  <dependencyManagement>
+    <dependencies>
+      <dependency>
+        <groupId>org.opendaylight.mdsal.model</groupId>
+        <artifactId>mdsal-model-artifacts</artifactId>
+        <version>0.9.0-SNAPSHOT</version>
+        <type>pom</type>
+        <scope>import</scope>
+      </dependency>
+    </dependencies>
+  </dependencyManagement>
+  <dependencies>
+    <dependency>
+      <groupId>org.opendaylight.neutron</groupId>
+      <artifactId>neutron-spi</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.neutron</groupId>
+      <artifactId>model</artifactId>
+      <version>${project.version}</version>
+    </dependency>
+    <dependency>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-api</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.opendaylight.controller</groupId>
+      <artifactId>config-api</artifactId>
+      <version>0.5.0-SNAPSHOT</version>
+    </dependency>
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix</groupId>
+        <artifactId>maven-bundle-plugin</artifactId>
+        <extensions>true</extensions>
+        <configuration>
+          <instructions>
+            <Import-Package>*</Import-Package>
+          </instructions>
+          <manifestLocation>${project.basedir}/src/main/resources/META-INF</manifestLocation>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+  <scm>
+    <connection>scm:git:ssh://git.opendaylight.org:29418/neutron.git</connection>
+    <developerConnection>scm:git:ssh://git.opendaylight.org:29418/neutron.git</developerConnection>
+    <tag>HEAD</tag>
+    <url>https://wiki.opendaylight.org/view/NeutronNorthBound:Main</url>
+  </scm>
+  <distributionManagement>
+    <!-- OpenDayLight Released artifact -->
+    <repository>
+      <id>opendaylight-release</id>
+      <url>${nexusproxy}/repositories/${nexus.repository.release}/</url>
+    </repository>
+    <!-- OpenDayLight Snapshot artifact -->
+    <snapshotRepository>
+      <id>opendaylight-snapshot</id>
+      <url>${nexusproxy}/repositories/${nexus.repository.snapshot}/</url>
+    </snapshotRepository>
+    <!-- Site deployment -->
+    <site>
+      <id>opendaylight-site</id>
+      <url>${nexus.site.url}/${project.artifactId}/</url>
+    </site>
+  </distributionManagement>
+
+  <!--
+      Maven Site Configuration
+
+      The following configuration is necessary for maven-site-plugin to
+      correctly identify the correct deployment path for OpenDaylight Maven
+      sites.
+  -->
+  <url>${odl.site.url}/${project.groupId}/${stream}/${project.artifactId}/</url>
+</project>
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 (file)
index 0000000..95de39f
--- /dev/null
@@ -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<? extends ClusteredDataTreeChangeListener> configurationRegisteredListener;
+    private ClusteredDataTreeChangeListener operationalDataTreeChangeListener;
+    private ListenerRegistration<? extends ClusteredDataTreeChangeListener> operationalRegisteredListener;
+
+    public NeutronLogger(@Nonnull DataBroker db) {
+        LOG.info("Creating NeutronLogger {}", db);
+        this.db = Preconditions.checkNotNull(db, "null db");
+    }
+
+    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()) {
+                    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 <T extends DataObject>
+        void formatChanges(@Nonnull final StringBuilder messageBuilder,
+                           @Nonnull final Collection<DataTreeModification<T>> changes) {
+        for (DataTreeModification modification : changes) {
+            final DataTreeIdentifier<T> identifier = modification.getRootPath();
+            final LogicalDatastoreType datastoreType = identifier.getDatastoreType();
+            if (datastoreType == LogicalDatastoreType.OPERATIONAL) {
+                messageBuilder.append("OPERATIONAL: ");
+            } else {
+                messageBuilder.append("CONFIGURATION: ");
+            }
+
+            final DataObjectModification<T> objectModification = modification.getRootNode();
+            formatModification(messageBuilder, objectModification);
+        }
+    }
+
+    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);
+    }
+
+    public void init() {
+        LOG.info("Register listener for Neutron model data changes");
+        InstanceIdentifier<Neutron> instanceId = Preconditions.checkNotNull(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);
+            }
+        };
+        configurationRegisteredListener = db.registerDataTreeChangeListener(configurationDataTreeId,
+                                                                            configurationDataTreeChangeListener);
+
+        DataTreeIdentifier<Neutron> operationalDataTreeId =
+            new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, instanceId);
+        operationalDataTreeChangeListener = new ClusteredDataTreeChangeListener<Neutron>() {
+            @Override
+            public void onDataTreeChanged(Collection<DataTreeModification<Neutron>> 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 (file)
index 0000000..a2dc1ba
--- /dev/null
@@ -0,0 +1,17 @@
+<?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">
+
+  <reference id="dataBroker"
+      interface="org.opendaylight.controller.md.sal.binding.api.DataBroker"
+      odl:type="default" />
+
+  <bean id="neutronLogger"
+     class="org.opendaylight.neutron.logger.NeutronLogger"
+     init-method="init"
+     destroy-method="close">
+    <argument ref="dataBroker"/>
+  </bean>
+
+</blueprint>
diff --git a/pom.xml b/pom.xml
index b44e593c753b45a23ee49cbb1d46dd629647e22d..843ef9a5c85b5a0eb2d3a9e33a02f0e15505c835 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -94,6 +94,7 @@
     <module>northbound-api</module>
     <module>transcriber</module>
     <module>dummyprovider</module>
+    <module>neutron-logger</module>
     <module>features/production</module>
     <module>features/test</module>
     <module>karaf</module>