use LOG.isInfoEnabled() in (expensive!) NeutronLogger
[neutron.git] / neutron-logger / src / main / java / org / opendaylight / neutron / logger / NeutronLogger.java
1 /*
2  * Copyright (c) 2016 Intel Corporation and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.neutron.logger;
10
11 import com.google.common.base.Preconditions;
12 import java.util.Collection;
13 import javax.annotation.Nonnull;
14 import javax.annotation.PostConstruct;
15 import javax.annotation.PreDestroy;
16 import javax.inject.Inject;
17 import javax.inject.Singleton;
18 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
23 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 @Singleton
32 public final class NeutronLogger {
33     private static final Logger LOG = LoggerFactory.getLogger(NeutronLogger.class);
34
35     private final DataBroker db;
36     private ClusteredDataTreeChangeListener<Neutron> configurationDataTreeChangeListener;
37     private ListenerRegistration<? extends ClusteredDataTreeChangeListener<Neutron>> configurationRegisteredListener;
38     private ClusteredDataTreeChangeListener<Neutron> operationalDataTreeChangeListener;
39     private ListenerRegistration<? extends ClusteredDataTreeChangeListener<Neutron>> operationalRegisteredListener;
40
41     @Inject
42     public NeutronLogger(@Nonnull DataBroker db) {
43         LOG.info("Creating NeutronLogger {}", db);
44         this.db = Preconditions.checkNotNull(db, "null db");
45     }
46
47     private <T extends DataObject> void formatModification(@Nonnull final StringBuilder messageBuilder,
48             @Nonnull final DataObjectModification<T> objectModification) {
49         final String typeName = objectModification.getDataType().getSimpleName();
50
51         switch (objectModification.getModificationType()) {
52             case SUBTREE_MODIFIED:
53                 for (final DataObjectModification<? extends DataObject> child :
54                         objectModification.getModifiedChildren()) {
55                     formatModification(messageBuilder, child);
56                 }
57                 break;
58             case WRITE:
59                 messageBuilder.append("\n");
60                 messageBuilder.append("WRITE: type: ").append(typeName).append("\n");
61                 final T dataAfter = objectModification.getDataAfter();
62                 messageBuilder.append(dataAfter.toString());
63                 break;
64             case DELETE:
65                 messageBuilder.append("\n");
66                 messageBuilder.append("DELETE: type: ").append(typeName).append("\n");
67                 final T dataBefore = objectModification.getDataBefore();
68                 messageBuilder.append(dataBefore.toString());
69                 break;
70             default:
71                 LOG.warn("unknown modification type: {}", typeName);
72                 break;
73         }
74     }
75
76     private <T extends DataObject> void formatChanges(@Nonnull final StringBuilder messageBuilder,
77             @Nonnull final Collection<DataTreeModification<T>> changes) {
78         for (DataTreeModification<T> modification : changes) {
79             final DataTreeIdentifier<T> identifier = modification.getRootPath();
80             final LogicalDatastoreType datastoreType = identifier.getDatastoreType();
81             if (datastoreType == LogicalDatastoreType.OPERATIONAL) {
82                 messageBuilder.append("OPERATIONAL: ");
83             } else {
84                 messageBuilder.append("CONFIGURATION: ");
85             }
86
87             final DataObjectModification<T> objectModification = modification.getRootNode();
88             formatModification(messageBuilder, objectModification);
89         }
90     }
91
92     private void logChanges(String prefix, @Nonnull Collection<DataTreeModification<Neutron>> changes) {
93         if (LOG.isInfoEnabled()) {
94             final StringBuilder messageBuilder = new StringBuilder(prefix);
95             formatChanges(messageBuilder, changes);
96             LOG.info("{}", messageBuilder.toString());
97         }
98     }
99
100     @PostConstruct
101     public void init() {
102         LOG.info("Register listener for Neutron model data changes");
103         InstanceIdentifier<Neutron> instanceId = Preconditions.checkNotNull(InstanceIdentifier.create(Neutron.class));
104
105         DataTreeIdentifier<Neutron> configurationDataTreeId = new DataTreeIdentifier<>(
106                 LogicalDatastoreType.CONFIGURATION, instanceId);
107         configurationDataTreeChangeListener = changes -> logChanges("Configuration DataTreeChanged ", changes);
108         configurationRegisteredListener = db.registerDataTreeChangeListener(configurationDataTreeId,
109                 configurationDataTreeChangeListener);
110
111         DataTreeIdentifier<
112                 Neutron> operationalDataTreeId = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, instanceId);
113         operationalDataTreeChangeListener = changes -> logChanges("Operational DataTreeChanged ", changes);
114         operationalRegisteredListener = db.registerDataTreeChangeListener(operationalDataTreeId,
115                 operationalDataTreeChangeListener);
116     }
117
118     @PreDestroy
119     public void close() throws Exception {
120         configurationRegisteredListener.close();
121         configurationRegisteredListener = null;
122         operationalRegisteredListener.close();
123         operationalRegisteredListener = null;
124     }
125 }