BUG-8360: add mdsal-binding-dom-codec-osgi
[mdsal.git] / binding / mdsal-binding-dom-codec-osgi / src / main / java / org / opendaylight / mdsal / binding / dom / codec / osgi / impl / Activator.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, 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.dom.codec.osgi.impl;
9
10 import java.util.Collection;
11 import org.opendaylight.mdsal.binding.dom.codec.osgi.BindingRuntimeContextService;
12 import org.opendaylight.mdsal.binding.generator.impl.ModuleInfoBackedContext;
13 import org.opendaylight.yangtools.concepts.ObjectRegistration;
14 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
15 import org.osgi.framework.Bundle;
16 import org.osgi.framework.BundleActivator;
17 import org.osgi.framework.BundleContext;
18 import org.osgi.framework.ServiceRegistration;
19 import org.osgi.util.tracker.BundleTracker;
20
21 public final class Activator implements BundleActivator {
22     private BundleTracker<Collection<ObjectRegistration<YangModuleInfo>>> moduleInfoResolvedBundleTracker;
23     private SimpleBindingRuntimeContextService service;
24     private ServiceRegistration<?> registration;
25
26     @Override
27     public void start(final BundleContext context) {
28         // XXX: this will use thread-context class loader, which is probably appropriate
29         final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create();
30
31         service = new SimpleBindingRuntimeContextService(context, moduleInfoBackedContext, moduleInfoBackedContext);
32
33         final OsgiModuleInfoRegistry registry = new OsgiModuleInfoRegistry(moduleInfoBackedContext,
34             moduleInfoBackedContext, service);
35
36         final ModuleInfoBundleTracker moduleInfoTracker = new ModuleInfoBundleTracker(registry);
37         moduleInfoResolvedBundleTracker = new BundleTracker<>(context, Bundle.RESOLVED | Bundle.STARTING
38                 | Bundle.STOPPING | Bundle.ACTIVE, moduleInfoTracker);
39         moduleInfoResolvedBundleTracker.open();
40         moduleInfoTracker.finishStart();
41
42         service.open();
43         registration = context.registerService(BindingRuntimeContextService.class, service, null);
44     }
45
46     @Override
47     public void stop(final BundleContext context) {
48         moduleInfoResolvedBundleTracker.close();
49         service.close();
50         registration.unregister();
51     }
52 }