Merge "ConfigurationService to create default config dir"
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / mapping / RuntimeGeneratedMappingServiceActivator.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.controller.config.manager.impl.osgi.mapping;
9
10 import javassist.ClassPool;
11 import org.opendaylight.yangtools.sal.binding.generator.impl.RuntimeGeneratedMappingServiceImpl;
12 import org.opendaylight.yangtools.yang.data.impl.codec.BindingIndependentMappingService;
13 import org.opendaylight.yangtools.yang.model.api.SchemaServiceListener;
14 import org.osgi.framework.BundleContext;
15 import org.osgi.framework.ServiceRegistration;
16
17 import java.util.Hashtable;
18
19 public class RuntimeGeneratedMappingServiceActivator implements AutoCloseable {
20
21     private static final ClassPool CLASS_POOL = ClassPool.getDefault();
22
23     private ServiceRegistration<SchemaServiceListener> listenerReg;
24     private ServiceRegistration<BindingIndependentMappingService> mappingReg;
25     private ModuleInfoBundleTracker moduleInfoBundleTracker;
26
27     public RuntimeGeneratedMappingServiceActivator(ModuleInfoBundleTracker moduleInfoBundleTracker) {
28         this.moduleInfoBundleTracker = moduleInfoBundleTracker;
29     }
30
31     public RuntimeGeneratedMappingServiceImpl startRuntimeMappingService(BundleContext context) {
32         RuntimeGeneratedMappingServiceImpl service = new RuntimeGeneratedMappingServiceImpl(moduleInfoBundleTracker.getModuleInfoLoadingStrategy());
33         service.setPool(CLASS_POOL);
34         service.init();
35         startRuntimeMappingService(service, context);
36         return service;
37     }
38
39     private void startRuntimeMappingService(RuntimeGeneratedMappingServiceImpl service, BundleContext context) {
40         Hashtable<String, String> properties = new Hashtable<String, String>();
41         listenerReg = context.registerService(SchemaServiceListener.class, service, properties);
42         mappingReg = context.registerService(BindingIndependentMappingService.class, service, properties);
43
44     }
45
46     @Override
47     public void close() throws Exception {
48         mappingReg.unregister();
49         listenerReg.unregister();
50     }
51 }