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