2 * Copyright (c) 2016 Intel Corporation and others. All rights reserved.
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
9 package org.opendaylight.neutron.logger;
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;
32 public final class NeutronLogger {
33 private static final Logger LOG = LoggerFactory.getLogger(NeutronLogger.class);
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;
42 public NeutronLogger(@Nonnull DataBroker db) {
43 LOG.info("Creating NeutronLogger {}", db);
44 this.db = Preconditions.checkNotNull(db, "null db");
47 private <T extends DataObject> void formatModification(@Nonnull final StringBuilder messageBuilder,
48 @Nonnull final DataObjectModification<T> objectModification) {
49 final String typeName = objectModification.getDataType().getSimpleName();
51 switch (objectModification.getModificationType()) {
52 case SUBTREE_MODIFIED:
53 for (final DataObjectModification<? extends DataObject> child :
54 objectModification.getModifiedChildren()) {
55 formatModification(messageBuilder, child);
59 messageBuilder.append("\n");
60 messageBuilder.append("WRITE: type: ").append(typeName).append("\n");
61 final T dataAfter = objectModification.getDataAfter();
62 messageBuilder.append(dataAfter.toString());
65 messageBuilder.append("\n");
66 messageBuilder.append("DELETE: type: ").append(typeName).append("\n");
67 final T dataBefore = objectModification.getDataBefore();
68 messageBuilder.append(dataBefore.toString());
71 LOG.warn("unknown modification type: {}", typeName);
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: ");
84 messageBuilder.append("CONFIGURATION: ");
87 final DataObjectModification<T> objectModification = modification.getRootNode();
88 formatModification(messageBuilder, objectModification);
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());
102 LOG.info("Register listener for Neutron model data changes");
103 InstanceIdentifier<Neutron> instanceId = Preconditions.checkNotNull(InstanceIdentifier.create(Neutron.class));
105 DataTreeIdentifier<Neutron> configurationDataTreeId = new DataTreeIdentifier<>(
106 LogicalDatastoreType.CONFIGURATION, instanceId);
107 configurationDataTreeChangeListener = changes -> logChanges("Configuration DataTreeChanged ", changes);
108 configurationRegisteredListener = db.registerDataTreeChangeListener(configurationDataTreeId,
109 configurationDataTreeChangeListener);
112 Neutron> operationalDataTreeId = new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, instanceId);
113 operationalDataTreeChangeListener = changes -> logChanges("Operational DataTreeChanged ", changes);
114 operationalRegisteredListener = db.registerDataTreeChangeListener(operationalDataTreeId,
115 operationalDataTreeChangeListener);
119 public void close() throws Exception {
120 configurationRegisteredListener.close();
121 configurationRegisteredListener = null;
122 operationalRegisteredListener.close();
123 operationalRegisteredListener = null;