Bump versions by x.y.(z+1)
[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: type: ").append(typeName).append("\n");
63                 final T dataBefore = objectModification.getDataBefore();
64                 messageBuilder.append(dataBefore.toString());
65                 break;
66             default:
67                 LOG.warn("unknown modification type: {}", typeName);
68                 break;
69         }
70     }
71
72     private <T extends DataObject>
73         void formatChanges(@Nonnull final StringBuilder messageBuilder,
74                            @Nonnull final Collection<DataTreeModification<T>> changes) {
75         for (DataTreeModification<T> modification : changes) {
76             final DataTreeIdentifier<T> identifier = modification.getRootPath();
77             final LogicalDatastoreType datastoreType = identifier.getDatastoreType();
78             if (datastoreType == LogicalDatastoreType.OPERATIONAL) {
79                 messageBuilder.append("OPERATIONAL: ");
80             } else {
81                 messageBuilder.append("CONFIGURATION: ");
82             }
83
84             final DataObjectModification<T> objectModification = modification.getRootNode();
85             formatModification(messageBuilder, objectModification);
86         }
87     }
88
89     private void logChanges(String prefix, @Nonnull Collection<DataTreeModification<Neutron>> changes) {
90         final StringBuilder messageBuilder = new StringBuilder();
91         messageBuilder.append(prefix);
92         formatChanges(messageBuilder, changes);
93         LOG.info(messageBuilder.toString());
94     }
95
96     private void configurationDataTreeChanged(@Nonnull Collection<DataTreeModification<Neutron>> changes) {
97         logChanges("Configuration DataTreeChanged ", changes);
98     }
99
100     private void operationalDataTreeChanged(@Nonnull Collection<DataTreeModification<Neutron>> changes) {
101         logChanges("Operational DataTreeChanged ", changes);
102     }
103
104     public void init() {
105         LOG.info("Register listener for Neutron model data changes");
106         InstanceIdentifier<Neutron> instanceId = Preconditions.checkNotNull(InstanceIdentifier.create(Neutron.class));
107
108         DataTreeIdentifier<Neutron> configurationDataTreeId =
109             new DataTreeIdentifier<>(LogicalDatastoreType.CONFIGURATION, instanceId);
110         configurationDataTreeChangeListener = new ClusteredDataTreeChangeListener<Neutron>() {
111             @Override
112             public void onDataTreeChanged(Collection<DataTreeModification<Neutron>> changes) {
113                 configurationDataTreeChanged(changes);
114             }
115         };
116         configurationRegisteredListener = db.registerDataTreeChangeListener(configurationDataTreeId,
117                                                                             configurationDataTreeChangeListener);
118
119         DataTreeIdentifier<Neutron> operationalDataTreeId =
120             new DataTreeIdentifier<>(LogicalDatastoreType.OPERATIONAL, instanceId);
121         operationalDataTreeChangeListener = new ClusteredDataTreeChangeListener<Neutron>() {
122             @Override
123             public void onDataTreeChanged(Collection<DataTreeModification<Neutron>> changes) {
124                 operationalDataTreeChanged(changes);
125             }
126         };
127         operationalRegisteredListener = db.registerDataTreeChangeListener(operationalDataTreeId,
128                                                                           operationalDataTreeChangeListener);
129     }
130
131     @Override
132     public void close() throws Exception {
133         configurationRegisteredListener.close();
134         configurationRegisteredListener = null;
135         operationalRegisteredListener.close();
136         operationalRegisteredListener = null;
137     }
138 }