Rework binding component instantiation
[mdsal.git] / dom / mdsal-dom-schema-osgi / src / main / java / org / opendaylight / mdsal / dom / schema / osgi / impl / OSGiEffectiveModelImpl.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 import static java.util.Objects.requireNonNull;
12
13 import com.google.common.annotations.Beta;
14 import com.google.common.annotations.VisibleForTesting;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import java.util.Dictionary;
17 import java.util.Hashtable;
18 import java.util.Map;
19 import org.gaul.modernizer_maven_annotations.SuppressModernizer;
20 import org.opendaylight.binding.runtime.api.ModuleInfoSnapshot;
21 import org.opendaylight.mdsal.dom.schema.osgi.OSGiModuleInfoSnapshot;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
25 import org.osgi.framework.Constants;
26 import org.osgi.service.component.annotations.Activate;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Deactivate;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 @Beta
33 @Component(factory = OSGiEffectiveModelImpl.FACTORY_NAME,
34            service = { OSGiModuleInfoSnapshot.class, ModuleInfoSnapshot.class })
35 public final class OSGiEffectiveModelImpl implements OSGiModuleInfoSnapshot {
36     // OSGi DS Component Factory name
37     static final String FACTORY_NAME = "org.opendaylight.mdsal.dom.schema.osgi.impl.OSGiEffectiveModelImpl";
38
39     // Keys to for activation properties
40     @VisibleForTesting
41     static final String GENERATION = "org.opendaylight.mdsal.dom.schema.osgi.impl.Generation";
42     @VisibleForTesting
43     static final String DELEGATE = "org.opendaylight.mdsal.dom.schema.osgi.impl.ModuleInfoSnapshot";
44
45     private static final Logger LOG = LoggerFactory.getLogger(OSGiEffectiveModelImpl.class);
46
47     private ModuleInfoSnapshot delegate;
48     private long generation;
49
50     @Override
51     public long getGeneration() {
52         return generation;
53     }
54
55     @Override
56     public EffectiveModelContext getEffectiveModelContext() {
57         return delegate.getEffectiveModelContext();
58     }
59
60     @Override
61     public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
62         return delegate.getSource(sourceIdentifier);
63     }
64
65     @Override
66     public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
67         return delegate.loadClass(fullyQualifiedName);
68     }
69
70     @Activate
71     void activate(final Map<String, ?> properties) {
72         generation = (Long) verifyNotNull(properties.get(GENERATION));
73         delegate = (ModuleInfoSnapshot) verifyNotNull(properties.get(DELEGATE));
74         LOG.debug("ClassLoadingEffectiveModelContext generation {} activated", generation);
75     }
76
77     @Deactivate
78     void deactivate() {
79         delegate = null;
80         LOG.debug("ClassLoadingEffectiveModelContext generation {} deactivated", generation);
81     }
82
83     @SuppressModernizer
84     static Dictionary<String, ?> props(final long generation, final ModuleInfoSnapshot delegate) {
85         final Dictionary<String, Object> ret = new Hashtable<>(4);
86         ret.put(Constants.SERVICE_RANKING, ranking(generation));
87         ret.put(GENERATION, generation);
88         ret.put(DELEGATE, requireNonNull(delegate));
89         return ret;
90     }
91
92     private static Integer ranking(final long generation) {
93         return generation >= 0 && generation <= Integer.MAX_VALUE ? (int) generation : Integer.MAX_VALUE;
94     }
95 }