Improve ModuleInfoSnapshotBuilder API
[mdsal.git] / dom / mdsal-dom-schema-osgi / src / main / java / org / opendaylight / mdsal / dom / schema / osgi / impl / OSGiModuleInfoSnapshotImpl.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 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.mdsal.binding.runtime.api.ModuleInfoSnapshot;
21 import org.opendaylight.mdsal.dom.schema.osgi.ModelGenerationAware;
22 import org.opendaylight.mdsal.dom.schema.osgi.OSGiModuleInfoSnapshot;
23 import org.osgi.framework.Constants;
24 import org.osgi.service.component.annotations.Activate;
25 import org.osgi.service.component.annotations.Component;
26 import org.osgi.service.component.annotations.Deactivate;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 @Beta
31 @Component(factory = OSGiModuleInfoSnapshotImpl.FACTORY_NAME, service = OSGiModuleInfoSnapshot.class)
32 public final class OSGiModuleInfoSnapshotImpl implements OSGiModuleInfoSnapshot {
33     // OSGi DS Component Factory name
34     static final String FACTORY_NAME = "org.opendaylight.mdsal.dom.schema.osgi.impl.OSGiEffectiveModelImpl";
35
36     // Keys to for activation properties
37     @VisibleForTesting
38     static final String GENERATION = "org.opendaylight.mdsal.dom.schema.osgi.impl.Generation";
39     @VisibleForTesting
40     static final String DELEGATE = "org.opendaylight.mdsal.dom.schema.osgi.impl.ModuleInfoSnapshot";
41
42     private static final Logger LOG = LoggerFactory.getLogger(OSGiModuleInfoSnapshotImpl.class);
43
44     private ModuleInfoSnapshot delegate;
45     private UnsignedLong generation;
46
47     @Override
48     public UnsignedLong getGeneration() {
49         return verifyNotNull(generation);
50     }
51
52     @Override
53     public ModuleInfoSnapshot getService() {
54         return verifyNotNull(delegate);
55     }
56
57     @Activate
58     void activate(final Map<String, ?> properties) {
59         generation = (UnsignedLong) verifyNotNull(properties.get(GENERATION));
60         delegate = (ModuleInfoSnapshot) verifyNotNull(properties.get(DELEGATE));
61         LOG.info("EffectiveModelContext generation {} activated", generation);
62     }
63
64     @Deactivate
65     void deactivate() {
66         delegate = null;
67         LOG.info("EffectiveModelContext generation {} deactivated", generation);
68     }
69
70     @SuppressModernizer
71     static Dictionary<String, ?> props(final long generation, final ModuleInfoSnapshot delegate) {
72         final Dictionary<String, Object> ret = new Hashtable<>(4);
73         ret.put(Constants.SERVICE_RANKING, ModelGenerationAware.computeServiceRanking(generation));
74         ret.put(GENERATION, UnsignedLong.fromLongBits(generation));
75         ret.put(DELEGATE, requireNonNull(delegate));
76         return ret;
77     }
78 }