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