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;
11 import static java.util.Objects.requireNonNull;
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;
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;
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";
39 // Keys to for activation properties
41 static final String GENERATION = "org.opendaylight.mdsal.dom.schema.osgi.impl.Generation";
43 static final String DELEGATE = "org.opendaylight.mdsal.dom.schema.osgi.impl.ModuleInfoSnapshot";
45 private static final Logger LOG = LoggerFactory.getLogger(OSGiEffectiveModelImpl.class);
47 private ModuleInfoSnapshot delegate;
48 private long generation;
51 public long getGeneration() {
56 public EffectiveModelContext getEffectiveModelContext() {
57 return delegate.getEffectiveModelContext();
61 public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
62 return delegate.getSource(sourceIdentifier);
66 public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
67 return delegate.loadClass(fullyQualifiedName);
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);
80 LOG.debug("ClassLoadingEffectiveModelContext generation {} deactivated", generation);
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));
92 private static Integer ranking(final long generation) {
93 return generation >= 0 && generation <= Integer.MAX_VALUE ? (int) generation : Integer.MAX_VALUE;