Improve OSGiDOMSchemaService printouts
[mdsal.git] / dom / mdsal-dom-schema-osgi / src / main / java / org / opendaylight / mdsal / dom / schema / osgi / impl / ModelContextListener.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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 package org.opendaylight.mdsal.dom.schema.osgi.impl;
9
10 import static com.google.common.base.Verify.verifyNotNull;
11
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.collect.ForwardingObject;
14 import java.util.Dictionary;
15 import java.util.Map;
16 import java.util.function.Consumer;
17 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
18 import org.osgi.framework.FrameworkUtil;
19 import org.osgi.service.component.annotations.Activate;
20 import org.osgi.service.component.annotations.Component;
21 import org.osgi.service.component.annotations.Deactivate;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * A Factory Component for OSGi SR manifestation of a {@code Consumer<EffectiveModelContext>}. Instances of this
27  * component are created through by {@link OSGiDOMSchemaService} each time a listener is registered.
28  */
29 @Component(factory = ModelContextListener.FACTORY_NAME, service = ModelContextListener.class)
30 public final class ModelContextListener extends ForwardingObject {
31     static final String FACTORY_NAME = "org.opendaylight.mdsal.dom.schema.osgi.impl.SchemaSchemaContextListener";
32
33     private static final Logger LOG = LoggerFactory.getLogger(ModelContextListener.class);
34
35     @VisibleForTesting
36     static final String DELEGATE = "org.opendaylight.mdsal.dom.schema.osgi.SchemaSchemaContextListener";
37
38     private Consumer<EffectiveModelContext> delegate = null;
39
40     @Activate
41     public ModelContextListener(final Map<String, ?> properties) {
42         delegate = (Consumer<EffectiveModelContext>) verifyNotNull(properties.get(DELEGATE));
43     }
44
45     @Deactivate
46     void deactivate() {
47         delegate = null;
48     }
49
50     @Override
51     protected Object delegate() {
52         return delegate;
53     }
54
55     @SuppressWarnings("checkstyle:illegalCatch")
56     void onModelContextUpdated(final EffectiveModelContext modelContext) {
57         LOG.trace("Notifying {} of {}", delegate, modelContext);
58         try {
59             delegate.accept(modelContext);
60         } catch (RuntimeException e) {
61             LOG.warn("Failed to notify listener {}", delegate, e);
62         }
63     }
64
65     static Dictionary<String, ?> props(final Consumer<EffectiveModelContext> delegate) {
66         return FrameworkUtil.asDictionary(Map.of(DELEGATE, delegate));
67     }
68 }