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