b9d15db7d1613808abb5bcb11db355c8dcb4aa89
[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
13 import java.util.Collection;
14 import javax.annotation.Nonnull;
15 import org.opendaylight.controller.md.sal.binding.api.ClusteredDataTreeChangeListener;
16 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
17 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
18 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
19 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
20 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.neutron.rev150712.Neutron;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 public class NeutronLogger implements AutoCloseable {
29     private static final Logger LOG = LoggerFactory.getLogger(NeutronLogger.class);
30
31     private DataBroker db;
32     private ClusteredDataTreeChangeListener<Neutron> configurationDataTreeChangeListener;
33     private ListenerRegistration<? extends ClusteredDataTreeChangeListener<Neutron>> configurationRegisteredListener;
34     private ClusteredDataTreeChangeListener<Neutron> operationalDataTreeChangeListener;
35     private ListenerRegistration<? extends ClusteredDataTreeChangeListener<Neutron>> operationalRegisteredListener;
36
37     public NeutronLogger(@Nonnull DataBroker db) {
38         LOG.info("Creating NeutronLogger {}", db);
39         this.db = Preconditions.checkNotNull(db, "null db");
40     }
41
42     private <T extends DataObject>
43         void formatModification(@Nonnull final StringBuilder messageBuilder,
44                                 @Nonnull final DataObjectModification<T> objectModification) {
45         final String typeName = objectModification.getDataType().getSimpleName();
46
47         switch (objectModification.getModificationType()) {
48             case SUBTREE_MODIFIED:
49                 for (final DataObjectModification<? extends DataObject> child :
50                              objectModification.getModifiedChildren()) {
51                     formatModification(messageBuilder, child);
52                 }
53                 break;
54             case WRITE:
55                 messageBuilder.append("\n");
56                 messageBuilder.append("WRITE: type: ").append(typeName).append("\n");
57                 final T dataAfter = objectModification.getDataAfter();
58                 messageBuilder.append(dataAfter.toString());
59                 break;
60             case DELETE:
61                 messageBuilder.append("\n");
62                 messageBuilder.append("DELETE: ").append(typeName);
63                 break;
64             default:
65                 LOG.warn("unknown modification type: {}", typeName);
66                 break;
67         }
68     }
69
70     private <T extends DataObject>
71         void formatChanges(@Nonnull final StringBuilder messageBuilder,
72                            @Nonnull final Collection<DataTreeModification<T>> changes) {
73         for (DataTreeModification<T> modification : changes) {
74             final DataTreeIdentifier<T> identifier = modification.getRootPath();
75             final LogicalDatastoreType datastoreType = identifier.getDatastoreType();
76             if (datastoreType == LogicalDatastoreType.OPERATIONAL) {
77                 messageBuilder.append("OPERATIONAL: ");
78             } else {
79                 messageBuilder.append("CONFIGURATION: ");
80             }
81
82             final DataObjectModification<T> objectModification = modification.getRootNode();
83             formatModification(messageBuilder, objectModification);
84         }
85     }
86
87     private void logChanges(String prefix, @Nonnull Collection<DataTreeModification<Neutron>> changes) {
88         final StringBuilder messageBuilder = new StringBuilder();
89         messageBuilder.append(prefix);
90         formatChanges(messageBuilder, changes);
91         LOG.info(messageBuilder.toString());
92     }
93
94     private void configurationDataTreeChanged(@Nonnull Collection<DataTreeModification<Neutron>> changes) {
95         logChanges("Configuration DataTreeChanged ", changes);
96     }
97
98     private void operationalDataTreeChanged(@Nonnull Collection<DataTreeModification<Neutron>> changes) {
99         logChanges("Operational DataTreeChanged ", changes);
100     }
101
102     public void init() {
103         LOG.info("Register listener for Neutron model data changes");
104         InstanceIdentifier<Neutron> instanceId = Preconditions.checkNotNull(InstanceIdentifier.create(Neutron.class));
105
106         DataTreeIdentifier<Neutron> configurationDataTreeId =
107             new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceId);
108         configurationDataTreeChangeListener = new ClusteredDataTreeChangeListener<Neutron>() {
109             @Override
110             public void onDataTreeChanged(Collection<DataTreeModification<Neutron>> changes) {
111                 configurationDataTreeChanged(changes);
112             }
113         };
114         configurationRegisteredListener = db.registerDataTreeChangeListener(configurationDataTreeId,
115                                                                             configurationDataTreeChangeListener);
116
117         DataTreeIdentifier<Neutron> operationalDataTreeId =
118             new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, instanceId);
119         operationalDataTreeChangeListener = new ClusteredDataTreeChangeListener<Neutron>() {
120             @Override
121             public void onDataTreeChanged(Collection<DataTreeModification<Neutron>> changes) {
122                 operationalDataTreeChanged(changes);
123             }
124         };
125         operationalRegisteredListener = db.registerDataTreeChangeListener(operationalDataTreeId,
126                                                                           operationalDataTreeChangeListener);
127     }
128
129     @Override
130     public void close() throws Exception {
131         configurationRegisteredListener.close();
132         configurationRegisteredListener = null;
133         operationalRegisteredListener.close();
134         operationalRegisteredListener = null;
135     }
136 }