9009dd33c0685033cf60dfd879acba5d055fc92d
[yangtools.git] / binding / binding-runtime-osgi / src / main / java / org / opendaylight / mdsal / binding / runtime / osgi / impl / OSGiBindingRuntimeContextImpl.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.binding.runtime.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.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.binding.runtime.api.BindingRuntimeContext;
19 import org.opendaylight.mdsal.binding.runtime.osgi.OSGiBindingRuntimeContext;
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 /**
29  * A Factory Component which implements {@link OSGiBindingRuntimeContext}.
30  */
31 @Beta
32 @Component(factory = OSGiBindingRuntimeContextImpl.FACTORY_NAME, service = OSGiBindingRuntimeContext.class)
33 public final class OSGiBindingRuntimeContextImpl implements OSGiBindingRuntimeContext {
34     // OSGi DS Component Factory name
35     static final String FACTORY_NAME = "org.opendaylight.mdsal.binding.runtime.osgi.impl.OSGiBindingRuntimeContextImpl";
36
37     // Keys to for activation properties
38     @VisibleForTesting
39     static final String GENERATION = "org.opendaylight.mdsal.binding.runtime.osgi.impl.Generation";
40     @VisibleForTesting
41     static final String DELEGATE = "org.opendaylight.mdsal.binding.runtime.osgi.impl.BindingRuntimeContext";
42
43     private static final Logger LOG = LoggerFactory.getLogger(OSGiBindingRuntimeContextImpl.class);
44
45     private BindingRuntimeContext delegate;
46     private UnsignedLong generation;
47
48     @Override
49     public UnsignedLong getGeneration() {
50         return verifyNotNull(generation);
51     }
52
53     @Override
54     public BindingRuntimeContext getService() {
55         return verifyNotNull(delegate);
56     }
57
58     @Activate
59     void activate(final Map<String, ?> properties) {
60         generation = (UnsignedLong) verifyNotNull(properties.get(GENERATION));
61         delegate = (BindingRuntimeContext) verifyNotNull(properties.get(DELEGATE));
62         LOG.info("BindingRuntimeContext generation {} activated", generation);
63     }
64
65     @Deactivate
66     void deactivate() {
67         delegate = null;
68         LOG.info("BindingRuntimeContext generation {} deactivated", generation);
69     }
70
71     static Dictionary<String, ?> props(final @NonNull UnsignedLong generation, final @NonNull Integer ranking,
72             final @NonNull BindingRuntimeContext delegate) {
73         return FrameworkUtil.asDictionary(Map.of(
74             Constants.SERVICE_RANKING, ranking,
75             GENERATION, generation,
76             DELEGATE, delegate));
77     }
78 }