Refactor OSGi ModuleInfoSnapshot/BindingRuntimeContext
[mdsal.git] / binding / mdsal-binding-runtime-osgi / src / main / java / org / opendaylight / mdsal / binding / runtime / osgi / impl / GlobalBindingRuntimeContext.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.primitives.UnsignedLong;
14 import org.opendaylight.binding.runtime.api.BindingRuntimeContext;
15 import org.opendaylight.binding.runtime.spi.ForwardingBindingRuntimeContext;
16 import org.opendaylight.mdsal.binding.runtime.osgi.OSGiBindingRuntimeContext;
17 import org.osgi.service.component.annotations.Activate;
18 import org.osgi.service.component.annotations.Component;
19 import org.osgi.service.component.annotations.Deactivate;
20 import org.osgi.service.component.annotations.Reference;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * A global {@link BindingRuntimeContext}. It is injected with latest {@link OSGiBindingRuntimeContext} generation.
26  */
27 @Beta
28 @Component(service = BindingRuntimeContext.class, immediate = true)
29 public final class GlobalBindingRuntimeContext extends ForwardingBindingRuntimeContext {
30     private static final Logger LOG = LoggerFactory.getLogger(GlobalBindingRuntimeContext.class);
31
32     @Reference
33     OSGiBindingRuntimeContext osgi = null;
34
35     private BindingRuntimeContext delegate;
36     private UnsignedLong generation;
37
38     @Override
39     protected BindingRuntimeContext delegate() {
40         return verifyNotNull(delegate);
41     }
42
43     @Activate
44     void activate() {
45         generation = osgi.getGeneration();
46         delegate = osgi.getService();
47         LOG.info("Global BindingRuntimeContext generation {} activated", generation);
48     }
49
50     @Deactivate
51     void deactivate() {
52         delegate = null;
53         LOG.info("Global BindingRuntimeContext generation {} deactivated", generation);
54     }
55 }