Rework binding component instantiation
[mdsal.git] / binding / mdsal-binding-runtime-osgi / src / main / java / org / opendaylight / mdsal / binding / runtime / osgi / impl / BindingRuntimeContextImpl.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 org.opendaylight.binding.runtime.api.AbstractBindingRuntimeContext;
11 import org.opendaylight.binding.runtime.api.BindingRuntimeContext;
12 import org.opendaylight.binding.runtime.api.BindingRuntimeGenerator;
13 import org.opendaylight.binding.runtime.api.BindingRuntimeTypes;
14 import org.opendaylight.binding.runtime.api.ClassLoadingStrategy;
15 import org.opendaylight.binding.runtime.api.DefaultBindingRuntimeContext;
16 import org.opendaylight.mdsal.dom.schema.osgi.OSGiModuleInfoSnapshot;
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 Factory Component which implements {@link BindingRuntimeContext}.
26  */
27 @Component(service = BindingRuntimeContext.class, immediate = true)
28 public final class BindingRuntimeContextImpl extends AbstractBindingRuntimeContext {
29     private static final Logger LOG = LoggerFactory.getLogger(BindingRuntimeContextImpl.class);
30
31     @Reference
32     OSGiModuleInfoSnapshot effectiveModel = null;
33     @Reference
34     BindingRuntimeGenerator generator = null;
35
36     private BindingRuntimeContext delegate = null;
37     private long generation;
38
39     @Override
40     public ClassLoadingStrategy getStrategy() {
41         return delegate.getStrategy();
42     }
43
44     @Override
45     public BindingRuntimeTypes getTypes() {
46         return delegate.getTypes();
47     }
48
49     @Activate
50     void activate() {
51         generation = effectiveModel.getGeneration();
52         delegate = DefaultBindingRuntimeContext.create(
53             generator.generateTypeMapping(effectiveModel.getEffectiveModelContext()), effectiveModel);
54
55         LOG.debug("BindingRuntimeContext generation {} activated", generation);
56     }
57
58     @Deactivate
59     void deactivate() {
60         delegate = null;
61         LOG.debug("BindingRuntimeContext generation {} deactivated", generation);
62     }
63 }