2 * Copyright (c) 2020 PANTHEON.tech, s.r.o. 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
8 package org.opendaylight.mdsal.dom.schema.osgi.impl;
10 import static com.google.common.base.Verify.verifyNotNull;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.collect.ForwardingObject;
14 import java.util.Dictionary;
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;
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.
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";
33 private static final Logger LOG = LoggerFactory.getLogger(ModelContextListener.class);
36 static final String DELEGATE = "org.opendaylight.mdsal.dom.schema.osgi.SchemaSchemaContextListener";
38 private Consumer<EffectiveModelContext> delegate = null;
41 public ModelContextListener(final Map<String, ?> properties) {
42 delegate = (Consumer<EffectiveModelContext>) verifyNotNull(properties.get(DELEGATE));
51 protected Object delegate() {
55 @SuppressWarnings("checkstyle:illegalCatch")
56 void onModelContextUpdated(final EffectiveModelContext modelContext) {
57 LOG.trace("Notifying {} of {}", delegate, modelContext);
59 delegate.accept(modelContext);
60 } catch (RuntimeException e) {
61 LOG.warn("Failed to notify listener {}", delegate, e);
65 static Dictionary<String, ?> props(final Consumer<EffectiveModelContext> delegate) {
66 return FrameworkUtil.asDictionary(Map.of(DELEGATE, delegate));