14dc0e9e52cd5fc22c54146f92f8deb2a3b4c810
[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 java.util.Dictionary;
14 import java.util.Map;
15 import java.util.function.Consumer;
16 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
17 import org.osgi.framework.FrameworkUtil;
18 import org.osgi.service.component.annotations.Activate;
19 import org.osgi.service.component.annotations.Component;
20 import org.osgi.service.component.annotations.Deactivate;
21
22 /**
23  * A Factory Component for OSGi SR manifestation of a {@code Consumer<EffectiveModelContext>}. Instances of this
24  * component are created through by {@link OSGiDOMSchemaService} each time a listener is registered.
25  */
26 @Component(factory = ModelContextListener.FACTORY_NAME, service = ModelContextListener.class)
27 public final class ModelContextListener {
28     static final String FACTORY_NAME = "org.opendaylight.mdsal.dom.schema.osgi.impl.SchemaSchemaContextListener";
29
30     @VisibleForTesting
31     static final String DELEGATE = "org.opendaylight.mdsal.dom.schema.osgi.SchemaSchemaContextListener";
32
33     private Consumer<EffectiveModelContext> delegate = null;
34
35     @Activate
36     public ModelContextListener(final Map<String, ?> properties) {
37         delegate = (Consumer<EffectiveModelContext>) verifyNotNull(properties.get(DELEGATE));
38     }
39
40     @Deactivate
41     void deactivate() {
42         delegate = null;
43     }
44
45     void onModelContextUpdated(final EffectiveModelContext newModelContext) {
46         delegate.accept(newModelContext);
47     }
48
49     static Dictionary<String, ?> props(final Consumer<EffectiveModelContext> delegate) {
50         return FrameworkUtil.asDictionary(Map.of(DELEGATE, delegate));
51     }
52 }